0

Is it possible to find out if a drive path (e.g. P:/temp/foo) is local or remote?

Here ( CMD line to tell if a file/path is local or remote? ) it's shown for a cmd evaluation, but I am looking for a C++/Qt way.

Related to:

  1. QDir::exists with mapped remote directory
  2. How to perform Cross-Platform Asynchronous File I/O in C++
Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • Is this a Windows-specific question? If so, tag it with winapi, there's no Qt way to do it. – sashoalm Feb 10 '15 at 03:16
  • I was not sure whether there was a Qt way to do it. So I left it open, however a platform independent way is of course preferable. Actually pepe has pointed out an interesting way which I will crosscheck, but I have to migrate to Qt 5.4 before. Both answers are useful! Thanks. – Horst Walter Feb 10 '15 at 12:43

3 Answers3

3

There's no way in Qt, at least up to Qt 5.5. QStorageInfo would be the closest fit, but there is no agreement about how such an API should look like (see the gigantic discussion that started in this thread; basically one risks to have Qt reporting misleading information).

So, for now, you're up to using native APIs. The aforementioned GetDriveType would be fine for Windows, but you're pretty much on your own on Linux and Mac.

peppe
  • 21,934
  • 4
  • 55
  • 70
2

you could use the GetDriveType function:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364939(v=vs.85).aspx

iedoc
  • 2,266
  • 3
  • 30
  • 53
  • He says he's looking for a cross-platform way, not a Windows-specific way, though. – sashoalm Feb 09 '15 at 17:24
  • he didn't say either or now that you mention it – iedoc Feb 09 '15 at 19:10
  • He doesn't say explicitly but it's tagged with Qt and not with winapi and no mention of Windows... I mean he might be targeting Linux for we know. Ok, I reread his question, and he says "P:/", so I guess it's Windows after all. But OP is not very clear. – sashoalm Feb 10 '15 at 03:14
  • Useful hint anyway, I will use it as fallback when I can not come up with a Qt solution. – Horst Walter Feb 10 '15 at 12:44
2

I recently filed a feature request about this exact question: https://bugreports.qt.io/browse/QTBUG-83321

A possible workaround emerged there. Using the following enum:

enum DeviceType {
    Physical,
    Other,
    Unknown
};

I could reliably check a mount to be a local device or something else (possibly a net mount) using the following function on Linux, Windows and macOS:

DeviceType deviceType(const QStorageInfo &volume) const
{
#ifdef Q_OS_LINUX
    if (QString::fromLatin1(volume.device()).startsWith(QLatin1String("/"))) {
        return DeviceType::Physical;
    } else {
        return DeviceType::Other;
    }
#endif
#ifdef Q_OS_WIN
    if (QString::fromLatin1(volume.device()).startsWith(QLatin1String("\\\\?\\Volume"))) {
        return DeviceType::Physical;
    } else {
        return DeviceType::Other;
    }
#endif
#ifdef Q_OS_MACOS
    if (! QString::fromLatin1(volume.device()).startsWith(QLatin1String("//"))) {
        return DeviceType::Physical;
    } else {
        return DeviceType::Other;
    }
#endif
    return DeviceType::Unknown;
}
Tobias Leupold
  • 1,512
  • 1
  • 16
  • 41