Working on converting an old app of mine to Cocoa and have this code that tests if two files are equal. Analyzing the project I am told that MacDirID is deprecated. Anyone have suggestions on how to test if two folderitems are referencing the same file on disk. This is for OS X only.
[code]equals(Extends f As FolderItem, g As FolderItem) As Boolean
// this method is passed two folderitems. it returns true if the two folderitems
// refer to the same file/folder and false if they don’t
if f = nil or g = nil then
Return false
else
Return (f.MacVRefNum = g.MacVRefNum and f.MacDirID = g.MacDirID and f.Name = g.Name)
end if
End Function
[/code]
I will happily take a reference to something in MBS to do this, if there is one; and, I will also happily take any non-MBS code that will test if two folderitems point to the same file on disk in OS X.
I’m interested in this. I assume that those functions aren’t “not working” yet. As far as NativePath, i can’t use it, still on REAL 2011r3 (for very good reasons!).
A declare to FSCompareFSRefs could be used on OS X, passing in the each item’s MacFSRef value. Note that FSCompareFSRefs is deprecated by Apple as of 10.8, along with all of the other functions that deal with FSRefs.
Having almost zero knowledge of declares into the realms of OS X I will go with Mr. S’s and Mr. Tekinay’s answers regarding NativePath. Thanks for the replies to all who gave them. Tis appreciated.
The reason NOT to use AbsolutePath etc is because Mac users (unlike Windows users) expect an app to find the document even if it has been moved (for example from a RecentItems menu).
As FSRef is deprecated how does one deal with this now?
@Harrie Westphal Actually your code is still the best way to do it, essentially because 2 different file paths can resolve to the same file (“hard links”) so any path is useless.
MacDirID and MacVRefNum may be deprecated by Xojo, but they are similar to the underlying UNIX “Device ID” and “inode”, so you can easily replace both functions by a declare:
[code]declare function stat lib "/usr/lib/libc.dylib" (path as CString, byref buf as stat) as integer
dim buf as stat //stat is a structure defined below
dim result as integer = stat(path, buf ) //path is the NativePath of the file
if result <> -1 then
//Here you can get buf.st_dev and buf.st_ino which are the values you need
//…
else
BREAK //Error
end if[/code]
The structure ‘stat’ is declared as
st_dev as UInt32 //<<<< The device ID (MacVRefNum)
st_ino as UInt32 //<<<< The inode (MacDirID)
st_mode as UInt16
st_nlink as UInt16
st_uid as UInt32
st_gid as UInt32
st_rdev as UInt32
st_atimespec as timespec
st_mtimespec as timespec
st_ctimespec as timespec
st_size as Int64
st_blocks as Int64
st_blksize as UInt32
st_flags as UInt32
st_gen as UInt32
st_lspare_DONOTUSE as Int32
st_qspare_DONOTUSE(1) as Int64