Debian Codegraph @

CLAUDE.md

raw
1 # CLAUDE.md
2
3 This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
5 ## Project Overview
6
7 desync is a Go library and CLI tool that re-implements [casync](https://github.com/systemd/casync) features for content-addressed binary distribution. It chunks large files using a rolling hash, deduplicates and compresses chunks (zstd), and distributes them via multiple store backends. Chunks are identified by SHA512/256 checksums (`ChunkID [32]byte`).
8
9 ## Build and Test Commands
10
11 ```bash
12 # Build the CLI binary (output into cmd/desync/)
13 go build -o cmd/desync/ ./cmd/desync
14
15 # Run all tests (library + CLI)
16 go test ./...
17
18 # Run library tests only
19 go test
20
21 # Run CLI tests only
22 go test ./cmd/desync
23
24 # Run a single test
25 go test -run TestFunctionName
26 go test ./cmd/desync -run TestFunctionName
27
28 # Install the binary
29 go install ./cmd/desync
30
31 # Format code (enforced by pre-commit)
32 go fmt ./...
33
34 # Tidy modules (enforced by pre-commit)
35 go mod tidy
36 ```
37
38 CI runs `go test` and `go build ./cmd/desync` on ubuntu, windows, and macOS.
39
40 ## Architecture
41
42 ### Core Interfaces (store.go)
43
44 The system is built around composable store interfaces:
45
46 - **`Store`** — read-only: `GetChunk(ChunkID)`, `HasChunk(ChunkID)`
47 - **`WriteStore`** — adds `StoreChunk(*Chunk)`
48 - **`PruneStore`** — adds `Prune(ctx, ids)`
49 - **`IndexStore`** — read indexes: `GetIndexReader(name)`, `GetIndex(name)`
50 - **`IndexWriteStore`** — adds `StoreIndex(name, idx)`
51
52 ### Store Implementations
53
54 Each backend implements the store interfaces:
55 - **LocalStore** (`local.go`) — filesystem-based chunk storage
56 - **RemoteHTTP** (`remotehttp.go`) — HTTP(S) with TLS/mutual auth support
57 - **S3Store** (`s3.go`) — S3-compatible storage (AWS, MinIO)
58 - **SFTPStore** (`sftp.go`) — SFTP over SSH
59 - **RemoteSSH** (`remotessh.go`) — casync protocol over SSH (read-only)
60 - **GCStore** (`gcs.go`) — Google Cloud Storage
61
62 ### Store Composition
63
64 Stores are composed for routing, caching, and failover:
65 - **StoreRouter** (`storerouter.go`) — tries multiple stores in order
66 - **FailoverGroup** (`failover.go`) — failover with active store rotation
67 - **Cache** (`cache.go`) — local (fast) + remote (slow) with auto-caching
68 - **RepairableCache** — converts ChunkInvalid to ChunkMissing for self-repair
69
70 ### Data Pipeline
71
72 **Chunking:** `Chunker` (`chunker.go`) uses a rolling hash (SipHash, 48-byte window) for content-defined chunking with configurable min/avg/max sizes (default 16KB/64KB/256KB).
73
74 **Converter pipeline** (`converter.go`): Layered data transformations applied in order for writes, reverse for reads: compression (zstd via `Compressor`) and optional encryption (`encrypt.go`, XChaCha20-Poly1305 or AES-256-GCM). Converter stacks determine the chunk file extension via `storageExtension()`.
75
76 **Assembly:** `AssembleFile()` (`assemble.go`) reconstructs files from an index and chunk stores, supporting self-seeding and file seeds for efficient cloning with reflink support (Btrfs/XFS).
77
78 ### Index Format
79
80 Index files (`.caibx`/`.caidx`) contain a table of `IndexChunk` entries mapping `ChunkID` to byte offsets and sizes. Parsed in `index.go`.
81
82 ### Seeds
83
84 Seeds optimize extraction by reusing data from existing files:
85 - **FileSeed** (`fileseed.go`) — existing file + its index
86 - **SelfSeed** (`selfseed.go`) — file being written seeds later chunks
87 - **NullSeed** (`nullseed.go`) — all-zero chunk optimization
88
89 ### CLI Structure (cmd/desync/)
90
91 Uses `cobra` for command framework. Key commands: `extract`, `make`, `tar`, `untar`, `chop`, `cache`, `verify`, `chunk-server`, `index-server`, `mount-index`, `prune`, `info`, `cat`.
92
93 Store factory in `cmd/desync/store.go` creates store instances from URL/path strings. Multiple stores are specified via CLI flags; failover groups use `|` separator.
94
95 ### Configuration
96
97 Config file at `$HOME/.config/desync/config.json` for S3 credentials (per-endpoint with glob patterns), store options, and TLS settings. Long-running processes (chunk-server, mount-index) support dynamic store config via JSON files reloaded on SIGHUP.
98
99 Key environment variables: `S3_ACCESS_KEY`, `S3_SECRET_KEY`, `S3_REGION`, `CASYNC_SSH_PATH`, `DESYNC_HTTP_AUTH`.
100
101 ## Key Patterns
102
103 - **Interface-driven composition** — small focused interfaces composed via routers/caches
104 - **Lazy evaluation** — chunks decompress only when plain data is accessed
105 - **Context-based cancellation** — goroutine lifecycle via `context.Context`
106 - **`t.Fatal()` restriction** — do not call `t.Fatal()`/`t.FailNow()` from non-main goroutines (see PR #291)
107 - **Avoid recompression** — if a chunk already has compressed form, don't recompress (see PR #289)
108
109 ## Code Style
110
111 - **Tests use testify** — write new (or modified) Go tests with `github.com/stretchr/testify`: `require` for fatal checks, `assert` for non-fatal. Don't hand-roll `if … { t.Fatalf(…) }` conditionals. Existing plain-`testing` tests don't need a mass rewrite — apply this to new tests and tests you're already changing. Note: `require.*` calls `FailNow()`, so per the non-main-goroutine `t.Fatal()` restriction in Key Patterns, only use `require.*` from the test goroutine — in spawned goroutines use `assert.*` or pass errors back over a channel.
112 - **Build into the main package's directory** — always `go build -o cmd/desync/ ./cmd/desync`, never a bare `go build ./cmd/desync`. The bare form drops an untracked `desync` binary in the repo root (it is not git-ignored). Build artifacts belong next to their `main` package, never the project root.
113
114 ## Module
115
116 Module path: `github.com/folbricht/desync`, Go 1.24.0.