The ideal solution to one of my problems is to, given a drive (e.g., partition / what _stat returns in st_dev / volume GUID), create a HANDLE or FILE* physically on that drive that can be written to but doesn't have a way to access it by a path (e.g., there are no hardlinks to it). I can later move (/create the first hardlink to) it when I want to "commit" the file, but if the program is closed early, nothing should be done (and the file is free space so can be reused by the filesystem). Is this possible?
I am currently doing this by creating file_name + ".tmp" then moving it to file_name when done, but some ".tmp" files are leftover if there is an unexpected early exit. FILE_FLAG_DELETE_ON_CLOSE doesn't seem to do what I later want to "keep" the file.
I really need to avoid creating a partial file_name, since what I am doing is copying from another device, and it should just be retried in full later if there is a failure.
This is on an NTFS drive, so NTFS-specific solutions are appreciated.
Windows lets you mark a file as such that will be deleted on close. The flag may be set and removed. So, when you open a file, you need to set a flag - then, if your application is closed prematurely, the file is deleted by the filesystem. When writing to the file is complete, your application removes the flag and closes the file, and the data stays.
To set and remove the flag, use the NtSetInformationFile function ( see MSDN and Is there a user space equivalent of NtSetInformationFile? for some detals).
You need to set FileInformationClass to FileDispositionInformationEx and use the corresponding structure ( MSDN ).
That structure has only one field, Flags, and you need to specify the corresponding flag there. My understanding is that you need FILE_DISPOSITION_DELETE and FILE_DISPOSITION_DO_NOT_DELETE, and I am not sure about what the FILE_DISPOSITION_ON_CLOSE flag would do in your situation.