Eryk Sun <> added the comment:

> I afraid that removing a file while the file descriptor is open 
> may not work on Windows. Seems this case is not well tested.

os.remove will succeed on a file that's opened with O_TEMPORARY, which shares 
delete access (i.e. FILE_SHARE_DELETE).

With classic Windows delete semantics, deleting a file sets the delete 
disposition on the filesystem's underlying file/link control block (FCB/LCB). 
The filesystem doesn't unlink the file from the directory until all File object 
references have been finalized. (A File handle refers to a kernel File object, 
which refers to an FCB/LCB in a filesystem. An FCB/LCB can be referenced by 
multiple File objects, such as from multiple CreateFileW calls, and a File 
object can be referenced by multiple handles, such as via inheritance or 
DuplicateHandle.) The deleted file remains accessible to existing File objects, 
and a File object with delete access can even be used to clear the delete 
disposition (i.e. undelete the file) via SetFileInformationByHandle: 
FileDispositionInfo. The file remains linked and visible in the parent 
directory, but no new access is allowed while its delete disposition is set. 
The parent directory cannot itself be deleted until the file is unlinked.

In Windows 10 1903, DeleteFileW has also been updated to use POSIX semantics if 
the filesystem supports it, which includes NTFS on the system drive, where temp 
files are usually created. With POSIX semantics, the file is unlinked as soon 
as DeleteFileW closes its File handle. All existing File objects can continue 
to access the file even though it's no longer linked in the directory. This 
includes being able to call GetFinalPathNameByHandleW, which, at least for 
NTFS, will show that the file has been moved to the special system directory 
"\$Extend\$Deleted" and renamed according to its file ID. As is usual for a 
deleted file, no new access is allowed, i.e. we cannot reopen a file in the 
"$Deleted" directory. Another change with POSIX semantics is that the delete is 
final. Existing File objects that have delete access are not allowed to clear 
the delete disposition (i.e. undelete the file).

nosy: +eryksun

Python tracker <>
Python-bugs-list mailing list

Reply via email to