From 1e3948724052366aaa49878c7872365ed5532e69 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 22 Aug 2020 22:56:44 -0700 Subject: [PATCH] poc: when deleting a file automatically adjust the current disk space being tracked in the cache --- server/filesystem.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server/filesystem.go b/server/filesystem.go index ad0a452..6eddf96 100644 --- a/server/filesystem.go +++ b/server/filesystem.go @@ -616,6 +616,19 @@ func (fs *Filesystem) Delete(p string) error { return errors.New("cannot delete root server directory") } + if st, err := os.Stat(resolved); err != nil { + if !os.IsNotExist(err) { + fs.Server.Log().WithField("error", err).WithField("path", resolved).Warn("error while attempting to stat file before deletion") + } + } else { + // TODO: handle deleting a directory by finding its current size. + go func (st os.FileInfo) { + if !st.IsDir() { + atomic.SwapInt64(&fs.diskUsage, fs.diskUsage - st.Size()) + } + }(st) + } + return os.RemoveAll(resolved) }