partage

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/partage.git
Log | Files | Refs

commit cc7caa5aec5806d9892516c8074d8b7632638f7e
parent 18aa29fccc0769886a79c7171c407cede717c6c4
Author: Willy Goiffon <dev@z3bra.org>
Date:   Tue, 19 Oct 2021 14:03:15 +0200

Add partage-trash to cleanup expired files

Diffstat:
Mmkfile | 17+++++++++++++----
Apartage-trash/partage-trash.go | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/mkfile b/mkfile @@ -1,21 +1,30 @@ <config.mk -all:V: partage +all:V: partage partage-trash/partage-trash %: %.go $GO build -o $stem clean:V: - rm -f partage + rm -f partage partage-trash/partage-trash -install:V: partage +install:V: partage partage-trash/partage-trash mkdir -p ${DESTDIR}${PREFIX}/bin cp partage ${DESTDIR}${PREFIX}/bin/partage + cp partage-trash/partage-trash ${DESTDIR}${PREFIX}/bin/partage-trash chmod 755 ${DESTDIR}${PREFIX}/bin/partage + chmod 755 ${DESTDIR}${PREFIX}/bin/partage-trash mkdir -p ${DESTDIR}${MANDIR}/man1 - cp partage.1 ${DESTDIR}${MANDIR}/man1/partage.1 + cp man/partage.1 ${DESTDIR}${MANDIR}/man1/partage.1 + cp man/partage-trash.1 ${DESTDIR}${MANDIR}/man1/partage-trash.1 + cp man/partage.conf.5 ${DESTDIR}${MANDIR}/man5/partage.conf.5 chmod 644 ${DESTDIR}${MANDIR}/man1/partage.1 + chmod 644 ${DESTDIR}${MANDIR}/man1/partage-trash.1 + chmod 644 ${DESTDIR}${MANDIR}/man5/partage.conf.5 uninstall:V: rm ${DESTDIR}${PREFIX}/bin/partage + rm ${DESTDIR}${PREFIX}/bin/partage-trash rm ${DESTDIR}${MANDIR}/man1/partage.1 + rm ${DESTDIR}${MANDIR}/man1/partage-trash.1 + rm ${DESTDIR}${MANDIR}/man5/partage.conf.5 diff --git a/partage-trash/partage-trash.go b/partage-trash/partage-trash.go @@ -0,0 +1,83 @@ +package main + +import ( + "log" + "flag" + "os" + "time" + "path/filepath" + "encoding/json" + + "github.com/dustin/go-humanize" +) + +type metadata struct { + Filename string + Size int64 + Expiry int64 +} + +var conf struct { + filepath string + metapath string +} + +var verbose bool +var count int64 +var size int64 + +func readmeta(filename string) (metadata, error) { + j, err := os.ReadFile(filename) + if err != nil { + return metadata{}, err + } + + var meta metadata + err = json.Unmarshal(j, &meta) + if err != nil { + return metadata{}, err + } + + return meta, nil +} + +func checkexpiry(path string, info os.FileInfo, err error) error { + if filepath.Ext(path) != ".json" { + return nil + } + meta, err := readmeta(path) + + now := time.Now().Unix() + + if meta.Expiry > 0 && now >= meta.Expiry { + if verbose { + expiration := humanize.Time(time.Unix(meta.Expiry, 0)) + log.Printf("%s/%s expired %s\n", conf.filepath, meta.Filename, expiration) + } + os.Remove(conf.filepath + "/" + meta.Filename) + os.Remove(path) + return nil + } else { + count++ + size += meta.Size + } + + return nil +} + +func main() { + flag.BoolVar(&verbose, "v", false, "Verbose logging") + flag.StringVar(&conf.filepath, "f", "./files", "Directory containing files") + flag.StringVar(&conf.metapath, "m", "./meta", "Directory containing metadata") + + flag.Parse() + + err := filepath.Walk(conf.metapath, checkexpiry) + if err != nil { + log.Fatal(err) + } + + if verbose && count > 0 { + log.Printf("%d file(s) remain on disk (total: %s)", count, humanize.IBytes(uint64(size))) + } +}