diff --git a/lib/backupclient/BackupClientRestore.cpp b/lib/backupclient/BackupClientRestore.cpp index 295a4252a..52dd2789e 100644 --- a/lib/backupclient/BackupClientRestore.cpp +++ b/lib/backupclient/BackupClientRestore.cpp @@ -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" @@ -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 { @@ -334,7 +334,7 @@ static int BackupClientRestoreDir(BackupProtocolCallable &rConnection, } #endif - int parentExists; + object_exists_t parentExists; try { @@ -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; } } @@ -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) { @@ -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; diff --git a/lib/backupstore/BackupStoreCheck.cpp b/lib/backupstore/BackupStoreCheck.cpp index 952935840..73890c6ad 100644 --- a/lib/backupstore/BackupStoreCheck.cpp +++ b/lib/backupstore/BackupStoreCheck.cpp @@ -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" @@ -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) { @@ -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... @@ -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 @@ -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 @@ -634,7 +638,7 @@ bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID, } // Report success - return true; + return isFile ? ObjectExists_File : ObjectExists_Dir; } @@ -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; } @@ -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; } diff --git a/lib/backupstore/BackupStoreCheck.h b/lib/backupstore/BackupStoreCheck.h index 2677a0d36..476d46076 100644 --- a/lib/backupstore/BackupStoreCheck.h +++ b/lib/backupstore/BackupStoreCheck.h @@ -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); diff --git a/lib/common/Test.cpp b/lib/common/Test.cpp index 2eea0b03c..82513a3d9 100644 --- a/lib/common/Test.cpp +++ b/lib/common/Test.cpp @@ -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; @@ -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) diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp index 8b1e8702c..38448456b 100644 --- a/lib/common/Utils.cpp +++ b/lib/common/Utils.cpp @@ -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) diff --git a/lib/common/Utils.h b/lib/common/Utils.h index 56b623a2d..45914d72e 100644 --- a/lib/common/Utils.h +++ b/lib/common/Utils.h @@ -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); diff --git a/lib/httpserver/S3Simulator.cpp b/lib/httpserver/S3Simulator.cpp index 55d08dd2b..ff27bd6dc 100644 --- a/lib/httpserver/S3Simulator.cpp +++ b/lib/httpserver/S3Simulator.cpp @@ -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" @@ -320,8 +321,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);