io/sparse_file — the single O_WRONLY output file (docs/04 §4). open()
posix_fallocate's the full size (falls back to ftruncate on
EOPNOTSUPP/ENOSYS, reported via preallocated()); write_at() pwrites at an
absolute offset, looping short writes and retrying EINTR; sync() is
fdatasync (timer/pause only); advise_dontneed() is
posix_fadvise(DONTNEED); resize() trims a preallocated tail or sizes a
chunked download. errno -> vdm::Error (ENOSPC->disk_full,
EACCES->permission_denied, ENOENT/ENOTDIR/...->path_rejected). No lock on
write_at — POSIX makes each pwrite atomic for a regular file, so N
segment threads writing disjoint ranges is safe (tested, TSan-clean).
io/write_buffer — per-segment accumulate-and-flush buffer, preallocated
at construction; append() only memcpys (no allocation on the write-
callback hot path, docs/04 §8 — asserted by a global-new counter in the
test). Flushes on fill via a caller-supplied FlushFn; a chunk >= capacity
arriving on an empty buffer writes straight through. On a flush error
next_offset() stays at the last durable position. Single-threaded; the
disk-writer-thread handoff is stage 8.
Also: vtest.hpp VT_CHECK_EQ/NE now copy operands (auto, not auto&&) — an
assertion must not outlive a temporary the expression returned a
reference into (ASan caught this on Result<void>{}.error().code).
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HPPSGhiArbvQgwC2DNiURS
81 lines
3.2 KiB
C++
81 lines
3.2 KiB
C++
// vdm/io/sparse_file.hpp — the one output file, written at absolute offsets.
|
|
//
|
|
// docs/04 §4: one file, opened once, O_WRONLY; posix_fallocate the full size up front;
|
|
// each segment pwrite()s at its own offset so there is no reassembly pass; fadvise
|
|
// DONTNEED on written ranges; fdatasync on a timer, never per write.
|
|
//
|
|
// This header compiles standalone.
|
|
|
|
#ifndef VDM_IO_SPARSE_FILE_HPP
|
|
#define VDM_IO_SPARSE_FILE_HPP
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "vdm/util/bytes.hpp"
|
|
#include "vdm/util/result.hpp"
|
|
|
|
namespace vdm::io {
|
|
|
|
class SparseFile {
|
|
public:
|
|
struct OpenOptions {
|
|
std::uint64_t total_size = 0; // full final size; 0 = unknown (chunked transfer)
|
|
bool preallocate = true; // posix_fallocate; falls back to ftruncate
|
|
bool truncate_existing = false; // start fresh (true) vs. resume into an existing
|
|
// part file (false)
|
|
};
|
|
|
|
SparseFile() = default;
|
|
~SparseFile();
|
|
|
|
SparseFile(SparseFile &&) noexcept;
|
|
SparseFile &operator=(SparseFile &&) noexcept;
|
|
SparseFile(const SparseFile &) = delete;
|
|
SparseFile &operator=(const SparseFile &) = delete;
|
|
|
|
// Open `path` for writing at absolute offsets, creating it if needed. With
|
|
// `preallocate` and a known `total_size`, posix_fallocate the whole file (contiguous
|
|
// extents, no ENOSPC surprise at 99%). EOPNOTSUPP/ENOSYS (tmpfs, some network FS)
|
|
// falls back to ftruncate — sparse, no extent reservation — and is reported by
|
|
// preallocated().
|
|
[[nodiscard]] Result<void> open(std::string_view path, const OpenOptions &opts);
|
|
[[nodiscard]] Result<void> open(std::string_view path); // default OpenOptions
|
|
|
|
[[nodiscard]] bool is_open() const noexcept { return fd_ >= 0; }
|
|
[[nodiscard]] bool preallocated() const noexcept { return preallocated_; }
|
|
[[nodiscard]] std::string_view path() const noexcept { return path_; }
|
|
|
|
// pwrite the whole span at `offset`, looping over short writes and retrying EINTR.
|
|
// Safe to call concurrently with other write_at()/sync() on the same object as long
|
|
// as the byte ranges do not overlap — POSIX guarantees each pwrite is atomic for a
|
|
// regular file, so no lock is taken on the hot path.
|
|
[[nodiscard]] Result<void> write_at(std::uint64_t offset, ConstByteSpan data);
|
|
|
|
// fdatasync. Call on a timer (default 5 s) and on pause — never per write (docs/04 §4).
|
|
[[nodiscard]] Result<void> sync();
|
|
|
|
// POSIX_FADV_DONTNEED on [offset, offset+len): drop already-written pages from the
|
|
// page cache so a 40 GB ISO does not evict the user's working set. Most effective
|
|
// after sync(). Best-effort — failures are ignored.
|
|
void advise_dontneed(std::uint64_t offset, std::uint64_t len) noexcept;
|
|
|
|
// ftruncate to `size`: give a chunked download its real size once known, or trim a
|
|
// preallocated tail that a steal/mirror never filled.
|
|
[[nodiscard]] Result<void> resize(std::uint64_t size);
|
|
|
|
[[nodiscard]] Result<void> close();
|
|
|
|
private:
|
|
void reset() noexcept;
|
|
|
|
int fd_ = -1;
|
|
bool preallocated_ = false;
|
|
std::string path_;
|
|
};
|
|
|
|
} // namespace vdm::io
|
|
|
|
#endif // VDM_IO_SPARSE_FILE_HPP
|