Question

I want to (bidirectionally) synchronize files between a PC (Win7, NTFS) and a Mac (OS X, HFS+). I have tried using a lot of existing tools / methods (Unison, SyncToy or other software working over Samba shares etc.) but none of them are able to preserve the file creation timestamps (hence-forward referred to as FCTs) on my files, which is unacceptable to me.

Unison doesn't understand the notion of FCTs, perhaps because it was primarily built with Unix in mind (See my Unison forum post)

Windows cannot get (or set) the FCTs when accessing SMB shares served by OS X. In such scenario, it shows and sets file modification times in place of FCTs (Samba is to be blamed here though, not Windows), so something like SyncToy can't be used.

OS X on the other hand can however get and set FCTs when working with SMB shares served by Windows. This was a good find as I thought I could then write a Python program that would run on OS X do the file synchronization between local files and the SMB share, using cp to copy files. This turned out to be a no-go too when I found that the FCTs are not preserved with the cp -p option. Only while copying files through the Finder GUI are the FCTs preserved.

So I've decided to write a file synchronization utility myself, likely using Python, an instance of which will run on the PC and Mac each, communicating over ssh. Since Python doesn't have a reliable way itself to get or set FCTs, I've decided to build a native helper application for each platform that Python would talk to for this purpose. (Either that or write Python modules in C, but I believe that'd be more complex to do)

That brings me to the ask - What's the API to set file timestamps in OS X?

I'm looking to build a console application that my Python script could communicate with over STDIN & STDOUT. Basically I'm looking for the OS X equivalents of GetFileTime and SetFileTime

Something I could do in C would be great as I have no experience developing in Objective C.

Was it helpful?

Solution

The Cocoa way to change file attributes would be to use NSFileManager:

NSError* error = nil;
NSString* filePath = [@"~/Desktop/test.txt" stringByExpandingTildeInPath];
if(![[NSFileManager defaultManager] setAttributes:@{NSFileCreationDate : [NSDate date]} ofItemAtPath:filePath error:&error])
{
    NSLog(@"Setting file creation date attribute failed with error:%@", error);
}

You could use the above code in a Foundation command line utility.

If you prefer plain C, you could use POSIX stat instead: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/stat.2.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top