Skip to content

Commit

Permalink
Make ObjectExists and BackupStoreCheck use object_exists_t enum
Browse files Browse the repository at this point in the history
  • Loading branch information
qris committed Aug 4, 2017
1 parent 026b645 commit 459345a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
19 changes: 9 additions & 10 deletions lib/backupclient/BackupClientRestore.cpp
Expand Up @@ -30,7 +30,7 @@
#include "BackupStoreFile.h"
#include "CollectInBufferStream.h"
#include "FileStream.h"
#include "Utils.h"
#include "Utils.h" // for ObjectExists_* (object_exists_t)

#include "MemLeakFindOn.h"

Expand Down Expand Up @@ -238,7 +238,7 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection,
// Create the local directory, if not already done.
// Path and owner set later, just use restrictive owner mode.

int exists;
object_exists_t exists;

try
{
Expand Down Expand Up @@ -334,7 +334,7 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection,
}
#endif

int parentExists;
object_exists_t parentExists;

try
{
Expand Down Expand Up @@ -379,9 +379,9 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection,
return Restore_TargetPathNotFound;

default:
BOX_ERROR("Failed to restore: unknown "
"result from ObjectExists('" <<
parentDirectoryName << "')");
BOX_ERROR("Failed to restore: unexpected result from "
"ObjectExists('" << parentDirectoryName << "'): " <<
parentExists);
return Restore_UnknownError;
}
}
Expand Down Expand Up @@ -515,9 +515,8 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection,
!= ObjectExists_NoObject &&
EMU_UNLINK(localFilename.c_str()) != 0)
{
BOX_LOG_SYS_ERROR("Failed to delete "
"file '" << localFilename <<
"'");
BOX_LOG_SYS_ERROR("Failed to delete file "
"'" << localFilename << "'");

if (Params.ContinueAfterErrors)
{
Expand Down Expand Up @@ -856,7 +855,7 @@ int BackupClientRestore(BackupProtocolCallable &rConnection,
params.mRestoreResumeInfoFilename += ".boxbackupresume";

// Target exists?
int targetExistance = ObjectExists(LocalDirectoryName);
object_exists_t targetExistance = ObjectExists(LocalDirectoryName);

// Does any resumption information exist?
bool doingResume = false;
Expand Down
37 changes: 22 additions & 15 deletions lib/backupstore/BackupStoreCheck.cpp
Expand Up @@ -30,7 +30,7 @@
#include "RaidFileUtil.h"
#include "RaidFileWrite.h"
#include "StoreStructure.h"
#include "Utils.h"
#include "Utils.h" // for ObjectExists_* (object_exists_t)

#include "MemLeakFindOn.h"

Expand Down Expand Up @@ -493,12 +493,11 @@ void BackupStoreCheck::CheckObjectsDir(int64_t StartID)
char leaf[8];
::snprintf(leaf, sizeof(leaf),
DIRECTORY_SEPARATOR "o%02x", i);
if(!CheckAndAddObject(StartID | i, dirName + leaf))
if(CheckAndAddObject(StartID | i, dirName + leaf) == ObjectExists_Unknown)
{
// File was bad, delete it
BOX_ERROR("Corrupted file " << dirName <<
leaf << " found" <<
(mFixErrors?", deleting":""));
BOX_ERROR("Object " << BOX_FORMAT_OBJECTID(StartID | i) << " "
"is corrupt" << (mFixErrors?", deleting":""));
++mNumberErrorsFound;
if(mFixErrors)
{
Expand All @@ -522,7 +521,7 @@ void BackupStoreCheck::CheckObjectsDir(int64_t StartID)
// Created: 21/4/04
//
// --------------------------------------------------------------------------
bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID,
object_exists_t BackupStoreCheck::CheckAndAddObject(int64_t ObjectID,
const std::string &rFilename)
{
// Info on object...
Expand All @@ -543,7 +542,9 @@ bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID,
if(file->Read(&signature, sizeof(signature)) != sizeof(signature))
{
// Too short, can't read signature from it
return false;
BOX_ERROR("Object " << BOX_FORMAT_OBJECTID(ObjectID) << " is "
"too small to have a valid header");
return ObjectExists_Unknown;
}

// Seek back to beginning
Expand All @@ -556,31 +557,34 @@ bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID,
#ifndef BOX_DISABLE_BACKWARDS_COMPATIBILITY_BACKUPSTOREFILE
case OBJECTMAGIC_FILE_MAGIC_VALUE_V0:
#endif
// File... check
// Check it as a file.
containerID = CheckFile(ObjectID, *file);
break;

case OBJECTMAGIC_DIR_MAGIC_VALUE:
// Check it as a directory.
isFile = false;
containerID = CheckDirInitial(ObjectID, *file);
break;

default:
// Unknown signature. Bad file. Very bad file.
return false;
return ObjectExists_Unknown;
break;
}
}
catch(...)
catch(std::exception &e)
{
// Error caught, not a good file then, let it be deleted
return false;
BOX_ERROR("Object " << BOX_FORMAT_OBJECTID(ObjectID) << " failed initial "
"validation: " << e.what());
return ObjectExists_Unknown;
}

// Got a container ID? (ie check was successful)
if(containerID == -1)
{
return false;
return ObjectExists_Unknown;
}

// Add to list of IDs known about
Expand Down Expand Up @@ -634,7 +638,7 @@ bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID,
}

// Report success
return true;
return isFile ? ObjectExists_File : ObjectExists_Dir;
}


Expand Down Expand Up @@ -664,7 +668,8 @@ int64_t BackupStoreCheck::CheckFile(int64_t ObjectID, IOStream &rStream)
0 /* don't want diffing from ID */,
&originalContainerID))
{
// Didn't verify
BOX_ERROR("Object " << BOX_FORMAT_OBJECTID(ObjectID) << " does not "
"verify as a file");
return -1;
}

