1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-10-04 12:31:00 +00:00
Commit graph

427 commits

Author SHA1 Message Date
Aleksa Sarai
38fd14f297
vis: switch to 'switch' for non-escaped logic
There was a TODO to make this code more legible. I still think it's
somewhat ugly, but it does read _slightly_ better as a switch statement.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:40:27 +10:00
Aleksa Sarai
47086b0654
vis: improve performance by reducing allocations
By avoiding lots of small string allocations and reallocations when
appending to the output buffer, we can get a pretty decent performance
improvement (~6x for strings that do not require escaping, and ~2x for
most other multi-byte utf8 strings).

    goos: linux
    goarch: amd64
    pkg: github.com/vbatts/go-mtree/pkg/govis
    cpu: AMD Ryzen 7 7840U w/ Radeon  780M Graphics
                    │    before    │                after                │
                    │    sec/op    │   sec/op     vs base                │
    Vis/NoChange-16   2372.5n ± 2%   379.1n ± 1%  -84.02% (p=0.000 n=10)
    Vis/Binary-16      2.104µ ± 8%   1.319µ ± 8%  -37.35% (p=0.000 n=10)
    Vis/ASCII-16      2070.0n ± 1%   737.3n ± 0%  -64.38% (p=0.000 n=10)
    Vis/German-16      3.380µ ± 1%   1.181µ ± 2%  -65.04% (p=0.000 n=10)
    Vis/Russian-16    10.927µ ± 2%   5.293µ ± 2%  -51.56% (p=0.000 n=10)
    Vis/Japanese-16    7.489µ ± 1%   3.990µ ± 0%  -46.72% (p=0.000 n=10)
    geomean            3.767µ        1.447µ       -61.58%

In theory we could get more performance if switch away from fmt.Sprintf,
but the %.N handling would be a little annoying to implement and so we
can punt on that for now.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:40:25 +10:00
Aleksa Sarai
70d3b19776
unvis: improve performance by reducing allocations
By using a buffer, we can avoid a bunch of small allocations that the
previous implementation did. Based on a few small benchmarks, the
performance improvement is very stark (~3x faster for strings that don't
require any escaping, and ~20% faster for multi-byte utf8 strings):

  goos: linux
  goarch: amd64
  pkg: github.com/vbatts/go-mtree/pkg/govis
  cpu: AMD Ryzen 7 7840U w/ Radeon  780M Graphics
                    │    before    │                after                │
                    │    sec/op    │   sec/op     vs base                │
  Unvis/NoChange-16   1501.0n ± 0%   497.7n ± 1%  -66.84% (p=0.000 n=10)
  Unvis/Binary-16     1317.5n ± 3%   934.9n ± 9%  -29.04% (p=0.000 n=10)
  Unvis/ASCII-16      1325.5n ± 1%   616.8n ± 1%  -53.47% (p=0.000 n=10)
  Unvis/German-16     1884.5n ± 1%   986.9n ± 2%  -47.63% (p=0.000 n=10)
  Unvis/Russian-16     4.636µ ± 1%   3.796µ ± 1%  -18.11% (p=0.000 n=10)
  Unvis/Japanese-16    3.453µ ± 1%   2.867µ ± 1%  -16.99% (p=0.000 n=10)
  geomean              2.072µ        1.206µ       -41.77%

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:40:23 +10:00
Aleksa Sarai
01d93a93e2
unvis: make Next return chars and add Step helper
This makes it a little easier to read the common case code which
consumes the token and helps highlight which sub-parsers are explicitly
not consuming tokens until we are sure we are using it.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:40:21 +10:00
Aleksa Sarai
9bd1dffd9b
unvis: switch to methods for parser
Passing the parsers as an argument is very C-like and is not really as
idiomadic as just using methods (in my defence, I was still pretty green
when I wrote this code and I was trying to port some logic from C).

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:40:18 +10:00
Aleksa Sarai
bcdb71fb56
unvis: add some more error tests
These error cases were already handled correctly, but we really should
have tests for them anyway. Now that we have proper error variables
declared we can also test for specific errors as well.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:40:15 +10:00
Aleksa Sarai
fbada2e081
govis: modernise errors
This code was written before %w was added to Go, and there were a fair
few mistakes in the copy-pasted error code.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:15:07 +10:00
Aleksa Sarai
3ce83fca15
govis: add SPDX license tag
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:14:37 +10:00
Aleksa Sarai
d02f298ad4
govis: switch to testify tests
testify makes most bog-standard test checks much easier to read and
maintain, and is quite widely used. It wasn't really well known back
when govis was first written, but the migration is fairly
straight-forward for most tests.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-23 04:13:42 +10:00
Aleksa Sarai
b0c7528ac6
mtree test: correctly check number of types
It seems that this test actually had a bug that was not caught until I
tried to refactor it. The new test has behaviour that makes more sense
to me.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-20 12:43:46 +10:00
Aleksa Sarai
3252a4ad82
test: migrate most tests to testify
testify makes most bog-standard test checks much easier to read and
maintain, and is quite widely used. It wasn't really well known back
when go-mtree was first written, but the migration is fairly
straight-forward for most tests.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-20 12:43:45 +10:00
Aleksa Sarai
f2b48a0e2f
test: use t.TempDir
This was added in Go 1.15 and avoids polluting the host /tmp with test
directories if the test crashes or is forcefully killed.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-20 12:43:45 +10:00
Aleksa Sarai
d997335f30
gha: add ci job to check go mod and vendor
In order to avoid forgetting to clean this up.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-20 12:43:45 +10:00
Aleksa Sarai
978ab874e2
go.mod: run tidy and vendor
It seems that this was missed at some point in the past.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-20 12:43:44 +10:00
8307d76bc1
Merge pull request #209 from cyphar/tartime-compare
compare: properly compare tar_time and time
2025-09-11 07:26:31 -04:00
Aleksa Sarai
604ab42863
compare: improve tar_time truncation
Rather than parsing the value as a float and then truncating it, just
parse it as an integer in the first place (this also adds some
validation that we are parsing a reasonable-looking value).

