Ignore:
Timestamp:
27/08/2010 10:12:27 (21 months ago)
Author:
chris
Message:

Count the number of files and directories in the account during account
check.

Move directory entry checking code into its own method for readability.

Reformat long lines for readability.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/backupstore/BackupStoreCheck.cpp

    r2127 r2701  
    4848          mLostAndFoundDirectoryID(0), 
    4949          mBlocksUsed(0), 
     50          mBlocksInCurrentFiles(0), 
    5051          mBlocksInOldFiles(0), 
    5152          mBlocksInDeletedFiles(0), 
    52           mBlocksInDirectories(0) 
     53          mBlocksInDirectories(0), 
     54          mNumFiles(0), 
     55          mNumOldFiles(0), 
     56          mNumDeletedFiles(0), 
     57          mNumDirectories(0) 
    5358{ 
    5459} 
     
    336341// Function 
    337342//              Name:    BackupStoreCheck::CheckObjectsDir(int64_t) 
    338 //              Purpose: Check all the files within this directory which has the given starting ID. 
     343//              Purpose: Check all the files within this directory which has 
     344//                       the given starting ID. 
    339345//              Created: 22/4/04 
    340346// 
     
    384390                        idsPresent[n] = true; 
    385391                } 
     392                // No other files should be present in subdirectories 
     393                else if(StartID != 0) 
     394                { 
     395                        fileOK = false; 
     396                } 
     397                // info and refcount databases are OK in the root directory 
     398                else if(*i == "info" || *i == "refcount.db") 
     399                { 
     400                        fileOK = true; 
     401                } 
    386402                else 
    387403                { 
    388                         // info file in root dir is OK! 
    389                         if(StartID != 0 || ::strcmp("info", (*i).c_str()) != 0) 
    390                         { 
    391                                 fileOK = false; 
    392                         } 
     404                        fileOK = false; 
    393405                } 
    394406                 
     
    437449// 
    438450// Function 
    439 //              Name:    BackupStoreCheck::CheckAndAddObject(int64_t, const std::string &) 
    440 //              Purpose: Check a specific object and add it to the list if it's OK -- if 
    441 //                               there are any errors with the reading, return false and it'll be deleted. 
     451//              Name:    BackupStoreCheck::CheckAndAddObject(int64_t, 
     452//                       const std::string &) 
     453//              Purpose: Check a specific object and add it to the list 
     454//                       if it's OK. If there are any errors with the 
     455//                       reading, return false and it'll be deleted. 
    442456//              Created: 21/4/04 
    443457// 
    444458// -------------------------------------------------------------------------- 
    445 bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID, const std::string &rFilename) 
     459bool BackupStoreCheck::CheckAndAddObject(int64_t ObjectID, 
     460        const std::string &rFilename) 
    446461{ 
    447462        // Info on object... 
     
    453468        { 
    454469                // Open file 
    455                 std::auto_ptr<RaidFileRead> file(RaidFileRead::Open(mDiscSetNumber, rFilename)); 
     470                std::auto_ptr<RaidFileRead> file( 
     471                        RaidFileRead::Open(mDiscSetNumber, rFilename)); 
    456472                size = file->GetDiscUsageInBlocks(); 
    457473                 
    458                 // Read in first four bytes -- don't have to worry about retrying if not all bytes read as is RaidFile 
     474                // Read in first four bytes -- don't have to worry about 
     475                // retrying if not all bytes read as is RaidFile 
    459476                uint32_t signature; 
    460477                if(file->Read(&signature, sizeof(signature)) != sizeof(signature)) 
     
    519536// Function 
    520537//              Name:    BackupStoreCheck::CheckFile(int64_t, IOStream &) 
    521 //              Purpose: Do check on file, return original container ID if OK, or -1 on error 
     538//              Purpose: Do check on file, return original container ID 
     539//                       if OK, or -1 on error 
    522540//              Created: 22/4/04 
    523541// 
     
    525543int64_t BackupStoreCheck::CheckFile(int64_t ObjectID, IOStream &rStream) 
    526544{ 
    527         // Check that it's not the root directory ID. Having a file as the root directory would be bad. 
     545        // Check that it's not the root directory ID. Having a file as 
     546        // the root directory would be bad. 
    528547        if(ObjectID == BACKUPSTORE_ROOT_DIRECTORY_ID) 
    529548        { 
     
    535554        // Check the format of the file, and obtain the container ID 
    536555        int64_t originalContainerID = -1; 
    537         if(!BackupStoreFile::VerifyEncodedFileFormat(rStream, 0 /* don't want diffing from ID */, 
     556        if(!BackupStoreFile::VerifyEncodedFileFormat(rStream, 
     557                0 /* don't want diffing from ID */, 
    538558                &originalContainerID)) 
    539559        { 
     
    550570// Function 
    551571//              Name:    BackupStoreCheck::CheckDirInitial(int64_t, IOStream &) 
    552 //              Purpose: Do initial check on directory, return container ID if OK, or -1 on error 
     572//              Purpose: Do initial check on directory, return container ID 
     573//                       if OK, or -1 on error 
    553574//              Created: 22/4/04 
    554575// 
     
    589610        // a note of all directories which are missing, and do initial fixing. 
    590611 
    591         // Scan all objects      
     612        // The root directory is not contained inside another directory, so 
     613        // it has no directory entry to scan, but we have to count it 
     614        // somewhere, so we'll count it here. 
     615        mNumDirectories++; 
     616 
     617        // Scan all objects. 
    592618        for(Info_t::const_iterator i(mInfo.begin()); i != mInfo.end(); ++i) 
    593619        { 
     
    636662                                        if(piBlock != 0) 
    637663                                        { 
    638                                                 // Found. Get flags 
    639                                                 uint8_t iflags = GetFlags(piBlock, iIndex); 
    640                                                  
    641                                                 // Is the type the same? 
    642                                                 if(((iflags & Flags_IsDir) == Flags_IsDir) 
    643                                                         != ((en->GetFlags() & BackupStoreDirectory::Entry::Flags_Dir) == BackupStoreDirectory::Entry::Flags_Dir)) 
    644                                                 { 
    645                                                         // Entry is of wrong type 
    646                                                         BOX_WARNING("Directory ID " << 
    647                                                                 BOX_FORMAT_OBJECTID(pblock->mID[e]) << 
    648                                                                 " references object " << 
    649                                                                 BOX_FORMAT_OBJECTID(en->GetObjectID()) << 
    650                                                                 " which has a different type than expected."); 
    651                                                         badEntry = true; 
    652                                                 } 
    653                                                 else 
    654                                                 { 
    655                                                         // Check that the entry is not already contained. 
    656                                                         if(iflags & Flags_IsContained) 
    657                                                         { 
    658                                                                 BOX_WARNING("Directory ID " << 
    659                                                                         BOX_FORMAT_OBJECTID(pblock->mID[e]) << 
    660                                                                         " references object " << 
    661                                                                         BOX_FORMAT_OBJECTID(en->GetObjectID()) << 
    662                                                                         " which is already contained."); 
    663                                                                 badEntry = true; 
    664                                                         } 
    665                                                         else 
    666                                                         { 
    667                                                                 // Not already contained -- mark as contained 
    668                                                                 SetFlags(piBlock, iIndex, iflags | Flags_IsContained); 
    669                                                                  
    670                                                                 // Check that the container ID of the object is correct 
    671                                                                 if(piBlock->mContainer[iIndex] != pblock->mID[e]) 
    672                                                                 { 
    673                                                                         // Needs fixing... 
    674                                                                         if(iflags & Flags_IsDir) 
    675                                                                         { 
    676                                                                                 // Add to will fix later list 
    677                                                                                 BOX_WARNING("Directory ID " << BOX_FORMAT_OBJECTID(en->GetObjectID()) << " has wrong container ID."); 
    678                                                                                 mDirsWithWrongContainerID.push_back(en->GetObjectID()); 
    679                                                                         } 
    680                                                                         else 
    681                                                                         { 
    682                                                                                 // This is OK for files, they might move 
    683                                                                                 BOX_WARNING("File ID " << BOX_FORMAT_OBJECTID(en->GetObjectID()) << " has different container ID, probably moved"); 
    684                                                                         } 
    685                                                                          
    686                                                                         // Fix entry for now 
    687                                                                         piBlock->mContainer[iIndex] = pblock->mID[e]; 
    688                                                                 } 
    689                                                         } 
    690                                                 } 
    691                                                  
    692                                                 // Check the object size, if it's OK and a file 
    693                                                 if(!badEntry && !((iflags & Flags_IsDir) == Flags_IsDir)) 
    694                                                 { 
    695                                                         if(en->GetSizeInBlocks() != piBlock->mObjectSizeInBlocks[iIndex]) 
    696                                                         { 
    697                                                                 // Correct 
    698                                                                 en->SetSizeInBlocks(piBlock->mObjectSizeInBlocks[iIndex]); 
    699                                                                 // Mark as changed 
    700                                                                 isModified = true; 
    701                                                                 // Tell user 
    702                                                                 BOX_WARNING("Directory ID " << BOX_FORMAT_OBJECTID(pblock->mID[e]) << " has wrong size for object " << BOX_FORMAT_OBJECTID(en->GetObjectID())); 
    703                                                         } 
    704                                                 } 
     664                                                badEntry = !CheckDirectoryEntry( 
     665                                                        *en, pblock->mID[e], 
     666                                                        iIndex, isModified); 
    705667                                        } 
    706668                                        else 
    707669                                        { 
    708670                                                // Item can't be found. Is it a directory? 
    709                                                 if(en->GetFlags() & BackupStoreDirectory::Entry::Flags_Dir) 
     671                                                if(en->IsDir()) 
    710672                                                { 
    711673                                                        // Store the directory for later attention 
     
    725687                                                toDelete.push_back(en->GetObjectID()); 
    726688                                        } 
    727                                         else 
     689                                        else if (en->IsFile()) 
    728690                                        { 
    729691                                                // Add to sizes? 
    730                                                 if(en->GetFlags() & BackupStoreDirectory::Entry::Flags_OldVersion) 
     692                                                if(en->IsOld()) 
    731693                                                { 
    732694                                                        mBlocksInOldFiles += en->GetSizeInBlocks(); 
    733695                                                } 
    734                                                 if(en->GetFlags() & BackupStoreDirectory::Entry::Flags_Deleted) 
     696                                                if(en->IsDeleted()) 
    735697                                                { 
    736698                                                        mBlocksInDeletedFiles += en->GetSizeInBlocks(); 
     699                                                } 
     700                                                if(!en->IsOld() && 
     701                                                        !en->IsDeleted()) 
     702                                                { 
     703                                                        mBlocksInCurrentFiles += en->GetSizeInBlocks(); 
    737704                                                } 
    738705                                        } 
     
    774741} 
    775742 
    776  
     743bool BackupStoreCheck::CheckDirectoryEntry(BackupStoreDirectory::Entry& rEntry, 
     744        int64_t DirectoryID, int32_t IndexInDirBlock, bool& rIsModified) 
     745{ 
     746        IDBlock *piBlock = LookupID(rEntry.GetObjectID(), IndexInDirBlock); 
     747        ASSERT(piBlock != 0); 
     748 
     749        uint8_t iflags = GetFlags(piBlock, IndexInDirBlock); 
     750        bool badEntry = false; 
     751         
     752        // Is the type the same? 
     753        if(((iflags & Flags_IsDir) == Flags_IsDir) != rEntry.IsDir()) 
     754        { 
     755                // Entry is of wrong type 
     756                BOX_WARNING("Directory ID " << 
     757                        BOX_FORMAT_OBJECTID(DirectoryID) << 
     758                        " references object " << 
     759                        BOX_FORMAT_OBJECTID(rEntry.GetObjectID()) << 
     760                        " which has a different type than expected."); 
     761                badEntry = true; 
     762        } 
     763        // Check that the entry is not already contained. 
     764        else if(iflags & Flags_IsContained) 
     765        { 
     766                BOX_WARNING("Directory ID " << 
     767                        BOX_FORMAT_OBJECTID(DirectoryID) << 
     768                        " references object " << 
     769                        BOX_FORMAT_OBJECTID(rEntry.GetObjectID()) << 
     770                        " which is already contained."); 
     771                badEntry = true; 
     772        } 
     773        else 
     774        { 
     775                // Not already contained -- mark as contained 
     776                SetFlags(piBlock, IndexInDirBlock, iflags | Flags_IsContained); 
     777                 
     778                // Check that the container ID of the object is correct 
     779                if(piBlock->mContainer[IndexInDirBlock] != DirectoryID) 
     780                { 
     781                        // Needs fixing... 
     782                        if(iflags & Flags_IsDir) 
     783                        { 
     784                                // Add to will fix later list 
     785                                BOX_WARNING("Directory ID " << 
     786                                        BOX_FORMAT_OBJECTID(rEntry.GetObjectID()) 
     787                                        << " has wrong container ID."); 
     788                                mDirsWithWrongContainerID.push_back(rEntry.GetObjectID()); 
     789                        } 
     790                        else 
     791                        { 
     792                                // This is OK for files, they might move 
     793                                BOX_WARNING("File ID " << 
     794                                        BOX_FORMAT_OBJECTID(rEntry.GetObjectID()) 
     795                                        << " has different container ID, " 
     796                                        "probably moved"); 
     797                        } 
     798                         
     799                        // Fix entry for now 
     800                        piBlock->mContainer[IndexInDirBlock] = DirectoryID; 
     801                } 
     802        } 
     803         
     804        // Check the object size, if it's OK and a file 
     805        if(!badEntry && !rEntry.IsDir()) 
     806        { 
     807                if(rEntry.GetSizeInBlocks() != piBlock->mObjectSizeInBlocks[IndexInDirBlock]) 
     808                { 
     809                        // Wrong size, correct it. 
     810                        rEntry.SetSizeInBlocks(piBlock->mObjectSizeInBlocks[IndexInDirBlock]); 
     811 
     812                        // Mark as changed 
     813                        rIsModified = true; 
     814 
     815                        // Tell user 
     816                        BOX_WARNING("Directory ID " << 
     817                                BOX_FORMAT_OBJECTID(DirectoryID) << 
     818                                " has wrong size for object " << 
     819                                BOX_FORMAT_OBJECTID(rEntry.GetObjectID())); 
     820                } 
     821        } 
     822 
     823        if (!badEntry) 
     824        { 
     825                if(rEntry.IsDir()) 
     826                { 
     827                        mNumDirectories++; 
     828                } 
     829                else 
     830                { 
     831                        mNumFiles++; 
     832 
     833                        if(rEntry.IsDeleted()) 
     834                        { 
     835                                mNumDeletedFiles++; 
     836                        } 
     837 
     838                        if(rEntry.IsOld()) 
     839                        { 
     840                                mNumOldFiles++; 
     841                        } 
     842                } 
     843        } 
     844 
     845        return !badEntry; 
     846} 
     847 
Note: See TracChangeset for help on using the changeset viewer.