Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
48d36b6
Fix SQLite paths through symlinked directories and audit path edge cases
boyter Oct 29, 2026
1cb7b0a
Resolve .. through symlinks as the OS does, check every sum file line
boyter Oct 30, 2026
e3de534
Keep .. as written when it names the same directory, match sum duplic…
boyter Oct 30, 2026
d4a000a
Clean .. through deleted directories as text, use one .. rule for arg…
boyter Oct 30, 2026
682c7be
Check every sum line against the file it names, never clean .. throug…
boyter Oct 30, 2026
19dc946
Compare paths through a symlinked working directory as it was reached…
boyter Oct 30, 2026
d483082
Compare paths through symlinked working directories by one canonical …
boyter Oct 30, 2026
b7aaf7e
Join paths through a symlink to / with a separator, index only re-spe…
boyter Oct 30, 2026
0d618ef
Resolve .. in the audit file's path before using its directory as a base
boyter Oct 30, 2026
6895807
Do not report a SQLite row as missing when an earlier row lists the s…
boyter Oct 31, 2026
d7648e1
Match an audit through one directory, so a file moved to where anothe…
boyter Oct 31, 2026
f666b15
Judge duplicate records through the directory an audit is matched thr…
boyter Oct 31, 2026
84d2eb8
Add --allow-new to pass an audit with files not in the audit file
boyter Oct 31, 2026
d79474e
Judge duplicates through every directory when none was chosen, and re…
boyter Oct 31, 2026
fac20bd
Judge SQLite duplicates through the invoked from directory when none …
boyter Oct 31, 2026
d0cdf6c
Refuse SQLite databases whose filepath is not plain text compared exa…
boyter Nov 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Usage:
Flags:
--absolute output absolute paths, which is what hashdeep does without -l
--allow-moved allow an audit to pass when known files have moved or been renamed but are unmodified
--allow-new allow an audit to pass when there are files which are not in the audit file
-a, --audit string audit against supplied file; accepts hashdeep, sum (md5sum, sha256sum etc...) or hashit sqlite format
--debug enable debug output
--exclude-dir strings directories to exclude when walking directories, by name such as node_modules or the end of a path such as vendor/cache
Expand Down Expand Up @@ -299,6 +300,8 @@ $ rm -rf /tmp/hashit-audit-test audit.txt

`hashit` can also detect moved files, distinguishing them from new or missing files. As with `hashdeep`, an audit only passes when every file is exactly where it was, so moved files cause the audit to fail. To check that every known file is still present and unmodified, while allowing files to have been moved or renamed, use `--allow-moved`. New, missing or modified files still fail the audit. Files are matched by path first, so a file renamed onto the path of another known file, such as two files swapping names, is reported as modified rather than moved.

Files which are not in the audit file are reported as new and fail the audit, as with `hashdeep`, such as another audit file created in the same directory. To only verify the files the audit file lists, as `md5sum -c` does, use `--allow-new`. New files are still listed with `-v`, and missing or modified files still fail the audit. A copy of a known file whose original is still present is a new file, so passes with `--allow-new`. The two flags can be combined.

```shell
$ mv a.txt archive/a.txt
$ hashit --allow-moved -a audit.txt .
Expand Down Expand Up @@ -331,6 +334,7 @@ Some other points worth knowing:
- A file which cannot be read, including files in a directory which cannot be read, is reported as could not be read in `-v` output, counted as missing when it is in the audit file and not counted otherwise, and `hashit` exits with `2` rather than `1`.
- The same file supplied more than once by the same absolute path, such as through `.` and `./sub`, is only counted once. The same file reached through a symbolic link is a different path, so is counted again.
- Modification times are recorded with `--mtime` but are not compared by an audit.
- A `sum` file which lists the same file more than once, such as once with `md5sum` and again with `sha1sum`, or as both `./file` and `file`, has every line checked as `md5sum -c` does, including when its paths only name the same file through the directory containing the `sum` file or a symbolic link, where a line is only not reported on its own when another line was checked against that same file and it names that file through the directory the audit is matched through, and a file listed with different values of the same hash is reported as modified with a warning. In a `hashdeep` file only the first entry for a path is used, with a warning. In a SQLite database a row for a path already listed with the same values is not counted, with a warning, while one with other values is reported as missing. Entries are only the same path when they name the same file through the directory the audit is matched through, so after a tree is copied elsewhere an absolute entry into the original is audited on its own. When every file matched an absolute entry no directory is chosen, and entries are the same path when they name the same file through the invoked from directory, or for a `sum` file, which has none, through any of them. A SQLite database whose `file_hashes` table does not have a text `filepath` on every row as its primary key compared exactly, as one not created by hashit may not, is refused rather than audited.
- The audit file of an empty directory passes when audited against an empty directory, as does an audit file which is empty or only contains comments, which `md5sum -c` treats as an error. `hashdeep` reports an error for an audit file with no files in it.
- An audit file written by a shell redirect into the directory being hashed, such as `hashit --format hashdeep . > manifest.txt`, lists itself as an empty file. That entry is ignored when auditing, but `--output manifest.txt` avoids it entirely and is recommended, especially for the `sum` format which writes as it goes and so may record part of itself, which then fails when audited from another directory.
- Concatenated `hashdeep` files are read, but only the first `## Invoked from:` line is used, so files concatenated from different directories may report moved files. `hashdeep` also fails to audit these.
Expand All @@ -351,7 +355,7 @@ hashdeep: Audit passed