While we're at it, add some integration tests for this code to make sure
this quite complicated special-case behaviour doesn't regress.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-09 22:48:56 +10:00
Aleksa Sarai
0beb885cbf
compare: properly compare tar_time and time
In certain circumstances, if a manifest with "time" keywords was
compared to another manifest with "tar_time" keywords, differences were
not actually reported despite there being logic for it.

Unfortunately, even though commit 26ff922da6 ("compare: implement
mtree.DirectoryHierarchy comparisons") explicitly included logic to
handle the necessary "time" -> "tar_time" conversions, the same commit
also made it so that Compare() could be instructed to only consider a
subset of keywords and failed to take into account said "time" ->
"tar_time" remapping.

Most users have likely not noticed this because gomtree will re-use the
keywords from a manifest when doing a straightforward "gomtree validate"
invocation, but if you explicitly requested "time" when validating a
"tar_time" manifest (and *not* the other way around) then any time
changes would be undetected.

Fixes: 26ff922da6 ("compare: implement mtree.DirectoryHierarchy comparisons")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-09 22:47:49 +10:00
b3097c1af5
Merge pull request #208 from vbatts/modules
update go modules
2025-09-08 11:22:04 -04:00
f0801f9f72
go*: update to go1.24, so all modules can be updated
frustratingly, `golang.org/x/sys/unix` started importing from `slices`
and didn't cleanly put the go headers version support, so all libraries
that used this library needed to be updated and could not take the
update without also updating to `go1.24`. So dumb.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2025-09-08 11:16:58 -04:00
1f88b08b7e
go versions: drop 1.18-1.23, add 1.24-1.25
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2025-09-08 11:16:06 -04:00
da220ef733
update github.com/urfave/cli/v2 v2.27.5 -> v2.27.7
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2025-09-08 10:47:07 -04:00
b454bc0bdd
Merge pull request #207 from cyphar/error-exit-code
gomtree: return exit status != 0 on error
2025-09-08 10:40:09 -04:00
Aleksa Sarai
945eeeda2d
tests: cli: properly check for exit status codes in error cases
POSIX explicitly requires "set -e" to NOT treat "! cmd" as an error even
if fails[1]:

> The -e setting shall be ignored when executing the compound list
> following the while, until, if, or elif reserved word, *a pipeline
> beginning with the ! reserved word*, or any command of an AND-OR list
> other than the last. *[emphasis added]*

And bash has similar documentation on this behaviour[2].

This means that our tests were completely ineffective at detecting error
codes from gomtree, and as a result we did not detect the regression in
commit 83c9fdb78b ("refactor: prefactor for adding new subcommands").

The simplest solution (as done in this patch) is to just wrap all of the
failing examples in a subshell, which causes the shell to no longer
consider them exempt from "set -e". A more complete solution would be to
either switch to something like bats entirely or at least use something
like their "run" helper function to test for exit status codes
correctly.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set
[2]: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

Fixes: 5d7f6c36e0 ("walk: directory is expected to be walked. A file is not.")
Fixes: 2d841d54bf ("test: testing the double -f comparison")
Fixes: f6c295f2e9 ("test: cli: add unicode verification test")
Fixes: 071977cef6 ("test: cli: add xattr tests")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-08 23:53:21 +10:00
Aleksa Sarai
23151ae80f
gomtree: return exit status != 0 on error
This was broken during the refactor for "gomtree validate" in commit
83c9fdb78b ("refactor: prefactor for adding new subcommands"),
resulting in any code that relied on our exit code to silently treat all
errors as non-fatal.

Our tests did not catch this due to a quirky POSIX-ism with regards to
"! cmd" and "set -e" which is fixed in a follow-up patch.

Fixes: 83c9fdb78b ("refactor: prefactor for adding new subcommands")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-09-08 23:52:51 +10:00
12e242c268
Merge pull request #203 from vbatts/update-mods
Some checks failed
continuous-integration/drone Build is failing
go*: update modules
2024-11-01 08:33:52 -04:00
5383508885
go*: update modules
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2024-10-24 17:33:51 +00:00
7c33cb7d95
Merge pull request #202 from vbatts/update_modules
go*: update modules
2024-06-11 11:56:41 -05:00
4fdc2fd3ed
go*: update modules
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2024-06-11 16:52:57 +00:00
d54530a564
Merge pull request #200 from vbatts/dependabot/go_modules/golang.org/x/crypto-0.17.0
build(deps): bump golang.org/x/crypto from 0.14.0 to 0.17.0
2024-01-09 15:03:05 -06:00
dependabot[bot]
8c533b9c6b
build(deps): bump golang.org/x/crypto from 0.14.0 to 0.17.0
Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.14.0 to 0.17.0.
- [Commits](https://github.com/golang/crypto/compare/v0.14.0...v0.17.0)

---
updated-dependencies:
- dependency-name: golang.org/x/crypto
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-18 23:27:15 +00:00
c216f6c013
Merge pull request #198 from vbatts/incorporate_subcommand
*: begin incorporating the "validate" subcommand
2023-10-24 16:05:24 -04:00
e19072ac1c
*: begin incorporating the "validate" subcommand
Update the README to show the validate subcommand by default.
This doesn't eliminate the default behavior of _not_ using the command,
but begins the visibility of using it by default.

Also copy one of the existing tests, to ensure the same behaviour works
as we add more subcommands and/or global flags.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-24 10:25:18 -04:00
48e5e86700
Merge pull request #197 from vbatts/fmt
*.go: `goimports -w .``
2023-10-24 09:18:03 -04:00
4ab44bff01
*.go: goimports -w .`
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-24 09:09:57 -04:00
732bc463ba
Merge pull request #196 from vbatts/golangci-lint
*.go: update to golangci-lint, and fix everything
2023-10-24 09:09:21 -04:00
42b655d8ee
*.go: update to golangci-lint, and fix everything
install tools in the workflow actions
Also switch away from deprecated ioutil

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-23 21:59:44 -04:00
efd1ad5ed5
Merge pull request #195 from vbatts/readme_cleanup
README: formatting, links, updates
2023-10-23 21:46:31 -04:00
ab2b6ed31f
README: formatting, links, updates
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-23 21:02:04 -04:00
e7a79ae8a7
Merge pull request #193 from vbatts/more_arches
Makefile: add a few additional architectures to build for
2023-10-22 17:51:56 -04:00
5d1916ee81
Merge pull request #194 from vbatts/sponsoring
Create FUNDING.yml
2023-10-22 17:51:42 -04:00
2eb1e12b41
Create FUNDING.yml
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-22 17:47:12 -04:00
2a2bf3c904
Makefile: add a few additional architectures to build for
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-22 17:43:33 -04:00
b5c5253f71
Merge pull request #192 from thesayyn/prefactor-subcommands
refactor: prefactor for adding new subcommands
2023-10-22 17:35:46 -04:00
thesayyn
83c9fdb78b
refactor: prefactor for adding new subcommands 2023-10-20 16:12:08 -07:00
7c8a752a64
Merge pull request #188 from cyphar/parse-fulltype-handling
parse: improve FullType handling
2023-10-07 20:14:59 -04:00
77f5530d89
Merge pull request #189 from vbatts/updates
go: go get -u ./... && go mod vendor && go mod tidy
2023-10-07 10:06:35 -04:00
e73ff94ef9
go: go get -u ./... && go mod vendor && go mod tidy
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-07 10:03:46 -04:00
a9c6969125
Merge pull request #190 from vbatts/workflows
workflows: add golang 1.21
2023-10-07 10:03:26 -04:00
fa57ee3a8e
workflows: add golang 1.21
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-10-07 10:00:42 -04:00
Aleksa Sarai
07c8c8e17a
parse: clean path after checking if entry is a FullType
The spec[1] doesn't mention anything about cleaning paths, but it does
explicitly refer to the path containing a "/". Cleaning the path before
checking if the entry is a FullType would result in the simplest way of
forcing directories to be FullTypes (appending a "/" to the pathname of
any directory) not working with go-mtree.

[1]: https://man.netbsd.org/mtree.5

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2023-06-04 20:14:00 +10:00