Compare commits
12 Commits
improvemen
...
golangci-v
| Author | SHA1 | Date | |
|---|---|---|---|
| a5d4cd6c13 | |||
| 7f75f2ee72 | |||
| 6230bb1c3a | |||
| 975116f13d | |||
| 1bf1c892f4 | |||
| 629613de1b | |||
| 5c2338d590 | |||
| 9f86bf1dc1 | |||
| 2e44e5bb78 | |||
| b9d65115c2 | |||
| 144d2de243 | |||
| d848c5e51b |
9
.gitea/workflows/check.yml
Normal file
9
.gitea/workflows/check.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
name: check
|
||||
on: [push]
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
# actions/checkout v4.2.2, 2026-02-28
|
||||
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
|
||||
- run: script/cibuild
|
||||
34
.golangci.yml
Normal file
34
.golangci.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
version: "2"
|
||||
|
||||
# Config schema uses the golangci-lint v2 layout (settings live under
|
||||
# linters.settings, not top-level linters-settings) so that the
|
||||
# thresholds below are actually applied by golangci-lint >= v2.
|
||||
|
||||
run:
|
||||
timeout: 5m
|
||||
modules-download-mode: readonly
|
||||
|
||||
linters:
|
||||
default: all
|
||||
disable:
|
||||
# Genuinely incompatible with project patterns
|
||||
- exhaustruct # Requires all struct fields
|
||||
- depguard # Dependency allow/block lists
|
||||
- godot # Requires comments to end with periods
|
||||
- wsl # Deprecated, replaced by wsl_v5
|
||||
- wrapcheck # Too verbose for internal packages
|
||||
- varnamelen # Short names like db, id are idiomatic Go
|
||||
settings:
|
||||
lll:
|
||||
line-length: 88
|
||||
funlen:
|
||||
lines: 80
|
||||
statements: 50
|
||||
cyclop:
|
||||
max-complexity: 15
|
||||
dupl:
|
||||
threshold: 100
|
||||
|
||||
issues:
|
||||
max-issues-per-linter: 0
|
||||
max-same-issues: 0
|
||||
44
Dockerfile
Normal file
44
Dockerfile
Normal file
@@ -0,0 +1,44 @@
|
||||
# Build stage
|
||||
# golang 1.25-alpine, 2026-02-28
|
||||
FROM golang@sha256:f6751d823c26342f9506c03797d2527668d095b0a15f1862cddb4d927a7a4ced AS builder
|
||||
|
||||
RUN apk add --no-cache git make gcc musl-dev binutils-gold
|
||||
|
||||
# golangci-lint v2.12.2, 2026-08-07
|
||||
RUN go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2
|
||||
# goimports v0.42.0
|
||||
RUN go install golang.org/x/tools/cmd/goimports@009367f5c17a8d4c45a961a3a509277190a9a6f0
|
||||
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Run the checks as an unprivileged user. Root bypasses file mode bits, which
|
||||
# would make the permission tests (expecting EACCES on a 0000 file) spuriously
|
||||
# pass with no error. Caches live under /tmp (world-writable) so the user needs
|
||||
# no home directory of its own.
|
||||
ENV GOCACHE=/tmp/gocache
|
||||
ENV XDG_CACHE_HOME=/tmp/xdgcache
|
||||
RUN adduser -D -u 1000 builder && chown -R builder:builder /src /go
|
||||
USER builder
|
||||
|
||||
# Run all checks - build fails if any check fails
|
||||
RUN make check
|
||||
|
||||
# Build the binary (still as the unprivileged user: it owns /src, so git VCS
|
||||
# stamping sees consistent ownership).
|
||||
RUN make build
|
||||
|
||||
# Runtime stage
|
||||
# alpine 3.21, 2026-02-28
|
||||
FROM alpine@sha256:c3f8e73fdb79deaebaa2037150150191b9dcbfba68b4a46d70103204c53f4709
|
||||
|
||||
RUN apk add --no-cache ca-certificates tzdata
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /src/attrsum /app/attrsum
|
||||
|
||||
ENTRYPOINT ["/app/attrsum"]
|
||||
34
Makefile
34
Makefile
@@ -1,9 +1,38 @@
|
||||
.PHONY: default bootstrap setup test lint fmt fmt-check check docker hooks build clean try
|
||||
|
||||
TESTDIR := $(HOME)/Documents/_SYSADMIN/cyberdyne
|
||||
|
||||
default: test
|
||||
# Standard targets are thin shims; the implementations live in script/
|
||||
# per the scripts-to-rule-them-all pattern.
|
||||
|
||||
default: check build
|
||||
|
||||
bootstrap:
|
||||
@script/bootstrap
|
||||
|
||||
setup:
|
||||
@script/setup
|
||||
|
||||
test:
|
||||
@go test ./... -v
|
||||
@script/test
|
||||
|
||||
lint:
|
||||
@script/lint
|
||||
|
||||
fmt:
|
||||
@script/fmt
|
||||
|
||||
fmt-check:
|
||||
@script/fmt-check
|
||||
|
||||
check:
|
||||
@script/check
|
||||
|
||||
docker:
|
||||
@script/docker
|
||||
|
||||
hooks:
|
||||
@script/install-precommit
|
||||
|
||||
build: clean
|
||||
@go build .
|
||||
@@ -21,4 +50,3 @@ try: build
|
||||
touch $(TESTDIR)/*
|
||||
./attrsum sum update -v $(TESTDIR)
|
||||
./attrsum check -v $(TESTDIR)
|
||||
|
||||
|
||||
@@ -55,8 +55,8 @@ attrsum -q sum add DIR
|
||||
|
||||
| xattr key | meaning |
|
||||
|---------------------------------------------|--------------------------------|
|
||||
| `berlin.sneak.app.attrsum.checksum` | base-58 multihash (sha2-256) |
|
||||
| `berlin.sneak.app.attrsum.sumtime` | RFC 3339 timestamp of checksum |
|
||||
| `user.berlin.sneak.app.attrsum.checksum` | base-58 multihash (sha2-256) |
|
||||
| `user.berlin.sneak.app.attrsum.sumtime` | RFC 3339 timestamp of checksum |
|
||||
|
||||
Flags:
|
||||
|
||||
|
||||
60
TODO.md
Normal file
60
TODO.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# Workflow
|
||||
|
||||
* branch (from `main`)
|
||||
* do the work in Next Step
|
||||
* move Next Step to the top of Completed Steps
|
||||
* move the top item of Future Steps into Next Step
|
||||
* commit (`TODO.md` changes in the same commit as the work)
|
||||
* merge to `main` if the branch is not protected, otherwise open a PR
|
||||
* push
|
||||
|
||||
# Status
|
||||
|
||||
1.0+
|
||||
|
||||
Tagged 1.0.0 (2025-05-08). Substantial correctness fixes and features
|
||||
have landed since the tag.
|
||||
|
||||
# Next Step
|
||||
|
||||
Policy scaffold commit: add LICENSE, REPO_POLICIES.md, .editorconfig,
|
||||
.golangci.yml, and a comprehensive .gitignore (currently only the
|
||||
attrsum binary), and extend the Makefile (only test/build/clean/try
|
||||
today) with lint, fmt, fmt-check, check, and hooks targets. Fix the try
|
||||
target to use a temp fixture instead of the hardcoded
|
||||
$(HOME)/Documents/_SYSADMIN/cyberdyne path.
|
||||
|
||||
# Completed Steps
|
||||
|
||||
* 2026-08-07: updated golangci-lint to v2.12.2: canonical
|
||||
`.golangci.yml` (settings moved under `linters.settings` so the
|
||||
configured thresholds actually apply), version pins bumped in
|
||||
`Dockerfile` and `script/bootstrap`, and long lines wrapped to
|
||||
satisfy the now-effective `lll` limit of 88
|
||||
* 2026-02-02: correctness pass: track actual bytes read instead of
|
||||
stale file size, atomic failure tracking in ProcessCheck, detect
|
||||
file modification during checksum (TOCTOU), propagate countFiles
|
||||
errors, single progress bar across paths, error on empty stdin,
|
||||
dead code removal
|
||||
* 2026-02-01: added quiet mode, progress bar, summary report, and stdin
|
||||
path input; multiple file/directory arguments for all commands
|
||||
* 2025-07-12: README update
|
||||
* 2025-05-08: initial working tool with passing tests, skips
|
||||
non-regular files, Makefile, README; tagged 1.0.0
|
||||
|
||||
# Future Steps
|
||||
|
||||
* Add Dockerfile and .dockerignore that run make check, images pinned
|
||||
by sha256, plus a Makefile docker target
|
||||
* Add .gitea/workflows/check.yml
|
||||
* Restructure README.md into the standard sections: Description,
|
||||
Getting Started, Rationale, Design, TODO, License, Author (Getting
|
||||
Started, Why?, TODO, License exist; Description, Design, Author are
|
||||
missing)
|
||||
* Tag a patch release to ship the 2026-02-02 correctness fixes
|
||||
* Dry-run mode (--dry-run, -n): show what would be done without making
|
||||
changes (from README TODO)
|
||||
* JSON output (--json) for scripting and integration (from README TODO)
|
||||
* Parallel processing (-j N) with multiple goroutines for faster
|
||||
checksumming on large trees (from README TODO)
|
||||
* Formalize and document exit codes for scripting (from README TODO)
|
||||
692
attrsum.go
692
attrsum.go
@@ -1,3 +1,5 @@
|
||||
// Command attrsum computes and verifies file checksums stored in
|
||||
// extended attributes.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -23,18 +25,36 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
checksumKey = "berlin.sneak.app.attrsum.checksum"
|
||||
sumTimeKey = "berlin.sneak.app.attrsum.sumtime"
|
||||
// The keys live in the user.* namespace so they are settable on Linux,
|
||||
// where regular-file xattrs outside user.*/trusted.*/security.*/system.*
|
||||
// are rejected with EOPNOTSUPP. macOS treats the whole string as an
|
||||
// opaque attribute name, so the same keys work there too.
|
||||
checksumKey = "user.berlin.sneak.app.attrsum.checksum"
|
||||
sumTimeKey = "user.berlin.sneak.app.attrsum.sumtime"
|
||||
|
||||
// progressThrottle is how often the progress bar repaints.
|
||||
progressThrottle = 250 * time.Millisecond
|
||||
)
|
||||
|
||||
// Sentinel errors returned by the command implementations.
|
||||
var (
|
||||
errNoPaths = errors.New("no paths provided")
|
||||
errNoPathsStdin = errors.New("no paths provided on stdin")
|
||||
errVerification = errors.New("verification failed")
|
||||
errModifiedDuringSum = errors.New("file modified during checksum calculation")
|
||||
)
|
||||
|
||||
// options holds the parsed command-line flag state that is shared across
|
||||
// the sum/check/clear operations. It replaces package-level globals so the
|
||||
// behaviour is explicit and the code is safe to exercise concurrently.
|
||||
type options struct {
|
||||
verbose bool
|
||||
quiet bool
|
||||
excludePatterns []string
|
||||
excludeDotfiles bool
|
||||
)
|
||||
}
|
||||
|
||||
// Stats tracks operation statistics for summary reporting
|
||||
// Stats tracks operation statistics for summary reporting.
|
||||
type Stats struct {
|
||||
FilesProcessed int64
|
||||
FilesSkipped int64
|
||||
@@ -47,11 +67,13 @@ func (s *Stats) Duration() time.Duration {
|
||||
return time.Since(s.StartTime)
|
||||
}
|
||||
|
||||
func (s *Stats) Print(operation string) {
|
||||
if quiet {
|
||||
func (s *Stats) Print(opts *options, operation string) {
|
||||
if opts.quiet {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "\n%s complete: %d files processed, %d skipped, %d failed, %s bytes in %s\n",
|
||||
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"\n%s complete: %d files processed, %d skipped, %d failed, %s bytes in %s\n",
|
||||
operation,
|
||||
s.FilesProcessed,
|
||||
s.FilesSkipped,
|
||||
@@ -66,15 +88,19 @@ func formatBytes(b int64) string {
|
||||
if b < unit {
|
||||
return fmt.Sprintf("%d B", b)
|
||||
}
|
||||
|
||||
div, exp := int64(unit), 0
|
||||
for n := b / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
func main() {
|
||||
opts := &options{}
|
||||
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "attrsum",
|
||||
Short: "Compute and verify file checksums via xattrs",
|
||||
@@ -82,47 +108,130 @@ func main() {
|
||||
rootCmd.SilenceUsage = true
|
||||
rootCmd.SilenceErrors = true
|
||||
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "suppress all output except errors")
|
||||
rootCmd.PersistentFlags().StringArrayVar(&excludePatterns, "exclude", nil, "exclude files/directories matching pattern (rsync-style, repeatable)")
|
||||
rootCmd.PersistentFlags().BoolVar(&excludeDotfiles, "exclude-dotfiles", false, "exclude any file or directory whose name starts with '.'")
|
||||
flags := rootCmd.PersistentFlags()
|
||||
flags.BoolVarP(&opts.verbose, "verbose", "v", false, "enable verbose output")
|
||||
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "suppress all output except errors")
|
||||
flags.StringArrayVar(&opts.excludePatterns, "exclude", nil,
|
||||
"exclude files/directories matching pattern (rsync-style, repeatable)")
|
||||
flags.BoolVar(&opts.excludeDotfiles, "exclude-dotfiles", false,
|
||||
"exclude any file or directory whose name starts with '.'")
|
||||
|
||||
rootCmd.AddCommand(newSumCmd())
|
||||
rootCmd.AddCommand(newCheckCmd())
|
||||
rootCmd.AddCommand(newClearCmd())
|
||||
rootCmd.AddCommand(newSumCmd(opts))
|
||||
rootCmd.AddCommand(newCheckCmd(opts))
|
||||
rootCmd.AddCommand(newClearCmd(opts))
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// expandPaths expands the given paths, reading from stdin if "-" is present
|
||||
// expandPaths expands the given paths, reading from stdin if "-" is present.
|
||||
func expandPaths(args []string) ([]string, error) {
|
||||
var paths []string
|
||||
|
||||
readFromStdin := false
|
||||
|
||||
for _, arg := range args {
|
||||
if arg == "-" {
|
||||
if arg != "-" {
|
||||
paths = append(paths, arg)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
readFromStdin = true
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line != "" {
|
||||
paths = append(paths, line)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
||||
err := scanner.Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading stdin: %w", err)
|
||||
}
|
||||
} else {
|
||||
paths = append(paths, arg)
|
||||
}
|
||||
|
||||
if len(paths) == 0 {
|
||||
if readFromStdin {
|
||||
return nil, errNoPathsStdin
|
||||
}
|
||||
|
||||
return nil, errNoPaths
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
// processFunc processes a single path within a command's run loop.
|
||||
type processFunc func(
|
||||
opts *options, path string, stats *Stats, bar *progressbar.ProgressBar,
|
||||
) error
|
||||
|
||||
// countAndBar counts the files under paths and returns a progress bar sized
|
||||
// to that total. It always returns either a non-nil bar or a non-nil error.
|
||||
func countAndBar(
|
||||
opts *options, paths []string, desc string,
|
||||
) (*progressbar.ProgressBar, error) {
|
||||
total, err := countFilesMultiple(opts, paths)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newProgressBar(total, desc), nil
|
||||
}
|
||||
|
||||
// finishBar finalizes a progress bar if one is present.
|
||||
func finishBar(bar *progressbar.ProgressBar) {
|
||||
if bar != nil {
|
||||
_ = bar.Finish()
|
||||
}
|
||||
}
|
||||
|
||||
// runOverPaths runs process over each path, sharing the progress/stats
|
||||
// bookkeeping common to the sum-add, sum-update and clear commands.
|
||||
func runOverPaths(
|
||||
opts *options, args []string, desc, op string, process processFunc,
|
||||
) error {
|
||||
paths, err := expandPaths(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stats := &Stats{StartTime: time.Now()}
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
|
||||
if !opts.quiet {
|
||||
bar, err = countAndBar(opts, paths, desc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range paths {
|
||||
perr := process(opts, p, stats, bar)
|
||||
if perr != nil {
|
||||
finishBar(bar)
|
||||
|
||||
return perr
|
||||
}
|
||||
}
|
||||
|
||||
finishBar(bar)
|
||||
stats.Print(opts, op)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Sum commands
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newSumCmd() *cobra.Command {
|
||||
func newSumCmd(opts *options) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "sum",
|
||||
Short: "Checksum maintenance operations",
|
||||
@@ -133,18 +242,7 @@ func newSumCmd() *cobra.Command {
|
||||
Short: "Write checksums for files missing them",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(_ *cobra.Command, a []string) error {
|
||||
paths, err := expandPaths(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stats := &Stats{StartTime: time.Now()}
|
||||
for _, p := range paths {
|
||||
if err := ProcessSumAdd(p, stats); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
stats.Print("sum add")
|
||||
return nil
|
||||
return runOverPaths(opts, a, "Adding checksums", "sum add", processSumAdd)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -153,76 +251,106 @@ func newSumCmd() *cobra.Command {
|
||||
Short: "Recalculate checksum when file newer than stored sumtime",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(_ *cobra.Command, a []string) error {
|
||||
paths, err := expandPaths(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stats := &Stats{StartTime: time.Now()}
|
||||
for _, p := range paths {
|
||||
if err := ProcessSumUpdate(p, stats); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
stats.Print("sum update")
|
||||
return nil
|
||||
return runOverPaths(opts, a, "Updating checksums", "sum update", processSumUpdate)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.AddCommand(add, upd)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func ProcessSumAdd(dir string, stats *Stats) error {
|
||||
return walkAndProcess(dir, stats, "Adding checksums", func(p string, info os.FileInfo, s *Stats) error {
|
||||
func processSumAdd(
|
||||
opts *options, dir string, stats *Stats, bar *progressbar.ProgressBar,
|
||||
) error {
|
||||
return walkAndProcess(opts, dir, stats, bar,
|
||||
func(p string, info os.FileInfo, s *Stats) error {
|
||||
if hasXattr(p, checksumKey) {
|
||||
atomic.AddInt64(&s.FilesSkipped, 1)
|
||||
|
||||
return nil
|
||||
}
|
||||
if err := writeChecksumAndTime(p, info, s); err != nil {
|
||||
|
||||
err := writeChecksumAndTime(opts, p, info, s)
|
||||
if err != nil {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func ProcessSumUpdate(dir string, stats *Stats) error {
|
||||
return walkAndProcess(dir, stats, "Updating checksums", func(p string, info os.FileInfo, s *Stats) error {
|
||||
func processSumUpdate(
|
||||
opts *options, dir string, stats *Stats, bar *progressbar.ProgressBar,
|
||||
) error {
|
||||
return walkAndProcess(opts, dir, stats, bar,
|
||||
func(p string, info os.FileInfo, s *Stats) error {
|
||||
t, err := readSumTime(p)
|
||||
if err != nil || info.ModTime().After(t) {
|
||||
if err := writeChecksumAndTime(p, info, s); err != nil {
|
||||
werr := writeChecksumAndTime(opts, p, info, s)
|
||||
if werr != nil {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
return err
|
||||
|
||||
return werr
|
||||
}
|
||||
} else {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
atomic.AddInt64(&s.FilesSkipped, 1)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func writeChecksumAndTime(path string, info os.FileInfo, stats *Stats) error {
|
||||
hash, err := fileMultihash(path)
|
||||
func writeChecksumAndTime(
|
||||
opts *options, path string, info os.FileInfo, stats *Stats,
|
||||
) error {
|
||||
// Record mtime before hashing to detect modifications during hash.
|
||||
mtimeBefore := info.ModTime()
|
||||
|
||||
hash, bytesRead, err := fileMultihash(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := xattr.Set(path, checksumKey, hash); err != nil {
|
||||
return fmt.Errorf("set checksum attr: %w", err)
|
||||
}
|
||||
if verbose && !quiet {
|
||||
fmt.Printf("%s %s written\n", path, hash)
|
||||
|
||||
// Check if file was modified during hashing.
|
||||
infoAfter, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stat after hash: %w", err)
|
||||
}
|
||||
|
||||
ts := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
if err := xattr.Set(path, sumTimeKey, []byte(ts)); err != nil {
|
||||
if !infoAfter.ModTime().Equal(mtimeBefore) {
|
||||
return fmt.Errorf("%s: %w", path, errModifiedDuringSum)
|
||||
}
|
||||
|
||||
err = xattr.Set(path, checksumKey, hash)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set checksum attr: %w", err)
|
||||
}
|
||||
|
||||
if opts.verbose && !opts.quiet {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s %s written\n", path, hash)
|
||||
}
|
||||
|
||||
// Store the file's mtime as sumtime (not wall-clock time). This makes
|
||||
// update comparisons semantically correct.
|
||||
ts := mtimeBefore.UTC().Format(time.RFC3339Nano)
|
||||
|
||||
err = xattr.Set(path, sumTimeKey, []byte(ts))
|
||||
if err != nil {
|
||||
return fmt.Errorf("set sumtime attr: %w", err)
|
||||
}
|
||||
if verbose && !quiet {
|
||||
fmt.Printf("%s %s written\n", path, ts)
|
||||
|
||||
if opts.verbose && !opts.quiet {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s %s written\n", path, ts)
|
||||
}
|
||||
|
||||
atomic.AddInt64(&stats.FilesProcessed, 1)
|
||||
atomic.AddInt64(&stats.BytesProcessed, info.Size())
|
||||
atomic.AddInt64(&stats.BytesProcessed, bytesRead)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -231,6 +359,7 @@ func readSumTime(path string) (time.Time, error) {
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
return time.Parse(time.RFC3339Nano, string(b))
|
||||
}
|
||||
|
||||
@@ -238,206 +367,290 @@ func readSumTime(path string) (time.Time, error) {
|
||||
// Clear command
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newClearCmd() *cobra.Command {
|
||||
func newClearCmd(opts *options) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "clear <path>... (use - to read paths from stdin)",
|
||||
Short: "Remove checksum xattrs from tree",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(_ *cobra.Command, a []string) error {
|
||||
paths, err := expandPaths(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stats := &Stats{StartTime: time.Now()}
|
||||
for _, p := range paths {
|
||||
if err := ProcessClear(p, stats); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
stats.Print("clear")
|
||||
return nil
|
||||
return runOverPaths(opts, a, "Clearing checksums", "clear", processClear)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ProcessClear(dir string, stats *Stats) error {
|
||||
return walkAndProcess(dir, stats, "Clearing checksums", func(p string, info os.FileInfo, s *Stats) error {
|
||||
cleared := false
|
||||
for _, k := range []string{checksumKey, sumTimeKey} {
|
||||
v, err := xattr.Get(p, k)
|
||||
func processClear(
|
||||
opts *options, dir string, stats *Stats, bar *progressbar.ProgressBar,
|
||||
) error {
|
||||
return walkAndProcess(opts, dir, stats, bar,
|
||||
func(p string, info os.FileInfo, s *Stats) error {
|
||||
cleared, err := clearOne(opts, p)
|
||||
if err != nil {
|
||||
if errors.Is(err, xattr.ENOATTR) {
|
||||
continue
|
||||
}
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
|
||||
return err
|
||||
}
|
||||
if verbose && !quiet {
|
||||
fmt.Printf("%s %s removed\n", p, string(v))
|
||||
}
|
||||
if err := xattr.Remove(p, k); err != nil {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
return err
|
||||
}
|
||||
cleared = true
|
||||
}
|
||||
|
||||
if cleared {
|
||||
atomic.AddInt64(&s.FilesProcessed, 1)
|
||||
atomic.AddInt64(&s.BytesProcessed, info.Size())
|
||||
} else {
|
||||
atomic.AddInt64(&s.FilesSkipped, 1)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// clearOne removes both checksum xattrs from a single path, reporting
|
||||
// whether any attribute was actually removed.
|
||||
func clearOne(opts *options, p string) (bool, error) {
|
||||
cleared := false
|
||||
|
||||
for _, k := range []string{checksumKey, sumTimeKey} {
|
||||
v, err := xattr.Get(p, k)
|
||||
if err != nil {
|
||||
if errors.Is(err, xattr.ENOATTR) {
|
||||
continue
|
||||
}
|
||||
|
||||
return cleared, err
|
||||
}
|
||||
|
||||
if opts.verbose && !opts.quiet {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s %s removed\n", p, string(v))
|
||||
}
|
||||
|
||||
rerr := xattr.Remove(p, k)
|
||||
if rerr != nil {
|
||||
return cleared, rerr
|
||||
}
|
||||
|
||||
cleared = true
|
||||
}
|
||||
|
||||
return cleared, nil
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Check command
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newCheckCmd() *cobra.Command {
|
||||
func newCheckCmd(opts *options) *cobra.Command {
|
||||
var cont bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "check <path>... (use - to read paths from stdin)",
|
||||
Short: "Verify stored checksums",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(_ *cobra.Command, a []string) error {
|
||||
paths, err := expandPaths(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stats := &Stats{StartTime: time.Now()}
|
||||
var finalErr error
|
||||
for _, p := range paths {
|
||||
if err := ProcessCheck(p, cont, stats); err != nil {
|
||||
if cont {
|
||||
finalErr = err
|
||||
} else {
|
||||
stats.Print("check")
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
stats.Print("check")
|
||||
return finalErr
|
||||
return runCheck(opts, a, cont)
|
||||
},
|
||||
}
|
||||
cmd.Flags().BoolVar(&cont, "continue", false, "continue after errors and report each file")
|
||||
cmd.Flags().BoolVar(&cont, "continue", false,
|
||||
"continue after errors and report each file")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func ProcessCheck(dir string, cont bool, stats *Stats) error {
|
||||
fail := errors.New("verification failed")
|
||||
bad := false
|
||||
func runCheck(opts *options, args []string, cont bool) error {
|
||||
paths, err := expandPaths(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := walkAndProcess(dir, stats, "Verifying checksums", func(p string, info os.FileInfo, s *Stats) error {
|
||||
stats := &Stats{StartTime: time.Now()}
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
|
||||
if !opts.quiet {
|
||||
bar, err = countAndBar(opts, paths, "Verifying checksums")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var finalErr error
|
||||
|
||||
for _, p := range paths {
|
||||
perr := processCheck(opts, p, cont, stats, bar)
|
||||
if perr != nil {
|
||||
if !cont {
|
||||
finishBar(bar)
|
||||
stats.Print(opts, "check")
|
||||
|
||||
return perr
|
||||
}
|
||||
|
||||
finalErr = perr
|
||||
}
|
||||
}
|
||||
|
||||
finishBar(bar)
|
||||
stats.Print(opts, "check")
|
||||
|
||||
return finalErr
|
||||
}
|
||||
|
||||
func processCheck(
|
||||
opts *options, dir string, cont bool, stats *Stats, bar *progressbar.ProgressBar,
|
||||
) error {
|
||||
// Track initial failed count to detect failures during this walk.
|
||||
initialFailed := atomic.LoadInt64(&stats.FilesFailed)
|
||||
|
||||
err := walkAndProcess(opts, dir, stats, bar,
|
||||
func(p string, _ os.FileInfo, s *Stats) error {
|
||||
return checkOne(opts, p, cont, s)
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, errVerification) {
|
||||
return errVerification
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if any failures occurred during this walk.
|
||||
if atomic.LoadInt64(&stats.FilesFailed) > initialFailed {
|
||||
return errVerification
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkOne verifies the stored checksum of a single path.
|
||||
func checkOne(opts *options, p string, cont bool, s *Stats) error {
|
||||
exp, err := xattr.Get(p, checksumKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, xattr.ENOATTR) {
|
||||
bad = true
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
if verbose && !quiet {
|
||||
fmt.Printf("%s <none> ERROR\n", p)
|
||||
if !errors.Is(err, xattr.ENOATTR) {
|
||||
return err
|
||||
}
|
||||
|
||||
return missingChecksum(opts, p, cont, s)
|
||||
}
|
||||
|
||||
act, bytesRead, err := fileMultihash(p)
|
||||
if err != nil {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
ok := bytes.Equal(exp, act)
|
||||
if ok {
|
||||
atomic.AddInt64(&s.FilesProcessed, 1)
|
||||
atomic.AddInt64(&s.BytesProcessed, bytesRead)
|
||||
} else {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
}
|
||||
|
||||
reportCheck(opts, p, string(act), ok)
|
||||
|
||||
if !ok && !cont {
|
||||
return errVerification
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// missingChecksum records and reports a file that has no stored checksum,
|
||||
// returning the verification error unless the caller asked to continue.
|
||||
func missingChecksum(opts *options, p string, cont bool, s *Stats) error {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
|
||||
if opts.verbose && !opts.quiet {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s <none> ERROR\n", p)
|
||||
}
|
||||
|
||||
if cont {
|
||||
return nil
|
||||
}
|
||||
return fail
|
||||
}
|
||||
return err
|
||||
|
||||
return errVerification
|
||||
}
|
||||
|
||||
act, err := fileMultihash(p)
|
||||
if err != nil {
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
return err
|
||||
// reportCheck prints a per-file verification result when verbose output is on.
|
||||
func reportCheck(opts *options, p, actual string, ok bool) {
|
||||
if !opts.verbose || opts.quiet {
|
||||
return
|
||||
}
|
||||
ok := bytes.Equal(exp, act)
|
||||
if !ok {
|
||||
bad = true
|
||||
atomic.AddInt64(&s.FilesFailed, 1)
|
||||
} else {
|
||||
atomic.AddInt64(&s.FilesProcessed, 1)
|
||||
atomic.AddInt64(&s.BytesProcessed, info.Size())
|
||||
}
|
||||
if verbose && !quiet {
|
||||
|
||||
status := "OK"
|
||||
if !ok {
|
||||
status = "ERROR"
|
||||
}
|
||||
fmt.Printf("%s %s %s\n", p, act, status)
|
||||
}
|
||||
if !ok && !cont {
|
||||
return fail
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, fail) {
|
||||
return fail
|
||||
}
|
||||
return err
|
||||
}
|
||||
if bad {
|
||||
return fail
|
||||
}
|
||||
return nil
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s %s %s\n", p, actual, status)
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Helpers
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// countFiles counts the total number of regular files that will be processed
|
||||
func countFiles(root string) int64 {
|
||||
// countFiles counts the total number of regular files that will be processed.
|
||||
func countFiles(opts *options, root string) (int64, error) {
|
||||
var count int64
|
||||
|
||||
root = filepath.Clean(root)
|
||||
filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
|
||||
|
||||
err := filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// Skip symlinks - note: filepath.Walk uses Lstat, so symlinks are
|
||||
// reported as ModeSymlink, never as directories. Walk doesn't follow them.
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
if info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, _ := filepath.Rel(root, p)
|
||||
if shouldExclude(rel, info) {
|
||||
if shouldExclude(opts, rel) {
|
||||
if info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if info.Mode().IsRegular() {
|
||||
count++
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return count
|
||||
|
||||
return count, err
|
||||
}
|
||||
|
||||
func walkAndProcess(root string, stats *Stats, description string, fn func(string, os.FileInfo, *Stats) error) error {
|
||||
root = filepath.Clean(root)
|
||||
// countFilesMultiple counts files across multiple roots.
|
||||
func countFilesMultiple(opts *options, roots []string) (int64, error) {
|
||||
var total int64
|
||||
|
||||
// Count files first for progress bar
|
||||
total := countFiles(root)
|
||||
for _, root := range roots {
|
||||
count, err := countFiles(opts, root)
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
|
||||
// Create progress bar
|
||||
var bar *progressbar.ProgressBar
|
||||
if !quiet {
|
||||
bar = progressbar.NewOptions64(total,
|
||||
total += count
|
||||
}
|
||||
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// newProgressBar creates a new progress bar with standard options.
|
||||
func newProgressBar(total int64, description string) *progressbar.ProgressBar {
|
||||
return progressbar.NewOptions64(total,
|
||||
progressbar.OptionSetDescription(description),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionShowCount(),
|
||||
progressbar.OptionShowIts(),
|
||||
progressbar.OptionSetItsString("files"),
|
||||
progressbar.OptionThrottle(250*time.Millisecond),
|
||||
progressbar.OptionThrottle(progressThrottle),
|
||||
progressbar.OptionShowElapsedTimeOnFinish(),
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
progressbar.OptionFullWidth(),
|
||||
@@ -451,92 +664,125 @@ func walkAndProcess(root string, stats *Stats, description string, fn func(strin
|
||||
)
|
||||
}
|
||||
|
||||
err := filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
|
||||
func walkAndProcess(
|
||||
opts *options,
|
||||
root string,
|
||||
stats *Stats,
|
||||
bar *progressbar.ProgressBar,
|
||||
fn func(string, os.FileInfo, *Stats) error,
|
||||
) error {
|
||||
root = filepath.Clean(root)
|
||||
|
||||
return filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// skip symlinks entirely
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
if verbose && !quiet {
|
||||
log.Printf("skip symlink %s", p)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, _ := filepath.Rel(root, p)
|
||||
if shouldExclude(rel, info) {
|
||||
if info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
if verbose && !quiet {
|
||||
log.Printf("skip non-regular %s", p)
|
||||
}
|
||||
return nil
|
||||
skip, skipErr := walkSkip(opts, root, p, info)
|
||||
if skip {
|
||||
return skipErr
|
||||
}
|
||||
|
||||
fnErr := fn(p, info, stats)
|
||||
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
_ = bar.Add(1)
|
||||
}
|
||||
|
||||
return fnErr
|
||||
})
|
||||
|
||||
if bar != nil {
|
||||
bar.Finish()
|
||||
}
|
||||
|
||||
return err
|
||||
// walkSkip reports whether a walked entry should be skipped before the
|
||||
// per-file callback runs. When skip is true, the returned error is what the
|
||||
// walk callback should return (filepath.SkipDir to prune an excluded
|
||||
// directory, otherwise nil). Symlinks, directories and non-regular files are
|
||||
// never checksummed.
|
||||
func walkSkip(opts *options, root, p string, info os.FileInfo) (bool, error) {
|
||||
// filepath.Walk uses Lstat, so symlinks are reported as ModeSymlink,
|
||||
// never as directories. Walk doesn't follow them.
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
if opts.verbose && !opts.quiet {
|
||||
log.Printf("skip symlink %s", p)
|
||||
}
|
||||
|
||||
func shouldExclude(rel string, info os.FileInfo) bool {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
rel, _ := filepath.Rel(root, p)
|
||||
if shouldExclude(opts, rel) {
|
||||
if info.IsDir() {
|
||||
return true, filepath.SkipDir
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if !info.Mode().IsRegular() {
|
||||
if opts.verbose && !opts.quiet {
|
||||
log.Printf("skip non-regular %s", p)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func shouldExclude(opts *options, rel string) bool {
|
||||
if rel == "." || rel == "" {
|
||||
return false
|
||||
}
|
||||
if excludeDotfiles {
|
||||
for _, part := range strings.Split(rel, string(os.PathSeparator)) {
|
||||
|
||||
if opts.excludeDotfiles {
|
||||
for part := range strings.SplitSeq(rel, string(os.PathSeparator)) {
|
||||
if strings.HasPrefix(part, ".") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, pat := range excludePatterns {
|
||||
|
||||
for _, pat := range opts.excludePatterns {
|
||||
if ok, _ := doublestar.PathMatch(pat, rel); ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func hasXattr(path, key string) bool {
|
||||
_, err := xattr.Get(path, key)
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func fileMultihash(path string) ([]byte, error) {
|
||||
func fileMultihash(path string) ([]byte, int64, error) {
|
||||
// The path is supplied by the operator as the tree to checksum; reading
|
||||
// it is the entire purpose of the tool.
|
||||
//nolint:gosec // G304: operator-specified path is the intended input
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return nil, err
|
||||
|
||||
bytesRead, err := io.Copy(h, f)
|
||||
if err != nil {
|
||||
return nil, bytesRead, err
|
||||
}
|
||||
|
||||
mh, err := multihash.Encode(h.Sum(nil), multihash.SHA2_256)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, bytesRead, err
|
||||
}
|
||||
return []byte(base58.Encode(mh)), nil
|
||||
|
||||
return []byte(base58.Encode(mh)), bytesRead, nil
|
||||
}
|
||||
|
||||
186
attrsum_test.go
186
attrsum_test.go
@@ -10,23 +10,58 @@ import (
|
||||
"github.com/pkg/xattr"
|
||||
)
|
||||
|
||||
func skipIfNoXattr(t *testing.T, path string) {
|
||||
if err := xattr.Set(path, "user.test", []byte("1")); err != nil {
|
||||
t.Skipf("skipping: xattr not supported: %v", err)
|
||||
} else {
|
||||
_ = xattr.Remove(path, "user.test")
|
||||
const (
|
||||
dirPerm = 0o755
|
||||
filePerm = 0o644
|
||||
noPerm = 0o000
|
||||
|
||||
// futureSkew advances a file's mtime far enough to be unambiguously
|
||||
// newer than a previously recorded sumtime.
|
||||
futureSkew = 2 * time.Second
|
||||
)
|
||||
|
||||
// skipIfNoXattr skips the test unless the filesystem under dir supports the
|
||||
// extended-attribute namespace the program actually uses. Probing with the
|
||||
// real checksumKey (rather than a user.* key) matters on Linux, where regular
|
||||
// files only accept xattrs in the user.* namespace and the program's
|
||||
// berlin.sneak.* keys yield "operation not supported".
|
||||
func skipIfNoXattr(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
|
||||
probe := filepath.Join(dir, "xattr-probe")
|
||||
|
||||
err := os.WriteFile(probe, []byte("probe"), filePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("write probe file: %v", err)
|
||||
}
|
||||
|
||||
defer func() { _ = os.Remove(probe) }()
|
||||
|
||||
err = xattr.Set(probe, checksumKey, []byte("1"))
|
||||
if err != nil {
|
||||
t.Skipf("skipping: xattr namespace %q not supported: %v", checksumKey, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_ = xattr.Remove(probe, checksumKey)
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, root, name, content string) string {
|
||||
t.Helper()
|
||||
|
||||
p := filepath.Join(root, name)
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
|
||||
err := os.MkdirAll(filepath.Dir(p), dirPerm)
|
||||
if err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
||||
|
||||
err = os.WriteFile(p, []byte(content), filePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -35,28 +70,46 @@ func newTestStats() *Stats {
|
||||
}
|
||||
|
||||
func TestSumAddAndUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts := &options{}
|
||||
dir := t.TempDir()
|
||||
skipIfNoXattr(t, dir)
|
||||
|
||||
f := writeFile(t, dir, "a.txt", "hello")
|
||||
|
||||
if err := ProcessSumAdd(dir, newTestStats()); err != nil {
|
||||
err := processSumAdd(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("add: %v", err)
|
||||
}
|
||||
if _, err := xattr.Get(f, checksumKey); err != nil {
|
||||
|
||||
_, err = xattr.Get(f, checksumKey)
|
||||
if err != nil {
|
||||
t.Fatalf("checksum missing: %v", err)
|
||||
}
|
||||
|
||||
tsb, _ := xattr.Get(f, sumTimeKey)
|
||||
origTime, _ := time.Parse(time.RFC3339Nano, string(tsb))
|
||||
|
||||
os.WriteFile(f, []byte(strings.ToUpper("hello")), 0o644)
|
||||
now := time.Now().Add(2 * time.Second)
|
||||
os.Chtimes(f, now, now)
|
||||
err = os.WriteFile(f, []byte(strings.ToUpper("hello")), filePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("rewrite: %v", err)
|
||||
}
|
||||
|
||||
if err := ProcessSumUpdate(dir, newTestStats()); err != nil {
|
||||
now := time.Now().Add(futureSkew)
|
||||
|
||||
err = os.Chtimes(f, now, now)
|
||||
if err != nil {
|
||||
t.Fatalf("chtimes: %v", err)
|
||||
}
|
||||
|
||||
err = processSumUpdate(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("update: %v", err)
|
||||
}
|
||||
|
||||
tsb2, _ := xattr.Get(f, sumTimeKey)
|
||||
|
||||
newTime, _ := time.Parse(time.RFC3339Nano, string(tsb2))
|
||||
if !newTime.After(origTime) {
|
||||
t.Fatalf("sumtime not updated")
|
||||
@@ -64,46 +117,74 @@ func TestSumAddAndUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProcessCheckIntegration(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts := &options{}
|
||||
dir := t.TempDir()
|
||||
skipIfNoXattr(t, dir)
|
||||
|
||||
writeFile(t, dir, "b.txt", "world")
|
||||
if err := ProcessSumAdd(dir, newTestStats()); err != nil {
|
||||
|
||||
err := processSumAdd(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("add: %v", err)
|
||||
}
|
||||
if err := ProcessCheck(dir, false, newTestStats()); err != nil {
|
||||
|
||||
err = processCheck(opts, dir, false, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("check ok: %v", err)
|
||||
}
|
||||
|
||||
f := filepath.Join(dir, "b.txt")
|
||||
os.WriteFile(f, []byte("corrupt"), 0o644)
|
||||
|
||||
if err := ProcessCheck(dir, false, newTestStats()); err == nil {
|
||||
err = os.WriteFile(f, []byte("corrupt"), filePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("corrupt: %v", err)
|
||||
}
|
||||
|
||||
err = processCheck(opts, dir, false, newTestStats(), nil)
|
||||
if err == nil {
|
||||
t.Fatalf("expected mismatch error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearRemovesAttrs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts := &options{}
|
||||
dir := t.TempDir()
|
||||
skipIfNoXattr(t, dir)
|
||||
|
||||
f := writeFile(t, dir, "c.txt", "data")
|
||||
if err := ProcessSumAdd(dir, newTestStats()); err != nil {
|
||||
|
||||
err := processSumAdd(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("add: %v", err)
|
||||
}
|
||||
|
||||
if err := ProcessClear(dir, newTestStats()); err != nil {
|
||||
err = processClear(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
}
|
||||
if _, err := xattr.Get(f, checksumKey); err == nil {
|
||||
|
||||
_, err = xattr.Get(f, checksumKey)
|
||||
if err == nil {
|
||||
t.Fatalf("checksum still present after clear")
|
||||
}
|
||||
if _, err := xattr.Get(f, sumTimeKey); err == nil {
|
||||
|
||||
_, err = xattr.Get(f, sumTimeKey)
|
||||
if err == nil {
|
||||
t.Fatalf("sumtime still present after clear")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcludeDotfilesAndPatterns(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts := &options{
|
||||
excludeDotfiles: true,
|
||||
excludePatterns: []string{"*.me"},
|
||||
}
|
||||
dir := t.TempDir()
|
||||
skipIfNoXattr(t, dir)
|
||||
|
||||
@@ -111,61 +192,82 @@ func TestExcludeDotfilesAndPatterns(t *testing.T) {
|
||||
keep := writeFile(t, dir, "keep.txt", "keep")
|
||||
skip := writeFile(t, dir, "skip.me", "skip")
|
||||
|
||||
oldDot := excludeDotfiles
|
||||
oldPat := excludePatterns
|
||||
excludeDotfiles = true
|
||||
excludePatterns = []string{"*.me"}
|
||||
defer func() { excludeDotfiles, excludePatterns = oldDot, oldPat }()
|
||||
|
||||
if err := ProcessSumAdd(dir, newTestStats()); err != nil {
|
||||
err := processSumAdd(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("add with excludes: %v", err)
|
||||
}
|
||||
|
||||
if _, err := xattr.Get(keep, checksumKey); err != nil {
|
||||
_, err = xattr.Get(keep, checksumKey)
|
||||
if err != nil {
|
||||
t.Fatalf("expected xattr on keep.txt: %v", err)
|
||||
}
|
||||
if _, err := xattr.Get(hidden, checksumKey); err == nil {
|
||||
|
||||
_, err = xattr.Get(hidden, checksumKey)
|
||||
if err == nil {
|
||||
t.Fatalf(".hidden should have been excluded")
|
||||
}
|
||||
if _, err := xattr.Get(skip, checksumKey); err == nil {
|
||||
|
||||
_, err = xattr.Get(skip, checksumKey)
|
||||
if err == nil {
|
||||
t.Fatalf("skip.me should have been excluded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkipBrokenSymlink(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts := &options{}
|
||||
dir := t.TempDir()
|
||||
skipIfNoXattr(t, dir)
|
||||
|
||||
// Create a dangling symlink
|
||||
// Create a dangling symlink.
|
||||
link := filepath.Join(dir, "dangling.lnk")
|
||||
if err := os.Symlink(filepath.Join(dir, "nonexistent.txt"), link); err != nil {
|
||||
|
||||
err := os.Symlink(filepath.Join(dir, "nonexistent.txt"), link)
|
||||
if err != nil {
|
||||
t.Fatalf("symlink: %v", err)
|
||||
}
|
||||
|
||||
// Should not error and should not create xattrs on link
|
||||
if err := ProcessSumAdd(dir, newTestStats()); err != nil {
|
||||
t.Fatalf("ProcessSumAdd with symlink: %v", err)
|
||||
// Should not error and should not create xattrs on link.
|
||||
err = processSumAdd(opts, dir, newTestStats(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("processSumAdd with symlink: %v", err)
|
||||
}
|
||||
if _, err := xattr.Get(link, checksumKey); err == nil {
|
||||
|
||||
_, err = xattr.Get(link, checksumKey)
|
||||
if err == nil {
|
||||
t.Fatalf("symlink should not have xattr")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPermissionErrors(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
opts := &options{}
|
||||
dir := t.TempDir()
|
||||
skipIfNoXattr(t, dir)
|
||||
|
||||
secret := writeFile(t, dir, "secret.txt", "data")
|
||||
os.Chmod(secret, 0o000)
|
||||
defer os.Chmod(secret, 0o644)
|
||||
|
||||
if err := ProcessSumAdd(dir, newTestStats()); err == nil {
|
||||
err := os.Chmod(secret, noPerm)
|
||||
if err != nil {
|
||||
t.Fatalf("chmod: %v", err)
|
||||
}
|
||||
|
||||
defer func() { _ = os.Chmod(secret, filePerm) }()
|
||||
|
||||
err = processSumAdd(opts, dir, newTestStats(), nil)
|
||||
if err == nil {
|
||||
t.Fatalf("expected permission error, got nil")
|
||||
}
|
||||
if err := ProcessSumUpdate(dir, newTestStats()); err == nil {
|
||||
|
||||
err = processSumUpdate(opts, dir, newTestStats(), nil)
|
||||
if err == nil {
|
||||
t.Fatalf("expected permission error on update, got nil")
|
||||
}
|
||||
if err := ProcessCheck(dir, false, newTestStats()); err == nil {
|
||||
|
||||
err = processCheck(opts, dir, false, newTestStats(), nil)
|
||||
if err == nil {
|
||||
t.Fatalf("expected permission error on check, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
82
script/bootstrap
Executable file
82
script/bootstrap
Executable file
@@ -0,0 +1,82 @@
|
||||
#!/bin/sh
|
||||
# script/bootstrap: install all dependencies needed to build and develop
|
||||
# this repo. Idempotent: every install is guarded by a check so already
|
||||
# installed tools are skipped. Base tooling comes from nix, apt, brew,
|
||||
# or apk (detected in that order); assumes nothing is present.
|
||||
# golangci-lint and goimports are installed via `go install` at the same
|
||||
# pinned refs the Dockerfile uses (never "latest").
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
# Pinned versions, 2026-08-07 (same pins as the Dockerfile)
|
||||
# golangci-lint v2.12.2
|
||||
GOLANGCI_LINT_REF="github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2"
|
||||
# goimports v0.42.0
|
||||
GOIMPORTS_REF="golang.org/x/tools/cmd/goimports@009367f5c17a8d4c45a961a3a509277190a9a6f0"
|
||||
|
||||
PKGMGR=""
|
||||
SUDO=""
|
||||
APT_UPDATED=""
|
||||
|
||||
detect_pkgmgr() {
|
||||
[ -n "$PKGMGR" ] && return 0
|
||||
if command -v nix-env >/dev/null 2>&1; then
|
||||
PKGMGR="nix"
|
||||
elif command -v apt-get >/dev/null 2>&1; then
|
||||
PKGMGR="apt"
|
||||
elif command -v brew >/dev/null 2>&1; then
|
||||
PKGMGR="brew"
|
||||
elif command -v apk >/dev/null 2>&1; then
|
||||
PKGMGR="apk"
|
||||
else
|
||||
echo "bootstrap: no supported package manager (nix, apt, brew, apk)" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ "$PKGMGR" = "apt" ]; then
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
if [ "$(id -u)" != "0" ]; then
|
||||
SUDO="sudo"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# pkg_install <nix-attr> <apt-pkg> <brew-formula> <apk-pkg>
|
||||
pkg_install() {
|
||||
detect_pkgmgr
|
||||
case "$PKGMGR" in
|
||||
nix) nix-env -iA "nixpkgs.$1" ;;
|
||||
apt)
|
||||
if [ -z "$APT_UPDATED" ]; then
|
||||
$SUDO env DEBIAN_FRONTEND=noninteractive apt-get update
|
||||
APT_UPDATED=1
|
||||
fi
|
||||
$SUDO env DEBIAN_FRONTEND=noninteractive apt-get install -y "$2"
|
||||
;;
|
||||
brew) brew install "$3" ;;
|
||||
apk) apk add --no-cache "$4" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
missing() {
|
||||
! command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
|
||||
if missing git; then pkg_install git git git git; fi
|
||||
if missing make; then pkg_install gnumake make make make; fi
|
||||
if missing go; then pkg_install go golang go go; fi
|
||||
|
||||
# Lint/format tools, pinned via go install (installs into
|
||||
# "$(go env GOPATH)/bin"; ensure that is on your PATH).
|
||||
if missing golangci-lint; then go install "$GOLANGCI_LINT_REF"; fi
|
||||
if missing goimports; then go install "$GOIMPORTS_REF"; fi
|
||||
|
||||
go mod download
|
||||
|
||||
echo "bootstrap complete"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
14
script/check
Executable file
14
script/check
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
# script/check: run all checks (test, lint, fmt-check). Our own
|
||||
# extension to scripts-to-rule-them-all. Must not modify any files.
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
|
||||
|
||||
main() {
|
||||
"$SCRIPT_DIR/test"
|
||||
"$SCRIPT_DIR/lint"
|
||||
"$SCRIPT_DIR/fmt-check"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
13
script/cibuild
Executable file
13
script/cibuild
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# script/cibuild: run the CI build. The Dockerfile runs make check, so
|
||||
# a successful build implies all checks pass.
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
docker build .
|
||||
}
|
||||
|
||||
main "$@"
|
||||
14
script/docker
Executable file
14
script/docker
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
# script/docker: build the Docker image tagged with the project name.
|
||||
# Identical in all repos; the tag comes from script/projectname.
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
|
||||
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
docker build -t "$("$SCRIPT_DIR/projectname")" .
|
||||
}
|
||||
|
||||
main "$@"
|
||||
13
script/fmt
Executable file
13
script/fmt
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# script/fmt: format all files (writes).
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
gofmt -s -w .
|
||||
goimports -w .
|
||||
}
|
||||
|
||||
main "$@"
|
||||
18
script/fmt-check
Executable file
18
script/fmt-check
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
# script/fmt-check: check formatting (read-only). Same scope as
|
||||
# script/fmt, but fails instead of writing.
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
files="$(gofmt -l .)"
|
||||
if [ -n "$files" ]; then
|
||||
echo "gofmt: files not formatted:" >&2
|
||||
echo "$files" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
16
script/install-precommit
Executable file
16
script/install-precommit
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
# script/install-precommit: install the git pre-commit hook that runs
|
||||
# script/precommit. Our own extension to scripts-to-rule-them-all.
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
hook=".git/hooks/pre-commit"
|
||||
printf '#!/bin/sh\nset -e\nscript/precommit\n' > "$hook"
|
||||
chmod +x "$hook"
|
||||
echo "pre-commit hook installed: runs script/precommit"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
12
script/lint
Executable file
12
script/lint
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
# script/lint: run the linter.
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
golangci-lint run --config .golangci.yml ./...
|
||||
}
|
||||
|
||||
main "$@"
|
||||
21
script/precommit
Executable file
21
script/precommit
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
# script/precommit: run by the git pre-commit hook; fails the commit if
|
||||
# checks fail. Our own extension to scripts-to-rule-them-all. Go extra:
|
||||
# go mod tidy must be a no-op before the checks run.
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
|
||||
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
go mod tidy
|
||||
if ! git diff --exit-code -- go.mod go.sum; then
|
||||
echo "precommit: go mod tidy changed go.mod/go.sum;" \
|
||||
"stage the changes and retry" >&2
|
||||
exit 1
|
||||
fi
|
||||
"$SCRIPT_DIR/check"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
12
script/projectname
Executable file
12
script/projectname
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
# script/projectname: output the name of this project. Our own
|
||||
# extension to scripts-to-rule-them-all. Other scripts that need the
|
||||
# name (e.g. script/docker) call this, so they can stay identical
|
||||
# across all repos.
|
||||
set -eu
|
||||
|
||||
main() {
|
||||
echo "attrsum"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
13
script/setup
Executable file
13
script/setup
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# script/setup: set up the repo for development after a fresh clone:
|
||||
# installs dependencies and the git pre-commit hook.
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
|
||||
|
||||
main() {
|
||||
"$SCRIPT_DIR/bootstrap"
|
||||
"$SCRIPT_DIR/install-precommit"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
12
script/test
Executable file
12
script/test
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
# script/test: run the test suite.
|
||||
set -eu
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||
|
||||
main() {
|
||||
cd "$ROOT"
|
||||
go test -v -race -timeout 30s -cover ./...
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user