btrfs_recovery

1. Overview

The btrfs_recovery.cpp file implements metadata inspection and file recovery from BTRFS volumes.

This is an implementation file. It contains executable logic for the subsystem, including parsing, I/O, validation, recovery flow, or graphical behavior declared by the corresponding interface.

Its paired source or interface file is btrfs_recovery.hpp.

Primary responsibilities:

  • Read BTRFS superblocks and tree structures
  • Locate filesystem roots and extents
  • Traverse recovered directory trees
  • Export recoverable files and metadata

2. File Identity and Scope

  • Source path: src/btrfs_recovery.cpp
  • Language: C++
  • File role: implementation file
  • Physical lines: 1086
  • Non-empty lines: 1011
  • SHA-256: 3cea4c7bdadb167f22c1c404cb08c58174f6240803db894e4931c9c08c8da4c8

The declarations in this file use the global namespace.


3. Dependencies

3.1 Project Headers

  • btrfs_recovery.hpp
  • i18n.hpp
  • text_utils.hpp
  • zlib.h
  • zstd.h

3.2 Standard and External Headers

  • algorithm
  • array
  • cstdint
  • filesystem
  • fstream
  • iomanip
  • map
  • optional
  • set
  • sstream
  • string
  • vector
  • lzo/lzo1x.h

These dependencies define the file’s compile-time integration with the recovery engine, GTK interface, on-disk format decoders, operating-system services, or C++ standard library.


4. Declared Types

  • struct BtrfsKey.
  • struct BtrfsItem.
  • struct BtrfsKeyPtr.
  • struct ChunkStripe.
  • struct ChunkMapEntry.
  • struct RootEntry.
  • struct InodeInfo.
  • struct DirEdge.
  • struct FileExtentInfo.
  • struct FsRootState.
  • class BtrfsRecoveryImpl.

5. Constants and Configuration Values

  • constexpr uint8_t BTRFS_INODE_ITEM_KEY=1
  • constexpr uint8_t BTRFS_INODE_REF_KEY=12
  • constexpr uint8_t BTRFS_DIR_ITEM_KEY=84
  • constexpr uint8_t BTRFS_DIR_INDEX_KEY=96
  • constexpr uint8_t BTRFS_EXTENT_DATA_KEY=108
  • constexpr uint8_t BTRFS_ROOT_ITEM_KEY=132
  • constexpr uint8_t BTRFS_ROOT_BACKREF_KEY=144
  • constexpr uint8_t BTRFS_ROOT_REF_KEY=156
  • constexpr uint8_t BTRFS_CHUNK_ITEM_KEY=228
  • constexpr uint8_t BTRFS_FILE_EXTENT_INLINE=0
  • constexpr uint8_t BTRFS_FILE_EXTENT_REG=1
  • constexpr uint8_t BTRFS_FILE_EXTENT_PREALLOC=2
  • constexpr uint64_t BTRFS_BLOCK_GROUP_RAID0=1ull << 3
  • constexpr uint64_t BTRFS_BLOCK_GROUP_RAID1=1ull << 4
  • constexpr uint64_t BTRFS_BLOCK_GROUP_DUP=1ull << 5
  • constexpr uint64_t BTRFS_BLOCK_GROUP_RAID10=1ull << 6
  • constexpr uint64_t BTRFS_BLOCK_GROUP_RAID5=1ull << 7
  • constexpr uint64_t BTRFS_BLOCK_GROUP_RAID6=1ull << 8

5.1 Operational Strings, Paths, Formats, or Commands

The following code literals are operationally relevant and may identify filesystem paths, helper commands, formats, signatures, or subsystem labels:

  • btrfs_recovery.hpp
  • BTRFS_COMPRESSION_BLOCKSIZE
  • Failed to read Btrfs superblock
  • Invalid Btrfs superblock magic
  • Invalid Btrfs nodesize
  • btrfs
  • Unable to parse Btrfs system chunk array
  • btrfs_metadata.txt
  • Btrfs recovery metadata
  • Btrfs chunk tree load was partial; continuing with system chunk mappings only.
  • Btrfs
  • /
  • Btrfs file recovery completed at offset
  • Btrfs chunk mappings loaded
  • Btrfs filesystem roots discovered
  • Btrfs files exported
  • Btrfs compressed extents exported
  • Btrfs compressed extents skipped
  • Btrfs chunk tree parsed
  • Invalid Btrfs root tree
  • Btrfs root tree parsed
  • Btrfs fs root parsed
  • Btrfs export failed

