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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ ctar dump pack_file.ctar
`--max-decompressed-size SIZE` to change it (`0` disables the limit). Larger files
are rejected until chunked unpacking (#38) lands.

Archive readers (`unpack`, `tree`, `ls`, `dump`, `stat`) also limit the stored
and uncompressed metadata regions to 1 GiB by default; use
`--max-metadata-size SIZE` to change it (`0` disables the limit). This keeps a
small crafted archive from expanding into a multi-gigabyte metadata buffer.

In most cases the `pack` and `unpack` commands are all you need, and the more files there are, the more noticeable
the benefit. If you have other requirements — for example, storing the packed file in the cloud but wanting to
retrieve only part of the data inside it — refer to the short tutorial below.
Expand Down
4 changes: 4 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ ctar dump pack_file.ctar
`--max-decompressed-size SIZE` 调整(`0` 表示不限制)。超过预算的文件会被拒绝,
在分块支持(#38)落地前需要显式调大预算。

归档读取命令(`unpack`、`tree`、`ls`、`dump`、`stat`)还会把存储和未压缩 metadata
都限制为 1 GiB;可用 `--max-metadata-size SIZE` 调整(`0` 表示不限制)。这样小的
恶意归档无法膨胀成数 GiB 的 metadata 缓冲区。

一般情况下,使用 `pack` 和 `unpack` 命令即可满足大部分的需求,文件越多收益越明显。如果有其他需求,如将打包后的文件放在云端,但只想获取打包文件中的部分数据时,可参考下面的简短教程。

## 简短教程
Expand Down
1 change: 1 addition & 0 deletions doc/file_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ The reader rejects archives that violate these cross-field invariants; a writer
- 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).
- All archive readers enforce a per-archive metadata budget (`--max-metadata-size`, default 1 GiB, `0` disables it) for both the stored and the uncompressed metadata before allocating either. Compressed metadata above the LZ4 `int` limit is always rejected, compressed metadata must be strictly smaller than its original size, and empty data blocks are invalid.

### version encoding and compatibility

Expand Down
1 change: 1 addition & 0 deletions doc/file_format_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ data_block_meta[data_block_count]
- 每个 `file_ids[i]` 都必须存在于 `file_entry_records`,其 `block_id`、`file_orig_size`、`file_compressed_size` 和绝对 `file_offset` 与块元数据一致;所有索引文件都必须被且仅被引用一次。
- 目录路径唯一,同一目录内文件名唯一,保证每个解压文件有唯一的输出路径。
- `unpack` 对每个文件施加解压大小预算(`--max-decompressed-size`,默认 1 GiB,`0` 表示不限制)。更大的文件需要调大预算,或等待分块支持(#38)。
- 所有归档读取命令都对存储和未压缩 metadata 施加每归档预算(`--max-metadata-size`,默认 1 GiB,`0` 表示不限制),并在分配任一缓冲区之前检查。超过 LZ4 `int` 上限的压缩 metadata 始终被拒绝,压缩 metadata 必须严格小于原始大小,空 data block 也是非法的。

### 版本编码与兼容性

Expand Down
86 changes: 86 additions & 0 deletions src/cli/arg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ COMMAND OPTIONS
Per-file decompressed-size limit (default 1G, 0 = unlimited).
Files above it are rejected during unpack.

--max-metadata-size SIZE
Per-archive limit on stored and uncompressed metadata for
unpack/tree/ls/dump/stat (default 1G, 0 = unlimited). Larger
metadata is rejected before it is allocated.

ls:
-h
Print sizes in human-readable form (1024-based: K, M, G).
Expand Down Expand Up @@ -318,6 +323,21 @@ namespace ctar::cli
opt.max_decompressed_file_size = size;
++pos;
}
else if (op == "--max-metadata-size" && subcommand_name == "unpack")
{
++pos;
if (pos >= argc)
{
return error_invalid_arg("missing size after {}", op);
}
uint64_t size = 0;
if (!ParseSize(argv[pos], size))
{
return error_invalid_arg("invalid size '{}' for {}; use e.g. 512M or 1G", argv[pos], op);
}
opt.max_metadata_size = size;
++pos;
}
else
{
return error_invalid_arg("unknown option for {}: {}", subcommand_name, op);
Expand Down Expand Up @@ -390,6 +410,29 @@ namespace ctar::cli
{
opt.cmd = CommandType::STAT;
pos++;
while (pos < argc && argv[pos][0] == '-')
{
std::string_view op{argv[pos]};
if (op == "--max-metadata-size")
{
++pos;
if (pos >= argc)
{
return error_invalid_arg("missing size after {}", op);
}
uint64_t size = 0;
if (!ParseSize(argv[pos], size))
{
return error_invalid_arg("invalid size '{}' for {}; use e.g. 512M or 1G", argv[pos], op);
}
opt.max_metadata_size = size;
}
else
{
return error_invalid_arg("unknown option for stat: {}", op);
}
pos++;
}
int remain_argc = argc - pos;
if (remain_argc != 1)
{
Expand Down Expand Up @@ -456,6 +499,20 @@ namespace ctar::cli
opt.enable_tree_dir_only = true;
else if (op == "-s")
opt.enable_tree_stats = true;
else if (op == "--max-metadata-size")
{
++pos;
if (pos >= argc)
{
return error_invalid_arg("missing size after {}", op);
}
uint64_t size = 0;
if (!ParseSize(argv[pos], size))
{
return error_invalid_arg("invalid size '{}' for {}; use e.g. 512M or 1G", argv[pos], op);
}
opt.max_metadata_size = size;
}
else
{
return error_invalid_arg("unknown option for tree: {}", op);
Expand Down Expand Up @@ -496,6 +553,20 @@ namespace ctar::cli
std::string_view op{argv[pos]};
if (op == "-h")
opt.enable_human_readable = true;
else if (op == "--max-metadata-size")
{
++pos;
if (pos >= argc)
{
return error_invalid_arg("missing size after {}", op);
}
uint64_t size = 0;
if (!ParseSize(argv[pos], size))
{
return error_invalid_arg("invalid size '{}' for {}; use e.g. 512M or 1G", argv[pos], op);
}
opt.max_metadata_size = size;
}
else
{
return error_invalid_arg("unknown option for ls: {}", op);
Expand Down Expand Up @@ -550,6 +621,20 @@ namespace ctar::cli
{
opt.enable_dump_header = true;
}
else if (op == "--max-metadata-size")
{
++pos;
if (pos >= argc)
{
return error_invalid_arg("missing size after {}", op);
}
uint64_t size = 0;
if (!ParseSize(argv[pos], size))
{
return error_invalid_arg("invalid size '{}' for {}; use e.g. 512M or 1G", argv[pos], op);
}
opt.max_metadata_size = size;
}
else
{
return error_invalid_arg("unknown option for dump: {}", op);
Expand Down Expand Up @@ -626,6 +711,7 @@ namespace ctar::cli
unsigned hw_thread_cnt = (hw == 0) ? 1 : (hw);
unsigned worker_thread_cnt = opt.thread_cnt > 0 ? opt.thread_cnt : hw_thread_cnt;
BlockStore store(worker_thread_cnt, opt.comp_config);
store.SetMaxMetadataSize(opt.max_metadata_size);
switch (opt.cmd)
{
case CommandType::HELP:
Expand Down
2 changes: 2 additions & 0 deletions src/cli/arg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace ctar::cli
bool force = false;
// Per-file decompressed-size limit for unpack; 0 disables the limit.
uint64_t max_decompressed_file_size = kDefaultMaxDecompressedFileSize;
// Per-archive uncompressed metadata limit for archive readers; 0 disables it.
uint64_t max_metadata_size = kDefaultMaxMetadataSize;

std::string input_data;
bool enable_hash_file = false;
Expand Down
Loading
Loading