You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
windows: attribute 'archive' only for completed downloads #3354
The archive bit (FILE_ATTRIBUTE_ARCHIVE, 0x20) tells files that shall be backed up from those that are either not ready or have not changed.
Downloads in progress are neither ready to be backed up, nor should they be opened by a different process. Only after a download has been completed it's sensible to include it in any integer snapshot or backup of the system.
For example, if you were downloading a Linux ISO you don't want to carry a fraction of it through the history of your backup sets. Indeed, it will give the wrong impression this were the file's valid state.
Open two command prompts, one to start the download and the second to see attributes. We'll focus on the output of the second while the download is in progress.
curl.exe --remote-name <URL>/<fname>
attrib <fname>
… which gives:
A <fname>
… but the archive bit shouldn't be set at this point, so this is what we'd ideally see (with or without any FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS):
<fname>
Happens with curl 7.61.1 (x86_64-pc-win32)
using Windows 10 1809, though likely not limited to 10 1809 or 7.61.1.
We're using good old fopen() to create new files so clearly Windows then gives it that attribute.
But why is it curl's job to tell what files that should be backed up or not? curl creates files on request, if that should be backed up or not while being transferred seems like someone else's decision.
Your assessment is correct. Nonetheless, if curl stripped that bit after fopen() and set it after a complete download (no matter what), it would amount to a much friendlier behaviour. For the reasons described above. – So far it's no bug but RFE.
Most Windows programs do this, but I believe one shouldn't follow bad examples. Although the usual practice is to create a temporary file. Curl does everything in-place, hence this becomes relevant.
Sure, but... if the almighty powers at Microsoft agree with your assessment on how this should work, why don't they just make sure that these flags are set accordingly and fully automatically since they are in full control?
I'm not actually arguing against your enhancement request, I just can't see the logic in how this works. But I'm not a Windows person, I don't use Windows, I don't backup on Windows and I will not personally work on implementing what you're suggesting (as it can only be done using windows-specific APIs and tests). If what you're suggesting is generally considered The Right Thing, then I'm sure others will give a possible future PR for this the necessary 👍
On Windows, much is not changed (evolved) for »legacy« reasons. I could tell a far better story for Linux.
Absence of divine intervention is no sign of disagreement (nor encouragement).
For my notes, and whoever else's, for a PR: I suspect it's two prayers of SetFileAttributesA
(Under Linux you avoid the create-times-attribs race condition by opening, trunc, (fallocate,) (madvise,), attribs, time. Ideally with an initial O_TMPFILE and linking the result into existence. Windows doesn't have the latter but said cludge, the archive attribute.)
We could probably do this with CreateFile and _open_osfhandle to avoid the race condition, I will take a look.
I tried it but it doesn't work as I expected, refer to curl:master...jay:win32_disable_archive_bit_until_close. Basically Windows can turn on the archive bit whenever the file is modified and AFAICS we can't really stop that.
What about an option to disable shared read, this way when curl opens an output file for writing it cannot be read by other programs until the file is closed?
Or how about an option to use something temporary (like this fopen_excl_genfile from an old example)
@jay are you planning on turning this work into a real PR? If not, I rather make this a TODO item and close this issue for now.
No because it doesn't work the way I expected, it is due to the nature of the OS that we can't make the guarantee AFAICT so there's nothing really TODO. I don't know how we can solve this in a palatable way and the alternate ideas don't have any traction. If the file must only contain the output of a valid completed transfer (or whatever) then the onus is on the user to, for example, download to a temporary file and if exit code 0 and http code 200 then replace the older version.
Activity
bagder commentedon Dec 9, 2018
We're using good old
fopen()to create new files so clearly Windows then gives it that attribute.But why is it curl's job to tell what files that should be backed up or not? curl creates files on request, if that should be backed up or not while being transferred seems like someone else's decision.
mark-kubacki commentedon Dec 9, 2018
Your assessment is correct. Nonetheless, if curl stripped that bit after
fopen()and set it after a complete download (no matter what), it would amount to a much friendlier behaviour. For the reasons described above. – So far it's no bug but RFE.Most Windows programs do this, but I believe one shouldn't follow bad examples. Although the usual practice is to create a temporary file. Curl does everything in-place, hence this becomes relevant.
Unfortunately the workaround:
… will not work because now unexpectedly curl re-sets the attribute (retaining all other, like
IorT).bagder commentedon Dec 9, 2018
... because that's what fopen() does.
mark-kubacki commentedon Dec 9, 2018
Prematurely, and as part of curl. ;-)
I think the same reasoning can be applied to flags like
--cacert: Why use it if Windows' good old whatever exists.Properly indicating that and when something is ready to be archived (or not) is an handy advancement.
bagder commentedon Dec 9, 2018
Sure, but... if the almighty powers at Microsoft agree with your assessment on how this should work, why don't they just make sure that these flags are set accordingly and fully automatically since they are in full control?
I'm not actually arguing against your enhancement request, I just can't see the logic in how this works. But I'm not a Windows person, I don't use Windows, I don't backup on Windows and I will not personally work on implementing what you're suggesting (as it can only be done using windows-specific APIs and tests). If what you're suggesting is generally considered The Right Thing, then I'm sure others will give a possible future PR for this the necessary 👍
mark-kubacki commentedon Dec 9, 2018
On Windows, much is not changed (evolved) for »legacy« reasons. I could tell a far better story for Linux.
Absence of divine intervention is no sign of disagreement (nor encouragement).
For my notes, and whoever else's, for a PR: I suspect it's two prayers of
SetFileAttributesA(Under Linux you avoid the create-times-attribs race condition by opening, trunc, (fallocate,) (madvise,), attribs, time. Ideally with an initial
O_TMPFILEand linking the result into existence. Windows doesn't have the latter but said cludge, the archive attribute.)dscho commentedon Dec 14, 2018
@wmark how about working on this? Should be fun.
And yes, the
SetFileAttributes()function would be the correct thing to use here (for inspiration: Git for Windows uses the same for setting the hidden flag: https://github.com/git-for-windows/git/blob/v2.20.0.windows.1/compat/mingw.c#L593-L604).jay commentedon Dec 14, 2018
We could probably do this with CreateFile and _open_osfhandle to avoid the race condition, I will take a look.
jay commentedon Dec 15, 2018
What about an option to disable shared read, this way when curl opens an output file for writing it cannot be read by other programs until the file is closed?
Or how about an option to use something temporary (like this fopen_excl_genfile from an old example)
bagder commentedon Jan 10, 2019
@jay are you planning on turning this work into a real PR? If not, I rather make this a TODO item and close this issue for now.
jay commentedon Jan 20, 2019
No because it doesn't work the way I expected, it is due to the nature of the OS that we can't make the guarantee AFAICT so there's nothing really TODO. I don't know how we can solve this in a palatable way and the alternate ideas don't have any traction. If the file must only contain the output of a valid completed transfer (or whatever) then the onus is on the user to, for example, download to a temporary file and if exit code 0 and http code 200 then replace the older version.