我如何检测文件是否已使用Cocoa重命名?

时间:2022-01-12 01:23:02

I'm building a utility application that synchronizes files across two systems for Mac OSX. I need to detect when a file has been renamed but is otherwise the same file. How do I do this in Cocoa?

我正在构建一个实用程序应用程序,用于为Mac OSX跨两个系统同步文件。我需要检测文件何时被重命名,否则是同一个文件。我怎么在Cocoa中做到这一点?

5 个解决方案

#1


There's no simple answer; you need to figure out the best strategy for your app.

没有简单的答案;你需要为你的应用找出最好的策略。

At a simple level there is working with the file system number. You can grab this using NSFileSystemFileNumber. Probably better for the job though is to use FSRef. It's a C API but relatively straightforward, and has a method for comparing to FSRefs for equality.

在简单的级别上,可以使用文件系统编号。你可以使用NSFileSystemFileNumber来获取它。虽然使用FSRef可能对工作更好。它是一个C API但相对简单,并且有一个与FSRef进行比较以获得相等性的方法。

But, there are plenty of applications which perform a save operation by replacing the file on disk, changing its file number. This could well upset your code. So consider using aliases. This is the same system as the Finder uses to keep track of the target of an alias file. Use either the Alias Manager (C API), or one of the open source Objective-C wrappers (e.g. NDAlias or BDAlias). An alias will do its best to maintain a reference to a file by both path and file number.

但是,有很多应用程序通过替换磁盘上的文件,更改其文件号来执行保存操作。这可能会破坏您的代码。所以考虑使用别名。这与Finder用于跟踪别名文件目标的系统相同。使用Alias Manager(C API)或其中一个开源Objective-C包装器(例如NDAlias或BDAlias)。别名将尽力通过路径和文件编号维护对文件的引用。

#2


A couple of additional details that I've come across:

我遇到的其他一些细节:

If the file is only renamed, then the file will retain the same inode (NSFileSystemFileNumber).

如果仅重命名该文件,则该文件将保留相同的inode(NSFileSystemFileNumber)。

However, if the file was modified, Mac OS X will generally replace the old file with a copy of the new file, so the inode numbers will not be the same.

但是,如果文件被修改,Mac OS X通常会用新文件的副本替换旧文件,因此inode编号将不相同。

If you edit a text file in TextEdit and then save it, you might notice the file disappearing and then reappearing in the Finder. However, if you edit this file using a command line editor (vi, pico, emacs, etc.), it will save to the file directly.

如果在TextEdit中编辑文本文件然后保存它,您可能会注意到文件消失然后再次出现在Finder中。但是,如果使用命令行编辑器(vi,pico,emacs等)编辑此文件,它将直接保存到文件中。

My guess is that Cocoa apps will save to a temp file first, and then replace the original version of the file, which explains why the inodes are different.

我的猜测是Cocoa应用程序将首先保存到临时文件,然后替换文件的原始版本,这解释了为什么inode不同。

#3


You can look at the inode number (NSFileSystemFileNumber in the attributes returned by NSFileManager), which would cover simple rename cases.

您可以查看inode编号(NSFileManager返回的属性中的NSFileSystemFileNumber),它将涵盖简单的重命名案例。

#4


I assume that you are targeting at the content of the files, not their attributes. The most easy way is to calculate and compare hashes. Just read the two files block by block and compare the hashes, e. g. with these MD5 routines.

我假设您的目标是文件的内容,而不是其属性。最简单的方法是计算和比较哈希值。只需逐块读取这两个文件并比较哈希值,例如: G。使用这些MD5例程。

#5


Everything mentioned above will fail if you have source and destination on different platforms/OS. Best way is to get a list of New files and Missing files in both source and destination locations and then if any two files have same size? if they have same size compare there Checksum/CRC and if this is same, than one of them has been renamed.

