Changeset 2543 for box/trunk


Ignore:
Timestamp:
05/07/2009 22:43:57 (3 years ago)
Author:
chris
Message:

Allow RaidFileWrite? to test that the reference count of an object is
correct before overwriting or deleting it.

Location:
box/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/raidfile/RaidFileException.txt

    r1519 r2543  
    2424ErrorOpeningWriteFileOnTruncate 21 
    2525FileIsCurrentlyOpenForWriting   22 
     26RequestedModifyUnreferencedFile 23      Internal error: the server attempted to modify a file which has no references. 
     27RequestedModifyMultiplyReferencedFile   24      Internal error: the server attempted to modify a file which has multiple references. 
     28RequestedDeleteReferencedFile   25      Internal error: the server attempted to delete a file which is still referenced. 
  • box/trunk/lib/raidfile/RaidFileWrite.cpp

    r2529 r2543  
    4343// Function 
    4444//              Name:    RaidFileWrite::RaidFileWrite(int, const std::string &) 
    45 //              Purpose: Construtor, just stores requried details 
     45//              Purpose: Simple constructor, just stores required details 
    4646//              Created: 2003/07/10 
    4747// 
     
    5050        : mSetNumber(SetNumber), 
    5151          mFilename(Filename), 
    52           mOSFileHandle(-1)             // not valid file handle 
    53 { 
     52          mOSFileHandle(-1), // not valid file handle 
     53          mRefCount(-1) // unknown refcount 
     54{ 
     55} 
     56 
     57// -------------------------------------------------------------------------- 
     58// 
     59// Function 
     60//              Name:    RaidFileWrite::RaidFileWrite(int, 
     61//                       const std::string &, int refcount) 
     62//              Purpose: Constructor with check for overwriting file 
     63//                       with multiple references 
     64//              Created: 2009/07/05 
     65// 
     66// -------------------------------------------------------------------------- 
     67RaidFileWrite::RaidFileWrite(int SetNumber, const std::string &Filename, 
     68        int refcount) 
     69        : mSetNumber(SetNumber), 
     70          mFilename(Filename), 
     71          mOSFileHandle(-1),            // not valid file handle 
     72          mRefCount(refcount) 
     73{ 
     74        // Can't check for zero refcount here, because it's legal 
     75        // to create a RaidFileWrite to delete an object with zero refcount. 
     76        // Check in Commit() and Delete() instead. 
     77        if (refcount > 1) 
     78        { 
     79                BOX_ERROR("Attempted to modify object " << mFilename << 
     80                        ", which has " << refcount << " references"); 
     81                THROW_EXCEPTION(RaidFileException, 
     82                        RequestedModifyMultiplyReferencedFile); 
     83        } 
    5484} 
    5585 
     
    251281                THROW_EXCEPTION(RaidFileException, NotOpen) 
    252282        } 
    253          
     283 
     284        if (mRefCount == 0) 
     285        { 
     286                BOX_ERROR("Attempted to modify object " << mFilename <<  
     287                        ", which has no references"); 
     288                THROW_EXCEPTION(RaidFileException, 
     289                        RequestedModifyUnreferencedFile); 
     290        } 
     291 
    254292        // Rename it into place -- BEFORE it's closed so lock remains 
    255293 
     
    639677void RaidFileWrite::Delete() 
    640678{ 
     679        if (mRefCount != 0 && mRefCount != -1) 
     680        { 
     681                BOX_ERROR("Attempted to delete object " << mFilename << 
     682                        " which has " << mRefCount << " references"); 
     683                THROW_EXCEPTION(RaidFileException, 
     684                        RequestedDeleteReferencedFile); 
     685        } 
     686 
    641687        // Get disc set 
    642688        RaidFileController &rcontroller(RaidFileController::GetController()); 
  • box/trunk/lib/raidfile/RaidFileWrite.h

    r217 r2543  
    2929public: 
    3030        RaidFileWrite(int SetNumber, const std::string &Filename); 
     31        RaidFileWrite(int SetNumber, const std::string &Filename, int refcount); 
    3132        ~RaidFileWrite(); 
    3233private: 
     
    6162        std::string mFilename; 
    6263        int mOSFileHandle; 
     64        int mRefCount; 
    6365}; 
    6466 
  • box/trunk/test/raidfile/testraidfile.cpp

    r2506 r2543  
    628628        } 
    629629 
     630        // Test that creating and deleting a RaidFile with the wrong 
     631        // reference counts throws the expected errors. 
     632        { 
     633                RaidFileWrite write1(0, "write1", 1); 
     634                write1.Open(); 
     635                write1.Commit(); 
     636                TEST_CHECK_THROWS(write1.Delete(), RaidFileException, 
     637                        RequestedDeleteReferencedFile); 
     638        } 
     639 
     640        { 
     641                RaidFileWrite write1(0, "write1", 0); 
     642                write1.Open(true); 
     643                TEST_CHECK_THROWS(write1.Commit(), RaidFileException, 
     644                        RequestedModifyUnreferencedFile); 
     645                write1.Delete(); 
     646        } 
     647 
     648        { 
     649                TEST_CHECK_THROWS(RaidFileWrite write1(0, "write1", 2), 
     650                        RaidFileException, 
     651                        RequestedModifyMultiplyReferencedFile); 
     652        } 
     653 
    630654        // Create a RaidFile 
    631655        RaidFileWrite write1(0, "test1"); 
Note: See TracChangeset for help on using the changeset viewer.