Expand All @@ -690,7 +695,9 @@ int64_t BackupStoreCheck::CheckDirInitial(int64_t ObjectID, IOStream &rStream)
// Check object ID
if(dir.GetObjectID() != ObjectID)
{
// Wrong object ID
BOX_ERROR("Directory " << BOX_FORMAT_OBJECTID(ObjectID) << " has a "
"different internal object ID than expected: " <<
BOX_FORMAT_OBJECTID(dir.GetObjectID()));
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/backupstore/BackupStoreCheck.h
Expand Up @@ -128,7 +128,7 @@ class BackupStoreCheck
// Checking functions
int64_t CheckObjectsScanDir(int64_t StartID, int Level, const std::string &rDirName);
void CheckObjectsDir(int64_t StartID);
bool CheckAndAddObject(int64_t ObjectID, const std::string &rFilename);
object_exists_t CheckAndAddObject(int64_t ObjectID, const std::string &rFilename);
bool CheckDirectory(BackupStoreDirectory& dir);
bool CheckDirectoryEntry(BackupStoreDirectory::Entry& rEntry,
int64_t DirectoryID, bool& rIsModified);
Expand Down
4 changes: 2 additions & 2 deletions lib/common/Test.cpp
Expand Up @@ -28,7 +28,7 @@
#include "BoxTime.h"
#include "FileStream.h"
#include "Test.h"
#include "Utils.h"
#include "Utils.h" // for ObjectExists_* (object_exists_t)

int num_tests_selected = 0;
int num_failures = 0;
Expand Down Expand Up @@ -119,7 +119,7 @@ bool setUp(const char* function_name)
std::string filepath = std::string("testfiles" DIRECTORY_SEPARATOR) +
filename;

int filetype = ObjectExists(filepath);
object_exists_t filetype = ObjectExists(filepath);
if(filetype == ObjectExists_File)
{
if(EMU_UNLINK(filepath.c_str()) != 0)
Expand Down
2 changes: 1 addition & 1 deletion lib/common/Utils.cpp
Expand Up @@ -361,7 +361,7 @@ bool FileExists(const std::string& rFilename, int64_t *pFileSize,
// Created: 23/11/03
//
// --------------------------------------------------------------------------
int ObjectExists(const std::string& rFilename)
object_exists_t ObjectExists(const std::string& rFilename)
{
EMU_STRUCT_STAT st;
if(EMU_STAT(rFilename.c_str(), &st) != 0)
Expand Down
8 changes: 5 additions & 3 deletions lib/common/Utils.h
Expand Up @@ -29,13 +29,15 @@ void DumpStackBacktrace(const std::string& filename, size_t size, void * const *
bool FileExists(const std::string& rFilename, int64_t *pFileSize = 0,
bool TreatLinksAsNotExisting = false);

enum
typedef enum
{
ObjectExists_Unknown = -1,
ObjectExists_NoObject = 0,
ObjectExists_File = 1,
ObjectExists_Dir = 2
};
int ObjectExists(const std::string& rFilename);
} object_exists_t;

object_exists_t ObjectExists(const std::string& rFilename);
std::string HumanReadableSize(int64_t Bytes);
std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max,
bool MachineReadable);
Expand Down
5 changes: 3 additions & 2 deletions lib/httpserver/S3Simulator.cpp
Expand Up @@ -23,6 +23,7 @@
#include "IOStream.h"
#include "Logging.h"
#include "S3Simulator.h"
#include "Utils.h" // for ObjectExists_* (object_exists_t)
#include "decode.h"
#include "encode.h"

Expand Down Expand Up @@ -318,8 +319,8 @@ void S3Simulator::HandlePut(HTTPRequest &rRequest, HTTPResponse &rResponse)
next_slash = file_uri.find('/', next_slash + 1))
{
std::string parent_dir_path = base_path + file_uri.substr(0, next_slash);
int what_exists = ObjectExists(parent_dir_path);
if(what_exists == 0)
object_exists_t what_exists = ObjectExists(parent_dir_path);
if(what_exists == ObjectExists_NoObject)
{
// Does not exist, need to create it
mkdir(parent_dir_path.c_str(), 0755);
Expand Down

0 comments on commit 459345a

Please sign in to comment.