简体   繁体   中英

QProcess() doesn't always work

I'm trying to format an USB drive from Qt by using mkfs.fat with QProcess(). The problem is that QProcess() is called but sometimes it exits without finishing the formatting. If I read exitCode() it is 1.

This is the function:

bool UsbDevice::formatFat(QByteArray name)
{
    QProcess * formatter = new QProcess(this);

    QString partition = baDevice + "1"; // baDevice is "/dev/sdb"
    QString mkfs = "mkfs.fat";

    QStringList args;
    args << "-F32" << "-v" << "-I" << "-n " + name << partition;

    formatter->start(mkfs, args);

    formatter->waitForStarted();

    formatter->waitForFinished(-1);

    emit logLine(formatter->readAll());

    if (formatter->exitCode() == 0)
        {
            emit logLine("mkfs.fat executed correctly.");
        }
    else
        {
            emit logLine(QString("Possible fail to format device (Error: %1).").arg(formatter->errorString()));
        }

    delete formatter;

    return true;
}

The function works if it is called 3 - 5 times. I understand that there are more issues than this (for example this function blocks the user interface because of the waitFor* functions, and one of them can block forever without having a timeout) but I don't get it why the process doesn't finish Ok sometimes.

logLine calls qDebug and the output when it doesn't work is like:

mkfs.fat 3.0.27 (2014-11-12)

Possible fail to format device (Error: Unknown error).

When it works, it outputs:

mkfs.fat 3.0.27 (2014-11-12)
/dev/sdb1 has 64 heads and 32 sectors per track,
hidden sectors 0x0800;
logical sector size is 512,
using 0xf8 media descriptor, with 30279937 sectors;
drive number 0x80;
filesystem has 2 32-bit FATs and 16 sectors per cluster.
FAT size is 14771 sectors, and provides 1890647 clusters.
There are 32 reserved sectors.
Volume ID is c35005fb, volume label  USB   .

mkfs.fat executed correctly.

This makes me think the arguments are correct.

I have also tried connecting finished() signal to a slot and block until the signal is emitted, but still no luck.

The documentation is not very clear about this, but it looks like readAll has the same output as readAllStandardOutput . Software typically outputs errors on stderr (StandardError), so you would have to call readAllStandardError to see the error message. So there is nothing wrong with QProcess() , it is just mkfs.fat which fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM