Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14a0ad3b85 | ||
|
|
4c06bdd39c | ||
|
|
e8f61d7bb9 | ||
|
|
88d1e3b183 | ||
|
|
c6e06a0d48 | ||
|
|
cd338d4fc0 | ||
|
|
da291f0f9a | ||
|
|
a2ecebb5e6 | ||
|
|
2d98c4aad3 | ||
|
|
902d10d0e7 | ||
|
|
38edbb9ac1 | ||
|
|
2dac37b40c |
No files matched your search
@@ -0,0 +1,135 @@
|
||||
# macOS build (CPU-only) — lzply has never been built on a Mac.
|
||||
#
|
||||
# Apple Silicon (arm64) is the standard runner now; Intel still has a
|
||||
# standard runner (macos-15-intel) until ~Aug 2027, so both are built.
|
||||
# Pinned to macos-15, not macos-latest: -latest is a moving target
|
||||
# (migrating 15 -> 26) and the OS generation is part of the build
|
||||
# contract for a release binary.
|
||||
#
|
||||
# No CUDA on Mac (no NVIDIA GPU on modern Macs) and no FUSE
|
||||
# (lzply_mount is Linux-only), so this builds the core tools only.
|
||||
# GNU coreutils (gnubin on PATH) supplies the GNU stat/dd/timeout the
|
||||
# shell suites need. Homebrew's lzlib formula provides liblz + lzlib.h
|
||||
# (the lzip formula is only the CLI tool, used by the test cross-check).
|
||||
#
|
||||
# The mount tests in the shell suites skip cleanly (no /dev/fuse, no
|
||||
# lzply_mount binary). lzply_util.c's /proc mount resolution compiles
|
||||
# on macOS and degrades gracefully (finds no mounts) — a known
|
||||
# limitation, not a failure: stat/heal/check/refresh still work on
|
||||
# plain archive paths.
|
||||
#
|
||||
# Diagnosability: run #3 failed at the chained "C unit tests" step
|
||||
# with no retrievable logs, so each C test is now its own step, every
|
||||
# test writes its output to a log file that is also printed to the
|
||||
# step log, and all logs are uploaded as artifacts on failure.
|
||||
name: macOS build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, ci/macos-build]
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: macOS ${{ matrix.arch }} (${{ matrix.os }})
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- os: macos-15
|
||||
arch: arm64
|
||||
- os: macos-15-intel
|
||||
arch: x86_64
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Check out
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Install dependencies (lzlib + lzip CLI + GNU coreutils)
|
||||
run: brew install lzip lzlib coreutils
|
||||
|
||||
- name: Build core tools (CPU-only, no FUSE mount)
|
||||
run: |
|
||||
exec > >(tee build.log) 2>&1
|
||||
export PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
|
||||
make -j"$(sysctl -n hw.ncpu)" CC=cc \
|
||||
CFLAGS="-O2 -Wall -Wextra -I$(brew --prefix lzlib)/include" \
|
||||
LDLIBS="-L$(brew --prefix lzlib)/lib -llz" \
|
||||
lzply lz_rs lzply_deep_repair lzply_stat lzply_heal \
|
||||
lzply_check lzply_refresh lzply_hyper \
|
||||
bch_test rs_test crc16_test chase_bench_cpu
|
||||
file lzply
|
||||
|
||||
- name: Pre-flight (test binaries and linked libraries)
|
||||
run: |
|
||||
ls -la bch_test rs_test crc16_test
|
||||
file bch_test rs_test crc16_test
|
||||
otool -L ./bch_test ./rs_test ./crc16_test
|
||||
ls -la "$(brew --prefix lzlib)/lib/"
|
||||
|
||||
- name: C unit test (bch)
|
||||
run: |
|
||||
./bch_test > bch_test.log 2>&1 || rc=$?
|
||||
cat bch_test.log
|
||||
exit ${rc:-0}
|
||||
|
||||
- name: C unit test (rs)
|
||||
run: |
|
||||
./rs_test > rs_test.log 2>&1 || rc=$?
|
||||
cat rs_test.log
|
||||
exit ${rc:-0}
|
||||
|
||||
- name: C unit test (crc16)
|
||||
run: |
|
||||
./crc16_test > crc16_test.log 2>&1 || rc=$?
|
||||
cat crc16_test.log
|
||||
exit ${rc:-0}
|
||||
|
||||
- name: Build test data
|
||||
run: |
|
||||
export PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
|
||||
make testdata
|
||||
|
||||
- name: "Shell suites (mount tests skip: no FUSE on macOS)"
|
||||
run: |
|
||||
export PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
|
||||
./tests/run_rs_tests.sh ./lz_rs
|
||||
./tests/run_tests.sh ./lzply
|
||||
./tests/run_deep_repair_tests.sh ./lzply_deep_repair ./lzply ./lz_rs
|
||||
./tests/run_hyper_tests.sh ./lzply_hyper
|
||||
|
||||
- name: Smoke test (round-trip)
|
||||
run: |
|
||||
for i in $(seq 1 100); do echo "hello macOS $i"; done > /tmp/smoke.txt
|
||||
./lzply -c /tmp/smoke.txt /tmp/smoke.lzply
|
||||
./lzply -t /tmp/smoke.lzply
|
||||
mkdir -p /tmp/smoke-out && ./lzply -d /tmp/smoke.lzply /tmp/smoke-out
|
||||
cmp /tmp/smoke.txt /tmp/smoke-out/smoke.txt
|
||||
|
||||
- name: Upload test logs on failure
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: test-logs-macos-${{ matrix.arch }}
|
||||
path: |
|
||||
build.log
|
||||
bch_test.log
|
||||
rs_test.log
|
||||
crc16_test.log
|
||||
if-no-files-found: warn
|
||||
|
||||
- name: Upload binaries
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: lzply-macos-${{ matrix.arch }}
|
||||
path: |
|
||||
lzply
|
||||
lz_rs
|
||||
lzply_deep_repair
|
||||
lzply_stat
|
||||
lzply_heal
|
||||
lzply_check
|
||||
lzply_refresh
|
||||
lzply_hyper
|
||||
if-no-files-found: error
|
||||
@@ -35,3 +35,5 @@ randfec/m2/p3-driver.log
|
||||
/experiments/bchcheck
|
||||
/testwork-int/
|
||||
/testwork-bench/
|
||||
/lzcsv
|
||||
/testwork-lzcsv/
|
||||
@@ -3,33 +3,84 @@ CFLAGS = -O2 -Wall -Wextra
|
||||
LDLIBS = -llz
|
||||
PREFIX = /usr/local
|
||||
|
||||
# CUDA BCH remainder (optional): if nvcc is on PATH, bch_cuda.o is
|
||||
# built and linked into liblzply.a and the tools; bch.c dispatches
|
||||
# to it via weak symbols for large blobs. Without nvcc the build is
|
||||
# unchanged (CPU-only). These definitions MUST stay above every rule
|
||||
# that references $(CUDA_OBJ): make expands prerequisites when the
|
||||
# rule is read, so a definition below the liblzply.a rule would
|
||||
# silently leave bch_cuda.o out of the archive. NOTE: bch_cuda.o is
|
||||
# still listed explicitly on each tool link line below, because the
|
||||
# weak references from bch.o do not make the linker pull it out of
|
||||
# the archive on their own.
|
||||
# CUDA (optional): nvcc is found on PATH first; on hot_house it lives
|
||||
# at /usr/local/cuda/bin/nvcc (not on PATH by default), so fall back
|
||||
# to that path when it exists. Two CUDA objects share this NVCC:
|
||||
# bch_cuda.o -- CUDA BCH remainder (bch.c dispatches via weak
|
||||
# symbols for large blobs); linked into liblzply.a
|
||||
# and every tool. Without nvcc the build is
|
||||
# unchanged (CPU-only).
|
||||
# chase_cuda.o -- CUDA Chase-1 scan for deep repair (t=8 only);
|
||||
# linked into lzply and lzply_deep_repair, with a
|
||||
# CPU fallback via weak references.
|
||||
# Without nvcc, the *_cuda_stub.o objects are linked instead, so
|
||||
# the CUDA entry points always resolve -- see the stub comment
|
||||
# below. The stubs are required on macOS: ld64 does not resolve
|
||||
# __attribute__((weak)) declarations to NULL the way GNU ld does,
|
||||
# so a CPU-only build failed at link time with undefined
|
||||
# _bch_cuda_available/_encode/_decode symbols (and, after the
|
||||
# chase-wired merge, _chase_cuda_available/_scan1). A stub is
|
||||
# never built alongside its CUDA object, so the symbols are
|
||||
# defined exactly once.
|
||||
# These definitions MUST stay above every rule that references
|
||||
# $(CUDA_OBJ): make expands prerequisites when the rule is read, so
|
||||
# a definition below the liblzply.a rule would silently leave
|
||||
# bch_cuda.o out of the archive. NOTE: bch_cuda.o is still listed
|
||||
# explicitly on each tool link line below, because the weak
|
||||
# references from bch.o do not make the linker pull it out of the
|
||||
# archive on their own. The stub is listed the same way.
|
||||
NVCC ?= $(shell command -v nvcc 2>/dev/null)
|
||||
ifeq ($(NVCC),)
|
||||
NVCC := $(wildcard /usr/local/cuda/bin/nvcc)
|
||||
endif
|
||||
CUDA_ARCH ?= sm_86
|
||||
NVCCFLAGS = -O3 -arch=$(CUDA_ARCH) -Xcompiler -O2
|
||||
CUDA_OBJ =
|
||||
CUDA_LIBS =
|
||||
ifneq ($(NVCC),)
|
||||
CUDA_OBJ = bch_cuda.o
|
||||
CUDA_LIBS = -L$(dir $(NVCC))../lib64 -lcudart -lstdc++
|
||||
endif
|
||||
CHASE_CUDA_OBJ =
|
||||
ifneq ($(wildcard $(NVCC)),)
|
||||
CHASE_CUDA_OBJ = chase_cuda.o
|
||||
endif
|
||||
|
||||
all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
|
||||
# CPU-only stubs: when nvcc is absent, bch_cuda_stub.o defines the
|
||||
# three BCH CUDA entry points (available() = 0, encode = 0,
|
||||
# decode = -2) and chase_cuda_stub.o defines the two Chase CUDA
|
||||
# entry points (available() = 0, scan1 = -2), so bch.c's and
|
||||
# deep_repair.c's references always resolve. This is required on
|
||||
# macOS: ld64 does not resolve __attribute__((weak)) declarations
|
||||
# to NULL the way GNU ld does, so a CPU-only build failed at link
|
||||
# time with undefined _bch_cuda_available/_encode/_decode symbols
|
||||
# (and, after the chase-wired merge, _chase_cuda_available/_scan1).
|
||||
# A stub is never built alongside its CUDA object, so the symbols
|
||||
# are defined exactly once.
|
||||
CUDA_STUB_OBJ =
|
||||
ifeq ($(NVCC),)
|
||||
CUDA_STUB_OBJ = bch_cuda_stub.o chase_cuda_stub.o
|
||||
endif
|
||||
|
||||
# lzcsv: standalone high-speed CSV compressor (column transpose + xz).
|
||||
# Completely separate from the lzply FEC core: own source (lzcsv-src/lzcsv.c),
|
||||
# own binary, links only liblzma -- no liblzply.a, no liblz, no CUDA.
|
||||
# Built only when lzma.h is present; skipped cleanly otherwise (e.g. a
|
||||
# minimal CI image without liblzma-dev), so `make all` never fails for
|
||||
# lack of it.
|
||||
LZCSV =
|
||||
ifneq ($(wildcard /usr/include/lzma.h),)
|
||||
LZCSV = lzcsv
|
||||
endif
|
||||
|
||||
all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair $(LZCSV)
|
||||
|
||||
# Shared core: the v5 archive format, the per-entry pipeline
|
||||
# (lzip -> BCH), header I/O, scan, and filters. The
|
||||
# command-line tool (lzply), the FUSE filesystem (lzply_mount),
|
||||
# the statistics tool (lzply_stat) and the heal tool (lzply_heal)
|
||||
# all link it.
|
||||
liblzply.a: lzply_lib.o bch.o sha256.o $(CUDA_OBJ)
|
||||
liblzply.a: lzply_lib.o bch.o sha256.o $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
lzply_lib.o: lzply_lib.c lzply_lib.h bch.h bch_polys.h sha256.h
|
||||
@@ -41,6 +92,12 @@ sha256.o: sha256.c sha256.h
|
||||
bch.o: bch.c bch.h bch_polys.h
|
||||
$(CC) $(CFLAGS) -c -o $@ bch.c
|
||||
|
||||
bch_cuda_stub.o: bch_cuda_stub.c bch_cuda.h bch.h
|
||||
$(CC) $(CFLAGS) -c -o $@ bch_cuda_stub.c
|
||||
|
||||
chase_cuda_stub.o: chase_cuda_stub.c chase_cuda.h bch.h
|
||||
$(CC) $(CFLAGS) -c -o $@ chase_cuda_stub.c
|
||||
|
||||
# CUDA BCH remainder rules (variables are defined at the top).
|
||||
ifneq ($(NVCC),)
|
||||
bch_cuda.o: bch_cuda.cu bch_cuda.h bch.h bch_polys.h
|
||||
@@ -61,15 +118,19 @@ else
|
||||
bch_bench.o: bch_bench.c bch.h
|
||||
$(CC) $(CFLAGS) -c -o $@ bch_bench.c
|
||||
|
||||
bch_bench: bch_bench.o bch.o
|
||||
$(CC) $(CFLAGS) -o $@ bch_bench.o bch.o
|
||||
bch_bench: bch_bench.o bch.o $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ bch_bench.o bch.o $(CUDA_STUB_OBJ)
|
||||
endif
|
||||
|
||||
lzply.o: lzply.c lzply_lib.h bch.h bch_polys.h
|
||||
lzply.o: lzply.c lzply_lib.h bch.h bch_polys.h deep_repair.h repair_cmd.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply.c
|
||||
|
||||
lzply: lzply.o liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply.o $(CUDA_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
lzply: lzply.o deep_repair.o repair_cmd.o liblzrs.a liblzply.a $(CUDA_OBJ) $(CHASE_CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
ifeq ($(CHASE_CUDA_OBJ),)
|
||||
$(CC) $(CFLAGS) -o $@ lzply.o deep_repair.o repair_cmd.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
else
|
||||
$(NVCC) -arch=$(CUDA_ARCH) -o $@ lzply.o deep_repair.o repair_cmd.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(CHASE_CUDA_OBJ) $(LDLIBS) $(CUDA_LIBS)
|
||||
endif
|
||||
|
||||
# FUSE filesystem. Needs libfuse3-dev; no pkg-config on the server, so
|
||||
# the flags are explicit (-D_FILE_OFFSET_BITS=64 is required by FUSE).
|
||||
@@ -79,8 +140,8 @@ FUSE_LIBS = -lfuse3
|
||||
lzply_mount.o: lzply_mount.c lzply_lib.h bch.h bch_polys.h
|
||||
$(CC) $(CFLAGS) $(FUSE_CFLAGS) -c -o $@ lzply_mount.c
|
||||
|
||||
lzply_mount: lzply_mount.o liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) $(FUSE_CFLAGS) -o $@ lzply_mount.o $(CUDA_OBJ) liblzply.a $(LDLIBS) $(FUSE_LIBS) $(CUDA_LIBS)
|
||||
lzply_mount: lzply_mount.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) $(FUSE_CFLAGS) -o $@ lzply_mount.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(FUSE_LIBS) $(CUDA_LIBS)
|
||||
|
||||
# Shared helpers for lzply_stat and lzply_heal (mount resolution via
|
||||
# /proc, human-size formatting). No FUSE needed.
|
||||
@@ -90,14 +151,14 @@ lzply_util.o: lzply_util.c lzply_lib.h lzply_util.h
|
||||
lzply_stat.o: lzply_stat.c lzply_lib.h lzply_util.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply_stat.c
|
||||
|
||||
lzply_stat: lzply_stat.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_stat.o lzply_util.o $(CUDA_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
lzply_stat: lzply_stat.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_stat.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
|
||||
lzply_heal.o: lzply_heal.c lzply_lib.h lzply_util.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply_heal.c
|
||||
|
||||
lzply_heal: lzply_heal.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_heal.o lzply_util.o $(CUDA_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
lzply_heal: lzply_heal.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_heal.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
|
||||
# Bit error rate detector. Read-only: decodes each BCH entry's stored
|
||||
# bytes in memory and counts the corrected bit errors. Links the same
|
||||
@@ -105,8 +166,8 @@ lzply_heal: lzply_heal.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
lzply_check.o: lzply_check.c lzply_lib.h lzply_util.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply_check.c
|
||||
|
||||
lzply_check: lzply_check.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_check.o lzply_util.o $(CUDA_OBJ) liblzply.a $(LDLIBS) -lm $(CUDA_LIBS)
|
||||
lzply_check: lzply_check.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_check.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) -lm $(CUDA_LIBS)
|
||||
|
||||
# In-place platter refresher. Reads a file (or every regular file
|
||||
# under a directory / mount point) and rewrites it over itself, with
|
||||
@@ -115,8 +176,8 @@ lzply_check: lzply_check.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
lzply_refresh.o: lzply_refresh.c lzply_lib.h lzply_util.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply_refresh.c
|
||||
|
||||
lzply_refresh: lzply_refresh.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_refresh.o lzply_util.o $(CUDA_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
lzply_refresh: lzply_refresh.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_refresh.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
|
||||
# Striped volume (bit-plane, 8 data stripes + 1-3 XOR/GF parity stripes
|
||||
# + BCH). No compression, so it links only bch.o + sha256.o — no
|
||||
@@ -124,20 +185,19 @@ lzply_refresh: lzply_refresh.o lzply_util.o liblzply.a $(CUDA_OBJ)
|
||||
lzply_hyper.o: lzply_hyper.c bch.h bch_polys.h sha256.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply_hyper.c
|
||||
|
||||
lzply_hyper: lzply_hyper.o bch.o sha256.o
|
||||
$(CC) $(CFLAGS) -o $@ lzply_hyper.o bch.o sha256.o
|
||||
lzply_hyper: lzply_hyper.o bch.o sha256.o $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_hyper.o bch.o sha256.o $(CUDA_STUB_OBJ)
|
||||
|
||||
bch_test: bch.c bch_test.c bch.h bch_polys.h
|
||||
$(CC) $(CFLAGS) -o $@ bch.c bch_test.c
|
||||
bch_test: bch.c bch_test.c bch_cuda_stub.c bch.h bch_polys.h bch_cuda.h
|
||||
$(CC) $(CFLAGS) -o $@ bch.c bch_test.c bch_cuda_stub.c
|
||||
|
||||
# CUDA Chase-1 bench (experiments/cuda-chase): GPU port of the
|
||||
# deep-repair Chase-1 scan (t=8 only). Needs CUDA 13.x; NVCC is
|
||||
# /usr/local/cuda/bin/nvcc on hot_house (not on PATH by default).
|
||||
NVCC ?= /usr/local/cuda/bin/nvcc
|
||||
NVCCFLAGS = -O3 -arch=sm_86 -Xcompiler -O2
|
||||
# NVCC / NVCCFLAGS are defined at the top of this file.
|
||||
|
||||
chase_bench_cpu: chase_bench.c bch.c bch.h bch_polys.h
|
||||
$(CC) $(CFLAGS) -o $@ chase_bench.c bch.c
|
||||
chase_bench_cpu: chase_bench.c bch.c bch_cuda_stub.c bch.h bch_polys.h bch_cuda.h
|
||||
$(CC) $(CFLAGS) -o $@ chase_bench.c bch.c bch_cuda_stub.c
|
||||
|
||||
chase_bench.o: chase_bench.c bch.h chase_cuda.h
|
||||
$(CC) $(CFLAGS) -DHAVE_CUDA -c -o $@ chase_bench.c
|
||||
@@ -163,16 +223,16 @@ liblzrs.a: lzrs_lib.o rs.o
|
||||
lz_rs.o: lz_rs.c lzrs_lib.h lzply_lib.h bch.h bch_polys.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lz_rs.c
|
||||
|
||||
lz_rs: lz_rs.o liblzrs.a liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lz_rs.o liblzrs.a $(CUDA_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
lz_rs: lz_rs.o liblzrs.a liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lz_rs.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
|
||||
rs_test: rs.c rs_test.c rs.h
|
||||
$(CC) $(CFLAGS) -o $@ rs.c rs_test.c
|
||||
|
||||
# CRC-16/CCITT-FALSE unit test (vectors + cross-checks). Links the
|
||||
# shared core for crc16_of.
|
||||
crc16_test: crc16_test.c liblzply.a
|
||||
$(CC) $(CFLAGS) -o $@ crc16_test.c liblzply.a $(LDLIBS)
|
||||
crc16_test: crc16_test.c liblzply.a $(CUDA_STUB_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ crc16_test.c liblzply.a $(CUDA_STUB_OBJ) $(LDLIBS)
|
||||
|
||||
# Deep repair: Chase / list decoding past t (bch.c) + the escalating
|
||||
# ladder (deep_repair.c) + the lzply_deep_repair CLI. Links both
|
||||
@@ -180,11 +240,15 @@ crc16_test: crc16_test.c liblzply.a
|
||||
deep_repair.o: deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h rs.h sha256.h
|
||||
$(CC) $(CFLAGS) -c -o $@ deep_repair.c
|
||||
|
||||
lzply_deep_repair.o: lzply_deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h
|
||||
lzply_deep_repair.o: lzply_deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h repair_cmd.h
|
||||
$(CC) $(CFLAGS) -c -o $@ lzply_deep_repair.c
|
||||
|
||||
lzply_deep_repair: lzply_deep_repair.o deep_repair.o liblzrs.a liblzply.a $(CUDA_OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_deep_repair.o deep_repair.o liblzrs.a $(CUDA_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
lzply_deep_repair: lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a liblzply.a $(CUDA_OBJ) $(CHASE_CUDA_OBJ) $(CUDA_STUB_OBJ)
|
||||
ifeq ($(CHASE_CUDA_OBJ),)
|
||||
$(CC) $(CFLAGS) -o $@ lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
|
||||
else
|
||||
$(NVCC) -arch=$(CUDA_ARCH) -o $@ lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(CHASE_CUDA_OBJ) $(LDLIBS) $(CUDA_LIBS)
|
||||
endif
|
||||
|
||||
# bch_polys.h is generated by gen_bch_polys.py from the rssearch data
|
||||
# (~/workspace/rssearch/polys.txt + finds.txt) and checked in; it is
|
||||
@@ -203,10 +267,17 @@ lzply_deep_repair: lzply_deep_repair.o deep_repair.o liblzrs.a liblzply.a $(CUDA
|
||||
# testdata/ is generated (it includes a 50 MiB random file) and gitignored,
|
||||
# so a fresh clone has none. Build it on demand so `make test` works
|
||||
# straight from a clone.
|
||||
# lzcsv: standalone CSV compressor. CPU-only, links liblzma.
|
||||
lzcsv-src/lzcsv.o: lzcsv-src/lzcsv.c
|
||||
$(CC) $(CFLAGS) -c -o $@ lzcsv-src/lzcsv.c
|
||||
|
||||
lzcsv: lzcsv-src/lzcsv.o
|
||||
$(CC) $(CFLAGS) -o $@ lzcsv-src/lzcsv.o -llzma
|
||||
|
||||
testdata:
|
||||
@if [ ! -d testdata/mixed ]; then ./tests/make_testdata.sh; fi
|
||||
|
||||
test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
|
||||
test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair $(LZCSV)
|
||||
./bch_test
|
||||
./rs_test
|
||||
./crc16_test
|
||||
@@ -235,6 +306,13 @@ test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_he
|
||||
elif [ -x ../tests/run_integration_tests.sh ]; then \
|
||||
../tests/run_integration_tests.sh; \
|
||||
else echo "run_integration_tests.sh not found"; exit 1; fi
|
||||
if [ -x ./lzcsv ]; then \
|
||||
if [ -x ./tests/run_lzcsv_tests.sh ]; then \
|
||||
./tests/run_lzcsv_tests.sh ./lzcsv; \
|
||||
elif [ -x ../tests/run_lzcsv_tests.sh ]; then \
|
||||
../tests/run_lzcsv_tests.sh ./lzcsv; \
|
||||
else echo "run_lzcsv_tests.sh not found"; exit 1; fi \
|
||||
else echo "lzcsv: skipped (no liblzma)"; fi
|
||||
|
||||
# Integrated experiment winners (docs/INTEGRATION.md): the ECC harness
|
||||
# (interleave d64, small-block BCH, LDPC r90) and the compression
|
||||
@@ -260,6 +338,6 @@ install: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply
|
||||
install -m 755 mount.lzply /sbin/mount.lzply
|
||||
|
||||
clean:
|
||||
rm -f lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair bch_test rs_test crc16_test bch_cuda_test bch_bench chase_bench_cpu chase_bench_cuda *.o liblzply.a liblzrs.a
|
||||
rm -f lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair lzcsv lzcsv-src/lzcsv.o bch_test rs_test crc16_test bch_cuda_test bch_bench chase_bench_cpu chase_bench_cuda *.o liblzply.a liblzrs.a
|
||||
|
||||
.PHONY: all test install clean exp experiments
|
||||
@@ -53,6 +53,35 @@ BCH-decode -> decompress (on read)
|
||||
and `-datestart` / `-dateend` / `-year` filter on it (see below).
|
||||
File mode is preserved too; ownership is not.
|
||||
|
||||
## Download
|
||||
|
||||
Just want the executables? Grab the latest release, **v0.1.1**, for your
|
||||
machine — no building needed:
|
||||
|
||||
| Platform | Download | Size | Needs |
|
||||
| -------- | -------- | ---- | ----- |
|
||||
| Linux x86_64, CPU-only | [lzply-v0.1.1-linux-x86_64-cpu.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-linux-x86_64-cpu.tar.gz) | 390,376 B | liblz.so.1 |
|
||||
| Linux x86_64, CUDA | [lzply-v0.1.1-linux-x86_64-cuda.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-linux-x86_64-cuda.tar.gz) | 2,440,044 B | libcudart.so.13 + liblz.so.1 |
|
||||
| Linux ARM64 (Raspberry Pi) | [lzply-v0.1.1-linux-aarch64-rpi.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-linux-aarch64-rpi.tar.gz) | 345,708 B | liblz.so.1 |
|
||||
| macOS arm64 (Apple Silicon) | [lzply-v0.1.1-macos-arm64.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-macos-arm64.tar.gz) | 326,010 B | liblz.so.1 |
|
||||
| macOS x86_64 (Intel) | [lzply-v0.1.1-macos-x86_64.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-macos-x86_64.tar.gz) | 343,953 B | liblz.so.1 |
|
||||
|
||||
Each archive holds `lzply`, `lz_rs`, `lzply_check`, `lzply_deep_repair`,
|
||||
`lzply_heal`, `lzply_hyper`, `lzply_refresh`, `lzply_stat` — plus
|
||||
`lzply_mount` on Linux x86_64 only — and a README. Unpack it and put the
|
||||
binaries somewhere on your `PATH`. On Debian/Ubuntu install the `liblz1`
|
||||
package if a binary complains about a missing `liblz.so.1`; on macOS run
|
||||
`brew install lzlib`. The CUDA build additionally needs the CUDA runtime
|
||||
(`libcudart.so.13`) installed just to start — with no CUDA device present
|
||||
it falls back to the CPU path, byte-identical.
|
||||
|
||||
Full release page (all assets, checksums, and what's new):
|
||||
https://github.com/cnmcdee/lzply/releases/tag/v0.1.1
|
||||
|
||||
Note: `lzcsv`, the standalone CSV compressor in `lzcsv-src/` (see its
|
||||
README there), is built from source — there is no binary release for it
|
||||
yet.
|
||||
|
||||
## The tools
|
||||
|
||||
Seven programs, one family. All are plain C, built by `make`
|
||||
@@ -191,7 +220,8 @@ is the only LZRS format.
|
||||
|
||||
## Deep repair — past the BCH limit
|
||||
|
||||
`lzply_deep_repair` recovers entries whose bit errors exceed the
|
||||
`lzply_deep_repair` -- also built into the main tool as `lzply -r` --
|
||||
recovers entries whose bit errors exceed the
|
||||
BCH correction limit t, in escalating levels, reporting per-level
|
||||
timing ("time of arrival": how long each level took and at which
|
||||
level the repair landed):
|
||||
@@ -227,7 +257,9 @@ CRC-checked, 1,024 rejected by CRC, 0 false accepts.
|
||||
|
||||
Limits, measured honestly: Chase scans are O(nbits) trials at
|
||||
~7,800 trials/s on m=22 blocks (a full scan of a 524 KiB block
|
||||
takes ~9 minutes), so beyond-t repair of large blocks is bounded
|
||||
takes ~9 minutes on the CPU; ~0.35 s on a CUDA build -- the L1
|
||||
scan runs on the GPU when a device is present, with CPU fallback),
|
||||
so beyond-t repair of large blocks is bounded
|
||||
by the time budget (default 120 s per entry) and reports partial
|
||||
coverage rather than pretending. Two bugs found by measurement
|
||||
and fixed: the trial flip was not applied to the reconstructed
|
||||
@@ -236,7 +268,9 @@ candidates before trying them, truncating the list before the
|
||||
genuine candidate arrived (a 600 KiB file with 9 errors in one
|
||||
block failed at a 60 s budget before the fix; it recovers after
|
||||
it). Full design, bounds, and measured numbers:
|
||||
`docs/deep-repair.md`. Tests: `tests/run_deep_repair_tests.sh`
|
||||
`docs/deep-repair.md`. CUDA Chase write-up:
|
||||
https://www.lzply.com/cuda-chase-500x-deep-repair/ . Tests:
|
||||
`tests/run_deep_repair_tests.sh`
|
||||
(round-trips, old-format rejection, corruption matrix,
|
||||
false-accept measurement).
|
||||
|
||||
@@ -268,7 +302,7 @@ Write-ups: [171 seconds to a third of a second: deep-repair Chase on CUDA runs 5
|
||||
## Building
|
||||
|
||||
```sh
|
||||
apt install build-essential liblz-dev # Debian/Ubuntu
|
||||
apt install build-essential liblz-dev libfuse3-dev fuse3 # Debian/Ubuntu
|
||||
make # produces ./lzply, ./lz_rs, ./lzply_mount, ./lzply_stat, ./lzply_heal, ./lzply_check, ./lzply_refresh, ./lzply_deep_repair
|
||||
make test # BCH + RS + CRC16 unit tests + full archive test suites (lzply + lz_rs + deep repair)
|
||||
sudo make install # /usr/local/bin/lzply* (including lz_rs), /sbin/mount.lzply
|
||||
|
||||
@@ -43,8 +43,13 @@
|
||||
#include "bch.h"
|
||||
|
||||
/* CUDA fast path (bch_cuda.cu, linked only when built with nvcc).
|
||||
* Weak symbols: a CPU-only build leaves them NULL and always takes
|
||||
* the CPU path below. bch_cuda_encode returns the stored length or 0
|
||||
* Without nvcc the Makefile links bch_cuda_stub.c, which defines
|
||||
* these three functions to report "no GPU", so a CPU-only build
|
||||
* always takes the CPU path below. The declarations are also marked
|
||||
* weak, so an out-of-tree consumer that links bch.o without either
|
||||
* object still resolves them to NULL on linkers that support that
|
||||
* (GNU ld does; macOS ld64 does not -- the stub is what the build
|
||||
* relies on). bch_cuda_encode returns the stored length or 0
|
||||
* on failure; bch_cuda_decode returns 0, -1 (uncorrectable) or -2
|
||||
* (internal error). Any CUDA failure falls through to the CPU path,
|
||||
* which re-verifies, so results are identical with or without a GPU. */
|
||||
|
||||
+6
-2
@@ -21,8 +21,12 @@
|
||||
* All functions fall back gracefully: bch_cuda_encode returns 0 and
|
||||
* bch_cuda_decode returns -2 on any CUDA problem, and bch.c treats
|
||||
* those as "use the CPU path". bch_cuda_available() is 0 when no
|
||||
* CUDA device is present. The symbols are referenced weakly from
|
||||
* bch.c, so a CPU-only build (no bch_cuda.o linked) works unchanged.
|
||||
* CUDA device is present. A CPU-only build links bch_cuda_stub.c,
|
||||
* which defines these three functions to report "no GPU", so the
|
||||
* references always resolve. (bch.c also declares them
|
||||
* __attribute__((weak)) as a fallback for out-of-tree consumers;
|
||||
* the build itself does not rely on weak-symbol resolution, which
|
||||
* macOS ld64 does not provide for declarations.)
|
||||
*/
|
||||
#ifndef LZPLY_BCH_CUDA_H
|
||||
#define LZPLY_BCH_CUDA_H
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/* bch_cuda_stub.c - CPU-only fallback for the CUDA BCH entry points.
|
||||
*
|
||||
* Defines the three symbols bch.c references (bch_cuda_available,
|
||||
* bch_cuda_encode, bch_cuda_decode) for builds without nvcc, so a
|
||||
* CPU-only link always resolves. The stub reports "no GPU": the
|
||||
* caller in bch.c then takes the CPU path, exactly as it does when
|
||||
* a CUDA build is present but no device is found.
|
||||
*
|
||||
* This exists because the weak-symbol fallback it replaces does not
|
||||
* work on macOS: bch.c declares these functions
|
||||
* __attribute__((weak)), expecting them to resolve to NULL when no
|
||||
* CUDA object is linked. GNU ld does that; macOS ld64 does not --
|
||||
* a weak declaration is a weak definition, not a weak import, so
|
||||
* the link fails with undefined _bch_cuda_available/_encode/_decode
|
||||
* symbols. Defining the symbols outright removes all reliance on
|
||||
* linker-specific weak-symbol behaviour, on every platform.
|
||||
*
|
||||
* The stub is built only when nvcc is absent (see the Makefile:
|
||||
* CUDA_STUB_OBJ is empty whenever CUDA_OBJ is set), so the symbols
|
||||
* are defined exactly once in every build.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bch.h"
|
||||
#include "bch_cuda.h"
|
||||
|
||||
int bch_cuda_available(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t bch_cuda_encode(const uint8_t *data, size_t len,
|
||||
const bch_poly_t *p, uint8_t *out)
|
||||
{
|
||||
(void)data;
|
||||
(void)len;
|
||||
(void)p;
|
||||
(void)out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bch_cuda_decode(const uint8_t *in, size_t len, size_t orig_len,
|
||||
const bch_poly_t *p, uint8_t *out, int *corrected)
|
||||
{
|
||||
(void)in;
|
||||
(void)len;
|
||||
(void)orig_len;
|
||||
(void)p;
|
||||
(void)out;
|
||||
(void)corrected;
|
||||
return -2;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/* chase_cuda_stub.c - CPU-only fallback for the CUDA Chase-1 entry points.
|
||||
*
|
||||
* Defines the two symbols deep_repair.c references (chase_cuda_available,
|
||||
* chase_cuda_scan1) for builds without nvcc, so a CPU-only link always
|
||||
* resolves. The stub reports "no GPU": the caller in deep_repair.c then
|
||||
* takes the CPU Chase-1 path, exactly as it does when a CUDA build is
|
||||
* present but no device is found.
|
||||
*
|
||||
* This exists for the same reason as bch_cuda_stub.c: the weak-symbol
|
||||
* fallback it replaces does not work on macOS. deep_repair.c declares
|
||||
* these functions __attribute__((weak)), expecting them to resolve to
|
||||
* NULL when no CUDA object is linked. GNU ld does that; macOS ld64
|
||||
* does not -- a weak declaration is a weak definition, not a weak
|
||||
* import, so the link fails with undefined _chase_cuda_available /
|
||||
* _chase_cuda_scan1 symbols (run 7 on main, both arm64 and x86_64).
|
||||
* Defining the symbols outright removes all reliance on
|
||||
* linker-specific weak-symbol behaviour, on every platform.
|
||||
*
|
||||
* The stub is built only when nvcc is absent (see the Makefile:
|
||||
* CUDA_STUB_OBJ is empty whenever CHASE_CUDA_OBJ is set), so the
|
||||
* symbols are defined exactly once in every build.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bch.h"
|
||||
#include "chase_cuda.h"
|
||||
|
||||
int chase_cuda_available(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int chase_cuda_scan1(const uint8_t *in, size_t in_len, size_t data_len,
|
||||
const bch_poly_t *p, uint8_t *cands, int max_cands,
|
||||
const size_t *suspects, int nsuspects,
|
||||
long *trials_out, long *split_pass_out,
|
||||
long *codewords_out)
|
||||
{
|
||||
(void)in;
|
||||
(void)in_len;
|
||||
(void)data_len;
|
||||
(void)p;
|
||||
(void)cands;
|
||||
(void)max_cands;
|
||||
(void)suspects;
|
||||
(void)nsuspects;
|
||||
(void)trials_out;
|
||||
(void)split_pass_out;
|
||||
(void)codewords_out;
|
||||
return -2;
|
||||
}
|
||||
+56
-3
@@ -23,6 +23,16 @@
|
||||
#include "rs.h"
|
||||
#include "sha256.h"
|
||||
|
||||
/* CUDA Chase-1 (chase_cuda.cu), linked only when nvcc is available
|
||||
* at build time; weak so a CPU-only build links and runs unchanged
|
||||
* (same pattern as bch_cuda in bch.c on main). */
|
||||
extern int chase_cuda_available(void) __attribute__((weak));
|
||||
extern int chase_cuda_scan1(const uint8_t *in, size_t in_len, size_t data_len,
|
||||
const bch_poly_t *p, uint8_t *cands, int max_cands,
|
||||
const size_t *suspects, int nsuspects,
|
||||
long *trials_out, long *split_pass_out,
|
||||
long *codewords_out) __attribute__((weak));
|
||||
|
||||
/* Decompress a blob and verify its SHA-256. Returns a malloc'd raw
|
||||
* buffer (caller frees) or NULL on failure. Level 0 means stored
|
||||
* uncompressed (the blob is the raw bytes). */
|
||||
@@ -139,6 +149,42 @@ static void pool_free(block_pool_t *pool, size_t *pool_bytes)
|
||||
pool->dlen = 0;
|
||||
}
|
||||
|
||||
/* Chase-1 for one block: the CUDA scan when a device is present,
|
||||
* the CPU scan otherwise. Same candidate contract as
|
||||
* bch_chase1_block (suspects first, then ascending positions;
|
||||
* candidates in scan order, so the L1 direct-try order is
|
||||
* unchanged). *used_cuda is set to 1 when the GPU scan ran.
|
||||
* The GPU scan has no deadline (it runs the full scan in ~0.35 s
|
||||
* on a 524 KiB block); when the deadline is already past, the CPU
|
||||
* scan runs instead and reports partial as usual. A -2 from the
|
||||
* GPU scan (not t=8, or CUDA failed) also falls back to CPU. */
|
||||
static int chase1_block(const uint8_t *in, size_t in_len, size_t data_len,
|
||||
const bch_poly_t *p, uint8_t *cands, int max_cands,
|
||||
const size_t *suspects, int nsuspects,
|
||||
bch_chase_stats_t *stats, double deadline,
|
||||
int *used_cuda)
|
||||
{
|
||||
*used_cuda = 0;
|
||||
if (chase_cuda_available && chase_cuda_available() &&
|
||||
(deadline < 0 || bch_now() < deadline)) {
|
||||
long trials = 0, split_pass = 0, codewords = 0;
|
||||
int ncc = chase_cuda_scan1(in, in_len, data_len, p, cands,
|
||||
max_cands, suspects, nsuspects,
|
||||
&trials, &split_pass, &codewords);
|
||||
if (ncc >= 0) {
|
||||
stats->trials = trials;
|
||||
stats->split_pass = split_pass;
|
||||
stats->codewords = codewords;
|
||||
stats->candidates = ncc;
|
||||
stats->partial = 0;
|
||||
*used_cuda = 1;
|
||||
return ncc;
|
||||
}
|
||||
}
|
||||
return bch_chase1_block(in, in_len, data_len, p, cands, max_cands,
|
||||
suspects, nsuspects, stats, deadline);
|
||||
}
|
||||
|
||||
int deep_repair_entry(const uint8_t *stored, size_t stored_len, int level,
|
||||
int is_bch, uint16_t poly_id, int rs_level,
|
||||
const uint8_t want_sha[32], size_t blob_size,
|
||||
@@ -372,7 +418,8 @@ int deep_repair_entry(const uint8_t *stored, size_t stored_len, int level,
|
||||
free(bc);
|
||||
}
|
||||
added1 = pools[bi].n; /* pool size after pass-2 */
|
||||
/* Chase-1 candidates (suspects first, then scan). */
|
||||
/* Chase-1 candidates (suspects first, then scan) --
|
||||
* GPU when a device is present, else CPU. */
|
||||
{
|
||||
size_t *susp = malloc(sizeof(size_t) *
|
||||
BCH_CHASE_MAX_SUSPECTS);
|
||||
@@ -389,10 +436,16 @@ int deep_repair_entry(const uint8_t *stored, size_t stored_len, int level,
|
||||
bslen[bi], bdlen[bi],
|
||||
poly, susp,
|
||||
BCH_CHASE_MAX_SUSPECTS);
|
||||
ncc = bch_chase1_block(bch_stored + s_off,
|
||||
{
|
||||
int used_cuda = 0;
|
||||
ncc = chase1_block(bch_stored + s_off,
|
||||
bslen[bi], bdlen[bi], poly,
|
||||
cc, BCH_CHASE_MAX_CANDS,
|
||||
susp, ns, &cs, deadline);
|
||||
susp, ns, &cs, deadline,
|
||||
&used_cuda);
|
||||
if (used_cuda)
|
||||
stats->chase_cuda_used = 1;
|
||||
}
|
||||
stats->trials[1] += cs.trials;
|
||||
stats->split_pass[1] += cs.split_pass;
|
||||
stats->codewords[1] += cs.codewords;
|
||||
|
||||
@@ -67,6 +67,7 @@ typedef struct {
|
||||
long blocks_total; /* BCH blocks in the blob */
|
||||
long blocks_failed; /* blocks failing standard decode */
|
||||
int partial; /* a level stopped early on its budget */
|
||||
int chase_cuda_used; /* L1 ran on the GPU (CUDA Chase-1 scan) */
|
||||
char fail_stage[24]; /* "ok", "rs", "len", "crc", "sha",
|
||||
"budget", "noecc", "nocand" */
|
||||
} deep_stats_t;
|
||||
|
||||
+56
-5
@@ -1,11 +1,13 @@
|
||||
# Deep repair — past the BCH limit t
|
||||
|
||||
`lzply_deep_repair` recovers archive entries whose bit errors exceed
|
||||
Deep repair recovers archive entries whose bit errors exceed
|
||||
the BCH correction limit t, in escalating levels, reporting
|
||||
per-level timing ("time of arrival": how long each level took and
|
||||
at which level the repair landed). The CRC16 is the oracle: every
|
||||
beyond-t candidate is CRC-verified before acceptance, then
|
||||
decompressed and SHA-256-checked.
|
||||
at which level the repair landed). It is built into the main tool
|
||||
(`lzply -r archive`, same levels and options as the standalone
|
||||
`lzply_deep_repair` binary, which is still shipped). The CRC16
|
||||
is the oracle: every beyond-t candidate is CRC-verified before
|
||||
acceptance, then decompressed and SHA-256-checked.
|
||||
|
||||
## The levels
|
||||
|
||||
@@ -37,6 +39,43 @@ decompressed and SHA-256-checked.
|
||||
holds its true data in the pool but neither single-block
|
||||
variation passes the CRC.
|
||||
|
||||
## The CUDA Chase-1 scan (L1 on the GPU)
|
||||
|
||||
On a CUDA build (nvcc present at build time -- `make` links
|
||||
`chase_cuda.o` into `lzply` and `lzply_deep_repair` automatically;
|
||||
without nvcc the build is CPU-only and nothing changes), the L1
|
||||
Chase-1 scan runs on the GPU when a CUDA device is present. The
|
||||
dispatch is automatic: there is no flag. The scan is the same
|
||||
algorithm as the CPU one (one thread per trial position:
|
||||
Berlekamp-Massey + the lambda-splits test, Chien search in
|
||||
parallel for survivors, host syndrome-zero verification), and
|
||||
produces the same candidates in the same order -- verified
|
||||
bit-identical across the 5-pattern validation and a 24-pattern
|
||||
sweep (10/10 CPU/CUDA hash agreements, zero mismatches).
|
||||
|
||||
Measured on hot_house (RTX 3080 Ti), one 524 KiB block (m=22,
|
||||
t=8, -e4), 4,194,296 trials: **171.571 s CPU -> 0.335-0.379 s
|
||||
CUDA (453-512x on the timed scan, ~190x end to end)**; the
|
||||
extended sweep runs 453-562x. A beyond-t repair of a 524 KiB
|
||||
block that took ~8 minutes at L1 now lands in under a second.
|
||||
`-v` marks the L1 line `(CUDA)` when the GPU scan ran.
|
||||
|
||||
Fallback: the CPU scan runs whenever the GPU scan cannot -- no
|
||||
CUDA device, a CPU-only build, a non-t=8 poly (the kernel is
|
||||
t=8 only), a CUDA failure, or a deadline already past when the
|
||||
scan would start. The symbols are referenced weakly, so a
|
||||
CPU-only build links and runs unchanged.
|
||||
|
||||
Limits: the kernel is t=8 only; Chase-2 is not ported (L2 stays
|
||||
on the CPU). The GPU scan has no deadline -- it runs the full
|
||||
scan, then truncates at `BCH_CHASE_MAX_CANDS` (the CPU scan
|
||||
stops early at the cap when the budget hits); the candidate set
|
||||
still matches. On small blocks the ~0.115 s CUDA first-touch
|
||||
cost dominates -- a 512-byte block repairs in ~0.11 s on the GPU
|
||||
vs ~0.04 s on the CPU -- so the win is on large blocks.
|
||||
|
||||
Full write-up: https://www.lzply.com/cuda-chase-500x-deep-repair/
|
||||
|
||||
## Why the suspect set does not help Chase-2 (much)
|
||||
|
||||
With hard-decision input there is no channel reliability, so
|
||||
@@ -98,6 +137,16 @@ lzply_deep_repair [-l max-level 0..3] [--time-budget seconds] [-v]
|
||||
[-o outdir] [--entry name] archive
|
||||
```
|
||||
|
||||
The same repair is built into the main tool:
|
||||
|
||||
```
|
||||
lzply -r [--level 0..3] [--time-budget seconds] [-v]
|
||||
[-o outdir] [--entry name] archive [outdir]
|
||||
```
|
||||
|
||||
`-r` takes the same levels, budget, and output options
|
||||
(`--level` in place of `-l`, which is the list mode in `lzply`).
|
||||
|
||||
Per-entry output:
|
||||
|
||||
```
|
||||
@@ -168,7 +217,9 @@ wrong-but-valid candidates per trial and the CRC rejects all
|
||||
`bch_test` 7,975 checks still pass). A full Chase-1 scan of a
|
||||
524 KiB block (4,194,296 trials) takes **471.9 s** (measured,
|
||||
probe5: RECOVERED at L1 in 471.918 s, trials 4,194,296,
|
||||
split 126, codewords 126). The tool scans fully before trying
|
||||
split 126, codewords 126). On a CUDA build the same scan runs
|
||||
on the GPU in ~0.35 s (see the CUDA section above); 471.9 s is
|
||||
the CPU scan. The tool scans fully before trying
|
||||
candidates (the chase layer has no oracle), so the landing time
|
||||
is the full-scan time, not the time to the lowest true error.
|
||||
At the default 120 s budget a beyond-t repair of a 524 KiB
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
# lzcsv — high-speed CSV compression
|
||||
|
||||
`lzcsv` compresses CSV files by transposing them column-wise and then
|
||||
compressing with xz (liblzma). Same-field values become adjacent long
|
||||
runs, which xz's matcher uses far better than alternating fields on
|
||||
every row. It is a standalone tool in the lzply repo, completely
|
||||
separate from the lzply FEC core: it links only liblzma, no
|
||||
`liblzply.a`, no liblz, no CUDA.
|
||||
|
||||
The idea comes from hot_qwen Cycle 1 (Dec 2026): the coordinator
|
||||
proposed grid-aware compression, hot_qwen tried to disprove it
|
||||
(prior art, failure modes) and called it testable, and the test on
|
||||
14 CSVs gave −13.9% vs `xz -6`, 13/14 files, all round-trips
|
||||
byte-exact. This tool is that test turned into a real command.
|
||||
|
||||
## Format (LZCSV v1)
|
||||
|
||||
```
|
||||
bytes 0..7 magic "LZCSV001"
|
||||
bytes 8..15 uint64 LE original (CSV) size
|
||||
bytes 16..23 uint64 LE transposed (COLT1) size
|
||||
byte 24 xz preset (0-9)
|
||||
byte 25 flags (bit 0 = extreme)
|
||||
bytes 26.. xz stream (liblzma, preset, CRC64 check) holding COLT1
|
||||
```
|
||||
|
||||
The transposed stream is COLT1, byte-compatible with the Cycle 1
|
||||
Python prototype (`/root/hot-qwen/cycle1/transpose.py`):
|
||||
|
||||
```
|
||||
line 1: COLT1 <nrows> <ncols> <crlf:0|1> <trailing_nl:0|1> <uniform:0|1>
|
||||
if uniform=0: line "COUNTS <len>" then <len> bytes of comma-separated
|
||||
per-row field counts (ragged rows; padding stripped on decode)
|
||||
per column j: line "COL <len>" then exactly <len> bytes:
|
||||
the column's raw field texts joined by "\n" (no trailing newline)
|
||||
```
|
||||
|
||||
The compressed payload is the same liblzma preset-6 stream `xz -6`
|
||||
produces on the same transposed bytes (within a few bytes: the
|
||||
encoder's output varies slightly with how it is fed — 1500 vs 1504
|
||||
bytes on tips.csv — same algorithm, same preset, not a different
|
||||
compressor).
|
||||
|
||||
Integrity chain: liblzma verifies the CRC64 of the xz stream, the
|
||||
COLT1 decoder is strict (magic, counts, column field counts, no
|
||||
trailing data), and the container header's original size must match
|
||||
the reconstructed output exactly.
|
||||
|
||||
## Build
|
||||
|
||||
```
|
||||
make lzcsv # needs liblzma-dev; skipped cleanly without it
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv> compress (preset 6)
|
||||
lzcsv d [-f] [-v] <in.lzcsv> <out.csv> decompress
|
||||
lzcsv test <file.csv> ... in-memory round-trip check
|
||||
lzcsv info <file.lzcsv> show container header
|
||||
lzcsv raw-enc <in.csv> <out.colt> write COLT1 intermediate
|
||||
lzcsv raw-dec <in.colt> <out.csv> decode COLT1 intermediate
|
||||
lzcsv selftest built-in synthetic tests
|
||||
```
|
||||
|
||||
## Measured results (hot_house, xz/liblzma 5.8.1)
|
||||
|
||||
14 corpus CSVs (12,096,190 bytes, `/root/fftdict-shared/corpus2`):
|
||||
|
||||
| file | xz -6 | xz -9e | lzcsv -6 | lzcsv -9e | lzcsv-6 vs xz-6 |
|
||||
|---|---|---|---|---|---|
|
||||
| covid-countries.csv | 646,112 | 654,748 | 464,786 | 464,446 | −28.1% |
|
||||
| gen-log.csv | 38,060 | 38,100 | 29,346 | 29,626 | −22.9% |
|
||||
| gen-numeric.csv | 171,016 | 171,256 | 136,142 | 137,990 | −20.4% |
|
||||
| hydra-passwords.csv | 79,832 | 79,816 | 70,434 | 70,050 | −11.8% |
|
||||
| gapminder.csv | 24,352 | 24,472 | 21,506 | 21,694 | −11.7% |
|
||||
| diabetes.csv | 8,076 | 8,080 | 7,422 | 7,446 | −8.1% |
|
||||
| default-passwords.csv | 25,900 | 25,892 | 23,898 | 23,906 | −7.7% |
|
||||
| tips.csv | 1,636 | 1,640 | 1,526 | 1,502 | −6.7% |
|
||||
| mam.csv | 192,364 | 192,496 | 179,690 | 179,906 | −6.6% |
|
||||
| iab.csv | 128,396 | 128,392 | 120,114 | 120,342 | −6.5% |
|
||||
| stocks.csv | 24,092 | 24,132 | 22,698 | 22,718 | −5.8% |
|
||||
| oui.csv | 718,664 | 713,760 | 692,506 | 687,758 | −3.6% |
|
||||
| iris.csv | 824 | 828 | 818 | 818 | −0.7% |
|
||||
| pin-codes.csv | 12,864 | 12,844 | 13,994 | 14,066 | **+8.8%** |
|
||||
| **aggregate** | **2,072,188 (0.1713)** | **2,076,456 (0.1717)** | **1,784,880 (0.1476)** | **1,782,268 (0.1473)** | **−13.9%** |
|
||||
|
||||
- lzcsv -6 wins on 13/14 files vs xz -6 and vs xz -9e. No file flips
|
||||
at any preset. xz's best does not erase the win: direct xz -9e is
|
||||
slightly *worse* than xz -6 on these CSVs (2,076,456 vs 2,072,188),
|
||||
and lzcsv -6 beats direct xz -9e by −14.0%.
|
||||
- Encode time (sums of per-file medians, 4 parallel workers,
|
||||
approximate): xz -6 3.59 s, lzcsv -6 4.08 s. Decode: xz -6 0.14 s,
|
||||
lzcsv -6 0.19 s. Roughly comparable; the transpose itself is
|
||||
negligible next to xz.
|
||||
|
||||
18 held-out CSVs (4,431,565 bytes — 5 downloaded real-world files
|
||||
plus 13 generated edge cases):
|
||||
|
||||
| aggregate | xz -6 | xz -9e | lzcsv -6 | lzcsv -9e | lzcsv-6 vs xz-6 |
|
||||
|---|---|---|---|---|---|
|
||||
| bytes | 844,752 (0.1906) | 842,764 (0.1902) | 730,756 (0.1649) | 726,864 (0.1640) | **−13.5%** |
|
||||
|
||||
- lzcsv -6 wins on 9/18. Best held-out wins: flights.csv −21.9%,
|
||||
text.csv −21.4%, diamonds.csv −18.9%, taxis.csv −13.3%.
|
||||
- All 9 losses are files under ~1 KB (or a single column): the fixed
|
||||
overhead — COLT1 header + per-column headers + the 26-byte LZCSV
|
||||
header + the xz stream itself — dominates on tiny files
|
||||
(empty.csv: 0 → 98 bytes; tiny.csv: 24 → 134 bytes). Single-column
|
||||
files gain nothing from transposition (there is only one column)
|
||||
and xz -9e beats lzcsv there (single-col.csv: 1,152 vs 1,474).
|
||||
- Honest scope: lzcsv wins on real-world CSVs above a few KB and
|
||||
loses on tiny files. A size gate (transpose only above a
|
||||
row/column threshold, pass small files through as plain xz) is the
|
||||
obvious next step and is left for a later cycle.
|
||||
|
||||
## Limitations (documented, tested)
|
||||
|
||||
- Fields holding a raw newline (embedded newlines in quoted fields)
|
||||
are rejected with a clear error — the COLT1 column join uses
|
||||
`"\n"`, so such a field cannot be represented. Same as the
|
||||
prototype. The test suite checks the rejection is clean.
|
||||
- Files with mixed line endings (some rows CRLF, some LF) are
|
||||
rejected: the single crlf flag cannot represent them, and the
|
||||
prototype would silently mis-round-trip such a file.
|
||||
- In-memory: the whole file is read into RAM (CSVs are small;
|
||||
a 5.5 MB file uses tens of MB during the transpose).
|
||||
- CSV only. Other file types gain nothing and are not the tool's job.
|
||||
|
||||
## Tests
|
||||
|
||||
```
|
||||
./lzcsv selftest # built-in synthetic cases
|
||||
bash tests/run_lzcsv_tests.sh ./lzcsv # full suite (also in `make test`)
|
||||
python3 lzcsv-src/bench.py # benchmark (corpus + held-out)
|
||||
```
|
||||
|
||||
The suite checks: selftest, `--help`, byte-exact round-trips on the
|
||||
14 corpus CSVs and the 18 held-out CSVs, byte-identical COLT1 output
|
||||
vs the Python prototype (oracle) plus cross-decoding both ways,
|
||||
clean rejection of embedded-newline input, and clean failure on
|
||||
truncated / bad-magic input.
|
||||
@@ -0,0 +1,32 @@
|
||||
covid-countries.csv 5512931 646112 654748 464786 464446 1.9201659050013404 1.9516634100000374 0.05220619498868473 0.07482820301083848
|
||||
default-passwords.csv 90856 25900 25892 23898 23906 0.025861336005618796 0.021365027991123497 0.0034550470008980483 0.002496671979315579
|
||||
diabetes.csv 23873 8076 8080 7422 7446 0.011048309010220692 0.009692073974292725 0.0023416100011672825 0.0014151250070426613
|
||||
gapminder.csv 82079 24352 24472 21506 21694 0.021015542006352916 0.015395115013234317 0.003706499992404133 0.002185456018196419
|
||||
gen-log.csv 373147 38060 38100 29346 29626 0.08884992098319344 0.0807244160096161 0.004460240976186469 0.004076949000591412
|
||||
gen-numeric.csv 971501 171016 171256 136142 137990 0.2775053840014152 0.2098268090048805 0.012041809997754171 0.015114195994101465
|
||||
hydra-passwords.csv 515376 79832 79816 70434 70050 0.10549499999615364 0.07998127699829638 0.006305735005298629 0.00882071300293319
|
||||
iab.csv 381908 128396 128392 120114 120342 0.08515957900090143 0.08999008699902333 0.006552350998390466 0.007555139978649095
|
||||
iris.csv 4601 824 828 818 818 0.00544012599857524 0.004784169985214248 0.0016728210030123591 0.0010296890104655176
|
||||
mam.csv 608108 192364 192496 179690 179906 0.16979076401912607 0.1856548530049622 0.00886850300594233 0.010595135012408718
|
||||
oui.csv 3372302 718664 713760 692506 687758 0.8365058559866156 1.3890049190085847 0.02745902701281011 0.05935608199797571
|
||||
pin-codes.csv 83641 12864 12844 13994 14066 0.016631082020467147 0.016714841010980308 0.003020479023689404 0.0030090670043136925
|
||||
stocks.csv 67924 24092 24132 22698 22718 0.01886416898923926 0.017659361008554697 0.003195147990481928 0.0021861470013391227
|
||||
tips.csv 7943 1636 1640 1526 1502 0.007472163008060306 0.004107314976863563 0.0019683470018208027 0.0010860360052902251
|
||||
crlf.csv 57 124 124 170 170 0.004888466995907947 0.007338372990489006 0.0019021829939447343 0.002908758004195988
|
||||
diamonds.csv 2772143 445196 445344 361098 359134 2.396331233001547 1.2090837279974949 0.033903217990882695 0.041442929999902844
|
||||
empty.csv 0 32 32 98 98 0.0018080949957948178 0.0020065490098204464 0.0013358870055526495 0.0006844690069556236
|
||||
flights.csv 2350 648 648 506 506 0.005633962980937213 0.00863322100485675 0.0015973990084603429 0.0018542629841249436
|
||||
header-only.csv 15 80 80 118 118 0.004120881989365444 0.006564754992723465 0.0027778010116890073 0.0021728830179199576
|
||||
narrow.csv 78337 18748 19016 17790 17926 0.032430835999548435 0.029884938994655386 0.005319860007148236 0.0038035030011087656
|
||||
no-trailing-nl.csv 11 76 76 122 122 0.0062791359960101545 0.0024431619967799634 0.003309124003862962 0.000765912001952529
|
||||
numeric.csv 471030 213188 211892 203770 204030 0.33525239600567147 0.3318267239956185 0.015222632006043568 0.017531852994579822
|
||||
one-row-no-nl.csv 5 72 72 114 114 0.002797479013679549 0.00583920898498036 0.0027009370096493512 0.0027930409996770322
|
||||
penguins.csv 13478 2412 2428 2182 2166 0.010619982989737764 0.009998533001635224 0.003402178001124412 0.003114015009487048
|
||||
quotes.csv 115 184 184 230 230 0.007522066996898502 0.0073039180133491755 0.003594912006519735 0.001310628023929894
|
||||
ragged.csv 30 96 96 166 166 0.006454857997596264 0.007829547015717253 0.002639713027747348 0.002263063011923805
|
||||
single-col.csv 31890 1448 1152 1474 1550 0.020343487994978204 0.019372289010789245 0.003808081994066015 0.0029039290093351156
|
||||
taxis.csv 869349 114268 113392 99090 96730 0.3596663609787356 0.2504055729950778 0.01067515800241381 0.01706002399441786
|
||||
text.csv 71105 13912 13960 10938 10974 0.0370593529951293 0.03674623399274424 0.004957627010298893 0.003607544000260532
|
||||
tiny.csv 24 88 88 134 134 0.00707226200029254 0.004299127991544083 0.003431374003412202 0.0024710540019441396
|
||||
titanic.csv 60302 18816 18808 17410 17374 0.03209059499204159 0.034872953983722255 0.0049819239939097315 0.004012949008028954
|
||||
wide.csv 61324 15364 15372 15346 15322 0.026844223000807688 0.02351557300426066 0.003356151981279254 0.003094587998930365
|
||||
|
@@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Benchmark lzcsv vs xz on the 14 corpus CSVs and the held-out set.
|
||||
Methods per file: direct xz -6, direct xz -9e, lzcsv -6, lzcsv -9e.
|
||||
Sizes are single-run (deterministic); times are median of 3.
|
||||
Usage: python3 lzcsv-src/bench.py [--csv-only] [--heldout-only]
|
||||
Writes lzcsv-src/bench-results.tsv and prints the tables.
|
||||
"""
|
||||
import concurrent.futures
|
||||
import glob
|
||||
import os
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = "/root/lzply"
|
||||
LZ = os.path.join(ROOT, "lzcsv")
|
||||
CORPUS = "/root/fftdict-shared/corpus2"
|
||||
HELD = "/root/hot-qwen/lzcsv-heldout"
|
||||
WORK = "/tmp/lzcsv-bench"
|
||||
os.makedirs(WORK, exist_ok=True)
|
||||
|
||||
def run_cmd(cmd):
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run(cmd, capture_output=True)
|
||||
dt = time.perf_counter() - t0
|
||||
return r.returncode, dt
|
||||
|
||||
def xz_size_time(path, preset):
|
||||
# encode: xz -preset -c path -> stdout (captured); time it
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run(["xz", preset, "-c", path], capture_output=True, check=True)
|
||||
dt = time.perf_counter() - t0
|
||||
# decode time: xz -dc the compressed bytes
|
||||
t0 = time.perf_counter()
|
||||
subprocess.run(["xz", "-dc"], input=r.stdout, capture_output=True, check=True)
|
||||
ddt = time.perf_counter() - t0
|
||||
return len(r.stdout), dt, ddt
|
||||
|
||||
def lzcsv_size_time(path, preset_args, tag):
|
||||
comp = os.path.join(WORK, tag + ".lzcsv")
|
||||
dec = os.path.join(WORK, tag + ".csv")
|
||||
enc_times, dec_times = [], []
|
||||
size = None
|
||||
for _ in range(3):
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run([LZ, "c"] + preset_args + ["-f", path, comp], capture_output=True)
|
||||
enc_times.append(time.perf_counter() - t0)
|
||||
assert r.returncode == 0, f"lzcsv c failed on {path}: {r.stderr.decode()[:200]}"
|
||||
size = os.path.getsize(comp)
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run([LZ, "d", "-f", comp, dec], capture_output=True)
|
||||
dec_times.append(time.perf_counter() - t0)
|
||||
assert r.returncode == 0, f"lzcsv d failed on {path}: {r.stderr.decode()[:200]}"
|
||||
return size, statistics.median(enc_times), statistics.median(dec_times)
|
||||
|
||||
def bench_one(path):
|
||||
name = os.path.basename(path)
|
||||
orig = os.path.getsize(path)
|
||||
tag = name.replace(".csv", "")
|
||||
row = {"file": name, "orig": orig}
|
||||
# direct xz -6 / -9e (encode median of 3 for time; size from first run)
|
||||
for preset, key in [("-6", "xz6"), ("-9e", "xz9e")]:
|
||||
sizes, ets, dts = [], [], []
|
||||
for _ in range(3):
|
||||
s, et, dt = xz_size_time(path, preset)
|
||||
sizes.append(s); ets.append(et); dts.append(dt)
|
||||
row[key] = sizes[0]
|
||||
row[key + "_enc"] = statistics.median(ets)
|
||||
row[key + "_dec"] = statistics.median(dts)
|
||||
# lzcsv -6 / -9e
|
||||
for args, key in [([], "lz6"), (["-p", "9", "-e"], "lz9e")]:
|
||||
s, et, dt = lzcsv_size_time(path, args, tag + "-" + key)
|
||||
row[key] = s
|
||||
row[key + "_enc"] = et
|
||||
row[key + "_dec"] = dt
|
||||
return row
|
||||
|
||||
def table(rows, title):
|
||||
print(f"\n## {title}")
|
||||
print("file\torig\txz-6\txz-9e\tlzcsv-6\tlzcsv-9e\tlz6-vs-xz6\tlz6-vs-xz9e\tenc-xz6\tenc-lz6\tdec-xz6\tdec-lz6")
|
||||
t = {k: 0 for k in ("orig", "xz6", "xz9e", "lz6", "lz9e", "xz6_enc", "lz6_enc", "xz6_dec", "lz6_dec")}
|
||||
for r in rows:
|
||||
print(f"{r['file']}\t{r['orig']}\t{r['xz6']}\t{r['xz9e']}\t{r['lz6']}\t{r['lz9e']}"
|
||||
f"\t{100.0*(r['lz6']-r['xz6'])/r['xz6']:+.1f}%\t{100.0*(r['lz6']-r['xz9e'])/r['xz9e']:+.1f}%"
|
||||
f"\t{r['xz6_enc']:.3f}\t{r['lz6_enc']:.3f}\t{r['xz6_dec']:.3f}\t{r['lz6_dec']:.3f}")
|
||||
for k in t:
|
||||
t[k] += r[k]
|
||||
n = len(rows)
|
||||
print(f"AGGREGATE\t{t['orig']}\t{t['xz6']}\t{t['xz9e']}\t{t['lz6']}\t{t['lz9e']}"
|
||||
f"\t{100.0*(t['lz6']-t['xz6'])/t['xz6']:+.1f}%\t{100.0*(t['lz6']-t['xz9e'])/t['xz9e']:+.1f}%"
|
||||
f"\t{t['xz6_enc']:.2f}\t{t['lz6_enc']:.2f}\t{t['xz6_dec']:.2f}\t{t['lz6_dec']:.2f}")
|
||||
print(f"ratios: xz-6 {t['xz6']/t['orig']:.4f} xz-9e {t['xz9e']/t['orig']:.4f} "
|
||||
f"lzcsv-6 {t['lz6']/t['orig']:.4f} lzcsv-9e {t['lz9e']/t['orig']:.4f}")
|
||||
wins6 = sum(1 for r in rows if r['lz6'] < r['xz6'])
|
||||
wins9e = sum(1 for r in rows if r['lz6'] < r['xz9e'])
|
||||
print(f"lzcsv-6 wins vs xz-6: {wins6}/{n}; vs xz-9e: {wins9e}/{n}")
|
||||
return t
|
||||
|
||||
def main():
|
||||
csv_only = "--heldout-only" not in sys.argv
|
||||
held_only = "--csv-only" not in sys.argv
|
||||
sets = []
|
||||
if csv_only:
|
||||
sets.append(("14 corpus CSVs", sorted(glob.glob(os.path.join(CORPUS, "*.csv")))))
|
||||
if held_only:
|
||||
held = sorted(f for f in glob.glob(os.path.join(HELD, "*.csv"))
|
||||
if os.path.basename(f) != "embedded-nl.csv")
|
||||
sets.append(("18 held-out CSVs", held))
|
||||
all_rows = []
|
||||
for title, files in sets:
|
||||
print(f"benchmarking {len(files)} files ({title}) ...", flush=True)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as ex:
|
||||
rows = list(ex.map(bench_one, files))
|
||||
rows.sort(key=lambda r: r["file"])
|
||||
table(rows, title)
|
||||
all_rows.extend(rows)
|
||||
with open(os.path.join(ROOT, "lzcsv-src", "bench-results.tsv"), "a") as f:
|
||||
for r in rows:
|
||||
f.write("\t".join(str(r[k]) for k in
|
||||
("file", "orig", "xz6", "xz9e", "lz6", "lz9e",
|
||||
"xz6_enc", "lz6_enc", "xz6_dec", "lz6_dec")) + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# fresh results file each run
|
||||
p = os.path.join(ROOT, "lzcsv-src", "bench-results.tsv")
|
||||
if os.path.exists(p):
|
||||
os.remove(p)
|
||||
main()
|
||||
@@ -0,0 +1,756 @@
|
||||
/* lzcsv - high-speed CSV compression: column transpose + xz (liblzma).
|
||||
*
|
||||
* A standalone tool in the lzply repo, completely separate from the
|
||||
* lzply FEC core (it does not link liblzply.a). Idea from hot_qwen
|
||||
* Cycle 1 (Dec 2026): transpose a CSV column-wise so same-field values
|
||||
* become adjacent long runs, then compress with xz. On the 14 CSVs of
|
||||
* the fftdict corpus2 the transposed stream compresses to 0.1475 with
|
||||
* xz -6 vs 0.1713 direct (-13.9%), 13/14 files win, all round-trips
|
||||
* byte-exact.
|
||||
*
|
||||
* Container (LZCSV v1):
|
||||
* bytes 0..7 magic "LZCSV001"
|
||||
* bytes 8..15 uint64 LE original (CSV) size
|
||||
* bytes 16..23 uint64 LE transposed (COLT1) size
|
||||
* byte 24 xz preset (0-9)
|
||||
* byte 25 flags (bit 0 = extreme)
|
||||
* bytes 26.. xz stream (liblzma, preset, CRC64 check) holding COLT1
|
||||
*
|
||||
* COLT1 (byte-compatible with /root/hot-qwen/cycle1/transpose.py):
|
||||
* line 1: COLT1 <nrows> <ncols> <crlf:0|1> <trailing_nl:0|1> <uniform:0|1>
|
||||
* if uniform=0: line "COUNTS <len>" then <len> bytes of comma-separated
|
||||
* per-row field counts (ragged rows; padding fields stripped on decode)
|
||||
* per column j: line "COL <len>" then exactly <len> bytes:
|
||||
* the column's raw field texts joined by "\n" (no trailing newline).
|
||||
* Fields containing a raw newline are rejected (same as the prototype).
|
||||
* Mixed line endings (some rows CRLF, some LF) are rejected: the single
|
||||
* crlf flag cannot represent them, and the prototype would silently
|
||||
* mis-round-trip such a file. Consistent endings round-trip exactly.
|
||||
*
|
||||
* Build: make lzcsv (needs liblzma-dev; skipped cleanly without it)
|
||||
* Usage: lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv>
|
||||
* lzcsv d [-f] [-v] <in.lzcsv> <out.csv>
|
||||
* lzcsv test <file.csv> ...
|
||||
* lzcsv info <file.lzcsv>
|
||||
* lzcsv raw-enc <in.csv> <out.colt>
|
||||
* lzcsv raw-dec <in.colt> <out.csv>
|
||||
* lzcsv selftest
|
||||
* lzcsv --help
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <lzma.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define LZCSV_MAGIC "LZCSV001"
|
||||
#define LZCSV_MAGIC_LEN 8
|
||||
#define LZCSV_HEADER_LEN 26
|
||||
#define LZCSV_VERSION "1.0"
|
||||
|
||||
static const char *prog = "lzcsv";
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* small helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void die(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
fprintf(stderr, "%s: ", prog);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
fputc('\n', stderr);
|
||||
va_end(ap);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void *xmalloc(size_t n) {
|
||||
void *p = malloc(n ? n : 1);
|
||||
if (!p) die("out of memory");
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *xrealloc(void *p, size_t n) {
|
||||
void *q = realloc(p, n ? n : 1);
|
||||
if (!q) die("out of memory");
|
||||
return q;
|
||||
}
|
||||
|
||||
/* Growable byte buffer. */
|
||||
typedef struct {
|
||||
uint8_t *v;
|
||||
size_t n, cap;
|
||||
} buf_t;
|
||||
|
||||
static void buf_init(buf_t *b) { b->v = NULL; b->n = 0; b->cap = 0; }
|
||||
|
||||
static void buf_reserve(buf_t *b, size_t extra) {
|
||||
if (b->n + extra <= b->cap) return;
|
||||
size_t nc = b->cap ? b->cap : 4096;
|
||||
while (nc < b->n + extra) nc *= 2;
|
||||
b->v = xrealloc(b->v, nc);
|
||||
b->cap = nc;
|
||||
}
|
||||
|
||||
static void buf_put(buf_t *b, const void *p, size_t n) {
|
||||
buf_reserve(b, n);
|
||||
memcpy(b->v + b->n, p, n);
|
||||
b->n += n;
|
||||
}
|
||||
|
||||
static void buf_putc(buf_t *b, uint8_t c) { buf_put(b, &c, 1); }
|
||||
|
||||
static void buf_printf(buf_t *b, const char *fmt, ...) {
|
||||
char tmp[256];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int n = vsnprintf(tmp, sizeof(tmp), fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0) die("format error");
|
||||
if ((size_t)n >= sizeof(tmp)) die("internal: formatted header too long");
|
||||
buf_put(b, tmp, (size_t)n);
|
||||
}
|
||||
|
||||
static void buf_free(buf_t *b) { free(b->v); b->v = NULL; b->n = b->cap = 0; }
|
||||
|
||||
static uint8_t *read_file(const char *path, size_t *len_out) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) die("cannot open %s: %s", path, strerror(errno));
|
||||
struct stat st;
|
||||
if (fstat(fileno(f), &st) != 0) die("cannot stat %s: %s", path, strerror(errno));
|
||||
if (st.st_size < 0) die("%s: bad size", path);
|
||||
size_t len = (size_t)st.st_size;
|
||||
uint8_t *data = xmalloc(len);
|
||||
if (len && fread(data, 1, len, f) != len) die("cannot read %s", path);
|
||||
fclose(f);
|
||||
*len_out = len;
|
||||
return data;
|
||||
}
|
||||
|
||||
static void write_file(const char *path, const uint8_t *data, size_t len, int force) {
|
||||
if (!force) {
|
||||
struct stat st;
|
||||
if (stat(path, &st) == 0) die("%s already exists (use -f to overwrite)", path);
|
||||
}
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) die("cannot open %s for writing: %s", path, strerror(errno));
|
||||
if (len && fwrite(data, 1, len, f) != len) die("cannot write %s", path);
|
||||
if (fclose(f) != 0) die("cannot close %s", path);
|
||||
}
|
||||
|
||||
static uint64_t rd64le(const uint8_t *p) {
|
||||
uint64_t v = 0;
|
||||
for (int i = 7; i >= 0; i--) v = (v << 8) | p[i];
|
||||
return v;
|
||||
}
|
||||
|
||||
static void wr64le(uint8_t *p, uint64_t v) {
|
||||
for (int i = 0; i < 8; i++) { p[i] = (uint8_t)(v & 0xff); v >>= 8; }
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* CSV parsing (RFC 4180 quote rules, byte-identical to the COLT1 */
|
||||
/* Python prototype's split_rows). Fields are slices into the input. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
typedef struct { const uint8_t *p; size_t n; } slice_t;
|
||||
|
||||
typedef struct {
|
||||
slice_t *fields; /* flat, row-major, unpadded */
|
||||
size_t *row_start; /* index of each row's first field in fields[] */
|
||||
size_t *row_count; /* field count per row (before padding) */
|
||||
size_t nrows, ncols;
|
||||
int crlf, trailing_nl, uniform;
|
||||
} csv_t;
|
||||
|
||||
static void csv_free(csv_t *c) {
|
||||
free(c->fields); free(c->row_start); free(c->row_count);
|
||||
memset(c, 0, sizeof(*c));
|
||||
}
|
||||
|
||||
/* Parse data into rows/fields. Returns 0 on success, -1 with *err set
|
||||
* on a rejected file (embedded newline / mixed line endings). */
|
||||
static int csv_parse(const uint8_t *data, size_t len, csv_t *csv, const char **err) {
|
||||
memset(csv, 0, sizeof(*csv));
|
||||
size_t fcap = 0, fn = 0;
|
||||
size_t rcap = 0, rn = 0;
|
||||
int in_q = 0;
|
||||
int crlf = 0, trailing_nl = 0;
|
||||
int crlf_rows = 0, lf_rows = 0;
|
||||
size_t cur_start = 0; /* start of the current field */
|
||||
size_t row_field_start = 0; /* fields[] index where the current row began */
|
||||
size_t i = 0;
|
||||
|
||||
while (i < len) {
|
||||
uint8_t b = data[i];
|
||||
if (in_q) {
|
||||
if (b == 0x22) { /* " */
|
||||
if (i + 1 < len && data[i + 1] == 0x22) { i += 2; continue; }
|
||||
in_q = 0;
|
||||
}
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (b == 0x22 && i == cur_start) {
|
||||
/* quote opens a quoted field only at field start (RFC 4180);
|
||||
* a quote mid-field is a literal character */
|
||||
in_q = 1;
|
||||
i++;
|
||||
} else if (b == 0x2c) { /* , */
|
||||
if (fn == fcap) { fcap = fcap ? fcap * 2 : 256; csv->fields = xrealloc(csv->fields, fcap * sizeof(slice_t)); }
|
||||
csv->fields[fn].p = data + cur_start;
|
||||
csv->fields[fn].n = i - cur_start;
|
||||
fn++;
|
||||
cur_start = i + 1;
|
||||
i++;
|
||||
} else if (b == 0x0a) { /* \n: end of row */
|
||||
size_t fend = i;
|
||||
int this_crlf = 0;
|
||||
if (i > cur_start && data[i - 1] == 0x0d) { fend = i - 1; this_crlf = 1; crlf = 1; }
|
||||
if (this_crlf) crlf_rows++; else lf_rows++;
|
||||
if (fn == fcap) { fcap = fcap ? fcap * 2 : 256; csv->fields = xrealloc(csv->fields, fcap * sizeof(slice_t)); }
|
||||
csv->fields[fn].p = data + cur_start;
|
||||
csv->fields[fn].n = fend - cur_start;
|
||||
fn++;
|
||||
if (rn == rcap) { rcap = rcap ? rcap * 2 : 64; csv->row_start = xrealloc(csv->row_start, rcap * sizeof(size_t)); csv->row_count = xrealloc(csv->row_count, rcap * sizeof(size_t)); }
|
||||
csv->row_start[rn] = row_field_start;
|
||||
csv->row_count[rn] = fn - row_field_start;
|
||||
rn++;
|
||||
row_field_start = fn;
|
||||
cur_start = i + 1;
|
||||
i++;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
/* EOF: leftover field/row (mirrors the prototype exactly). */
|
||||
if (cur_start < len || fn > row_field_start) {
|
||||
if (fn == fcap) { fcap = fcap ? fcap * 2 : 256; csv->fields = xrealloc(csv->fields, fcap * sizeof(slice_t)); }
|
||||
csv->fields[fn].p = data + cur_start;
|
||||
csv->fields[fn].n = len - cur_start;
|
||||
fn++;
|
||||
if (rn == rcap) { rcap = rcap ? rcap * 2 : 64; csv->row_start = xrealloc(csv->row_start, rcap * sizeof(size_t)); csv->row_count = xrealloc(csv->row_count, rcap * sizeof(size_t)); }
|
||||
csv->row_start[rn] = row_field_start;
|
||||
csv->row_count[rn] = fn - row_field_start;
|
||||
rn++;
|
||||
trailing_nl = 0;
|
||||
} else {
|
||||
trailing_nl = 1; /* data ended exactly on a newline (or is empty) */
|
||||
}
|
||||
if (len == 0) trailing_nl = 0;
|
||||
|
||||
csv->nrows = rn;
|
||||
csv->crlf = crlf;
|
||||
csv->trailing_nl = trailing_nl;
|
||||
|
||||
size_t ncols = 0;
|
||||
int uniform = 1;
|
||||
for (size_t r = 0; r < rn; r++) {
|
||||
if (csv->row_count[r] > ncols) ncols = csv->row_count[r];
|
||||
if (r && csv->row_count[r] != csv->row_count[0]) uniform = 0;
|
||||
}
|
||||
csv->ncols = ncols;
|
||||
csv->uniform = rn ? uniform : 1;
|
||||
|
||||
if (crlf_rows && lf_rows) {
|
||||
*err = "mixed line endings (some rows CRLF, some LF); the single crlf flag cannot represent them";
|
||||
return -1;
|
||||
}
|
||||
/* COLT1 joins column fields with "\n": a field holding a raw newline
|
||||
* cannot be represented. Same rejection as the prototype. */
|
||||
for (size_t k = 0; k < fn; k++) {
|
||||
if (memchr(csv->fields[k].p, '\n', csv->fields[k].n)) {
|
||||
*err = "field contains a raw newline (embedded newlines in quoted fields are not supported by the COLT1 transpose)";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* field (row i, col j), honouring ragged-row padding with empty fields */
|
||||
static slice_t csv_field(const csv_t *csv, size_t i, size_t j) {
|
||||
static const uint8_t empty = 0;
|
||||
if (j < csv->row_count[i]) return csv->fields[csv->row_start[i] + j];
|
||||
slice_t s = { &empty, 0 };
|
||||
return s;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* COLT1 transpose (byte-compatible with transpose.py) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int colt_encode(const uint8_t *data, size_t len, buf_t *out, const char **err) {
|
||||
csv_t csv;
|
||||
if (csv_parse(data, len, &csv, err) != 0) return -1;
|
||||
buf_printf(out, "COLT1 %zu %zu %d %d %d\n", csv.nrows, csv.ncols,
|
||||
csv.crlf, csv.trailing_nl, csv.uniform);
|
||||
if (!csv.uniform) {
|
||||
buf_t cb;
|
||||
buf_init(&cb);
|
||||
for (size_t r = 0; r < csv.nrows; r++) {
|
||||
if (r) buf_putc(&cb, ',');
|
||||
buf_printf(&cb, "%zu", csv.row_count[r]);
|
||||
}
|
||||
buf_printf(out, "COUNTS %zu\n", cb.n);
|
||||
buf_put(out, cb.v, cb.n);
|
||||
buf_free(&cb);
|
||||
}
|
||||
for (size_t j = 0; j < csv.ncols; j++) {
|
||||
size_t blen = 0;
|
||||
for (size_t r = 0; r < csv.nrows; r++) blen += csv_field(&csv, r, j).n;
|
||||
if (csv.nrows) blen += csv.nrows - 1; /* "\n" separators */
|
||||
buf_printf(out, "COL %zu\n", blen);
|
||||
for (size_t r = 0; r < csv.nrows; r++) {
|
||||
if (r) buf_putc(out, '\n');
|
||||
slice_t f = csv_field(&csv, r, j);
|
||||
buf_put(out, f.p, f.n);
|
||||
}
|
||||
}
|
||||
csv_free(&csv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Parse one "WORD <num>\n" header line at *pos. Advances *pos past it. */
|
||||
static int colt_header(const uint8_t *data, size_t len, size_t *pos,
|
||||
const char *word, size_t *num) {
|
||||
size_t p = *pos;
|
||||
size_t eol = p;
|
||||
while (eol < len && data[eol] != '\n') eol++;
|
||||
if (eol >= len) return -1;
|
||||
size_t wlen = strlen(word);
|
||||
if (eol - p < wlen + 2 || memcmp(data + p, word, wlen) != 0 || data[p + wlen] != ' ')
|
||||
return -1;
|
||||
char tmp[32];
|
||||
size_t nlen = eol - (p + wlen + 1);
|
||||
if (nlen == 0 || nlen >= sizeof(tmp)) return -1;
|
||||
memcpy(tmp, data + p + wlen + 1, nlen);
|
||||
tmp[nlen] = 0;
|
||||
char *end = NULL;
|
||||
unsigned long v = strtoul(tmp, &end, 10);
|
||||
if (!end || *end) return -1;
|
||||
*num = (size_t)v;
|
||||
*pos = eol + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int colt_decode(const uint8_t *data, size_t len, buf_t *out, const char **err) {
|
||||
size_t pos = 0;
|
||||
size_t eol = 0;
|
||||
while (eol < len && data[eol] != '\n') eol++;
|
||||
if (eol >= len) { *err = "COLT1: truncated header"; return -1; }
|
||||
char hdr[128];
|
||||
if (eol >= sizeof(hdr)) { *err = "COLT1: header line too long"; return -1; }
|
||||
memcpy(hdr, data, eol);
|
||||
hdr[eol] = 0;
|
||||
long nrows, ncols, crlf, tnl, uniform;
|
||||
if (sscanf(hdr, "COLT1 %ld %ld %ld %ld %ld", &nrows, &ncols, &crlf, &tnl, &uniform) != 5 ||
|
||||
nrows < 0 || ncols < 0 || (crlf != 0 && crlf != 1) || (tnl != 0 && tnl != 1) ||
|
||||
(uniform != 0 && uniform != 1)) {
|
||||
*err = "COLT1: bad magic/header";
|
||||
return -1;
|
||||
}
|
||||
if ((nrows == 0) != (ncols == 0)) {
|
||||
*err = "COLT1: row/column counts inconsistent";
|
||||
return -1;
|
||||
}
|
||||
pos = eol + 1;
|
||||
|
||||
size_t *counts = NULL;
|
||||
if (nrows > 0) {
|
||||
counts = xmalloc((size_t)nrows * sizeof(size_t));
|
||||
for (long r = 0; r < nrows; r++) counts[r] = (size_t)ncols;
|
||||
}
|
||||
if (!uniform) {
|
||||
size_t clen = 0;
|
||||
if (colt_header(data, len, &pos, "COUNTS", &clen) != 0) { *err = "COLT1: bad COUNTS header"; free(counts); return -1; }
|
||||
if (pos + clen > len) { *err = "COLT1: truncated COUNTS blob"; free(counts); return -1; }
|
||||
/* parse comma-separated decimal counts */
|
||||
size_t r = 0;
|
||||
size_t p = pos;
|
||||
size_t end = pos + clen;
|
||||
while (p < end) {
|
||||
if (r >= (size_t)nrows) { *err = "COLT1: too many counts"; free(counts); return -1; }
|
||||
unsigned long v = 0;
|
||||
int ndig = 0;
|
||||
while (p < end && data[p] >= '0' && data[p] <= '9') { v = v * 10 + (data[p] - '0'); p++; ndig++; }
|
||||
if (!ndig) { *err = "COLT1: bad COUNTS blob"; free(counts); return -1; }
|
||||
counts[r++] = (size_t)v;
|
||||
if (p < end && data[p] == ',') p++;
|
||||
else if (p < end) { *err = "COLT1: bad COUNTS blob"; free(counts); return -1; }
|
||||
}
|
||||
if (r != (size_t)nrows) { *err = "COLT1: counts length mismatch"; free(counts); return -1; }
|
||||
pos = end;
|
||||
}
|
||||
|
||||
/* columns: split each COL blob on "\n" into nrows slices */
|
||||
slice_t *cols = NULL; /* ncols x nrows, column-major */
|
||||
if (ncols > 0 && nrows > 0) cols = xmalloc((size_t)ncols * (size_t)nrows * sizeof(slice_t));
|
||||
for (long j = 0; j < ncols; j++) {
|
||||
size_t blen = 0;
|
||||
if (colt_header(data, len, &pos, "COL", &blen) != 0) { *err = "COLT1: bad COL header"; free(counts); free(cols); return -1; }
|
||||
if (pos + blen > len) { *err = "COLT1: truncated COL blob"; free(counts); free(cols); return -1; }
|
||||
const uint8_t *blob = data + pos;
|
||||
pos += blen;
|
||||
if (blen == 0) {
|
||||
for (long r = 0; r < nrows; r++) { cols[(size_t)j * (size_t)nrows + r].p = blob; cols[(size_t)j * (size_t)nrows + r].n = 0; }
|
||||
} else {
|
||||
size_t start = 0;
|
||||
long r = 0;
|
||||
for (size_t k = 0; k <= blen; k++) {
|
||||
if (k == blen || blob[k] == '\n') {
|
||||
if (r >= nrows) { *err = "COLT1: column field count mismatch"; free(counts); free(cols); return -1; }
|
||||
cols[(size_t)j * (size_t)nrows + r].p = blob + start;
|
||||
cols[(size_t)j * (size_t)nrows + r].n = k - start;
|
||||
r++;
|
||||
start = k + 1;
|
||||
}
|
||||
}
|
||||
if (r != nrows) { *err = "COLT1: column field count mismatch"; free(counts); free(cols); return -1; }
|
||||
}
|
||||
}
|
||||
if (pos != len) { *err = "COLT1: trailing data after last column"; free(counts); free(cols); return -1; }
|
||||
|
||||
if (ncols == 0) { free(counts); free(cols); return 0; } /* empty input */
|
||||
const uint8_t *eolb = crlf ? (const uint8_t *)"\r\n" : (const uint8_t *)"\n";
|
||||
size_t eollen = crlf ? 2 : 1;
|
||||
for (long i = 0; i < nrows; i++) {
|
||||
for (size_t j = 0; j < counts[i]; j++) {
|
||||
if (j) buf_putc(out, ',');
|
||||
slice_t f = cols[j * (size_t)nrows + (size_t)i];
|
||||
buf_put(out, f.p, f.n);
|
||||
}
|
||||
buf_put(out, eolb, eollen);
|
||||
}
|
||||
if (!tnl && out->n >= eollen) out->n -= eollen;
|
||||
free(counts);
|
||||
free(cols);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* xz via liblzma (easy encoder, CRC64 check: same stream as xz -6) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int xz_compress(const uint8_t *in, size_t in_len, uint32_t preset,
|
||||
uint8_t *out, size_t out_cap, size_t *out_len) {
|
||||
/* NOTE: out_pos is in/out and MUST start at 0 (it is the write offset,
|
||||
* not the buffer size); passing the capacity here made the encoder
|
||||
* think the buffer was already full -> LZMA_BUF_ERROR on every call. */
|
||||
size_t out_pos = 0;
|
||||
lzma_ret r = lzma_easy_buffer_encode(preset, LZMA_CHECK_CRC64, NULL,
|
||||
in, in_len, out, &out_pos, out_cap);
|
||||
if (r != LZMA_OK) return -1;
|
||||
*out_len = out_pos;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int xz_decompress(const uint8_t *in, size_t in_len, uint8_t *out,
|
||||
size_t *out_len) {
|
||||
uint64_t memlimit = UINT64_MAX;
|
||||
size_t in_pos = 0, out_pos = 0;
|
||||
lzma_ret r = lzma_stream_buffer_decode(&memlimit, 0, NULL,
|
||||
in, &in_pos, in_len,
|
||||
out, &out_pos, *out_len);
|
||||
if (r != LZMA_OK) return -1;
|
||||
if (in_pos != in_len) return -1; /* trailing data */
|
||||
*out_len = out_pos;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* LZCSV container */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void put_header(uint8_t *h, uint64_t orig, uint64_t trans,
|
||||
uint8_t preset, uint8_t flags) {
|
||||
memcpy(h, LZCSV_MAGIC, LZCSV_MAGIC_LEN);
|
||||
wr64le(h + 8, orig);
|
||||
wr64le(h + 16, trans);
|
||||
h[24] = preset;
|
||||
h[25] = flags;
|
||||
}
|
||||
|
||||
static int get_header(const uint8_t *h, size_t len, uint64_t *orig,
|
||||
uint64_t *trans, uint8_t *preset, uint8_t *flags) {
|
||||
if (len < LZCSV_HEADER_LEN) return -1;
|
||||
if (memcmp(h, LZCSV_MAGIC, LZCSV_MAGIC_LEN) != 0) return -1;
|
||||
*orig = rd64le(h + 8);
|
||||
*trans = rd64le(h + 16);
|
||||
*preset = h[24];
|
||||
*flags = h[25];
|
||||
if (*preset > 9 || (*flags & ~1u)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* commands */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void usage(FILE *f) {
|
||||
fprintf(f,
|
||||
"lzcsv %s - high-speed CSV compression (column transpose + xz)\n"
|
||||
"usage: lzcsv <cmd> [options] <in> [out]\n"
|
||||
" c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv> compress (preset 6 default)\n"
|
||||
" d [-f] [-v] <in.lzcsv> <out.csv> decompress\n"
|
||||
" test <file.csv> ... in-memory round-trip check\n"
|
||||
" info <file.lzcsv> show container header\n"
|
||||
" raw-enc <in.csv> <out.colt> write COLT1 intermediate\n"
|
||||
" raw-dec <in.colt> <out.csv> decode COLT1 intermediate\n"
|
||||
" selftest built-in synthetic tests\n"
|
||||
" --help this text\n"
|
||||
"options: -p N xz preset 0-9 (default 6); -e extreme (-9e style);\n"
|
||||
" -f overwrite output; -v print sizes\n"
|
||||
"The COLT1 transpose is byte-compatible with the hot_qwen Cycle 1\n"
|
||||
"Python prototype. Fields holding a raw newline and files with mixed\n"
|
||||
"line endings are rejected (documented limitation).\n", LZCSV_VERSION);
|
||||
}
|
||||
|
||||
static int do_compress(const char *in_path, const char *out_path,
|
||||
int preset, int extreme, int force, int verbose) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
buf_t trans;
|
||||
buf_init(&trans);
|
||||
const char *err = NULL;
|
||||
if (colt_encode(in, in_len, &trans, &err) != 0) {
|
||||
free(in);
|
||||
die("%s: %s", in_path, err);
|
||||
}
|
||||
size_t bound = lzma_stream_buffer_bound(trans.n);
|
||||
uint8_t *xz = xmalloc(bound);
|
||||
size_t xz_len = 0;
|
||||
uint32_t pr = (uint32_t)preset | (extreme ? LZMA_PRESET_EXTREME : 0);
|
||||
if (xz_compress(trans.v, trans.n, pr, xz, bound, &xz_len) != 0) {
|
||||
free(in); buf_free(&trans); free(xz);
|
||||
die("%s: xz compression failed", in_path);
|
||||
}
|
||||
buf_t file;
|
||||
buf_init(&file);
|
||||
uint8_t hdr[LZCSV_HEADER_LEN];
|
||||
put_header(hdr, (uint64_t)in_len, (uint64_t)trans.n, (uint8_t)preset, extreme ? 1 : 0);
|
||||
buf_put(&file, hdr, LZCSV_HEADER_LEN);
|
||||
buf_put(&file, xz, xz_len);
|
||||
write_file(out_path, file.v, file.n, force);
|
||||
if (verbose)
|
||||
printf("%s -> %s: %zu -> %zu bytes (ratio %.4f, preset %d%s)\n",
|
||||
in_path, out_path, in_len, file.n,
|
||||
in_len ? (double)file.n / (double)in_len : 0.0,
|
||||
preset, extreme ? "e" : "");
|
||||
free(in); buf_free(&trans); free(xz); buf_free(&file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_decompress(const char *in_path, const char *out_path,
|
||||
int force, int verbose) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
uint64_t orig = 0, trans = 0;
|
||||
uint8_t preset = 0, flags = 0;
|
||||
if (get_header(in, in_len, &orig, &trans, &preset, &flags) != 0) {
|
||||
free(in);
|
||||
die("%s: not an LZCSV file (bad magic/header)", in_path);
|
||||
}
|
||||
if (trans > (uint64_t)SIZE_MAX || orig > (uint64_t)SIZE_MAX) {
|
||||
free(in);
|
||||
die("%s: implausible sizes in header", in_path);
|
||||
}
|
||||
uint8_t *colt = xmalloc((size_t)trans ? (size_t)trans : 1);
|
||||
size_t colt_len = (size_t)trans;
|
||||
if (xz_decompress(in + LZCSV_HEADER_LEN, in_len - LZCSV_HEADER_LEN,
|
||||
colt, &colt_len) != 0) {
|
||||
free(in); free(colt);
|
||||
die("%s: xz stream corrupt or truncated", in_path);
|
||||
}
|
||||
if (colt_len != (size_t)trans) {
|
||||
free(in); free(colt);
|
||||
die("%s: transposed size mismatch (header says %llu, got %zu)",
|
||||
in_path, (unsigned long long)trans, colt_len);
|
||||
}
|
||||
buf_t out;
|
||||
buf_init(&out);
|
||||
const char *err = NULL;
|
||||
if (colt_decode(colt, colt_len, &out, &err) != 0) {
|
||||
free(in); free(colt); buf_free(&out);
|
||||
die("%s: COLT1 decode failed: %s", in_path, err);
|
||||
}
|
||||
if (out.n != (size_t)orig) {
|
||||
free(in); free(colt); buf_free(&out);
|
||||
die("%s: output size mismatch (header says %llu, got %zu)",
|
||||
in_path, (unsigned long long)orig, out.n);
|
||||
}
|
||||
write_file(out_path, out.v, out.n, force);
|
||||
if (verbose)
|
||||
printf("%s -> %s: %zu -> %zu bytes\n", in_path, out_path, in_len, out.n);
|
||||
free(in); free(colt); buf_free(&out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_raw_enc(const char *in_path, const char *out_path, int force) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
buf_t trans;
|
||||
buf_init(&trans);
|
||||
const char *err = NULL;
|
||||
if (colt_encode(in, in_len, &trans, &err) != 0) {
|
||||
free(in);
|
||||
die("%s: %s", in_path, err);
|
||||
}
|
||||
write_file(out_path, trans.v, trans.n, force);
|
||||
free(in); buf_free(&trans);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_raw_dec(const char *in_path, const char *out_path, int force) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
buf_t out;
|
||||
buf_init(&out);
|
||||
const char *err = NULL;
|
||||
if (colt_decode(in, in_len, &out, &err) != 0) {
|
||||
free(in); buf_free(&out);
|
||||
die("%s: %s", in_path, err);
|
||||
}
|
||||
write_file(out_path, out.v, out.n, force);
|
||||
free(in); buf_free(&out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_info(const char *path) {
|
||||
size_t len = 0;
|
||||
uint8_t *in = read_file(path, &len);
|
||||
uint64_t orig = 0, trans = 0;
|
||||
uint8_t preset = 0, flags = 0;
|
||||
if (get_header(in, len, &orig, &trans, &preset, &flags) != 0) {
|
||||
free(in);
|
||||
die("%s: not an LZCSV file (bad magic/header)", path);
|
||||
}
|
||||
printf("%s: LZCSV v1, original %llu bytes, transposed %llu bytes, "
|
||||
"xz preset %d%s, container %zu bytes\n",
|
||||
path, (unsigned long long)orig, (unsigned long long)trans,
|
||||
preset, flags ? "e" : "", len);
|
||||
free(in);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* In-memory round-trip of one file; returns 0 on success. */
|
||||
static int roundtrip_mem(const uint8_t *data, size_t len, const char **err) {
|
||||
buf_t trans;
|
||||
buf_init(&trans);
|
||||
if (colt_encode(data, len, &trans, err) != 0) { buf_free(&trans); return -1; }
|
||||
buf_t out;
|
||||
buf_init(&out);
|
||||
if (colt_decode(trans.v, trans.n, &out, err) != 0) { buf_free(&trans); buf_free(&out); return -1; }
|
||||
int ok = (out.n == len && (len == 0 || memcmp(out.v, data, len) == 0));
|
||||
if (!ok) *err = "round-trip output differs from input";
|
||||
buf_free(&trans); buf_free(&out);
|
||||
return ok ? 0 : -1;
|
||||
}
|
||||
|
||||
static int do_test(int nfiles, char **files) {
|
||||
int fail = 0;
|
||||
for (int i = 0; i < nfiles; i++) {
|
||||
size_t len = 0;
|
||||
uint8_t *data = read_file(files[i], &len);
|
||||
const char *err = NULL;
|
||||
if (roundtrip_mem(data, len, &err) == 0) {
|
||||
printf("OK %s (%zu bytes)\n", files[i], len);
|
||||
} else {
|
||||
printf("FAIL %s: %s\n", files[i], err);
|
||||
fail++;
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
return fail ? 1 : 0;
|
||||
}
|
||||
|
||||
static int selftest_one(const char *name, const uint8_t *data, size_t len) {
|
||||
const char *err = NULL;
|
||||
if (roundtrip_mem(data, len, &err) == 0) {
|
||||
printf("PASS %s\n", name);
|
||||
return 0;
|
||||
}
|
||||
printf("FAIL %s: %s\n", name, err);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int do_selftest(void) {
|
||||
int fail = 0;
|
||||
fail += selftest_one("empty", (const uint8_t *)"", 0);
|
||||
fail += selftest_one("one-row-no-nl", (const uint8_t *)"a,b,c", 5);
|
||||
fail += selftest_one("header-only", (const uint8_t *)"col1,col2,col3\n", 15);
|
||||
fail += selftest_one("crlf", (const uint8_t *)"name,age\r\nann,34\r\nbob,29\r\n", 26);
|
||||
fail += selftest_one("ragged", (const uint8_t *)"a,b,c\n1,2\n3,4,5,6,7\n8\n", 22);
|
||||
fail += selftest_one("quotes", (const uint8_t *)"id,note\n1,\"hello, world\"\n2,\"say \"\"hi\"\"\"\n3,plain \"quoted\" text\n", 62);
|
||||
fail += selftest_one("no-trailing-nl", (const uint8_t *)"x,y\n1,2\n3,4", 11);
|
||||
fail += selftest_one("single-col", (const uint8_t *)"v\n1\n22\n333\n", 11);
|
||||
fail += selftest_one("empty-fields", (const uint8_t *)"a,,c\n,,\n1,2,\n", 13);
|
||||
fail += selftest_one("quoted-empty", (const uint8_t *)"a,b\n\"\",x\n", 9);
|
||||
printf("selftest: %s\n", fail ? "FAIL" : "all pass");
|
||||
return fail ? 1 : 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) { usage(stderr); return 2; }
|
||||
const char *cmd = argv[1];
|
||||
if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") || !strcmp(cmd, "help")) {
|
||||
usage(stdout);
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(cmd, "selftest")) return do_selftest();
|
||||
|
||||
int preset = 6, extreme = 0, force = 0, verbose = 0;
|
||||
int i = 2;
|
||||
/* options come after the subcommand */
|
||||
while (i < argc && argv[i][0] == '-' && argv[i][1] != 0) {
|
||||
if (!strcmp(argv[i], "-p")) {
|
||||
if (++i >= argc) { fprintf(stderr, "%s: -p needs a preset\n", prog); return 2; }
|
||||
preset = atoi(argv[i]);
|
||||
if (preset < 0 || preset > 9) { fprintf(stderr, "%s: preset must be 0-9\n", prog); return 2; }
|
||||
} else if (!strcmp(argv[i], "-e")) {
|
||||
extreme = 1;
|
||||
} else if (!strcmp(argv[i], "-f")) {
|
||||
force = 1;
|
||||
} else if (!strcmp(argv[i], "-v")) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
fprintf(stderr, "%s: unknown option %s\n", prog, argv[i]);
|
||||
return 2;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
int npos = argc - i;
|
||||
char **pos = argv + i;
|
||||
|
||||
if (!strcmp(cmd, "c")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv>\n"); return 2; }
|
||||
return do_compress(pos[0], pos[1], preset, extreme, force, verbose);
|
||||
}
|
||||
if (!strcmp(cmd, "d")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv d [-f] [-v] <in.lzcsv> <out.csv>\n"); return 2; }
|
||||
return do_decompress(pos[0], pos[1], force, verbose);
|
||||
}
|
||||
if (!strcmp(cmd, "test")) {
|
||||
if (npos < 1) { fprintf(stderr, "usage: lzcsv test <file.csv> ...\n"); return 2; }
|
||||
return do_test(npos, pos);
|
||||
}
|
||||
if (!strcmp(cmd, "info")) {
|
||||
if (npos != 1) { fprintf(stderr, "usage: lzcsv info <file.lzcsv>\n"); return 2; }
|
||||
return do_info(pos[0]);
|
||||
}
|
||||
if (!strcmp(cmd, "raw-enc")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv raw-enc <in.csv> <out.colt>\n"); return 2; }
|
||||
return do_raw_enc(pos[0], pos[1], force);
|
||||
}
|
||||
if (!strcmp(cmd, "raw-dec")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv raw-dec <in.colt> <out.csv>\n"); return 2; }
|
||||
return do_raw_dec(pos[0], pos[1], force);
|
||||
}
|
||||
fprintf(stderr, "%s: unknown command '%s'\n", prog, cmd);
|
||||
usage(stderr);
|
||||
return 2;
|
||||
}
|
||||
@@ -105,7 +105,9 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bch.h"
|
||||
#include "deep_repair.h"
|
||||
#include "lzply_lib.h"
|
||||
#include "repair_cmd.h"
|
||||
|
||||
|
||||
|
||||
@@ -1571,7 +1573,7 @@ static int cmd_tree(const char *archive, const char **selectors,
|
||||
static void usage(FILE *out)
|
||||
{
|
||||
fprintf(out,
|
||||
"Usage: %s -c|-d|-l|-t|-a|-i|-tr [options] <input> [output] [selectors...]\n"
|
||||
"Usage: %s -c|-d|-l|-t|-a|-i|-tr|-r [options] <input> [output] [selectors...]\n"
|
||||
"\n"
|
||||
" -c compress a file or directory into one .lzply archive\n"
|
||||
" -a append files/dirs to an archive (creates it if\n"
|
||||
@@ -1594,6 +1596,15 @@ static void usage(FILE *out)
|
||||
" -tr print an archive as a tree, like the Linux\n"
|
||||
" `tree` command, with a directory/file count.\n"
|
||||
" Selectors filter it.\n"
|
||||
" -r repair an archive: recover entries whose bit\n"
|
||||
" errors exceed the BCH limit t, in escalating\n"
|
||||
" levels L0-L3 (standard decode, Chase-1,\n"
|
||||
" Chase-2, cross-block). Recovered files go to\n"
|
||||
" [output] dir (default ./repaired). --level N\n"
|
||||
" caps the level (default 3), --time-budget S\n"
|
||||
" the seconds per entry (default 120). On a\n"
|
||||
" CUDA build the L1 Chase-1 scan runs on the\n"
|
||||
" GPU when a device is present, else CPU.\n"
|
||||
" -datestart <date> only entries with mtime on or after this date\n"
|
||||
" -dateend <date> only entries with mtime on or before this date\n"
|
||||
" Dates are YYYY-MM-DD (whole day, inclusive) or\n"
|
||||
@@ -1648,16 +1659,18 @@ static void usage(FILE *out)
|
||||
" %s -i backup.lzply -datestart 2024-01-01 -dateend 2024-06-30\n"
|
||||
" inspect only Jan-Jun 2024 entries\n"
|
||||
" %s -tr backup.lzply photos/ -year 2025 tree of 2025 photos only\n"
|
||||
" %s -r backup.lzply repair into ./repaired\n"
|
||||
" %s -r -o fixed/ backup.lzply repair into fixed/\n"
|
||||
"\n"
|
||||
"Exit codes: 0 ok, 1 usage, 2 I/O error, 3 corrupt/unrecoverable.\n",
|
||||
lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname,
|
||||
lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname,
|
||||
lzply_progname, lzply_progname, lzply_progname, lzply_progname,
|
||||
lzply_progname);
|
||||
lzply_progname, lzply_progname, lzply_progname);
|
||||
}
|
||||
|
||||
enum { MODE_NONE, MODE_COMPRESS, MODE_DECOMPRESS, MODE_LIST, MODE_TEST,
|
||||
MODE_APPEND, MODE_INSPECT, MODE_TREE };
|
||||
MODE_APPEND, MODE_INSPECT, MODE_TREE, MODE_REPAIR };
|
||||
|
||||
/* Parse a poly=NN value: hex if it has a 0x prefix or any a-f/A-F
|
||||
* letter, else decimal. Returns the poly, or NULL if it is not a
|
||||
@@ -1696,6 +1709,11 @@ int main(int argc, char **argv)
|
||||
int ecc_level = 4; /* default -e 4 -> BCH t=8 */
|
||||
const bch_poly_t *pinned_poly = NULL;
|
||||
int ow_mode = OW_ASK;
|
||||
const char *repair_outdir = "./repaired";
|
||||
int repair_verbose = 0;
|
||||
int repair_max_level = DEEP_MAX_LEVEL;
|
||||
double repair_budget = DEEP_DEFAULT_BUDGET_SEC;
|
||||
const char *repair_only_entry = NULL;
|
||||
date_filter_t dfilter;
|
||||
const char **args = NULL; /* positional arguments after the options */
|
||||
int nargs = 0;
|
||||
@@ -1744,6 +1762,64 @@ int main(int argc, char **argv)
|
||||
mode = MODE_INSPECT;
|
||||
} else if (strcmp(a, "-tr") == 0 || strcmp(a, "--tree") == 0) {
|
||||
mode = MODE_TREE;
|
||||
} else if (strcmp(a, "-r") == 0 || strcmp(a, "--repair") == 0) {
|
||||
mode = MODE_REPAIR;
|
||||
} else if (strcmp(a, "-o") == 0 || strcmp(a, "--output") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "%s: -o needs a directory\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
repair_outdir = argv[++i];
|
||||
} else if (strcmp(a, "-v") == 0 || strcmp(a, "--verbose") == 0) {
|
||||
repair_verbose = 1;
|
||||
} else if (strcmp(a, "--level") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "%s: --level needs 0..3\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
repair_max_level = atoi(argv[++i]);
|
||||
if (repair_max_level < 0 || repair_max_level > DEEP_MAX_LEVEL) {
|
||||
fprintf(stderr, "%s: --level must be 0..3\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
} else if (strncmp(a, "--level=", 8) == 0) {
|
||||
repair_max_level = atoi(a + 8);
|
||||
if (repair_max_level < 0 || repair_max_level > DEEP_MAX_LEVEL) {
|
||||
fprintf(stderr, "%s: --level must be 0..3\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
} else if (strcmp(a, "--time-budget") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "%s: --time-budget needs seconds\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
repair_budget = atof(argv[++i]);
|
||||
if (repair_budget <= 0) {
|
||||
fprintf(stderr, "%s: --time-budget must be > 0\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
} else if (strncmp(a, "--time-budget=", 14) == 0) {
|
||||
repair_budget = atof(a + 14);
|
||||
if (repair_budget <= 0) {
|
||||
fprintf(stderr, "%s: --time-budget must be > 0\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
} else if (strcmp(a, "--entry") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "%s: --entry needs a stored path\n",
|
||||
lzply_progname);
|
||||
return EXIT_USAGE;
|
||||
}
|
||||
repair_only_entry = argv[++i];
|
||||
} else if (strncmp(a, "--entry=", 8) == 0) {
|
||||
repair_only_entry = a + 8;
|
||||
} else if (strcmp(a, "-datestart") == 0 ||
|
||||
strcmp(a, "--datestart") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
@@ -1931,6 +2007,22 @@ int main(int argc, char **argv)
|
||||
nargs >= 2 ? nargs - 1 : 0,
|
||||
&dfilter);
|
||||
break;
|
||||
case MODE_REPAIR:
|
||||
/* archive, optional outdir. */
|
||||
if (nargs < 1 || nargs > 2) {
|
||||
fprintf(stderr,
|
||||
"%s: -r needs an archive and an optional output directory\n",
|
||||
lzply_progname);
|
||||
usage(stderr);
|
||||
rc = EXIT_USAGE;
|
||||
break;
|
||||
}
|
||||
if (nargs == 2)
|
||||
repair_outdir = args[1];
|
||||
rc = repair_archive(lzply_progname, args[0], repair_outdir,
|
||||
repair_max_level, repair_budget,
|
||||
repair_only_entry, repair_verbose);
|
||||
break;
|
||||
default:
|
||||
rc = EXIT_USAGE;
|
||||
break;
|
||||
|
||||
+6
-312
@@ -18,27 +18,23 @@
|
||||
* and how many bits were corrected. -v adds the per-level trial
|
||||
* counts (BM trials, splitting-filter passes, valid codewords).
|
||||
*
|
||||
* The archive-walking driver lives in repair_cmd.c (shared with
|
||||
* the lzply -r mode); this file is only the option parsing.
|
||||
*
|
||||
* Exit codes: 0 all entries recovered, 1 usage, 2 I/O error,
|
||||
* 3 any entry unrecoverable (matching lzply's EXIT_CORRUPT).
|
||||
*/
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#define _DEFAULT_SOURCE
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bch.h"
|
||||
#include "deep_repair.h"
|
||||
#include "lzply_lib.h"
|
||||
#include "lzrs_lib.h"
|
||||
#include "repair_cmd.h"
|
||||
|
||||
static const char *prog = "lzply_deep_repair";
|
||||
|
||||
@@ -57,56 +53,6 @@ static void usage(void)
|
||||
prog, DEEP_DEFAULT_BUDGET_SEC);
|
||||
}
|
||||
|
||||
/* mkdir -p (mode 0755 for created parents). Returns 0 on success. */
|
||||
static int mkdir_p(const char *path, mode_t mode)
|
||||
{
|
||||
char *tmp = xstrdup(path);
|
||||
size_t n = strlen(tmp);
|
||||
size_t i;
|
||||
if (n == 0) {
|
||||
free(tmp);
|
||||
return 0;
|
||||
}
|
||||
while (n > 1 && tmp[n - 1] == '/')
|
||||
tmp[--n] = '\0';
|
||||
for (i = 1; i <= n; i++) {
|
||||
if (tmp[i] == '/' || tmp[i] == '\0') {
|
||||
char save = tmp[i];
|
||||
struct stat st;
|
||||
tmp[i] = '\0';
|
||||
if (stat(tmp, &st) != 0) {
|
||||
if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
|
||||
free(tmp);
|
||||
return -1;
|
||||
}
|
||||
} else if (!S_ISDIR(st.st_mode)) {
|
||||
free(tmp);
|
||||
return -1;
|
||||
}
|
||||
tmp[i] = save;
|
||||
}
|
||||
}
|
||||
free(tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_out_file(const char *path, const uint8_t *data, size_t len,
|
||||
uint32_t mode)
|
||||
{
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f)
|
||||
return -1;
|
||||
if (len > 0 && fwrite(data, 1, len, f) != len) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (fclose(f) != 0)
|
||||
return -1;
|
||||
if (mode)
|
||||
chmod(path, (mode_t)(mode & 07777));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int max_level = DEEP_MAX_LEVEL;
|
||||
@@ -115,9 +61,6 @@ int main(int argc, char **argv)
|
||||
const char *outdir = "./repaired";
|
||||
const char *only_entry = NULL;
|
||||
const char *archive = NULL;
|
||||
int is_lzrs = 0;
|
||||
FILE *f = NULL;
|
||||
uint8_t magic[4];
|
||||
int opt;
|
||||
static struct option long_opts[] = {
|
||||
{ "time-budget", required_argument, NULL, 1000 },
|
||||
@@ -167,255 +110,6 @@ int main(int argc, char **argv)
|
||||
}
|
||||
archive = argv[optind];
|
||||
|
||||
f = fopen(archive, "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "%s: cannot open %s: %s\n", prog, archive,
|
||||
strerror(errno));
|
||||
return EXIT_IO;
|
||||
}
|
||||
if (fread(magic, 1, 4, f) != 4) {
|
||||
fprintf(stderr, "%s: %s is too short to be an archive\n",
|
||||
prog, archive);
|
||||
fclose(f);
|
||||
return EXIT_CORRUPT;
|
||||
}
|
||||
if (memcmp(magic, "LZRS", 4) == 0)
|
||||
is_lzrs = 1;
|
||||
else if (memcmp(magic, "LZPL", 4) != 0) {
|
||||
fprintf(stderr, "%s: %s is not an LZPL or LZRS archive\n",
|
||||
prog, archive);
|
||||
fclose(f);
|
||||
return EXIT_CORRUPT;
|
||||
}
|
||||
/* Validate the archive up front (version, headers, end marker),
|
||||
* so a structurally broken archive fails before any repair is
|
||||
* attempted. The scan also gives the clear v4 message for old
|
||||
* LZPL archives. */
|
||||
{
|
||||
int version = 0;
|
||||
uint32_t count = 0;
|
||||
off_t size = 0, end_off = 0;
|
||||
scan_list_t entries;
|
||||
int s;
|
||||
rewind(f);
|
||||
if (is_lzrs)
|
||||
s = lzrs_scan_archive(archive, &version, &count, &size,
|
||||
&entries, &end_off);
|
||||
else
|
||||
s = scan_archive(archive, &version, &count, &size,
|
||||
&entries, &end_off);
|
||||
if (s != 0) {
|
||||
fclose(f);
|
||||
return EXIT_CORRUPT;
|
||||
}
|
||||
scan_list_free(&entries);
|
||||
rewind(f);
|
||||
}
|
||||
if (mkdir_p(outdir, 0755) != 0) {
|
||||
fprintf(stderr, "%s: cannot create output directory %s: %s\n",
|
||||
prog, outdir, strerror(errno));
|
||||
fclose(f);
|
||||
return EXIT_IO;
|
||||
}
|
||||
|
||||
/* Skip the 16-byte global header; then walk the entries. */
|
||||
if (fseeko(f, 16, SEEK_SET) != 0) {
|
||||
fprintf(stderr, "%s: cannot seek in %s\n", prog, archive);
|
||||
fclose(f);
|
||||
return EXIT_IO;
|
||||
}
|
||||
{
|
||||
int worst = 0;
|
||||
long n_ok = 0, n_fail = 0, n_dir = 0;
|
||||
long landed[4] = { 0, 0, 0, 0 };
|
||||
double t_start = bch_now();
|
||||
for (;;) {
|
||||
char *path = NULL;
|
||||
int level = 0, is_bch = 0, rs_level = 0, is_dir = 0;
|
||||
uint16_t poly_id = 0, want_crc16 = 0;
|
||||
uint64_t stored_size = 0, blob_size = 0, orig_size = 0;
|
||||
uint32_t mode = 0;
|
||||
uint8_t sha[32];
|
||||
uint8_t *stored = NULL;
|
||||
int r;
|
||||
if (is_lzrs) {
|
||||
lzrs_ehdr_t h;
|
||||
r = lzrs_read_entry_header(f, &h);
|
||||
if (r == 1)
|
||||
break;
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "%s: truncated or corrupt entry header\n",
|
||||
prog);
|
||||
worst = EXIT_CORRUPT;
|
||||
break;
|
||||
}
|
||||
path = h.path;
|
||||
level = h.level;
|
||||
is_bch = (h.flags & FLAG_BCH) != 0;
|
||||
rs_level = h.rs_level;
|
||||
is_dir = (h.flags & FLAG_ISDIR) != 0;
|
||||
poly_id = h.bch_poly_id;
|
||||
want_crc16 = h.crc16;
|
||||
stored_size = h.stored_size;
|
||||
blob_size = h.blob_size;
|
||||
orig_size = h.orig_size;
|
||||
mode = h.mode;
|
||||
memcpy(sha, h.sha, 32);
|
||||
} else {
|
||||
ehdr_t h;
|
||||
r = read_entry_header(f, &h);
|
||||
if (r == 1)
|
||||
break;
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "%s: truncated or corrupt entry header\n",
|
||||
prog);
|
||||
worst = EXIT_CORRUPT;
|
||||
break;
|
||||
}
|
||||
path = h.path;
|
||||
level = h.level;
|
||||
is_bch = (h.flags & FLAG_BCH) != 0;
|
||||
rs_level = 0;
|
||||
is_dir = (h.flags & FLAG_ISDIR) != 0;
|
||||
poly_id = (uint16_t)(h.rs_level |
|
||||
((uint16_t)h.reserved << 8));
|
||||
want_crc16 = h.crc16;
|
||||
stored_size = h.stored_size;
|
||||
blob_size = h.blob_size;
|
||||
orig_size = h.orig_size;
|
||||
mode = h.mode;
|
||||
memcpy(sha, h.sha, 32);
|
||||
}
|
||||
stored = xmalloc(stored_size ? (size_t)stored_size : 1);
|
||||
if (stored_size > 0 &&
|
||||
fread(stored, 1, (size_t)stored_size, f) !=
|
||||
(size_t)stored_size) {
|
||||
fprintf(stderr, "%s: truncated data for %s\n", prog, path);
|
||||
free(stored);
|
||||
free(path);
|
||||
worst = EXIT_CORRUPT;
|
||||
break;
|
||||
}
|
||||
if (only_entry && strcmp(path, only_entry) != 0) {
|
||||
free(stored);
|
||||
free(path);
|
||||
continue;
|
||||
}
|
||||
if (is_dir) {
|
||||
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
|
||||
sprintf(dp, "%s/%s", outdir, path);
|
||||
if (mkdir_p(dp, mode ? (mode_t)(mode & 07777) : 0755) != 0)
|
||||
fprintf(stderr, "%s: cannot create directory %s: %s\n",
|
||||
prog, dp, strerror(errno));
|
||||
free(dp);
|
||||
free(stored);
|
||||
free(path);
|
||||
n_dir++;
|
||||
continue;
|
||||
}
|
||||
{
|
||||
uint8_t *raw = NULL;
|
||||
size_t raw_len = 0;
|
||||
deep_stats_t st;
|
||||
int drc = deep_repair_entry(stored, (size_t)stored_size,
|
||||
level, is_bch, poly_id,
|
||||
rs_level, sha,
|
||||
(size_t)blob_size, want_crc16,
|
||||
max_level, budget,
|
||||
&raw, &raw_len, &st);
|
||||
free(stored);
|
||||
if (drc != 0) {
|
||||
printf("%s: FAILED (stage %s) after %.3fs, "
|
||||
"cands %ld crc-rejects %ld sha-rejects %ld\n",
|
||||
path, st.fail_stage, st.t_total,
|
||||
st.cands_tried, st.crc_rejects, st.sha_rejects);
|
||||
if (verbose) {
|
||||
int lv;
|
||||
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
|
||||
printf(" L%d: %.3fs trials %ld split %ld "
|
||||
"codewords %ld\n", lv, st.t_level[lv],
|
||||
st.trials[lv], st.split_pass[lv],
|
||||
st.codewords[lv]);
|
||||
printf(" blocks %ld/%ld failed%s\n",
|
||||
st.blocks_failed, st.blocks_total,
|
||||
st.partial ? " (partial: budget hit)" : "");
|
||||
}
|
||||
n_fail++;
|
||||
if (EXIT_CORRUPT > worst)
|
||||
worst = EXIT_CORRUPT;
|
||||
free(path);
|
||||
continue;
|
||||
}
|
||||
if (raw_len != orig_size) {
|
||||
fprintf(stderr, "%s: %s: size mismatch after repair "
|
||||
"(got %zu, expected %llu)\n", prog, path,
|
||||
raw_len, (unsigned long long)orig_size);
|
||||
free(raw);
|
||||
free(path);
|
||||
n_fail++;
|
||||
if (EXIT_CORRUPT > worst)
|
||||
worst = EXIT_CORRUPT;
|
||||
continue;
|
||||
}
|
||||
{
|
||||
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
|
||||
char *pd;
|
||||
sprintf(dp, "%s/%s", outdir, path);
|
||||
pd = parent_dir(dp);
|
||||
if (mkdir_p(pd, 0755) != 0)
|
||||
fprintf(stderr, "%s: cannot create %s: %s\n",
|
||||
prog, pd, strerror(errno));
|
||||
free(pd);
|
||||
if (write_out_file(dp, raw, raw_len, mode) != 0) {
|
||||
fprintf(stderr, "%s: cannot write %s: %s\n",
|
||||
prog, dp, strerror(errno));
|
||||
if (EXIT_IO > worst)
|
||||
worst = EXIT_IO;
|
||||
free(dp);
|
||||
free(raw);
|
||||
free(path);
|
||||
continue;
|
||||
}
|
||||
free(dp);
|
||||
}
|
||||
{
|
||||
char rssuf[48];
|
||||
rssuf[0] = '\0';
|
||||
if (st.rs_corrected > 0)
|
||||
snprintf(rssuf, sizeof rssuf,
|
||||
" (RS corrected %d symbols)",
|
||||
st.rs_corrected);
|
||||
printf("%s: RECOVERED at L%d in %.3fs "
|
||||
"(L0 %.3fs, L1 %.3fs, L2 %.3fs, L3 %.3fs) "
|
||||
"cands %ld crc-rejects %ld sha-rejects %ld "
|
||||
"bch-corrected %d%s\n",
|
||||
path, st.level_landed, st.t_total,
|
||||
st.t_level[0], st.t_level[1], st.t_level[2],
|
||||
st.t_level[3], st.cands_tried, st.crc_rejects,
|
||||
st.sha_rejects, st.bch_corrected, rssuf);
|
||||
}
|
||||
if (verbose) {
|
||||
int lv;
|
||||
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
|
||||
printf(" L%d: %.3fs trials %ld split %ld "
|
||||
"codewords %ld\n", lv, st.t_level[lv],
|
||||
st.trials[lv], st.split_pass[lv],
|
||||
st.codewords[lv]);
|
||||
printf(" blocks %ld/%ld failed%s\n",
|
||||
st.blocks_failed, st.blocks_total,
|
||||
st.partial ? " (partial: budget hit)" : "");
|
||||
}
|
||||
landed[st.level_landed]++;
|
||||
n_ok++;
|
||||
free(raw);
|
||||
free(path);
|
||||
}
|
||||
}
|
||||
printf("deep repair: %ld recovered (L0 %ld, L1 %ld, L2 %ld, "
|
||||
"L3 %ld), %ld failed, %ld directories, %.3fs total\n",
|
||||
n_ok, landed[0], landed[1], landed[2], landed[3],
|
||||
n_fail, n_dir, bch_now() - t_start);
|
||||
fclose(f);
|
||||
return worst;
|
||||
}
|
||||
return repair_archive(prog, archive, outdir, max_level, budget,
|
||||
only_entry, verbose);
|
||||
}
|
||||
+354
@@ -0,0 +1,354 @@
|
||||
/* repair_cmd.c - shared archive repair driver for `lzply -r` and
|
||||
* `lzply_deep_repair`.
|
||||
*
|
||||
* repair_archive() validates an LZPL v5 or LZRS v1 archive, then
|
||||
* attempts the escalating deep-repair ladder (L0 standard decode
|
||||
* to t, L1 Chase-1 flips, L2 Chase-2 flips, L3 cross-block list
|
||||
* decoding) per entry, writing recovered files under outdir with
|
||||
* their stored paths preserved. Nothing is ever written unless it
|
||||
* passes both oracles (CRC16, then decompress + SHA-256).
|
||||
*
|
||||
* Per entry it reports the level the repair landed at and the
|
||||
* per-level timing -- the "time of arrival": how long each level
|
||||
* took, how many candidates were tried, how many the CRC rejected,
|
||||
* and how many bits were corrected. -v adds the per-level trial
|
||||
* counts (BM trials, splitting-filter passes, valid codewords);
|
||||
* the L1 line carries a (CUDA) marker when the Chase-1 scan ran
|
||||
* on the GPU.
|
||||
*
|
||||
* Returns the worst exit code: 0 all entries recovered, 2 I/O
|
||||
* error, 3 any entry unrecoverable (matching lzply's EXIT_*).
|
||||
*/
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#define _DEFAULT_SOURCE
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bch.h"
|
||||
#include "deep_repair.h"
|
||||
#include "lzply_lib.h"
|
||||
#include "lzrs_lib.h"
|
||||
#include "repair_cmd.h"
|
||||
|
||||
/* mkdir -p (mode 0755 for created parents). Returns 0 on success. */
|
||||
static int mkdir_p(const char *path, mode_t mode)
|
||||
{
|
||||
char *tmp = xstrdup(path);
|
||||
size_t n = strlen(tmp);
|
||||
size_t i;
|
||||
if (n == 0) {
|
||||
free(tmp);
|
||||
return 0;
|
||||
}
|
||||
while (n > 1 && tmp[n - 1] == '/')
|
||||
tmp[--n] = '\0';
|
||||
for (i = 1; i <= n; i++) {
|
||||
if (tmp[i] == '/' || tmp[i] == '\0') {
|
||||
char save = tmp[i];
|
||||
struct stat st;
|
||||
tmp[i] = '\0';
|
||||
if (stat(tmp, &st) != 0) {
|
||||
if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
|
||||
free(tmp);
|
||||
return -1;
|
||||
}
|
||||
} else if (!S_ISDIR(st.st_mode)) {
|
||||
free(tmp);
|
||||
return -1;
|
||||
}
|
||||
tmp[i] = save;
|
||||
}
|
||||
}
|
||||
free(tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_out_file(const char *path, const uint8_t *data, size_t len,
|
||||
uint32_t mode)
|
||||
{
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f)
|
||||
return -1;
|
||||
if (len > 0 && fwrite(data, 1, len, f) != len) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (fclose(f) != 0)
|
||||
return -1;
|
||||
if (mode)
|
||||
chmod(path, (mode_t)(mode & 07777));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int repair_archive(const char *prog, const char *archive,
|
||||
const char *outdir, int max_level, double budget,
|
||||
const char *only_entry, int verbose)
|
||||
{
|
||||
int is_lzrs = 0;
|
||||
FILE *f = NULL;
|
||||
uint8_t magic[4];
|
||||
|
||||
lzply_set_progname(prog);
|
||||
f = fopen(archive, "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "%s: cannot open %s: %s\n", prog, archive,
|
||||
strerror(errno));
|
||||
return EXIT_IO;
|
||||
}
|
||||
if (fread(magic, 1, 4, f) != 4) {
|
||||
fprintf(stderr, "%s: %s is too short to be an archive\n", prog, archive);
|
||||
fclose(f);
|
||||
return EXIT_CORRUPT;
|
||||
}
|
||||
if (memcmp(magic, "LZRS", 4) == 0)
|
||||
is_lzrs = 1;
|
||||
else if (memcmp(magic, "LZPL", 4) != 0) {
|
||||
fprintf(stderr, "%s: %s is not an LZPL or LZRS archive\n", prog, archive);
|
||||
fclose(f);
|
||||
return EXIT_CORRUPT;
|
||||
}
|
||||
/* Validate the archive up front (version, headers, end marker),
|
||||
* so a structurally broken archive fails before any repair is
|
||||
* attempted. The scan also gives the clear v4 message for old
|
||||
* LZPL archives. */
|
||||
{
|
||||
int version = 0;
|
||||
uint32_t count = 0;
|
||||
off_t size = 0, end_off = 0;
|
||||
scan_list_t entries;
|
||||
int s;
|
||||
rewind(f);
|
||||
if (is_lzrs)
|
||||
s = lzrs_scan_archive(archive, &version, &count, &size,
|
||||
&entries, &end_off);
|
||||
else
|
||||
s = scan_archive(archive, &version, &count, &size,
|
||||
&entries, &end_off);
|
||||
if (s != 0) {
|
||||
fclose(f);
|
||||
return EXIT_CORRUPT;
|
||||
}
|
||||
scan_list_free(&entries);
|
||||
rewind(f);
|
||||
}
|
||||
if (mkdir_p(outdir, 0755) != 0) {
|
||||
fprintf(stderr, "%s: cannot create output directory %s: %s\n",
|
||||
prog, outdir, strerror(errno));
|
||||
fclose(f);
|
||||
return EXIT_IO;
|
||||
}
|
||||
|
||||
/* Skip the 16-byte global header; then walk the entries. */
|
||||
if (fseeko(f, 16, SEEK_SET) != 0) {
|
||||
fprintf(stderr, "%s: cannot seek in %s: %s\n", prog, archive,
|
||||
strerror(errno));
|
||||
fclose(f);
|
||||
return EXIT_IO;
|
||||
}
|
||||
{
|
||||
int worst = 0;
|
||||
long n_ok = 0, n_fail = 0, n_dir = 0;
|
||||
long landed[4] = { 0, 0, 0, 0 };
|
||||
double t_start = bch_now();
|
||||
for (;;) {
|
||||
char *path = NULL;
|
||||
int level = 0, is_bch = 0, rs_level = 0, is_dir = 0;
|
||||
uint16_t poly_id = 0, want_crc16 = 0;
|
||||
uint64_t stored_size = 0, blob_size = 0, orig_size = 0;
|
||||
uint32_t mode = 0;
|
||||
uint8_t sha[32];
|
||||
uint8_t *stored = NULL;
|
||||
int r;
|
||||
if (is_lzrs) {
|
||||
lzrs_ehdr_t h;
|
||||
r = lzrs_read_entry_header(f, &h);
|
||||
if (r == 1)
|
||||
break;
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "%s: truncated or corrupt entry header\n",
|
||||
prog);
|
||||
worst = EXIT_CORRUPT;
|
||||
break;
|
||||
}
|
||||
path = h.path;
|
||||
level = h.level;
|
||||
is_bch = (h.flags & FLAG_BCH) != 0;
|
||||
rs_level = h.rs_level;
|
||||
is_dir = (h.flags & FLAG_ISDIR) != 0;
|
||||
poly_id = h.bch_poly_id;
|
||||
want_crc16 = h.crc16;
|
||||
stored_size = h.stored_size;
|
||||
blob_size = h.blob_size;
|
||||
orig_size = h.orig_size;
|
||||
mode = h.mode;
|
||||
memcpy(sha, h.sha, 32);
|
||||
} else {
|
||||
ehdr_t h;
|
||||
r = read_entry_header(f, &h);
|
||||
if (r == 1)
|
||||
break;
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "%s: truncated or corrupt entry header\n",
|
||||
prog);
|
||||
worst = EXIT_CORRUPT;
|
||||
break;
|
||||
}
|
||||
path = h.path;
|
||||
level = h.level;
|
||||
is_bch = (h.flags & FLAG_BCH) != 0;
|
||||
rs_level = 0;
|
||||
is_dir = (h.flags & FLAG_ISDIR) != 0;
|
||||
poly_id = (uint16_t)(h.rs_level |
|
||||
((uint16_t)h.reserved << 8));
|
||||
want_crc16 = h.crc16;
|
||||
stored_size = h.stored_size;
|
||||
blob_size = h.blob_size;
|
||||
orig_size = h.orig_size;
|
||||
mode = h.mode;
|
||||
memcpy(sha, h.sha, 32);
|
||||
}
|
||||
stored = xmalloc(stored_size ? (size_t)stored_size : 1);
|
||||
if (stored_size > 0 &&
|
||||
fread(stored, 1, (size_t)stored_size, f) !=
|
||||
(size_t)stored_size) {
|
||||
fprintf(stderr, "%s: truncated data for %s\n", prog, path);
|
||||
free(stored);
|
||||
free(path);
|
||||
worst = EXIT_CORRUPT;
|
||||
break;
|
||||
}
|
||||
if (only_entry && strcmp(path, only_entry) != 0) {
|
||||
free(stored);
|
||||
free(path);
|
||||
continue;
|
||||
}
|
||||
if (is_dir) {
|
||||
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
|
||||
sprintf(dp, "%s/%s", outdir, path);
|
||||
if (mkdir_p(dp, mode ? (mode_t)(mode & 07777) : 0755) != 0)
|
||||
fprintf(stderr, "%s: cannot create directory %s: %s\n",
|
||||
prog, dp, strerror(errno));
|
||||
free(dp);
|
||||
free(stored);
|
||||
free(path);
|
||||
n_dir++;
|
||||
continue;
|
||||
}
|
||||
{
|
||||
uint8_t *raw = NULL;
|
||||
size_t raw_len = 0;
|
||||
deep_stats_t st;
|
||||
int drc = deep_repair_entry(stored, (size_t)stored_size,
|
||||
level, is_bch, poly_id,
|
||||
rs_level, sha,
|
||||
(size_t)blob_size, want_crc16,
|
||||
max_level, budget,
|
||||
&raw, &raw_len, &st);
|
||||
free(stored);
|
||||
if (drc != 0) {
|
||||
printf("%s: FAILED (stage %s) after %.3fs, "
|
||||
"cands %ld crc-rejects %ld sha-rejects %ld\n",
|
||||
path, st.fail_stage, st.t_total,
|
||||
st.cands_tried, st.crc_rejects, st.sha_rejects);
|
||||
if (verbose) {
|
||||
int lv;
|
||||
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
|
||||
printf(" L%d: %.3fs trials %ld split %ld "
|
||||
"codewords %ld%s\n", lv, st.t_level[lv],
|
||||
st.trials[lv], st.split_pass[lv],
|
||||
st.codewords[lv],
|
||||
(lv == 1 && st.chase_cuda_used) ?
|
||||
" (CUDA)" : "");
|
||||
printf(" blocks %ld/%ld failed%s\n",
|
||||
st.blocks_failed, st.blocks_total,
|
||||
st.partial ? " (partial: budget hit)" : "");
|
||||
}
|
||||
n_fail++;
|
||||
if (EXIT_CORRUPT > worst)
|
||||
worst = EXIT_CORRUPT;
|
||||
free(path);
|
||||
continue;
|
||||
}
|
||||
if (raw_len != orig_size) {
|
||||
fprintf(stderr, "%s: %s: size mismatch after repair "
|
||||
"(got %zu, expected %llu)\n", prog, path,
|
||||
raw_len, (unsigned long long)orig_size);
|
||||
free(raw);
|
||||
free(path);
|
||||
n_fail++;
|
||||
if (EXIT_CORRUPT > worst)
|
||||
worst = EXIT_CORRUPT;
|
||||
continue;
|
||||
}
|
||||
{
|
||||
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
|
||||
char *pd;
|
||||
sprintf(dp, "%s/%s", outdir, path);
|
||||
pd = parent_dir(dp);
|
||||
if (mkdir_p(pd, 0755) != 0)
|
||||
fprintf(stderr, "%s: cannot create %s: %s\n",
|
||||
prog, pd, strerror(errno));
|
||||
free(pd);
|
||||
if (write_out_file(dp, raw, raw_len, mode) != 0) {
|
||||
fprintf(stderr, "%s: cannot write %s: %s\n",
|
||||
prog, dp, strerror(errno));
|
||||
if (EXIT_IO > worst)
|
||||
worst = EXIT_IO;
|
||||
free(dp);
|
||||
free(raw);
|
||||
free(path);
|
||||
continue;
|
||||
}
|
||||
free(dp);
|
||||
}
|
||||
{
|
||||
char rssuf[48];
|
||||
rssuf[0] = '\0';
|
||||
if (st.rs_corrected > 0)
|
||||
snprintf(rssuf, sizeof rssuf,
|
||||
" (RS corrected %d symbols)",
|
||||
st.rs_corrected);
|
||||
printf("%s: RECOVERED at L%d in %.3fs "
|
||||
"(L0 %.3fs, L1 %.3fs, L2 %.3fs, L3 %.3fs) "
|
||||
"cands %ld crc-rejects %ld sha-rejects %ld "
|
||||
"bch-corrected %d%s\n",
|
||||
path, st.level_landed, st.t_total,
|
||||
st.t_level[0], st.t_level[1], st.t_level[2],
|
||||
st.t_level[3], st.cands_tried, st.crc_rejects,
|
||||
st.sha_rejects, st.bch_corrected, rssuf);
|
||||
}
|
||||
if (verbose) {
|
||||
int lv;
|
||||
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
|
||||
printf(" L%d: %.3fs trials %ld split %ld "
|
||||
"codewords %ld%s\n", lv, st.t_level[lv],
|
||||
st.trials[lv], st.split_pass[lv],
|
||||
st.codewords[lv],
|
||||
(lv == 1 && st.chase_cuda_used) ?
|
||||
" (CUDA)" : "");
|
||||
printf(" blocks %ld/%ld failed%s\n",
|
||||
st.blocks_failed, st.blocks_total,
|
||||
st.partial ? " (partial: budget hit)" : "");
|
||||
}
|
||||
landed[st.level_landed]++;
|
||||
n_ok++;
|
||||
free(raw);
|
||||
free(path);
|
||||
}
|
||||
}
|
||||
printf("deep repair: %ld recovered (L0 %ld, L1 %ld, L2 %ld, "
|
||||
"L3 %ld), %ld failed, %ld directories, %.3fs total\n",
|
||||
n_ok, landed[0], landed[1], landed[2], landed[3],
|
||||
n_fail, n_dir, bch_now() - t_start);
|
||||
fclose(f);
|
||||
return worst;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/* repair_cmd.h - shared archive repair driver.
|
||||
*
|
||||
* repair_archive() is the archive-walking repair loop used by both
|
||||
* the lzply -r mode and the lzply_deep_repair binary: it validates
|
||||
* the archive, then attempts the escalating deep-repair ladder
|
||||
* (L0-L3) per entry, writing recovered files under outdir.
|
||||
* Returns the worst exit code: 0 all entries recovered, 2 I/O
|
||||
* error, 3 any entry unrecoverable (matching lzply's EXIT_*).
|
||||
*/
|
||||
#ifndef LZPLY_REPAIR_CMD_H
|
||||
#define LZPLY_REPAIR_CMD_H
|
||||
|
||||
int repair_archive(const char *prog, const char *archive,
|
||||
const char *outdir, int max_level, double budget,
|
||||
const char *only_entry, int verbose);
|
||||
|
||||
#endif /* LZPLY_REPAIR_CMD_H */
|
||||
@@ -186,6 +186,19 @@ sys.exit(1 if fails else 0)
|
||||
PYEOF
|
||||
if [ $? -ne 0 ]; then FAIL=$((FAIL+1)); echo "FAIL: old-format rejection section (see above)"; else PASS=$((PASS+1)); echo "PASS: old-format rejection section"; fi
|
||||
|
||||
# --- 2b. lzply -r smoke test (repair built into the main CLI) -----------
|
||||
"$LZPLY" -c -e 4 "$WORK/r512.bin" "$WORK/r-smoke.lzpl" >/dev/null
|
||||
SMOKESZ=$(stat -c %s "$WORK/r-smoke.lzpl")
|
||||
cp "$WORK/r-smoke.lzpl" "$WORK/r-smoke-corr.lzpl"
|
||||
"$CORRUPT_BITS" "$WORK/r-smoke-corr.lzpl" 4242 9 104 $((SMOKESZ - 2)) >/dev/null
|
||||
rm -rf "$WORK/r-smoke-out"
|
||||
if "$LZPLY" -r -o "$WORK/r-smoke-out" "$WORK/r-smoke-corr.lzpl" >/dev/null 2>&1 \
|
||||
&& cmp -s "$WORK/r512.bin" "$WORK/r-smoke-out/r512.bin"; then
|
||||
ok "lzply -r repairs 9-bit corruption (L1)"
|
||||
else
|
||||
bad "lzply -r repairs 9-bit corruption (L1)"
|
||||
fi
|
||||
|
||||
# --- 3. Corruption matrix -------------------------------------------------
|
||||
# repair_trial ARCHIVE ORIG DATA_OFF BLOB_END SEED NERR MODE BUDGET TRIALNO
|
||||
# MODE: scatter | burst1 | burst2 | conc1 (9 in block 1) | conc2 (9+9)
|
||||
@@ -281,7 +294,7 @@ run_cell() {
|
||||
|
||||
: > "$WORK/matrix-results.txt"
|
||||
: > "$WORK/matrix-notes.txt"
|
||||
echo "Corruption matrix (hot_newyork, $(date -u +%F %T UTC))" >> "$WORK/matrix-results.txt"
|
||||
echo "Corruption matrix (hot_newyork, $(date -u "+%F %T UTC"))" >> "$WORK/matrix-results.txt"
|
||||
echo "Tool: lzply_deep_repair -l 3; t=8 (-e 4) unless noted; scattered = corrupt_bits over the whole blob, burst = corrupt_run" >> "$WORK/matrix-results.txt"
|
||||
|
||||
# r512 -e 4: single block, m=13, nbits 4200. data offset 16+80+8=104, blob end = size-2.
|
||||
|
||||
@@ -120,7 +120,7 @@ mkcfg "$T/badpoly" 1 00ff 4
|
||||
mkvol "$T/rotest" 1 >/dev/null 2>&1
|
||||
echo "ro test content" > "$T/rotest/f.txt"
|
||||
"$HYPER" -c "$T/rotest/cfg.txt" write "$T/rotest/f.txt" f.txt >/dev/null 2>&1
|
||||
sed -i 's|/vol/s0.bin:rw:|/vol/s0.bin:ro:|' "$T/rotest/cfg.txt"
|
||||
sed 's|/vol/s0.bin:rw:|/vol/s0.bin:ro:|' "$T/rotest/cfg.txt" > "$T/rotest/cfg.txt.tmp" && mv "$T/rotest/cfg.txt.tmp" "$T/rotest/cfg.txt"
|
||||
"$HYPER" -c "$T/rotest/cfg.txt" write "$T/rotest/f.txt" f2.txt >/dev/null 2>&1 && \
|
||||
bad "config: write with an ro stripe rejected" || ok "config: write with an ro stripe rejected"
|
||||
"$HYPER" -c "$T/rotest/cfg.txt" read f.txt "$T/rotest/f.out" >/dev/null 2>&1 && \
|
||||
|
||||
Executable
+144
@@ -0,0 +1,144 @@
|
||||
#!/bin/bash
|
||||
# run_lzcsv_tests.sh - test suite for lzcsv (standalone CSV compressor).
|
||||
# Usage: run_lzcsv_tests.sh [path-to-lzcsv]
|
||||
# Follows the repo's test conventions (PASS/FAIL counters, ok/bad).
|
||||
# The corpus tests run only when the fftdict corpus is present
|
||||
# (/root/fftdict-shared/corpus2); otherwise they are skipped, and the
|
||||
# synthetic round-trips (generated here) still run. The oracle check
|
||||
# runs only when the Python COLT1 prototype is present.
|
||||
set -u
|
||||
LZCSV="${1:-./lzcsv}"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
WORK="$ROOT/testwork-lzcsv"
|
||||
CORPUS="/root/fftdict-shared/corpus2"
|
||||
HELD="/root/hot-qwen/lzcsv-heldout"
|
||||
PY="/root/hot-qwen/cycle1/transpose.py"
|
||||
PASS=0
|
||||
FAIL=0
|
||||
SKIP=0
|
||||
|
||||
ok() { PASS=$((PASS+1)); echo "PASS: $1"; }
|
||||
bad() { FAIL=$((FAIL+1)); echo "FAIL: $1"; }
|
||||
skip() { SKIP=$((SKIP+1)); echo "SKIP: $1"; }
|
||||
|
||||
rm -rf "$WORK"
|
||||
mkdir -p "$WORK"
|
||||
[ -x "$LZCSV" ] || { echo "lzcsv binary not found: $LZCSV"; exit 1; }
|
||||
|
||||
# --- 1. selftest + help ---------------------------------------------
|
||||
if "$LZCSV" selftest > "$WORK/selftest.log" 2>&1; then ok "selftest"; else bad "selftest"; cat "$WORK/selftest.log"; fi
|
||||
if "$LZCSV" --help > "$WORK/help.log" 2>&1; then ok "--help"; else bad "--help"; fi
|
||||
|
||||
# --- 2. synthetic round-trips (generated here, always run) -----------
|
||||
printf 'a,b,c\n1,2,3\n4,5,6\n' > "$WORK/synth1.csv"
|
||||
printf 'name,age,city\r\nann,34,oslo\r\nbob,29,bergen\r\n' > "$WORK/synth-crlf.csv"
|
||||
printf 'a,b,c\n1,2\n3,4,5,6,7\n8\n' > "$WORK/synth-ragged.csv"
|
||||
printf 'id,note\n1,"hello, world"\n2,"say ""hi"""\n3,plain "quoted" text\n' > "$WORK/synth-quotes.csv"
|
||||
printf 'x,y\n1,2\n3,4' > "$WORK/synth-noeol.csv"
|
||||
printf 'col1,col2,col3\n' > "$WORK/synth-header.csv"
|
||||
: > "$WORK/synth-empty.csv"
|
||||
for f in "$WORK"/synth*.csv; do
|
||||
n="$(basename "$f")"
|
||||
if "$LZCSV" c -f "$f" "$WORK/$n.lzcsv" > /dev/null 2>&1 && \
|
||||
"$LZCSV" d -f "$WORK/$n.lzcsv" "$WORK/$n.rt.csv" > /dev/null 2>&1 && \
|
||||
cmp -s "$f" "$WORK/$n.rt.csv"; then
|
||||
ok "round-trip $n"
|
||||
else
|
||||
bad "round-trip $n"
|
||||
fi
|
||||
done
|
||||
# in-memory test subcommand over the synthetic set
|
||||
if "$LZCSV" test "$WORK"/synth*.csv > "$WORK/test.log" 2>&1; then ok "lzcsv test (synthetic)"; else bad "lzcsv test (synthetic)"; cat "$WORK/test.log"; fi
|
||||
|
||||
# --- 3. corpus round-trips (only where the corpus exists) ------------
|
||||
if [ -d "$CORPUS" ]; then
|
||||
n=0; ok_n=0
|
||||
for f in "$CORPUS"/*.csv; do
|
||||
n=$((n+1))
|
||||
b="$(basename "$f")"
|
||||
if "$LZCSV" c -f "$f" "$WORK/$b.lzcsv" > /dev/null 2>&1 && \
|
||||
"$LZCSV" d -f "$WORK/$b.lzcsv" "$WORK/$b.rt.csv" > /dev/null 2>&1 && \
|
||||
cmp -s "$f" "$WORK/$b.rt.csv"; then
|
||||
ok_n=$((ok_n+1))
|
||||
else
|
||||
bad "corpus round-trip $b"
|
||||
fi
|
||||
done
|
||||
[ "$ok_n" = "$n" ] && ok "corpus round-trips $ok_n/$n"
|
||||
else
|
||||
skip "corpus round-trips (no $CORPUS)"
|
||||
fi
|
||||
|
||||
# --- 4. held-out round-trips (only where the set exists) --------------
|
||||
if [ -d "$HELD" ]; then
|
||||
n=0; ok_n=0
|
||||
for f in "$HELD"/*.csv; do
|
||||
b="$(basename "$f")"
|
||||
[ "$b" = "embedded-nl.csv" ] && continue
|
||||
n=$((n+1))
|
||||
if "$LZCSV" c -f "$f" "$WORK/h-$b.lzcsv" > /dev/null 2>&1 && \
|
||||
"$LZCSV" d -f "$WORK/h-$b.lzcsv" "$WORK/h-$b.rt.csv" > /dev/null 2>&1 && \
|
||||
cmp -s "$f" "$WORK/h-$b.rt.csv"; then
|
||||
ok_n=$((ok_n+1))
|
||||
else
|
||||
bad "held-out round-trip $b"
|
||||
fi
|
||||
done
|
||||
[ "$ok_n" = "$n" ] && ok "held-out round-trips $ok_n/$n"
|
||||
else
|
||||
skip "held-out round-trips (no $HELD)"
|
||||
fi
|
||||
|
||||
# --- 5. oracle: COLT1 output byte-identical to the Python prototype --
|
||||
if [ -f "$PY" ] && [ -d "$CORPUS" ]; then
|
||||
n=0; ok_n=0
|
||||
for f in "$CORPUS"/*.csv; do
|
||||
n=$((n+1))
|
||||
b="$(basename "$f")"
|
||||
python3 "$PY" enc "$f" "$WORK/$b.py.colt" 2>/dev/null
|
||||
"$LZCSV" raw-enc -f "$f" "$WORK/$b.c.colt" 2>/dev/null
|
||||
if cmp -s "$WORK/$b.py.colt" "$WORK/$b.c.colt"; then
|
||||
ok_n=$((ok_n+1))
|
||||
else
|
||||
bad "oracle raw-enc $b"
|
||||
fi
|
||||
# cross-decode both ways
|
||||
"$LZCSV" raw-dec -f "$WORK/$b.py.colt" "$WORK/$b.c-dec.csv" 2>/dev/null
|
||||
python3 "$PY" dec "$WORK/$b.c.colt" "$WORK/$b.py-dec.csv" 2>/dev/null
|
||||
cmp -s "$WORK/$b.c-dec.csv" "$f" || bad "C raw-dec of Python .colt $b"
|
||||
cmp -s "$WORK/$b.py-dec.csv" "$f" || bad "Python dec of C .colt $b"
|
||||
done
|
||||
[ "$ok_n" = "$n" ] && ok "oracle raw-enc byte-identical $ok_n/$n"
|
||||
else
|
||||
skip "oracle check (no prototype or no corpus)"
|
||||
fi
|
||||
|
||||
# --- 6. clean rejections ----------------------------------------------
|
||||
printf 'id,note\n1,"first line\nsecond line"\n2,plain\n' > "$WORK/embedded.csv"
|
||||
if "$LZCSV" c "$WORK/embedded.csv" "$WORK/embedded.lzcsv" > /dev/null 2>&1; then
|
||||
bad "embedded-newline input must be rejected"
|
||||
else
|
||||
ok "embedded-newline input rejected cleanly"
|
||||
fi
|
||||
# truncated input
|
||||
if [ -f "$WORK/synth1.csv.lzcsv" ]; then
|
||||
head -c 20 "$WORK/synth1.csv.lzcsv" > "$WORK/trunc.lzcsv"
|
||||
if "$LZCSV" d "$WORK/trunc.lzcsv" "$WORK/trunc.csv" > /dev/null 2>&1; then
|
||||
bad "truncated input must fail"
|
||||
else
|
||||
ok "truncated input fails cleanly"
|
||||
fi
|
||||
printf 'NOTLZCSV' > "$WORK/badmagic.lzcsv"
|
||||
cat "$WORK/synth1.csv.lzcsv" >> "$WORK/badmagic.lzcsv"
|
||||
if "$LZCSV" d "$WORK/badmagic.lzcsv" "$WORK/bm.csv" > /dev/null 2>&1; then
|
||||
bad "bad-magic input must fail"
|
||||
else
|
||||
ok "bad-magic input fails cleanly"
|
||||
fi
|
||||
fi
|
||||
# info works
|
||||
if "$LZCSV" info "$WORK/synth1.csv.lzcsv" > /dev/null 2>&1; then ok "info"; else bad "info"; fi
|
||||
|
||||
echo "lzcsv tests: $PASS passed, $FAIL failed, $SKIP skipped"
|
||||
[ "$FAIL" = 0 ]
|
||||
Reference in new issue
Block a user