added cleanup.go

This commit is contained in:
Altair-sh
2026-11-06 23:12:09 +01:00
parent 509668088f
commit faa1eca7c3
3 changed files with 44 additions and 4 deletions
+40
View File
@@ -0,0 +1,40 @@
package server
import (
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
)
// PUT writes into filePath+UploadingSuffix and renames it when the body is received
const UploadingSuffix = ".uploading"
// RemoveUploadingFiles deletes UploadingSuffix files left by interrupted uploads
// (server crash, kill -9). Call it before the server starts, while nothing is uploading.
// Errors are logged, not returned: a share that can't be cleaned is still usable.
func (shares Shares) RemoveUploadingFiles() {
for _, sh := range shares {
// WalkDir doesn't follow symlinks, so it doesn't leave the share
_ = filepath.WalkDir(sh.Path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
confLogger.Warn("cleanup: ", err)
return nil // skip unreadable entry, continue walking
}
if !d.Type().IsRegular() || !strings.HasSuffix(d.Name(), UploadingSuffix) {
return nil
}
err = os.Remove(path)
switch {
case err == nil:
confLogger.Info("cleanup: removed ", path)
// nested shares (shared/ and shared/private) walk the same files twice
case errors.Is(err, fs.ErrNotExist):
default:
confLogger.Warn("cleanup: ", err)
}
return nil
})
}
}