0

I found the following solution to determine whether a drive supports hard links:

CString strDrive = _T("C:\\");
DWORD dwSysFlags;
if(GetVolumeInformation(strDrive, NULL, 0, NULL, NULL, &dwSysFlags, NULL, 0))
{
    if((dwSysFlags & FILE_SUPPORTS_HARD_LINKS) != 0)
    {
        // Hard links can be created on the specified drive.
    }
    else
    {
        // Hard links cannot be created on the specified drive.
    }
}

However, according to MSDN the flag FILE_SUPPORTS_HARD_LINKS is not supported until Windows Server 2008 R2 and Windows 7.

I also thought about using CreateHardLink() in order to try to create a dummy hard link. If the hard link is created then I know that creating hard links on the corresponding drive is possible. However, it may happen that I have no access rights to the said drive. In this case I assume that this method would fail.

Does anybody know how to determine whether a drive supports hard links in Windows XP without requiring write access to that drive?

honk
  • 9,137
  • 11
  • 75
  • 83
  • 1
    Merely out of curiosity, what is your reason for continuing to support XP? Microsoft is ending support on April 8, 2014 - 3 days from today. – Nathan Apr 05 '14 at 08:57
  • 3
    I nearly expected this question :) Even if MS ends support for XP there are still users who continue to use it. And they still ask me for support. – honk Apr 05 '14 at 09:00
  • 1
    Simply check if file system is NTFS. Thats all! – user2120666 Apr 05 '14 at 18:45
  • 1
    To expand on that a little: for Windows 7 or later, use the solution you already found; that way, you'll get the right answer if MS add a new file system or add hard link support to ReFS in the future. For XP and Vista it should be safe enough to assume that only NTFS will have hard link support. (Although I guess you might run across a third-party implementation of a Linux file system.) – Harry Johnston Apr 06 '14 at 01:42
  • Actually it depends on what you're doing with the information. In some scenarios, you might need to take a safer approach: have a list of file systems that definitely do support hard links, and file systems that definitely don't. If the target file system isn't in either list, you'll have to admit that you don't know. – Harry Johnston Apr 06 '14 at 01:44

1 Answers1

3

Thanks to all the commentators. I put your suggestions together and ended up with the following solution. This solution should work for Vista as well:

CString strDrive = _T("C:\\");
DWORD dwSysFlags;

TCHAR szFileSysName[1024];
ZeroMemory(szFileSysName, 1024);

if(GetVolumeInformation(strDrive, NULL, 0, NULL, NULL, &dwSysFlags, szFileSysName, 1024))
{
    // The following check can be realized using GetVersionEx().
    if(bIsWin7OrHigher())
    {
        if((dwSysFlags & FILE_SUPPORTS_HARD_LINKS) != 0)
        {
            // Hard links can be created on the specified drive.
        }
        else
        {
            // Hard links cannot be created on the specified drive.
        }
    }
    else
    {
        if(_tcsicmp(szFileSysName, _T("NTFS")) == 0)
        {
            // Hard links can be created on the specified drive.
        }
        else
        {
            // Hard links cannot be created on the specified drive (maybe).
        }
    }
}

The nice thing about this solution is that GetVolumeInformation() provides all required information.

honk
  • 9,137
  • 11
  • 75
  • 83