Do two file names point to the same file? - Programmer

This is a discussion on Do two file names point to the same file? - Programmer ; Hi, in a function i have two file names and i'd like to test if these file names point to the same file. For example if one file name is a symbolic link that points to the other file name, ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Do two file names point to the same file?

  1. Do two file names point to the same file?

    Hi,

    in a function i have two file names and i'd like
    to test if these file names point to the same file.

    For example if one file name is a symbolic link that
    points to the other file name, i'd like to detect
    that.

    If just the data in those two files are the same then
    i could delete one of them.


    I'm more used to program in unix, there there is the
    function stat(char* filename, struct stat* buf)
    that tells me on which filesystem the file is and
    gives me a unique number that describes a file.
    If for two filenames the filesystem and the unique
    id (inode) is the same, then the filenames point to
    the same file.

    Is some test like this available on Windows?


    Thanks for any hints,
    Torsten.



  2. Re: Do two file names point to the same file?

    Torsten Mohr wrote:
    > Hi,
    >
    > in a function i have two file names and i'd like
    > to test if these file names point to the same file.
    >
    > For example if one file name is a symbolic link that
    > points to the other file name, i'd like to detect
    > that.



    Are you really talking about symbolic links? There is not anything like
    symbolic links at filesystem level in Windows. There are Shell Link
    files, called shortcuts, and there are also a kind of symbolic links
    implemented in a Posix subsystem called Interix, but no general solution
    at filesystem level.

    But if you really mean hard links I have an answer.

    > Is some test like this available on Windows?



    Yes, GetFileInformationByHandle() is probably what you are looking for.

    http://msdn2.microsoft.com/en-us/library/aa364952.aspx

    It returns a BY_HANDLE_FILE_INFORMATION structure and the unique id for
    the file, called the Index, and the change count, called the Sequence,
    are returned in the nFileIndexHigh and nFileIndexLow fields. This is not
    very well documented so it has lead to many mistakes by programmers
    using that data over the years.

    Example:
    #include
    #include

    int main()
    {
    // Open the file
    HANDLE h = CreateFile("filename.txt", 0,
    FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
    OPEN_EXISTING, 0, NULL);

    // Get information
    BY_HANDLE_FILE_INFORMATION info;
    GetFileInformationByHandle(h, &info);

    // The sequence is the high-order word in nFileIndexHigh field
    WORD FileSequence = HIWORD(info.nFileIndexHigh);

    // The index is the rest of nFileIndexHigh and nFileIndexLow
    // fields. Put the fields in a ULARGE_INTEGER structure and
    // mask away the sequence number.
    ULARGE_INTEGER FileIndex;
    FileIndex.HighPart = info.nFileIndexHigh;
    FileIndex.LowPart = info.nFileIndexLow;
    FileIndex.QuadPart &= 0x0000FFFFFFFFFFFF;

    printf("Sequence: %u\n", FileSequence);
    printf("Index: %I64i\n", FileIndex.QuadPart);
    }

    Note also that if you want to compare the file index numbers for two
    files you also need to compare the dwVolumeSerialNumber fields to make
    sure that you are looking at files at the same physical volume in both
    cases. (Just as in Unix, file index numbers are only unique within a
    filesystem.) The nNumberOfLinks tells you how many links there are at
    the volume.

    --
    Olof Lagerkvist
    ICQ: 724451
    Web: http://here.is/olof


  3. Re: Do two file names point to the same file?

    Hi,

    thanks for the answer, it seems this is exactly what i
    was looking for.


    Thanks,
    Torsten.


  4. Re: Do two file names point to the same file?

    >Are you really talking about symbolic links? There is not anything like
    >symbolic links at filesystem level in Windows. There are Shell Link


    Actually, there is, it's called a "Junction" in windows.
    However, for files; you're stuck wtih hard links.


    >files, called shortcuts, and there are also a kind of symbolic links
    >implemented in a Posix subsystem called Interix, but no general solution
    >at filesystem level.
    >
    >But if you really mean hard links I have an answer.
    >
    >> Is some test like this available on Windows?

    >
    >
    >Yes, GetFileInformationByHandle() is probably what you are looking for.
    >
    >http://msdn2.microsoft.com/en-us/library/aa364952.aspx
    >
    >It returns a BY_HANDLE_FILE_INFORMATION structure and the unique id for
    >the file, called the Index, and the change count, called the Sequence,
    >are returned in the nFileIndexHigh and nFileIndexLow fields. This is not
    >very well documented so it has lead to many mistakes by programmers
    >using that data over the years.
    >
    >Example:
    >#include
    >#include
    >
    >int main()
    >{
    > // Open the file
    > HANDLE h = CreateFile("filename.txt", 0,
    > FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
    > OPEN_EXISTING, 0, NULL);
    >
    > // Get information
    > BY_HANDLE_FILE_INFORMATION info;
    > GetFileInformationByHandle(h, &info);
    >
    > // The sequence is the high-order word in nFileIndexHigh field
    > WORD FileSequence = HIWORD(info.nFileIndexHigh);
    >
    > // The index is the rest of nFileIndexHigh and nFileIndexLow
    > // fields. Put the fields in a ULARGE_INTEGER structure and
    > // mask away the sequence number.
    > ULARGE_INTEGER FileIndex;
    > FileIndex.HighPart = info.nFileIndexHigh;
    > FileIndex.LowPart = info.nFileIndexLow;
    > FileIndex.QuadPart &= 0x0000FFFFFFFFFFFF;
    >
    > printf("Sequence: %u\n", FileSequence);
    > printf("Index: %I64i\n", FileIndex.QuadPart);
    >}
    >
    >Note also that if you want to compare the file index numbers for two
    >files you also need to compare the dwVolumeSerialNumber fields to make
    >sure that you are looking at files at the same physical volume in both
    >cases. (Just as in Unix, file index numbers are only unique within a
    >filesystem.) The nNumberOfLinks tells you how many links there are at
    >the volume.



    --- AntiSpam/harvest ---
    Remove X's to send email to me.

  5. Re: Do two file names point to the same file?

    Josh Assing wrote:

    >>Are you really talking about symbolic links? There is not anything like
    >>symbolic links at filesystem level in Windows. There are Shell Link

    >
    >
    > Actually, there is, it's called a "Junction" in windows.
    > However, for files; you're stuck wtih hard links.



    Yes of course, forgot to mention that.

    To the OP:
    A junction is a directory pointing to another directory, possibly on
    another filesystem. To find out if a directory is a junction you can
    check the file attributes for the directory. The GetFileAttributes()
    return value would then include the FILE_ATTRIBUTE_REPARSE_POINT flag.
    This means that there is something special done at filesystem level, it
    can be a junction, or something else.

    You can look at the source code for my "junc.exe" tool if you want an
    example. Check the junc.c file in this archive:
    http://www.ltr-data.se/files/source.tar.lzma
    (Uncompress with lzma, http://www.ltr-data.se/files/lzma.exe )

    --
    Olof Lagerkvist
    ICQ: 724451
    Web: http://here.is/olof


+ Reply to Thread