Создание\удаление файла в цикле
От: tdiff  
Дата: 31.10.16 09:37
Оценка:
Всем привет,

Наткнулся в просторах сети на вот такую заметку: https://boostgsoc13.github.io/boost.afio/doc/html/afio/FAQ/deleting_open_files.html. В частности, заинтересовал следующий момент:

... when the very last open handle to the file in the system is closed, only then is it truly deleted. Well, actually only sort of truly deleted, because Windows only appears to remove the file entry from the directory, but in fact that entry is merely hidden and actually still exists and attempting to create a file with the same name will return an access denied error. How long it silently exists for depends on a range of factors, but put it this way: if your code loops creating and deleting the same file name as you might when operating a lock file, you're going to see lots of random spurious access denied errors.



Я попробовал добиться этого таким нехитрым образом, но ничего не сломалось (Win7):
for (int i=0; i<10000; ++i) {                                                                                                                     
    HANDLE file = CreateFileA("test.file", GENERIC_WRITE, FILE_SHARE_DELETE, NULL, CREATE_NEW, 0, INVALID_HANDLE_VALUE);
    if (file == INVALID_HANDLE_VALUE) {                                                                                                                   
        std::cout << "c " << i << " " << GetLastError() << std::endl;                                                   
    }                                                                                                                   
    else {                                                                                                                   
        if (0 == DeleteFileA("test.file")) {                                                                                                               
            std::cout << "d " << i << " " << GetLastError() << std::endl;                                               
        }                                                                                                               
        CloseHandle(file);                                                                                              
    }                                                                                                                   
}


Значит ли это, что статья устарела, или я неправильно понял, что там написано и эту ситуацию можно воспроизвести как-то по-другому?
Отредактировано 31.10.2016 9:37 tdiff . Предыдущая версия .
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.