0

我在安装 smb:// 连接时具有此功能。如果有一个不属于我的情况的错误怎么办。有没有更好的方法来确定挂载是否成功?我使用的是 ubuntu 11.04 和 qt 4.7.3

bool mwDM::mountFolder()
{

QString smbUsername,smbPassword,serverPath,mountPath;
QProcess connectSamba;
QString terminalCommand,linuxPassword;
QDir dir("/mnt/backup");

    smbUsername=ReadINIStr(iniPath,"Server","Username","");
    smbPassword=ReadINIStr(iniPath,"Server","Password","");
    serverPath=ReadINIStr(iniPath,"Server","Hostname","");
    serverPath="//" + serverPath;
    mountPath="/mnt/backup";
    linuxPassword=ReadINIStr(iniPath,"Server","AdminPassword","");
    terminalCommand="echo "+linuxPassword+" | sudo -S mount -t cifs -o username="+smbUsername+",password="+smbPassword+" "+serverPath+ " "+mountPath;

connectSamba.start("sh",QStringList() << "-c" << terminalCommand );
if(!connectSamba.waitForStarted())
{
   LogWrite("Failed to start mount command", Qt::red);
}
if(!connectSamba.waitForFinished() )
{
    LogWrite("Failed to finish mount command", Qt::red);
}

QString connectSamba_stderr = connectSamba.readAllStandardError();
qDebug() << "connectSamba_stderr" << connectSamba_stderr;
if(connectSamba_stderr.contains("is not a valid block device"))
{
    LogWrite("Hostname is invalid", Qt::red);
    return false;
}
else if(connectSamba_stderr.contains("3 incorrect password attempts"))
{

  LogWrite("Admin password is incorrect", Qt::red);
    return false;
}
else if(connectSamba_stderr.contains("wrong fs type, bad option, bad superblock on"))
{
    LogWrite("Hostname is invalid", Qt::red);
    return false;
}
else if(connectSamba_stderr.contains("Invalid argument"))
{

    LogWrite("Mount error(22): Invalid argument", Qt::red);
    return false;
}
else if(!dir.exists())
{
    LogWrite("Directory doesn't exists", Qt::red);
    return false;
}
else
{
    return true;
}

}

4

1 回答 1

0

您可以QProcess使用errorandstate函数检查 a 的最后一个错误(“错误” 的文档“状态”的文档)。

“如果出现不属于我的错误怎么办。”

您可以在代码中添加类似这样的内容:

else if(connectSamba.state() == QProcess::NotRunning && connectSamba.error() >= 0)
{
    LogWrite("Unknown error", Qt::red);
    return false;
}

或者,如果您想提供更具体的信息,您可以分别为每个错误代码创建一个条件。是代码列表。

或者,不要将上述块添加到错误检查代码中。相反,创建一个slotconnect的类error信号connectSamba

// add this line below "QProcess connectSamba;" line in mwDm::mountFolder
connect(&connectSamba, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError));
// after that, use your original error checking code in mountFolder

// slot code
void mwDm::onError(QProcess::ProcessError)
{
   //use switch-case or if to check type of error if you want
   processErrorOccurred = true; // processErrorOccurred is a member of mwDm
}
于 2013-06-19T06:25:02.970 回答