如何判断mount是否在QT代码中成功

时间:2013-06-19 06:10:31

标签: qt samba cifs

我在安装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;
}

}

1 个答案:

答案 0 :(得分:0)

您可以使用QProcesserror函数(Documentation for "error" Documentation for "state")检查state的上一个错误。

“如果有错误不符合我的条件,该怎么办。”

您可以在代码中添加以下内容:

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

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

或者,请勿将上述块添加到错误检查代码中。相反,请为slot connecterror信号创建一个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
}