Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions doc/file_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ data_block_meta[data_block_count]
| 32 | 64 | `entry_meta.owner_name` | NUL-padded |
| 96 | 64 | `entry_meta.group_name` | NUL-padded |
| 160 | 8 | `entry_meta.modify_time` | `i64`, unix epoch milliseconds |
| 168 | 8 | `entry_meta.offset` | file payload offset inside the block |
| 168 | 8 | `entry_meta.file_offset` | absolute offset of the file payload from the start of the archive (`file_data_offset + block_file_offset + DataBlockMeta.file_offsets[i]`) |
| 176 | 8 | `entry_meta.file_orig_size` | original file size |
| 184 | 8 | `entry_meta.file_compressed_size` | stored (compressed) size |

Expand Down Expand Up @@ -150,19 +150,21 @@ The 4-byte `magic_number` (`CTAR`) follows the metadata at `metadata_offset + me

### locating a file payload

1. Read `file_entry_records[id]`; take `entry_meta.block_id`, `entry_meta.offset`, `entry_meta.file_compressed_size`.
1. Read `file_entry_records[id]`; take `entry_meta.block_id`, `entry_meta.file_offset`, `entry_meta.file_compressed_size`.
2. Read `data_block_metas[block_id]`; its payload starts at `file_data_offset + block_file_offset`.
3. The file bytes are at `payload_start + entry_meta.offset`, `file_compressed_size` bytes long.
3. The file bytes are at `entry_meta.file_offset`, `file_compressed_size` bytes long. Equivalently, the block-relative offset is `data_block_metas[block_id].file_offsets[index]`, where `index` is the position of `id` in `file_ids`; the absolute position is `payload_start + file_offsets[index]`.
4. Decompress according to `file_compressed_types`; verify `file_hashes`.

`dump`'s `offset` column reports this absolute `entry_meta.file_offset`; `ls` does not display it.

### reader validation contracts

The reader rejects archives that violate these cross-field invariants; a writer must satisfy them even though they are not separate fields:

- `file_ids` are strictly increasing within a block, so a block cannot reference the same file twice.
- File ranges (`file_offsets` + `file_compressed_sizes`) are ordered, non-overlapping, and fit inside `payload_size`.
- For `kLZ4`/`kLZ4HC`, `file_orig_sizes` and `file_compressed_sizes` fit the LZ4 `int` API, `file_orig_sizes <= 255 * file_compressed_sizes`, and a zero original size requires a zero stored size.
- Every `file_ids[i]` exists in `file_entry_records`; the entry's `block_id`, `file_orig_size`, `file_compressed_size` and absolute `offset` agree with the block, and every indexed file is referenced exactly once.
- Every `file_ids[i]` exists in `file_entry_records`; the entry's `block_id`, `file_orig_size`, `file_compressed_size` and absolute `file_offset` agree with the block, and every indexed file is referenced exactly once.
- Directory paths are unique, and a directory contains no duplicate file names, so every extracted file has a distinct output path.
- `unpack` enforces a per-file decompressed-size budget (`--max-decompressed-size`, default 1 GiB, `0` disables it). Archives with larger files need a larger budget or chunked files (#38).

Expand Down
10 changes: 6 additions & 4 deletions doc/file_format_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ data_block_meta[data_block_count]
| 32 | 64 | `entry_meta.owner_name` | NUL 填充 |
| 96 | 64 | `entry_meta.group_name` | NUL 填充 |
| 160 | 8 | `entry_meta.modify_time` | `i64`,Unix epoch 毫秒 |
| 168 | 8 | `entry_meta.offset` | 文件 payload 在块内的偏移 |
| 168 | 8 | `entry_meta.file_offset` | 文件 payload 从归档起点算起的绝对偏移(`file_data_offset + block_file_offset + DataBlockMeta.file_offsets[i]`) |
| 176 | 8 | `entry_meta.file_orig_size` | 文件原始大小 |
| 184 | 8 | `entry_meta.file_compressed_size` | 存储(压缩后)大小 |

Expand Down Expand Up @@ -148,19 +148,21 @@ data_block_meta[data_block_count]

### 定位文件 payload

1. 读取 `file_entry_records[id]`,取出 `entry_meta.block_id`、`entry_meta.offset`、`entry_meta.file_compressed_size`。
1. 读取 `file_entry_records[id]`,取出 `entry_meta.block_id`、`entry_meta.file_offset`、`entry_meta.file_compressed_size`。
2. 读取 `data_block_metas[block_id]`,其 payload 起点为 `file_data_offset + block_file_offset`。
3. 文件数据位于 `payload_start + entry_meta.offset`,长度为 `file_compressed_size` 字节。
3. 文件数据位于 `entry_meta.file_offset`,长度为 `file_compressed_size` 字节。等价地,块内相对偏移是 `data_block_metas[block_id].file_offsets[index]`(`index` 是 `id` 在 `file_ids` 中的位置),绝对位置为 `payload_start + file_offsets[index]`。
4. 按 `file_compressed_types` 解压,并用 `file_hashes` 校验。

`dump` 的 `offset` 列显示这个绝对值;`ls` 不显示 offset。

### 读取端校验约束

以下跨字段不变量不是独立的序列化字段,但 reader 会拒绝违反它们的归档,writer 必须满足:

- 块内 `file_ids` 严格递增,同一块不能引用同一个文件两次。
- 文件区间(`file_offsets` + `file_compressed_sizes`)有序、互不重叠,且都位于 `payload_size` 内。
- 对 `kLZ4`/`kLZ4HC`,`file_orig_sizes` 和 `file_compressed_sizes` 必须落在 LZ4 的 `int` API 范围内,`file_orig_sizes <= 255 * file_compressed_sizes`,且原始大小为 0 时存储大小也必须为 0。
- 每个 `file_ids[i]` 都必须存在于 `file_entry_records`,其 `block_id`、`file_orig_size`、`file_compressed_size` 和绝对 `offset` 与块元数据一致;所有索引文件都必须被且仅被引用一次。
- 每个 `file_ids[i]` 都必须存在于 `file_entry_records`,其 `block_id`、`file_orig_size`、`file_compressed_size` 和绝对 `file_offset` 与块元数据一致;所有索引文件都必须被且仅被引用一次。
- 目录路径唯一,同一目录内文件名唯一,保证每个解压文件有唯一的输出路径。
- `unpack` 对每个文件施加解压大小预算(`--max-decompressed-size`,默认 1 GiB,`0` 表示不限制)。更大的文件需要调大预算,或等待分块支持(#38)。

Expand Down
2 changes: 1 addition & 1 deletion src/core/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ namespace ctar
file_data_offset_ + block_meta.block_file_offset + block_meta.file_offsets[f];
if (entry.block_id != block_meta.block_id || entry.file_orig_size != block_meta.file_orig_sizes[f] ||
entry.file_compressed_size != block_meta.file_compressed_sizes[f] ||
entry.offset != absolute_offset)
entry.file_offset != absolute_offset)
{
return error_corruption(
"corrupt archive '{}': file '{}' in data block {} disagrees with the entry index",
Expand Down
14 changes: 7 additions & 7 deletions src/core/file_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace
else
{
std::format_to(it, tsv::kTsvRowFormat, block_id, dir_file_ref.dir_id, dir_file_ref.file_id, permissions, owner,
group, modify_time, offset, file_orig_size, file_compressed_size, dir_file_ref.dir_name,
group, modify_time, file_offset, file_orig_size, file_compressed_size, dir_file_ref.dir_name,
dir_file_ref.file_name);
}
}
Expand Down Expand Up @@ -498,7 +498,7 @@ namespace

size_t EntryMeta::SerializedSize() const noexcept
{
return FixedFieldTotalSize(entry_type, permissions, block_id, owner_name, group_name, modify_time, offset,
return FixedFieldTotalSize(entry_type, permissions, block_id, owner_name, group_name, modify_time, file_offset,
file_orig_size, file_compressed_size);
}

Expand All @@ -510,7 +510,7 @@ namespace
dst->append(owner_name, sizeof(owner_name));
dst->append(group_name, sizeof(group_name));
PutFixed64(dst, static_cast<uint64_t>(modify_time));
PutFixed64(dst, offset);
PutFixed64(dst, file_offset);
PutFixed64(dst, file_orig_size);
PutFixed64(dst, file_compressed_size);
}
Expand All @@ -533,7 +533,7 @@ namespace
ptr += sizeof(group_name);
modify_time = static_cast<long long>(DecodeFixed64(ptr));
ptr += sizeof(uint64_t);
offset = DecodeFixed64(ptr);
file_offset = DecodeFixed64(ptr);
ptr += sizeof(uint64_t);
file_orig_size = DecodeFixed64(ptr);
ptr += sizeof(uint64_t);
Expand Down Expand Up @@ -978,11 +978,11 @@ namespace
return (it != pack_file_meta_.dir_id_to_child_file_sizes.end()) ? it->second : kEmptyVec;
}

void PackFileHandle::SetFileEntryMeta(const entry_id_t file_id, uint64_t block_id, const uint64_t offset,
void PackFileHandle::SetFileEntryMeta(const entry_id_t file_id, uint64_t block_id, const uint64_t file_offset,
const uint64_t compressed_size)
{
pack_file_meta_.file_entry_records[file_id].entry_meta.block_id = block_id;
pack_file_meta_.file_entry_records[file_id].entry_meta.offset = offset;
pack_file_meta_.file_entry_records[file_id].entry_meta.file_offset = file_offset;
pack_file_meta_.file_entry_records[file_id].entry_meta.file_compressed_size = compressed_size;
}

Expand Down Expand Up @@ -1037,7 +1037,7 @@ namespace
std::snprintf(file_meta.owner_name, std::size(file_meta.owner_name), "%s", owner_name.c_str());
std::snprintf(file_meta.group_name, std::size(file_meta.group_name), "%s", group_name.c_str());
file_meta.block_id = 0;
file_meta.offset = 0;
file_meta.file_offset = 0;
return file_meta;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/file_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace ctar
char group_name[64];
// modify_time: fs::last_write_time represented as unix epoch milliseconds
long long modify_time;
uint64_t offset;
uint64_t file_offset;
uint64_t file_orig_size;
uint64_t file_compressed_size;

Expand Down Expand Up @@ -440,7 +440,7 @@ namespace ctar
/// @param block_id block identifier
/// @param offset block payload offset inside pack file
/// @param compressed_size compressed size inside pack file
void SetFileEntryMeta(entry_id_t file_id, uint64_t block_id, uint64_t offset, uint64_t compressed_size);
void SetFileEntryMeta(entry_id_t file_id, uint64_t block_id, uint64_t file_offset, uint64_t compressed_size);

/// @brief Set actual compressed payload bytes, compute block‑region padding bytes
/// @param compressed_bytes Real size of compressed payload data
Expand Down
4 changes: 2 additions & 2 deletions test/cases/test_file_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ namespace ctar
std::snprintf(rec.entry_meta.owner_name, sizeof(rec.entry_meta.owner_name), "alice");
std::snprintf(rec.entry_meta.group_name, sizeof(rec.entry_meta.group_name), "staff");
rec.entry_meta.modify_time = -1234567890123LL;
rec.entry_meta.offset = 4096;
rec.entry_meta.file_offset = 4096;
rec.entry_meta.file_orig_size = 1234;
rec.entry_meta.file_compressed_size = 999;
return rec;
Expand All @@ -876,7 +876,7 @@ namespace ctar
EXPECT_STREQ(decoded.owner_name, meta.owner_name);
EXPECT_STREQ(decoded.group_name, meta.group_name);
EXPECT_EQ(decoded.modify_time, meta.modify_time);
EXPECT_EQ(decoded.offset, meta.offset);
EXPECT_EQ(decoded.file_offset, meta.file_offset);
EXPECT_EQ(decoded.file_orig_size, meta.file_orig_size);
EXPECT_EQ(decoded.file_compressed_size, meta.file_compressed_size);
EXPECT_EQ(ptr, buf.data() + buf.size());
Expand Down
Loading