0

I have this function on mounting a smb:// connection. What if there is an error that is not in my condition. Is there a better way to determine if the mount is sucessful or not? Im using ubuntu 11.04 and 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;
}

}

reggie
  • 626
  • 3
  • 13
  • 31

1 Answers1

0

You can check last error of a QProcess by using error and state functions (Documentation for "error" Documentation for "state").

"What if there is an error that is not in my condition."

You can add something like this in your code:

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

Or, if you want to give more specific information, you can create a condition for each error code separately. Here's a list of codes.

Alternatively, don't add the above block to the error checking code. Instead, create a slot to which you connect the error signal of connectSamba class:

// 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
}
user2448027
  • 1,628
  • 10
  • 11
  • can i directly put the condition on connectSamba.error? why do i need to put the connectsamba.state first? sorry im using qt for 1month. – reggie Jun 19 '13 at 06:34
  • @reggie There's no `NoError` state in QProcess::Error so you need to check if the process is not running. – user2448027 Jun 19 '13 at 06:35
  • @reggie Added an alternative where you don't check the state of connectSamba. – user2448027 Jun 19 '13 at 06:47
  • after i put your condition i got this warning: control reaches end of non-void function but your condition is doing fine i already tested it. should i be aware on that warning. – reggie Jun 19 '13 at 06:50
  • on your alternate code on the function onerror can i still use the string function contains? – reggie Jun 19 '13 at 06:57
  • @reggie No, but I think you still can use `LogWrite` in onError. I forgot to mention that in the alternate solution, you use your original error checking code in addition to the onError function. I edited my answer. – user2448027 Jun 19 '13 at 07:01
  • ok but base on that signal slot thing how can i passed a true condition? i only see i can only return a false when an error occured how about wihtout error? because when there is no error i will do my copyfile function (i didnt put my copyfunction on the code) – reggie Jun 19 '13 at 07:09
  • there is always an error because of my echo in terminal command readallstandard error is the same as .error right? the readallstandard output is asking for password which is my linuxpassword variable – reggie Jun 19 '13 at 07:31
  • @reggie I forgot to say that the signal slot system is asynchronous. This means that there may initially be no errors, but after a while some may occur. This signal slot system "gets" those errors. In your copyfile code, you check if processErrorOccurred is true and copy file(s) only if it isn't. – user2448027 Jun 19 '13 at 11:15