-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.cpp
More file actions
55 lines (48 loc) · 1.65 KB
/
formatter.cpp
File metadata and controls
55 lines (48 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "formatter.h"
#include <QDebug>
#include <QtConcurrent/QtConcurrent>
Formatter::Formatter(QObject *parent) : QObject(parent) {}
void Formatter::formatDrive(const QString &devicePath,
const QString &fileSystem) {
QtConcurrent::run(
[this, devicePath, fileSystem]() { runFormat(devicePath, fileSystem); });
}
void Formatter::runFormat(const QString &devicePath,
const QString &fileSystem) {
QString program;
QStringList arguments;
if (fileSystem == "ext4") {
program = "mkfs.ext4";
arguments << "-F" << devicePath; // -F forces creation
} else if (fileSystem == "ntfs") {
program = "mkfs.ntfs";
arguments << "-f" << devicePath; // -f fast format
} else if (fileSystem == "fat32") {
program = "mkfs.vfat";
arguments << "-I" << devicePath; // -I required for some devices
} else if (fileSystem == "exfat") {
program = "mkfs.exfat";
arguments << devicePath;
} else {
emit finished(false, "Unsupported filesystem: " + fileSystem);
return;
}
QProcess process;
process.start(program, arguments);
if (!process.waitForStarted()) {
emit finished(false, "Failed to start format process: " + program);
return;
}
if (!process.waitForFinished(-1)) { // Wait indefinitely
emit finished(false, "Format process timed out or crashed.");
return;
}
if (process.exitCode() == 0) {
emit finished(true, "Drive formatted successfully as " + fileSystem);
} else {
QString error = process.readAllStandardError();
if (error.isEmpty())
error = "Unknown error occurred.";
emit finished(false, "Format failed: " + error);
}
}