From 6f454f1836427f722a13e8e890b66e28b600e8be Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Sun, 18 Aug 2024 21:36:22 +0200 Subject: [PATCH] fix status --- cmd/chkbit/main.go | 14 +++++++------- index.go | 3 +-- scripts/run_test_prep | 7 ++++--- scripts/run_tests | 33 +++++++++++++++++++++++++++++---- status.go | 10 +++++----- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cmd/chkbit/main.go b/cmd/chkbit/main.go index ee2f832..c2a908a 100644 --- a/cmd/chkbit/main.go +++ b/cmd/chkbit/main.go @@ -80,18 +80,18 @@ func (m *Main) log(text string) { m.logger.Println(time.Now().UTC().Format("2006-01-02 15:04:05"), text) } -func (m *Main) logStatus(stat chkbit.Status, path string) bool { +func (m *Main) logStatus(stat chkbit.Status, message string) bool { if stat == chkbit.STATUS_UPDATE_INDEX { m.numIdxUpd++ } else { if stat == chkbit.STATUS_ERR_DMG { m.total++ - m.dmgList = append(m.dmgList, path) + m.dmgList = append(m.dmgList, message) } else if stat == chkbit.STATUS_PANIC { - m.errList = append(m.errList, path) - } else if stat == chkbit.STATUS_OK || stat == chkbit.STATUS_UPDATE || stat == chkbit.STATUS_NEW { + m.errList = append(m.errList, message) + } else if stat == chkbit.STATUS_OK || stat == chkbit.STATUS_UPDATE || stat == chkbit.STATUS_NEW || stat == chkbit.STATUS_UP_WARN_OLD { m.total++ - if stat == chkbit.STATUS_UPDATE { + if stat == chkbit.STATUS_UPDATE || stat == chkbit.STATUS_UP_WARN_OLD { m.numUpd++ } else if stat == chkbit.STATUS_NEW { m.numNew++ @@ -99,7 +99,7 @@ func (m *Main) logStatus(stat chkbit.Status, path string) bool { } if m.logVerbose || stat != chkbit.STATUS_OK && stat != chkbit.STATUS_IGNORE { - m.log(stat.String() + " " + path) + m.log(stat.String() + " " + message) } if m.verbose || !stat.IsVerbose() { @@ -107,7 +107,7 @@ func (m *Main) logStatus(stat chkbit.Status, path string) bool { if stat.IsErrorOrWarning() { col = termAlertFG } - lterm.Printline(col, stat.String(), " ", path, lterm.Reset) + lterm.Printline(col, stat.String(), " ", message, lterm.Reset) return true } } diff --git a/index.go b/index.go index aa6a3c9..24f0ccc 100644 --- a/index.go +++ b/index.go @@ -125,7 +125,6 @@ func (i *Index) checkFix(force bool) { if a, ok := i.cur[name]; !ok { i.logFile(STATUS_NEW, name) i.setMod(true) - continue } else { amod := int64(a.ModTime) bmod := int64(b.ModTime) @@ -148,7 +147,7 @@ func (i *Index) checkFix(force bool) { i.logFile(STATUS_UPDATE, name) i.setMod(true) } else if amod > bmod { - i.logFile(STATUS_WARN_OLD, name) + i.logFile(STATUS_UP_WARN_OLD, name) i.setMod(true) } } diff --git a/scripts/run_test_prep b/scripts/run_test_prep index f8d561a..e8b2d61 100755 --- a/scripts/run_test_prep +++ b/scripts/run_test_prep @@ -1,13 +1,14 @@ #!/bin/bash export TZ='UTC' -root="/tmp/chkbit" +test_dir="/tmp/chkbit" go run scripts/run_test_prep.go -cd $root/root -mv $root/root/people $root/people +cd $test_dir/root +mv $test_dir/root/people $test_dir/people ln -s ../people people ln -s ../../people/face/office-door.pdf day/friend/office-door.pdf + find -L -type f | wc -l diff --git a/scripts/run_tests b/scripts/run_tests index fe0537d..4e5315c 100755 --- a/scripts/run_tests +++ b/scripts/run_tests @@ -4,10 +4,10 @@ set -e export TZ='UTC' script_dir=$(dirname "$(realpath "$0")") base_dir=$(dirname "$script_dir") -#dir=$(realpath "$script_dir/../testdata/run_test") -root="/tmp/chkbit/root" +test_dir="/tmp/chkbit" +function chkbit() { "$base_dir/chkbit" "$@"; } -if [[ ! -d $root ]]; then +if [[ ! -d $test_dir ]]; then echo "must run run_test_prep first" exit 1 fi @@ -15,7 +15,32 @@ fi # setup $script_dir/build +cd $test_dir -"$base_dir/chkbit" -u /tmp/chkbit/root +chkbit -u root # todo: validate (300 files, 1 symlink dir followed, 1 symlink file ignored) + +echo "- test damage" + +rm -rf $test_dir/test_dmg +mkdir $test_dir/test_dmg +cd $test_dir/test_dmg + +echo "-- create test and set the modified time" +echo foo1 > test; touch -t 201501010001 test +# says new: +chkbit -u . + +echo "-- update test & old modified (expect 'old')" +echo foo2 > test; touch -t 201501010000 test +chkbit -u . + +echo "-- update test & new modified (expect 'upd')" +echo foo3 > test; touch -t 201501010002 test +chkbit -u . + +echo "-- Now update test with the same modified to simulate damage (expect DMG)" +echo foo4 > test; touch -t 201501010002 test +chkbit -u . + diff --git a/status.go b/status.go index 48fdf8f..4fd09b8 100644 --- a/status.go +++ b/status.go @@ -4,14 +4,14 @@ type Status string const ( STATUS_PANIC Status = "EXC" - STATUS_ERR_DMG Status = "DMG" STATUS_ERR_IDX Status = "EIX" - STATUS_WARN_OLD Status = "old" - STATUS_NEW Status = "new" + STATUS_ERR_DMG Status = "DMG" + STATUS_UPDATE_INDEX Status = "iup" + STATUS_UP_WARN_OLD Status = "old" STATUS_UPDATE Status = "upd" + STATUS_NEW Status = "new" STATUS_OK Status = "ok " STATUS_IGNORE Status = "ign" - STATUS_UPDATE_INDEX Status = "iup" ) func (s Status) String() string { @@ -19,7 +19,7 @@ func (s Status) String() string { } func (s Status) IsErrorOrWarning() bool { - return s == STATUS_PANIC || s == STATUS_ERR_DMG || s == STATUS_ERR_IDX || s == STATUS_WARN_OLD + return s == STATUS_PANIC || s == STATUS_ERR_DMG || s == STATUS_ERR_IDX || s == STATUS_UP_WARN_OLD } func (s Status) IsVerbose() bool {