Setting NSFileCreationDate does not affect

I am trying to set the creation and change dates in a file.

NSFileManager *fileManager = [NSFileManager defaultManager]; NSDate *now = [NSDate date]; NSDictionary *timestampAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileCreationDate, now, NSFileModificationDate, nil]; BOOL ret = [fileManager setAttributes:timestampAttr ofItemAtPath:path error:&err]; 

Date modified successfully changed for file. Creation date has not changed. What for?

ret true, and err is zero. -attributesOfItemAtPath returns a dictionary containing both keys in timestampAttr , and the correct (modified) modification date and the incorrect (unmodified) creation date.

Edit: Use OS X version 10.6. The base SDK of the Xcode project is 10.5. The file is located on my computer only on the hard drive (without RAID), in a folder inside my home folder. The file is inside the application package, if that matters.

Edit: This simple example works:

 #import <Cocoa/Cocoa.h> int main(void) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSFileManager *m = [NSFileManager defaultManager]; NSString *path = ...; [m setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSDate date],NSFileCreationDate,nil] ofItemAtPath:path error:nil]; [pool drain]; return 0; } 

I pasted the code above into the application that I originally asked about, setting the attributes for the same file as the simple example (on my desktop). When the code is inside the application, it does not work.

Edit: Alright, this is crazy. The simple example above worked (my colleague and I both saw how it worked on my computer), but now it does not work.

+6
source share
2 answers

After another game with a simple example, I noticed that it only works for files that I recently changed.

I was able to get a simple example, as well as the source code that I asked for, to work consistently by setting the change date before setting the creation date.

 NSFileManager *fileManager = [NSFileManager defaultManager]; NSDate *now = [NSDate date]; NSDictionary *creationDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileCreationDate, nil]; NSDictionary *modificationDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileModificationDate, nil]; [fileManager setAttributes:modificationDateAttr ofItemAtPath:path error:&err]; [fileManager setAttributes:creationDateAttr ofItemAtPath:path error:&err]; 
+9
source

What version of OS X? Where is the target file located? What is the disk format of the volume on which the file is located? What is your setup on your Mac (do you have software RAID)?

The test here, on Mac OS X 10.7, with a file on my desktop, and HFS + works just fine (both dates changed to now ).

I have had some weird results in the past with some of the new NSFileManager APIs. For example, -copyItemAtPath:toPath:error: does not save the file creation date when copying files on HFS + volumes. In particular, the date of creation is cleared in a copy, which corresponds to the date in 1904.

Knowing your setup can help determine the cause.

0
source

Source: https://habr.com/ru/post/894072/


All Articles