#### Paths in Audit Files

Relative paths in a `hashdeep` file are resolved against the directory recorded in its `## Invoked from:` line, so an audit file created with `hashit --format hashdeep .` can be audited from anywhere using either a relative or absolute path. When the invoked from directory does not contain the files, such as after they have been copied elsewhere, paths are also resolved against the directory containing the audit file, being where the file really is and then where the path supplied for it is when that is a symbolic link, and then the current directory. Relative paths in `sum` files have no invoked from directory, so are resolved against the current directory first. Matching is case sensitive on every platform. Paths are always compared as absolute paths, so the path to audit may be supplied as relative or absolute, and `./file` and `file` are the same file. So a manifest stored at the root of a set of files, created from that directory, can be audited from anywhere after the files are copied or shipped elsewhere. Symbolic links in the path to audit are not resolved, so a manifest created through `real/` and audited through a link to it reports every file as moved, as `hashdeep` does. When two files on disk match the same record, such as when the path to audit overlaps a copy of the files, the one matching through the invoked from directory is used and the other is reported as new.
Relative paths in a `hashdeep` file are resolved against the directory recorded in its `## Invoked from:` line, so an audit file created with `hashit --format hashdeep .` can be audited from anywhere using either a relative or absolute path. When the invoked from directory does not contain the files, such as after they have been copied elsewhere, paths are also resolved against the directory containing the audit file, being where the file really is and then where the path supplied for it is when that is a symbolic link, and then the current directory. An audit is matched through only one of these, the one through which the most files match their records, or the first of those in this order, so a file found only through another, such as a file moved to where the directory containing the audit file places its record, is reported as moved or new. Relative paths in `sum` files have no invoked from directory, so are resolved against the current directory first. Matching is case sensitive on every platform. Paths are always compared as absolute paths, so the path to audit may be supplied as relative or absolute, and `./file` and `file` are the same file. So a manifest stored at the root of a set of files, created from that directory, can be audited from anywhere after the files are copied or shipped elsewhere. Symbolic links in the path to audit are not resolved, so a manifest created through `real/` and audited through a link to it reports every file as moved, as `hashdeep` does. When the current directory was reached through a symbolic link, paths through where it, or a directory containing it, really is are compared through the link, so a manifest of absolute paths through `real/` audited with `.` from the link passes. A path containing `..`, such as `../dir` or `link/../dir`, names the directory the operating system reads, being the parent of the directory a symbolic link points to rather than the directory containing the link, in the paths supplied, the paths recorded in audit files and with `--absolute`. Where removing the `..` names the same directory, such as from a current directory reached through a symbolic link, the path is kept as written, A recorded path through a directory which no longer exists, such as the directory an audit file was created in, has its `..` removed as text, as a missing directory cannot be a symbolic link, while a recorded path with `..` after a link which no longer exists, a file, or a directory which cannot be searched never matches another file. When two files on disk match the same record, such as when the path to audit overlaps a copy of the files, the one matching through the invoked from directory is used and the other is reported as new.

If the audit file is inside the directory being audited it is skipped, as it can never contain its own hash. `hashdeep` and `md5sum` do not do this, so when generating a file for them to verify, write it outside the directory being hashed, otherwise they will report the partially written file as changed.

