Home > Net >  How to check if a drive is connected (mounted)?
How to check if a drive is connected (mounted)?

Time:11-09

is it possible wit Qt Qprocess to detect if a mounted Loacation(maybe NAS) is connected? Generally i would check in /proc/mounts if there is an entry, but if i disconnect to the NAS the file doesn't realizes it. with the df command i can check if a mountpoint is available. But if the connection is disconnected the df process doesn't gives an output. Maybe infinite.

I tried it with

QProcess p;
p.start("bash", QStringList() << "-c" <<
      "df -P -T /media/storage/ | grep QIS | awk -F ' ' '{print $1}'");
if (p.waitForFinished(2))
{
  qDebug() << "Nothing";
  p.close();
}

But nothing happens.

It seems that my program "freezes" when i try to df to a directory which ist'n mounted. Is it possible to cancel the process if there is no answer from the df process after, for example, 2 seconds?

CodePudding user response:

Regarding QProcess, QProcess::waitForFinished takes msec as argument, not sec. Furthermore, it is better to connect to QProcess::finished signal instead of blocking the event loop.

That being said, why using QProcess, when there is a QStorageInfo with a mountedVolumes method:

for (const auto &storage : QStorageInfo::mountedVolumes()) {
    if (storage.isValid() && storage.isReady()) {
        if (!storage.isReadOnly()) {
            // ...
        }
    }
}
  • Related