Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting NSFileCreationDate has no effect

I'm trying to set the creation and modification dates on 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];

The modification date is successfully modified for the file. The creation date is unchanged. Why?

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

Edit: Using OS X version 10.6. Base SDK of Xcode project is 10.5. File is on my computer's only hard drive (no RAID), in a folder inside my home folder. The file is inside of an app bundle if that makes a difference.

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 app I originally asked about, setting attributes for the same file as the simple example (on my desktop). When the code is inside the app, it does not work.

Edit: OK, this is crazy. The simple example above was working (my coworker and I both saw it working on my computer) but now it's not working.

like image 941
jlstrecker Avatar asked Aug 01 '11 23:08

jlstrecker


1 Answers

After playing around with the simple example some more, I noticed that it was only working for files that I had recently modified.

I was able to get the simple example, as well as the original code I was asking about, to work consistently by setting the modification 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];
like image 64
jlstrecker Avatar answered Oct 11 '22 01:10

jlstrecker