Expand All @@ -364,7 +368,7 @@ $ md5sum * > checksums.md5
$ hashit -a checksums.md5 .
```

Note that `md5sum *` does not include files in subdirectories, while `hashit` audits the whole directory, so for a directory with subdirectories generate the file with something like `find . -type f -exec md5sum {} +`.
Note that `md5sum *` does not include files in subdirectories, while `hashit` audits the whole directory, so for a directory with subdirectories generate the file with something like `find -L . -type f -exec md5sum {} +`, where `-L` follows symbolic links as `hashit` does.

The hash is determined by the file extension where it is one of `.md4`, `.md5`, `.sha1`, `.sha224`, `.sha256`, `.sha384`, `.sha512`, `.sha3-224`, `.sha3-256`, `.sha3-384`, `.sha3-512`, `.b2` or `.blake2b` (Blake2b-512 or Blake2b-256), `.b3` or `.blake3`, `.ed2k`, or `.crc32`, and otherwise by the length of the hash. A hash of 8 characters is assumed to be `crc32` and one of 16 `xxhash64`. Where lengths are shared the more common hash is assumed, being `md5` over `md4` and `ed2k`, `sha224` over `sha3-224`, `sha256` over `sha3-256`, `blake3` and `b2sum -l 256`, `sha384` over `sha3-384`, and `sha512` over `sha3-512` and `b2sum`. A wrong guess reports every file as modified, so name `b2sum` files `.b2` or use `--tag` output for the others. Relative paths are resolved against both the current directory and the directory containing the sum file, so run the audit from the directory the sum file was created in, or keep the sum file at the root of the files it lists.

Expand Down Expand Up @@ -437,7 +441,7 @@ If a file is modified, the audit will fail and report the change, and `-v` lists
$ hashit --hash all -a audit.db .
```

The database is opened read-only and is never modified by an audit. If another tool has switched the database to WAL mode, SQLite creates `-wal` and `-shm` files next to it, and the audit fails when the directory is read only, so run `sqlite3 audit.db 'pragma journal_mode=delete'` first. Which files have been seen is tracked in a temporary table, which SQLite moves to a file once it outgrows its cache, so memory use does not grow with the size of the database. The table is created in SQLite's temporary directory, which can be changed by setting `SQLITE_TMPDIR` for very large audits. Files on disk which are not in the database, being new or moved files, are held in memory until the end of the audit, so auditing a directory with many new files will use more memory. The directory `hashit` was run from is stored in the database, and paths are matched the same way as `hashdeep` files, see [Paths in Audit Files](#paths-in-audit-files), so a database of relative paths can be audited from any directory using relative or absolute paths, and a database stored at the root of a set of files can be audited after they are copied elsewhere. To do this the absolute path of every record is first indexed in a temporary table.
The database is opened read-only and is never modified by an audit. If another tool has switched the database to WAL mode, SQLite creates `-wal` and `-shm` files next to it, and the audit fails when the directory is read only, so run `sqlite3 audit.db 'pragma journal_mode=delete'` first. Which files have been seen is tracked in a temporary table, which SQLite moves to a file once it outgrows its cache, so memory use does not grow with the size of the database. The table is created in SQLite's temporary directory, which can be changed by setting `SQLITE_TMPDIR` for very large audits. Files on disk which are not in the database, being new or moved files, are held in memory until the end of the audit, so auditing a directory with many new files will use more memory. The directory `hashit` was run from is stored in the database, and paths are matched the same way as `hashdeep` files, see [Paths in Audit Files](#paths-in-audit-files), so a database of relative paths can be audited from any directory using relative or absolute paths, and a database stored at the root of a set of files can be audited after they are copied elsewhere. Records are looked up by the path of each file relative to these directories, and records written another way, such as with `..` or `//`, are first indexed by their absolute path in a temporary table.

```shell
# Example of a failed audit after modifying a file
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func main() {
false,
"allow an audit to pass when known files have moved or been renamed but are unmodified",
)
flags.BoolVar(
&processor.AllowNew,
"allow-new",
false,
"allow an audit to pass when there are files which are not in the audit file",
)
flags.BoolVar(
&processor.Absolute,
"absolute",
Expand Down
Loading
Loading