API在OS X中设置文件时间戳 [英] API to set file timestamps in OS X

查看:156
本文介绍了API在OS X中设置文件时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想(双向)在PC(Win7,NTFS)和Mac(OS X,HFS +)之间同步文件。我试过使用很多现有的工具/方法(Unison,SyncToy或其他软件工作在Samba共享等),但没有一个能够保留我的文件上的文件创建时间戳(因此向前称为FCT)这是我不能接受的。

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不明白FCT的概念,也许是因为它主要是用Unix构建的(参见我的Unison论坛帖子

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

在访问由OS X提供的SMB共享时,Windows无法获取(或设置)FCT。在这种情况下,它会显示并设置文件修改时间以代替FCT(Samba将被归咎于此处,而不是Windows),所以类似 SyncToy 无法使用。

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另一方面可以获得和设置FCT在使用由Windows服务的SMB共享。这是一个很好的发现,因为我想我可以写一个Python程序,将运行在OS X上执行本地文件和SMB共享之间的文件同步,使用 cp 复制文件。当我发现FCT没有保留与 cp -p 选项时,结果是一个没有去。只有在通过Finder GUI复制文件时,才会保存FCT。

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.

所以我决定自己编写一个文件同步实用程序,可能使用Python,在PC和Mac上运行,通过ssh进行通信。由于Python没有一个可靠的方法来获取或设置FCT,我决定为每个平台构建一个本地帮助应用程序,Python会与之进行交流。 (或者在C中编写Python模块,但我相信这将是更复杂的)

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)

这让我问到 - 在OS X中设置文件时间戳?

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

我正在寻找一个控制台应用程序,我的Python脚本可以通过STDIN& STDOUT。基本上我正在寻找OS X等效的 GetFileTime SetFileTime

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

推荐答案

Cocoa更改文件属性的方法将使用 NSFileManager

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);
}

您可以在Foundation命令行实用程序中使用上述代码。

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

如果你喜欢简单的C,你可以使用POSIX stat:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/stat.2.html

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

这篇关于API在OS X中设置文件时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