[utilities/isoimagewriter] isoimagewriter: Say that the target device is in use instead of blaming the last block
Méven Car <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 72e925414b23363e2cd5def49a380f80b4b2f451 by Méven Car.
Committed on 27/07/2026 at 17:09.
Pushed by meven into branch 'master'.
Say that the target device is in use instead of blaming the last block
The device is opened through UDisks2 with O_EXCL, so the open is turned down
while anything else still holds the device. That is what a live system does
to the medium it was booted from, and what any program still reading from
the stick does.
Neither the reply to OpenDevice nor the QFile::open that follows it were
looked at. The invalid reply gives a file descriptor of -1, opening a QFile
on it fails, and the write loop then ran against a closed file. Every write
returned -1, so the user was told "The last block was not fully written
(-1 of 1048576 bytes)" for the very first block, with nothing about the
device being busy, which is the part they can do something about.
Report what UDisks2 said about the open, and what to do about it. Report a
failed write with the reason as well, in place of the debug line that was
left where the throw had been commented out.
BUG: 523537
M +13 -4 isoimagewriter/imagewriter.cpp
https://invent.kde.org/utilities/isoimagewriter/-/commit/72e925414b23363e2cd5def49a380f80b4b2f451
diff --git a/isoimagewriter/imagewriter.cpp b/isoimagewriter/imagewriter.cpp
index 283444c..a77d7c5 100644
--- a/isoimagewriter/imagewriter.cpp
+++ b/isoimagewriter/imagewriter.cpp
@@ -201,9 +201,19 @@ void ImageWriter::writeImage()
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
QDBusInterface deviceDBus("org.freedesktop.UDisks2", m_Device->m_PhysicalDevice, "org.freedesktop.UDisks2.Block", QDBusConnection::systemBus(), this);
QDBusReply<QDBusUnixFileDescriptor> reply = deviceDBus.call(QDBus::Block, "OpenDevice", "rw", Properties{{"flags", O_EXCL | O_SYNC | O_CLOEXEC}} );
- QDBusUnixFileDescriptor fd = reply.value();
+ if (!reply.isValid()) {
+ // O_EXCL turns the open down while anything else still holds the device.
+ throw i18nc("@info",
+ "Failed to open the target device %1:\n%2\n\n"
+ "The device is in use. Close the programs using it and unmount it, or write to it from a system that was not booted from it.",
+ m_Device->m_VisibleName,
+ reply.error().message());
+ }
+ const QDBusUnixFileDescriptor fd = reply.value();
QFile deviceFile;
- deviceFile.open(fd.fileDescriptor(), QIODevice::WriteOnly);
+ if (!deviceFile.open(fd.fileDescriptor(), QIODevice::WriteOnly)) {
+ throw i18nc("@info", "Failed to open the target device %1:\n%2", m_Device->m_VisibleName, deviceFile.errorString());
+ }
#endif
qint64 readBytes;
@@ -225,8 +235,7 @@ void ImageWriter::writeImage()
readBytes = alignNumber(readBytes, (qint64)m_Device->m_SectorSize);
writtenBytes = deviceFile.write(static_cast<char*>(buffer), readBytes);
if (writtenBytes < 0) {
- qDebug() << "write writtenBytes: " << writtenBytes;
- //throw i18n("Failed to write to the device:\n%1"); //, "ook"); //deviceFile.errorString());
+ throw i18nc("@info", "Failed to write to the device:\n%1", deviceFile.errorString());
}
if (writtenBytes != readBytes)
throw i18n("The last block was not fully written (%1 of %2 bytes)!\nAborting.", writtenBytes, readBytes);