Ignore:
Timestamp:
31/08/2006 23:50:02 (6 years ago)
Author:
chris
Message:

(refs #3)

Open files in binary mode (Win32)

Disable the lock failure block when we don't have any locking mechanism

Close and delete files before renaming over them on Win32. This breaks
Ben's desired recovery semantics, so it's not done on other platforms,
but Win32 requires it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/chris/merge/lib/raidfile/RaidFileWrite.cpp

    r895 r896  
    105105 
    106106        // Attempt to open 
    107         mOSFileHandle = ::open(writeFilename.c_str(), O_WRONLY | O_CREAT, 
     107        mOSFileHandle = ::open(writeFilename.c_str(),  
     108                O_WRONLY | O_CREAT | O_BINARY, 
    108109                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); 
    109110        if(mOSFileHandle == -1) 
     
    116117        int errnoBlock = EWOULDBLOCK; 
    117118        if(::flock(mOSFileHandle, LOCK_EX | LOCK_NB) != 0) 
    118 #else 
     119#elif HAVE_DECL_F_SETLK 
    119120        int errnoBlock = EAGAIN; 
    120121        struct flock desc; 
     
    124125        desc.l_len = 0; 
    125126        if(::fcntl(mOSFileHandle, F_SETLK, &desc) != 0) 
     127#else 
     128        int errnoBlock = ENOSYS; 
     129        if (0) 
    126130#endif 
    127131        { 
     
    243247         
    244248        // Rename it into place -- BEFORE it's closed so lock remains 
     249 
     250#ifdef WIN32 
     251        // Except on Win32 which doesn't allow renaming open files 
     252        // Close file... 
     253        if(::close(mOSFileHandle) != 0) 
     254        { 
     255                THROW_EXCEPTION(RaidFileException, OSError) 
     256        } 
     257        mOSFileHandle = -1; 
     258#endif // WIN32 
     259 
    245260        RaidFileController &rcontroller(RaidFileController::GetController()); 
    246261        RaidFileDiscSet rdiscSet(rcontroller.GetDiscSet(mSetNumber)); 
     
    249264        // And the current name 
    250265        std::string renameFrom(renameTo + 'X'); 
     266 
     267#ifdef WIN32 
     268        // need to delete the target first 
     269        if(::unlink(renameTo.c_str()) != 0 &&  
     270                GetLastError() != ERROR_FILE_NOT_FOUND) 
     271        { 
     272                THROW_EXCEPTION(RaidFileException, OSError) 
     273        } 
     274#endif 
     275 
    251276        if(::rename(renameFrom.c_str(), renameTo.c_str()) != 0) 
    252277        { 
     
    254279        } 
    255280         
     281#ifndef WIN32    
    256282        // Close file... 
    257283        if(::close(mOSFileHandle) != 0) 
     
    260286        } 
    261287        mOSFileHandle = -1; 
     288#endif // !WIN32 
    262289         
    263290        // Raid it? 
     
    293320         
    294321        // Unlink and close it 
    295         if((::unlink(writeFilename.c_str()) != 0) 
    296                 || (::close(mOSFileHandle) != 0)) 
     322 
     323#ifdef WIN32 
     324        // On Win32 we must close it first 
     325        if (::close(mOSFileHandle) != 0 || 
     326                ::unlink(writeFilename.c_str()) != 0) 
     327#else // !WIN32 
     328        if (::unlink(writeFilename.c_str()) != 0 || 
     329                ::close(mOSFileHandle) != 0) 
     330#endif // !WIN32 
    297331        { 
    298332                THROW_EXCEPTION(RaidFileException, OSError) 
     
    389423        { 
    390424#if HAVE_DECL_O_EXLOCK 
    391                 FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK)> stripe1(stripe1FilenameW.c_str()); 
    392                 FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK)> stripe2(stripe2FilenameW.c_str()); 
    393                 FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK)> parity(parityFilenameW.c_str()); 
     425                FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK | O_BINARY)> stripe1(stripe1FilenameW.c_str()); 
     426                FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK | O_BINARY)> stripe2(stripe2FilenameW.c_str()); 
     427                FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK | O_BINARY)> parity(parityFilenameW.c_str()); 
    394428#else 
    395                 FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL)> stripe1(stripe1FilenameW.c_str()); 
    396                 FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL)> stripe2(stripe2FilenameW.c_str()); 
    397                 FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL)> parity(parityFilenameW.c_str()); 
     429                FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_BINARY)> stripe1(stripe1FilenameW.c_str()); 
     430                FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_BINARY)> stripe2(stripe2FilenameW.c_str()); 
     431                FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_BINARY)> parity(parityFilenameW.c_str()); 
    398432#endif 
    399433 
     
    531565                stripe2.Close(); 
    532566                stripe1.Close(); 
     567 
     568#ifdef WIN32 
     569                // Must delete before renaming 
     570                if (::unlink(stripe1Filename.c_str()) != 0 && errno != ENOENT) 
     571                { 
     572                        THROW_EXCEPTION(RaidFileException, OSError); 
     573                } 
     574                if (::unlink(stripe2Filename.c_str()) != 0 && errno != ENOENT) 
     575                { 
     576                        THROW_EXCEPTION(RaidFileException, OSError); 
     577                } 
     578                if (::unlink(parityFilename.c_str()) != 0 && errno != ENOENT) 
     579                { 
     580                        THROW_EXCEPTION(RaidFileException, OSError); 
     581                } 
     582#endif 
    533583                 
    534584                // Rename them into place 
Note: See TracChangeset for help on using the changeset viewer.