Test if 2 folderitems point to same file on disk

  1. 5 years ago

    Harrie W

    5 Sep 2014 Crossville, Tennessee, USA

    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.

    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

    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.

    Just a thought... but would not this work?

    if f1=nil and f2=nil then return true
    if f1<>Nil and f2<>nil and f1.nativepath=f2.nativepath then return true
    return false

  2. Tim P

    5 Sep 2014 Pre-Release Testers Rochester, NY

    Rather than pretending I know what I'm talking about with carbon related items, I've got a link for you.
    You may find this post by Thomas Tempelmann helpful:
    https://forum.xojo.com/5619-questions-about-tt-s-zip-package/p2#p103843

  3. Kem T

    5 Sep 2014 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    In an Extends function, f will never be nil.

    I might be missing the issue. Why can't you compare NativePath?

  4. 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!).

  5. Kem T

    5 Sep 2014 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    AbsolutePath then?

  6. Dave S

    5 Sep 2014 Answer San Diego, California USA

    Just a thought... but would not this work?

    if f1=nil and f2=nil then return true
    if f1<>Nil and f2<>nil and f1.nativepath=f2.nativepath then return true
    return false

  7. Joe R

    6 Sep 2014 Xojo Inc

    @Garth H 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.

  8. Harrie W

    6 Sep 2014 Crossville, Tennessee, USA

    Having almost zero knowledge of declares into the realms of OS X I will go with Mr. Sisemore's and Mr. Tekinay's answers regarding NativePath. Thanks for the replies to all who gave them. Tis appreciated.

  9. 4 months ago

    Markus W

    Jan 11 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

    @Joe R 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.

    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?

  10. Christian S

    Jan 12 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    compare URLPath.

  11. Edited 4 months ago

    @Harrie W — 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:

    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

    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

  12. Christian S

    Jan 15 Pre-Release Testers, Xojo Pro, XDC Speakers Germany
    Edited 4 months ago

    folderitem.MacNodeIDMBS In our plugins could help, too.

or Sign Up to reply!