Rollback function or design pattern in C ++

Now I am faced with a new problem, and I cannot figure out how to fix it. I have two files. One is a video file and the other is a thumbnail. They have the same name. I want to rename these two files using C ++. I use a function renameand it works. This is what I wrote:

if(rename(videoFile) == 0)
{
     if(rename(thumbnail) != 0)
     {
         printf("Fail rename \n");
     }
}

The problem occurs when the video file is successfully renamed, but for some reason the thumbnail cannot be renamed. When this happens, I would like to cancel the renaming of the video file, because the name of the video file and the name of the thumbnail file in my program must be the same. What I want to do is rename after both files have been renamed well. Please be guided by me, any design pattern for a function, such as rollback or third-party software.

+4
source share
2 answers

There is no absolutely safe way to do this.

-: . , ; . , ; - , , , .

, , , 90% , , . , A B C D. :

, - - , , , , ( ), .

( O_RDONLY) fsync(2) , , Unix. , , . , , ext3. Linus , DTRT , POSIX. Windows, MSDN . , Windows API ( open() , , fsync()).

Nitpick. - , , , - NTFS Microsoft . / btrfs , .

+2

Windows, Vista, .

#include "KtmW32.h"

bool RenameFileTransact( LPCTSTR lpctszOldVideoFile, LPCTSTR  lpctszNewVideoFile, LPCTSTR lpctszOldThumbnailFile, LPCTSTR  lpctszNewThumbnailFile )
{
    bool bReturn = false;
    HANDLE hRnameTransaction = CreateTransaction(NULL, NULL, 0, 0, 0, 0, NULL);
    if (MoveFileTransacted(lpctszOldVideoFile, lpctszNewVideoFile, NULL, NULL, 0, hRnameTransaction) &&
        MoveFileTransacted(lpctszOldThumbnailFile, lpctszNewThumbnailFile, NULL, NULL, 0, hRnameTransaction))
    {
        if ( CommitTransaction(hRnameTransaction))
        {
            bReturn = true;
        }
    }
    CloseHandle( hRnameTransaction );
    return bReturn;
}

, , Microsoft .

+1

Source: https://habr.com/ru/post/1617933/


All Articles