From 962343f2202607085a870263a3853493e6925f27 Mon Sep 17 00:00:00 2001 From: yann64 Date: Sat, 28 Nov 2026 23:49:13 +0100 Subject: [PATCH] haiku: working USB backend, symlink-safe extraction - usb_haiku.cpp: each disk device transaction kept minimal, because Haiku R1/beta6 rejects or hangs on combined ones: erase foreign content, delete old MBR partitions, create the 0x0c partition ("FAT 32-bit, LBA-mapped", with "active false" parameters) and format FAT32 are separate commits; Uninitialize is never used on an MBR - OPENPU_DEBUG=1 traces the Haiku disk device steps on stderr - extractTar resolves the destination first: SECURE_SYMLINKS refused destinations under a symlink (/tmp on Haiku) - check.sh: clang-tidy skips sources not built on Linux - validated in the Haiku VM: write-usb on the virtual stick gives an MBR, a FAT32 partition at 1 MiB, the label, license/ and the archive tree without UpdateInfo.xml, with identical checksums; 53 unit tests pass Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar --- CHANGELOG.md | 8 ++++ src/core/archive.cpp | 14 +++++-- src/core/usb_haiku.cpp | 94 ++++++++++++++++++++++++++++++++++++------ tools/check.sh | 3 +- 4 files changed, 103 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e2610c..6551ffd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ complete; every release is an annotated git tag `vX.Y.Z` (see `tools/release.sh` ### Added - Haiku development VM tooling: `tools/haiku-vm.sh` (Haiku R1/beta6 x86_64 under QEMU/KVM, with a virtual USB stick) and `tools/qmp.py` (headless screenshots, clicks and typing through QMP). +- Haiku USB backend (Storage Kit disk device API): lists USB drives, writes an MBR with one FAT32 + partition at 1 MiB, mounts, unmounts and ejects. Validated end to end in the VM. +- The core library and `openpu-cli` build and pass all unit tests on Haiku R1/beta6 x86_64. + +### Fixed +- Archive extraction failed when the destination path contained a symbolic link (e.g. `/tmp` on + Haiku). +- `tools/release.sh` produced empty release notes. ## [0.2.0] - 2026-11-28 diff --git a/src/core/archive.cpp b/src/core/archive.cpp index 00df20c..54a9381 100644 --- a/src/core/archive.cpp +++ b/src/core/archive.cpp @@ -186,13 +186,21 @@ std::uint64_t tarPayloadSize(const fs::path& path) return total; } -void extractTar(const fs::path& path, const fs::path& destination, const ArchiveProgressCallback& onProgress, +void extractTar(const fs::path& path, const fs::path& requestedDestination, const ArchiveProgressCallback& onProgress, const CancelToken* cancel) { std::error_code ec; - fs::create_directories(destination, ec); + fs::create_directories(requestedDestination, ec); if (ec) - throw ArchiveError(ArchiveError::Kind::Io, "cannot create " + destination.string() + ": " + ec.message()); + throw ArchiveError(ArchiveError::Kind::Io, + "cannot create " + requestedDestination.string() + ": " + ec.message()); + // ARCHIVE_EXTRACT_SECURE_SYMLINKS rejects a symlink anywhere in the target path, including the + // destination itself (e.g. /tmp is a symlink on Haiku). Resolve the destination first so that + // only links *inside* it are refused. + const fs::path destination = fs::canonical(requestedDestination, ec); + if (ec) + throw ArchiveError(ArchiveError::Kind::Io, + "cannot resolve " + requestedDestination.string() + ": " + ec.message()); ReadPtr a = openTar(path); WritePtr w(archive_write_disk_new()); diff --git a/src/core/usb_haiku.cpp b/src/core/usb_haiku.cpp index 11b6a84..e9244ee 100644 --- a/src/core/usb_haiku.cpp +++ b/src/core/usb_haiku.cpp @@ -12,6 +12,8 @@ #include #include +#include +#include #include #include @@ -21,7 +23,12 @@ namespace fs = std::filesystem; namespace { -constexpr off_t kPartitionOffset = 1024 * 1024; // 1 MiB, like the Linux backend +constexpr off_t kPartitionOffset = off_t{1024} * 1024; // 1 MiB, like the Linux backend +// Partition type as named by the "intel" partitioning add-on (MBR type 0x0c, as on Linux). +// Not kPartitionTypeFAT32: that one is the *file system* name ("FAT32 File System"). +constexpr const char* kMbrTypeFat32Lba = "FAT 32-bit, LBA-mapped"; +// The intel add-on rejects a NULL parameter string when the job runs; DriveSetup always sends this. +constexpr const char* kMbrChildParameters = "active false\n"; std::string statusText(status_t s) { @@ -33,6 +40,14 @@ std::string statusText(status_t s) throw UsbError(UsbError::Kind::Failed, what + ": " + statusText(status)); } +/// Step tracing on stderr when OPENPU_DEBUG is set (diagnosing disk device jobs). +void trace(const std::string& step) +{ + static const bool enabled = std::getenv("OPENPU_DEBUG") != nullptr; // NOLINT(concurrency-mt-unsafe) + if (enabled) + (void)std::fprintf(stderr, "[usb_haiku] %s\n", step.c_str()); +} + bool startsWith(const char* s, const char* prefix) { return s != nullptr && std::strncmp(s, prefix, std::strlen(prefix)) == 0; @@ -61,7 +76,7 @@ bool hostsBootVolume(BDiskDevice& device) partition_id parseId(const std::string& id) { try { - return static_cast(std::stol(id)); + return static_cast(std::stoi(id)); } catch (const std::exception&) { throw UsbError(UsbError::Kind::Failed, "invalid drive id " + id); } @@ -121,27 +136,82 @@ public: if (device.GetPath(&path) != B_OK || !startsWith(path.Path(), "/dev/disk/usb/") || hostsBootVolume(device)) throw UsbError(UsbError::Kind::Failed, drive.device + " is not a removable USB drive"); + trace("unmount"); unmountAll(device); + + const bool hasMbr = device.ContentType() != nullptr && std::strcmp(device.ContentType(), kPartitionTypeIntel) == 0; + trace(std::string("content type: ") + (device.ContentType() != nullptr ? device.ContentType() : "(none)")); + if (device.ContentType() != nullptr && !hasMbr) { + // Other content (GPT, a file system without partition table...): erase it in its own + // transaction. Uninitialize + Initialize in one transaction hangs the disk device + // manager on Haiku R1/beta6. + if ((s = device.PrepareModifications()) != B_OK) + fail("cannot modify " + drive.device, s); + if ((s = device.Uninitialize()) != B_OK || (s = device.CommitModifications()) != B_OK) { + device.CancelModifications(); + fail("cannot erase " + drive.device, s); + } + if ((s = roster.GetDeviceWithID(id, &device)) != B_OK) + fail("USB drive " + drive.device + " not found", s); + } + + if (hasMbr && device.CountChildren() > 0) { + // Keep the existing MBR and drop its partitions (Uninitialize would hang, see above). + // Own transaction too: freed space is not reusable within the same transaction. + if ((s = device.PrepareModifications()) != B_OK) + fail("cannot modify " + drive.device, s); + for (int32 i = device.CountChildren() - 1; i >= 0 && s == B_OK; --i) { + trace("delete partition " + std::to_string(i)); + s = device.DeleteChild(i); + } + if (s != B_OK || (s = device.CommitModifications()) != B_OK) { + device.CancelModifications(); + fail("cannot delete the existing partitions", s); + } + if ((s = roster.GetDeviceWithID(id, &device)) != B_OK) + fail("USB drive " + drive.device + " not found", s); + } + + trace("prepare modifications"); if ((s = device.PrepareModifications()) != B_OK) fail("cannot modify " + drive.device, s); try { - if (device.ContentType() != nullptr && (s = device.Uninitialize()) != B_OK) - fail("cannot erase the partition table", s); - if ((s = initialize(&device, "intel", kPartitionTypeIntel, nullptr, nullptr)) != B_OK) - fail("cannot create the MBR partition table", s); + if (!hasMbr) { + trace("initialize intel"); + if ((s = initialize(&device, "intel", kPartitionTypeIntel, nullptr, nullptr)) != B_OK) + fail("cannot create the MBR partition table", s); + } off_t start = kPartitionOffset; off_t size = device.Size() - start; BString name; - if ((s = device.ValidateCreateChild(&start, &size, kPartitionTypeFAT32, &name, nullptr)) != B_OK) + if ((s = device.ValidateCreateChild(&start, &size, kMbrTypeFat32Lba, &name, kMbrChildParameters)) != B_OK) fail("cannot create the FAT32 partition", s); - BPartition* child = nullptr; - if ((s = device.CreateChild(start, size, kPartitionTypeFAT32, nullptr, nullptr, &child)) != B_OK) + trace("create child at " + std::to_string(start) + " size " + std::to_string(size)); + if ((s = device.CreateChild(start, size, kMbrTypeFat32Lba, nullptr, kMbrChildParameters)) != B_OK) fail("cannot create the FAT32 partition", s); - if ((s = initialize(child, "fat", "FAT32 File System", label.c_str(), "fat 32\n")) != B_OK) - fail("cannot format the FAT32 partition", s); + trace("commit partition table"); if ((s = device.CommitModifications()) != B_OK) - fail("cannot write the changes to " + drive.device, s); + fail("cannot write the partition table to " + drive.device, s); + } catch (...) { + device.CancelModifications(); + throw; + } + + // Format in a separate transaction: initializing a partition created in the same + // transaction fails when the job runs ("Invalid Argument"). + if ((s = roster.GetDeviceWithID(id, &device)) != B_OK || device.CountChildren() == 0) + fail("the new partition is not visible", s != B_OK ? s : B_ENTRY_NOT_FOUND); + if ((s = device.PrepareModifications()) != B_OK) + fail("cannot modify " + drive.device, s); + try { + trace("initialize fat"); + // No parameters: the FAT driver picks FAT32 for volumes this size (checked by the e2e test). + if ((s = initialize(device.ChildAt(0), "fat", "FAT32 File System", label.c_str(), nullptr)) != B_OK) + fail("cannot format the FAT32 partition", s); + trace("commit format"); + if ((s = device.CommitModifications()) != B_OK) + fail("cannot format " + drive.device, s); } catch (...) { device.CancelModifications(); throw; diff --git a/tools/check.sh b/tools/check.sh index b635998..352f883 100644 --- a/tools/check.sh +++ b/tools/check.sh @@ -28,7 +28,8 @@ fi if command -v clang-tidy >/dev/null; then step "clang-tidy" # Only our sources; compile_commands.json comes from the debug build. - find "$SRC/src" -name '*.cpp' -not -path '*/ui-*' -print0 | + # Only files of this build: usb_haiku.cpp / usb_none.cpp are not compiled on Linux. + find "$SRC/src" -name '*.cpp' -not -path '*/ui-*' -not -name 'usb_haiku.cpp' -not -name 'usb_none.cpp' -print0 | xargs -0 clang-tidy -p "$OUT/debug" --quiet --warnings-as-errors='*' fi