Reputation: 784
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?
Upvotes: 5
Views: 1134
Reputation: 1
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.
Upvotes: 0
Reputation: 136
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.
Upvotes: 0
Reputation: 86
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).
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.
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.
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.
Upvotes: 1
Reputation: 15013
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.
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.
Upvotes: 3
Reputation: 21629
You can look at the inode number (NSFileSystemFileNumber in the attributes returned by NSFileManager), which would cover simple rename cases.
Upvotes: 0