diff --git a/TODO.md b/TODO.md index fef4869..f3c96e5 100644 --- a/TODO.md +++ b/TODO.md @@ -22,6 +22,15 @@ the tag exists and is exercised; what is left is merging `next` to # Completed Steps +- 2026-10-06: Made `--older-than` and `--keep-newer-than` reject a + duration with characters outside its number-and-unit parts + ([issue #215](https://git.eeqj.de/sneak/vaultik/issues/215)). The + parser picked out the parts it recognised and skipped the rest, so + `1.0y` became zero and `--prune --keep-newer-than 1.0y` deleted every + snapshot of the backed-up names, the new one included. `1.0y`, + `1,5y`, `30 days` and `x7d` are now errors; decimals still work in Go + units such as `1.5h`. + - 2026-10-06: Made a backup re-chunk a known file that lists a chunk no uploaded blob holds ([issue #214](https://git.eeqj.de/sneak/vaultik/issues/214)). File diff --git a/internal/vaultik/helpers.go b/internal/vaultik/helpers.go index 64a1d85..db68dd3 100644 --- a/internal/vaultik/helpers.go +++ b/internal/vaultik/helpers.go @@ -140,7 +140,10 @@ func parseSnapshotName(snapshotID string) string { // parseDuration parses a duration string with support for human-friendly units: // d/day/days, w/week/weeks, mo/month/months, y/year/years, plus standard Go // duration units. Following Go, m is minutes and mo is months. A bare number, -// an unknown unit, and a negative value are all rejected. +// an unknown unit, and a negative value are all rejected. Outside the Go +// units the input must be whole numbers each followed directly by a unit +// (2w3d), so 1.0y and 30 days are errors; decimals work only in Go units +// (1.5h). func parseDuration(s string) (time.Duration, error) { if strings.HasPrefix(strings.TrimSpace(s), "-") { return 0, errNegativeDuration @@ -151,13 +154,13 @@ func parseDuration(s string) (time.Duration, error) { return d, nil } - re := regexp.MustCompile(`(\d+)\s*([a-zA-Z]+)`) - - matches := re.FindAllStringSubmatch(s, -1) - if len(matches) == 0 { + if !regexp.MustCompile(`^(\d+[a-zA-Z]+)+$`).MatchString(s) { return 0, fmt.Errorf("%w: %q", errInvalidDuration, s) } + re := regexp.MustCompile(`(\d+)([a-zA-Z]+)`) + matches := re.FindAllStringSubmatch(s, -1) + var total time.Duration for _, match := range matches { diff --git a/internal/vaultik/helpers_test.go b/internal/vaultik/helpers_test.go index eb58781..81b4242 100644 --- a/internal/vaultik/helpers_test.go +++ b/internal/vaultik/helpers_test.go @@ -59,6 +59,7 @@ func TestParseDuration(t *testing.T) { {"30s", 30 * time.Second, false}, {"6m", 6 * time.Minute, false}, {"1h", time.Hour, false}, + {"1.5h", 90 * time.Minute, false}, // Extended calendar units. {"30d", 30 * 24 * time.Hour, false}, {"3days", 3 * 24 * time.Hour, false}, @@ -78,6 +79,14 @@ func TestParseDuration(t *testing.T) { {"-5h", 0, true}, // negative, Go unit {"", 0, true}, // empty {"garbage", 0, true}, + // Characters outside the number-and-unit parts must not be + // skipped: 1.0y read as 0y would select every snapshot. + {"1.0y", 0, true}, + {"2.1w", 0, true}, + {"1,5y", 0, true}, + {"30 days", 0, true}, + {"30 days ago", 0, true}, + {"x7d", 0, true}, } for _, tt := range tests {