Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange timestamp duplication when renaming and recreating a file

I'm trying to rename a log file named appname.log into the form appname_DDMMYY.log for archiving purposes and recreate an empty appname.log for further writing. When doing this in Windows 7 using C++ and either WinAPI or Qt calls (which may be the same internally) the newly created .log file strangely inherits the timestamps (last modified, created) from the renamed file. This behaviour is also observable when renaming a file in Windows Explorer and creating a file with the same name quickly afterwards in the same directory. But it has to be done fast. After clicking on "new Text File" the timestamps are normal but after renaming they change to the timestamps the renamed file had or still has.

Is this some sort of Bug? How can I rename a file and recreate it shortly afterwards without getting the timestamps messed up?

like image 679
Wopfi Avatar asked Feb 02 '15 19:02

Wopfi


1 Answers

This looks like it is by design, perhaps to try to preserve the time for "atomic saving." If an application does something like (save to temp, delete original, rename temp to original) to eliminate the risk of a mangled file, every time you saved a file the create time would increase. A file you have been editing for years would appear to have been created today. This kind of save pattern is very common.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx If you rename or delete a file, then restore it shortly thereafter, Windows searches the cache for file information to restore. Cached information includes its short/long name pair and creation time. Notice that modification time is not restored. So after saving the file appears to have been modified and the creation time is the same as before.

If you create "a-new" and rename it back to "a" you get the old creation time of "a". If you delete "a" and recreate "a" you get the old creation time of "a".

like image 69
DaveSawyer Avatar answered Oct 14 '22 01:10

DaveSawyer