如果您在不同平台/操作系统上拥有源和目标,则上述所有内容都将失败。最好的方法是在源位置和目标位置获取新文件和丢失文件的列表,然后如果任何两个文件具有相同的大小?如果它们具有相同的大小,则比较校验和/ CRC,如果相同,则重命名其中一个。

#1


There's no simple answer; you need to figure out the best strategy for your app.

没有简单的答案;你需要为你的应用找出最好的策略。

At a simple level there is working with the file system number. You can grab this using NSFileSystemFileNumber. Probably better for the job though is to use FSRef. It's a C API but relatively straightforward, and has a method for comparing to FSRefs for equality.

在简单的级别上,可以使用文件系统编号。你可以使用NSFileSystemFileNumber来获取它。虽然使用FSRef可能对工作更好。它是一个C API但相对简单,并且有一个与FSRef进行比较以获得相等性的方法。

But, there are plenty of applications which perform a save operation by replacing the file on disk, changing its file number. This could well upset your code. So consider using aliases. This is the same system as the Finder uses to keep track of the target of an alias file. Use either the Alias Manager (C API), or one of the open source Objective-C wrappers (e.g. NDAlias or BDAlias). An alias will do its best to maintain a reference to a file by both path and file number.

但是,有很多应用程序通过替换磁盘上的文件,更改其文件号来执行保存操作。这可能会破坏您的代码。所以考虑使用别名。这与Finder用于跟踪别名文件目标的系统相同。使用Alias Manager(C API)或其中一个开源Objective-C包装器(例如NDAlias或BDAlias)。别名将尽力通过路径和文件编号维护对文件的引用。

#2


A couple of additional details that I've come across:

我遇到的其他一些细节:

If the file is only renamed, then the file will retain the same inode (NSFileSystemFileNumber).

如果仅重命名该文件,则该文件将保留相同的inode(NSFileSystemFileNumber)。

However, if the file was modified, Mac OS X will generally replace the old file with a copy of the new file, so the inode numbers will not be the same.

但是,如果文件被修改,Mac OS X通常会用新文件的副本替换旧文件,因此inode编号将不相同。

If you edit a text file in TextEdit and then save it, you might notice the file disappearing and then reappearing in the Finder. However, if you edit this file using a command line editor (vi, pico, emacs, etc.), it will save to the file directly.

如果在TextEdit中编辑文本文件然后保存它,您可能会注意到文件消失然后再次出现在Finder中。但是,如果使用命令行编辑器(vi,pico,emacs等)编辑此文件,它将直接保存到文件中。

My guess is that Cocoa apps will save to a temp file first, and then replace the original version of the file, which explains why the inodes are different.

我的猜测是Cocoa应用程序将首先保存到临时文件,然后替换文件的原始版本,这解释了为什么inode不同。

#3


You can look at the inode number (NSFileSystemFileNumber in the attributes returned by NSFileManager), which would cover simple rename cases.

您可以查看inode编号(NSFileManager返回的属性中的NSFileSystemFileNumber),它将涵盖简单的重命名案例。

#4


I assume that you are targeting at the content of the files, not their attributes. The most easy way is to calculate and compare hashes. Just read the two files block by block and compare the hashes, e. g. with these MD5 routines.

我假设您的目标是文件的内容,而不是其属性。最简单的方法是计算和比较哈希值。只需逐块读取这两个文件并比较哈希值,例如: G。使用这些MD5例程。

#5


Everything mentioned above will fail if you have source and destination on different platforms/OS. Best way is to get a list of New files and Missing files in both source and destination locations and then if any two files have same size? if they have same size compare there Checksum/CRC and if this is same, than one of them has been renamed.

如果您在不同平台/操作系统上拥有源和目标,则上述所有内容都将失败。最好的方法是在源位置和目标位置获取新文件和丢失文件的列表,然后如果任何两个文件具有相同的大小?如果它们具有相同的大小,则比较校验和/ CRC,如果相同,则重命名其中一个。