File::getFileIdentifier() result depends on current directory on Windows

Sample code:

	File::findFileSystemRoots (roots);
	File f = roots[0];

	File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory();
	int64 n1 = f.getFileIdentifier();

	File::getSpecialLocation (File::userApplicationDataDirectory).setAsCurrentWorkingDirectory();
	int64 n2 = f.getFileIdentifier();

	jassert (n1 == n2);

Result:

		n1	15481123719386479	__int64
		n2	3377699721030766	__int64

Project: https://github.com/FigBug/juce_bugs/blob/master/FileIDs/Source/Main.cpp

Fixed code:

uint64 File::getFileIdentifier() const
{
    uint64 result = 0;

	String path = getFullPathName();
	if (path.endsWith (":"))
		path += "\\";

    auto h = CreateFile (path.toWideCharPointer(),
                         GENERIC_READ, FILE_SHARE_READ, nullptr,
                         OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);

    if (h != INVALID_HANDLE_VALUE)
    {
        BY_HANDLE_FILE_INFORMATION info;
        zerostruct (info);

        if (GetFileInformationByHandle (h, &info))
            result = (((uint64) info.nFileIndexHigh) << 32) | info.nFileIndexLow;

        CloseHandle (h);
    }

    return result;
}
3 Likes

Thank you for reporting.

2 Likes