6. Data Members and Persistent State

No class data members are declared directly in this file, or the file primarily implements methods declared elsewhere.


7. API and Function Inventory

The following callable interfaces or definitions were detected:

  • uint16_t le16(const unsigned char*p) – Implements the operation identified by this API and updates or returns the associated state.
  • uint32_t le32(const unsigned char*p) – Implements the operation identified by this API and updates or returns the associated state.
  • uint64_t le64(const unsigned char*p) – Implements the operation identified by this API and updates or returns the associated state.
  • std::string sanitize(const std::string&in) – Implements the operation identified by this API and updates or returns the associated state.
  • std::string to_hex(uint64_t v) – Implements the operation identified by this API and updates or returns the associated state.
  • BtrfsKey parse_key(const unsigned char*p) – Reads or derives the requested information and returns or stores the decoded result.
  • bool decompress_payload(const std::vector<unsigned char>&in,uint8_t compression,size_t expected_size,std::vector<unsigned char>&out) – Implements the operation identified by this API and updates or returns the associated state.
  • uint8_t gf_mul_byte(uint8_t a,uint8_t b) – Implements the operation identified by this API and updates or returns the associated state.
  • uint8_t gf_pow2(size_t exp) – Implements the operation identified by this API and updates or returns the associated state.
  • uint8_t gf_inv_byte(uint8_t a) – Implements the operation identified by this API and updates or returns the associated state.
  • uint32_t crc32c_update(uint32_t crc,const unsigned char*data,size_t len) – Implements the operation identified by this API and updates or returns the associated state.
  • bool validate_tree_block_checksum(const std::vector<unsigned char>&block) – Evaluates the stated condition and returns the corresponding Boolean result.
  • bool validate_tree_block_header(const std::vector<unsigned char>&block,uint64_t logical_bytenr,uint32_t nodesize,const std::array<unsigned char,16>*fsid=nullptr,uint64_t max_generation=0) – Evaluates the stated condition and returns the corresponding Boolean result.
  • explicit BtrfsRecoveryImpl(DiskReader&reader): reader_(reader) – Implements the operation identified by this API and updates or returns the associated state.
  • bool recover(uint64_t volume_offset,const std::string&output_dir,std::vector<RecoveredFileRecord>&recovered,std::vector<std::string>&logs,std::string&error) – Performs the associated recovery or extraction operation.
  • void parse_system_chunk_array(const unsigned char*data,uint32_t size) – Reads or derives the requested information and returns or stores the decoded result.
  • bool validate_raid6_candidate(const std::vector<std::vector<unsigned char>>&bufs,const std::vector<bool>&ok,const std::vector<size_t>&data_cols,const std::map<size_t,size_t>&data_index,size_t p_col,size_t q_col,const unsigned char*candidate,size_t candidate_col,size_t chunk) – Evaluates the stated condition and returns the corresponding Boolean result.
  • bool reconstruct_raid56_read(const ChunkMapEntry&entry,uint64_t logical,unsigned char*out,size_t chunk) – Implements the operation identified by this API and updates or returns the associated state.
  • void sort_chunk_map() – Implements the operation identified by this API and updates or returns the associated state.
  • bool logical_to_physical(uint64_t logical,std::vector<uint64_t>&physical_candidates,uint64_t&available)const – Implements the operation identified by this API and updates or returns the associated state.
  • bool read_logical(uint64_t logical,void*buffer,size_t len) – Reads or derives the requested information and returns or stores the decoded result.
  • bool read_tree_block(uint64_t logical_bytenr,std::vector<unsigned char>&block) – Reads or derives the requested information and returns or stores the decoded result.
  • bool load_chunk_tree(std::vector<std::string>&logs) – Reads or derives the requested information and returns or stores the decoded result.
  • walk_tree(chunk_root_bytenr_,[&](uint64_t,uint64_t,const std::vector<unsigned char>&block,const BtrfsItem&item) – Implements the operation identified by this API and updates or returns the associated state.
  • bool load_root_tree(std::vector<std::string>&logs,std::string&error) – Reads or derives the requested information and returns or stores the decoded result.
  • walk_tree(root_tree_bytenr_,[&](uint64_t,uint64_t,const std::vector<unsigned char>&block,const BtrfsItem&item) – Implements the operation identified by this API and updates or returns the associated state.
  • void walk_tree(uint64_t logical_bytenr,LeafFn&&leaf_fn) – Implements the operation identified by this API and updates or returns the associated state.
  • void walk_tree_internal(uint64_t logical_bytenr,LeafFn&leaf_fn) – Implements the operation identified by this API and updates or returns the associated state.
  • bool collect_fs_root(uint64_t root_id,const RootEntry&root,FsRootState&state,std::vector<std::string>&logs) – Implements the operation identified by this API and updates or returns the associated state.
  • walk_tree(root.bytenr,[&](uint64_t,uint64_t,const std::vector<unsigned char>&block,const BtrfsItem&item) – Implements the operation identified by this API and updates or returns the associated state.
  • fs::path inode_relative_path(uint64_t inode,const FsRootState&state)const – Implements the operation identified by this API and updates or returns the associated state.
  • bool read_logical_bytes(uint64_t logical,uint64_t length,std::vector<unsigned char>&out) – Reads or derives the requested information and returns or stores the decoded result.
  • bool render_extent(const FileExtentInfo&extent,std::vector<unsigned char>&out,bool&compressed_ok,bool&compressed_skipped) – Implements the operation identified by this API and updates or returns the associated state.
  • size_t export_fs_root(uint64_t root_id,const RootEntry&root,const FsRootState&state,std::vector<RecoveredFileRecord>&recovered,size_t&compressed_exported,size_t&compressed_skipped,std::vector<std::string>&logs) – Persists or exports the supplied state to the relevant destination.
  • BtrfsRecovery::BtrfsRecovery(DiskReader&reader): reader_(reader) – Implements the operation identified by this API and updates or returns the associated state.
  • bool BtrfsRecovery::recover(uint64_t volume_offset,const std::string&output_dir,std::vector<RecoveredFileRecord>&recovered,std::vector<std::string>&logs,std::string&error) – Performs the associated recovery or extraction operation.

8. Operational Flow

The subsystem represented by this file normally participates in the following flow:

  1. Read BTRFS superblocks and tree structures.
  2. Locate filesystem roots and extents.
  3. Traverse recovered directory trees.
  4. Export recoverable files and metadata.
  5. Expose or execute the detected APIs: le16, le32, le64, sanitize, to_hex, parse_key, decompress_payload, gf_mul_byte, gf_pow2, gf_inv_byte, crc32c_update, validate_tree_block_checksum, validate_tree_block_header, BtrfsRecoveryImpl, recover, parse_system_chunk_array, validate_raid6_candidate, reconstruct_raid56_read and additional helpers.
  6. Return explicit results, progress data, recovered records, or diagnostics to the calling layer.

9. Validation and Error Handling

  • Errors are propagated through Boolean results, error strings, logs, exceptions, or status records depending on the API.
  • Byte offsets, sizes and lengths must be validated before arithmetic and before every read to avoid overflow and out-of-bounds access.
  • On-disk structures are accepted only after validating signatures, magic values and structural constraints.
  • Input and output paths require existence checks, permission handling and safe destination construction.

10. Runtime, Concurrency, and Resource Management

  • I/O: filesystem and raw-source operations can fail partially and must preserve exact byte counts and diagnostic context.
  • Error handling: explicit error, exception or optional-result paths are present and form part of the file contract.
  • File descriptors and streams must be closed on success, cancellation and error paths.

11. Security and Data-Recovery Considerations

  • Recovery code should prefer read-only source access and avoid writing to the original damaged medium.
  • Raw devices and image files are treated as untrusted binary input; every offset and length must remain bounded by the source size.
  • Recovered names and destination paths must be sanitized to prevent directory traversal and accidental overwrite outside the selected export directory.

12. Implementation Considerations

  • The implementation should remain aligned with declarations in the paired header and preserve documented return-value semantics.
  • The file is large and combines multiple parsing or recovery stages; refactoring should preserve byte-level behavior and regression coverage.
  • Binary decoding uses low-level memory operations or casts; alignment, endianness and packed-structure assumptions require careful review.
  • Large-device arithmetic uses 64-bit values; additions and multiplications should be checked before use as offsets or allocation sizes.
  • Filesystem operations can cross trust boundaries and should use normalized, contained destination paths.

13. Integration with the Recovery Pipeline

  • The file belongs to the extraction layer and consumes scan candidates or validated filesystem metadata.
  • Recovered output is represented through shared models and returned to the exporter or UI.
  • Partial recovery must be reported explicitly rather than silently treated as complete.

14. Licensing and Provenance

No explicit copyright or software-license statement was detected in the opening section of this individual file. Distribution terms should therefore be verified against the project-level license and repository metadata.

The SHA-256 value in this document can be used to associate the generated documentation with the exact source version analyzed.