开发者

Setting NSFileCreationDate has no effect

开发者 https://www.devze.com 2023-03-24 17:30 出处:网络
I\'m trying to set the creation and modification dates on a file. NSFileManager *fileManager = [NSFileManager defaultManager];

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) modificati开发者_如何学编程on 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.


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];


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 kind of setup do you have on your Mac (do you have a Software RAID by any chance)?

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

I have had some strange results here in the past with some of the newer NSFileManager APIs. For example, -copyItemAtPath:toPath:error: does not preserve the file creation date when copying files on HFS+ volumes. Specifically, the creation date is cleared in the copy, which equates to a date in the year 1904.

Knowing more about your setup may help pinpoint the cause.

0

精彩评论

暂无评论...
验证码 换一张
取 消