From e9ac2e89a0868b6c723250f72cac52d31111a203 Mon Sep 17 00:00:00 2001 From: LTLA Date: Wed, 10 Apr 2024 09:27:45 -0700 Subject: [PATCH] No need for the responses subdirectory, purge protection. --- main.go | 38 +++++++++++--------------------------- purge.go | 11 ++--------- purge_test.go | 30 +++++++++--------------------- 3 files changed, 22 insertions(+), 57 deletions(-) diff --git a/main.go b/main.go index edb1384..64bb552 100644 --- a/main.go +++ b/main.go @@ -54,21 +54,6 @@ func main() { globals.Administrators = strings.Split(*mstr, ",") } - // Setting up special subdirectories. - response_name := "responses" - response_dir := filepath.Join(staging, response_name) - if _, err := os.Stat(response_dir); errors.Is(err, os.ErrNotExist) { - err := os.Mkdir(response_dir, 0755) - if err != nil { - log.Fatalf("failed to create a 'responses' subdirectory in %q; %v", staging, err) - } - } else { - err := os.Chmod(response_dir, 0755) - if err != nil { - log.Fatalf("failed to validate permissions for the 'responses' subdirectory in %q; %v", staging, err) - } - } - log_dir := filepath.Join(globals.Registry, logDirName) if _, err := os.Stat(log_dir); errors.Is(err, os.ErrNotExist) { err := os.Mkdir(log_dir, 0755) @@ -78,9 +63,15 @@ func main() { } // Launching a watcher to pick up changes and launch jobs. - http.HandleFunc("POST /new/{path}", func(w http.ResponseWriter, r *http.Request) { - reqpath := r.PathValue("path") - log.Println("processing " + reqpath) + http.HandleFunc("/new/{path}", func(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + dumpErrorResponse(w, http.StatusMethodNotAllowed, "expected a POST request", "???") + return + } + + path := filepath.Base(r.PathValue("path")) + log.Println("processing " + path) + reqpath := filepath.Join(staging, path) info, err := os.Stat(reqpath) if err != nil { @@ -159,23 +150,16 @@ func main() { // Adding a per-day job that purges various old files. ticker := time.NewTicker(time.Hour * 24) defer ticker.Stop() - protected := map[string]bool{} - protected[response_name] = true go func() { for { <-ticker.C - err := purgeOldFiles(staging, time.Hour * 24 * 7, protected) - if err != nil { - log.Println(err) - } - - err = purgeOldFiles(response_dir, time.Hour * 24 * 7, nil) + err := purgeOldFiles(staging, time.Hour * 24 * 7) if err != nil { log.Println(err) } - err = purgeOldFiles(log_dir, time.Hour * 24 * 7, nil) + err = purgeOldFiles(log_dir, time.Hour * 24 * 7) if err != nil { log.Println(err) } diff --git a/purge.go b/purge.go index 25e6531..421187e 100644 --- a/purge.go +++ b/purge.go @@ -8,7 +8,7 @@ import ( "errors" ) -func purgeOldFiles(dir string, limit time.Duration, protected map[string]bool) error { +func purgeOldFiles(dir string, limit time.Duration) error { var to_delete []string present := time.Now() messages := []string{} @@ -24,14 +24,7 @@ func purgeOldFiles(dir string, limit time.Duration, protected map[string]bool) e delta := present.Sub(info.ModTime()) if (delta > limit) { - is_protected := false - if protected != nil { - rel, _ := filepath.Rel(dir, path) - _, is_protected = protected[rel] - } - if !is_protected { - to_delete = append(to_delete, path) - } + to_delete = append(to_delete, path) } if (info.IsDir()) { diff --git a/purge_test.go b/purge_test.go index 8881691..3fd1673 100644 --- a/purge_test.go +++ b/purge_test.go @@ -49,7 +49,7 @@ func TestPurgeOldFiles (t *testing.T) { } // Deleting with a 1-hour expiry. - err = purgeOldFiles(dir, 1 * time.Hour, nil) + err = purgeOldFiles(dir, 1 * time.Hour) if (err != nil) { t.Fatal(err) } @@ -63,27 +63,8 @@ func TestPurgeOldFiles (t *testing.T) { t.Error("should not have deleted this file") } - // Deleting with an immediate expiry but also protection. - err = purgeOldFiles(dir, 0 * time.Hour, map[string]bool{ "A": true, "sub": true }) - if (err != nil) { - t.Fatal(err) - } - if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { - t.Error("should not have deleted this file") - } - if _, err := os.Stat(subdir); errors.Is(err, os.ErrNotExist) { - t.Error("should not have deleted this directory") - } - - if _, err := os.Stat(sympath); !errors.Is(err, os.ErrNotExist) { // Symlink can be deleted, but not its target. - t.Error("should have deleted the symlink") - } - if _, err := os.Stat(target); errors.Is(err, os.ErrNotExist) { - t.Error("should not have deleted the symlink target") - } - // Deleting with an immediate expiry. - err = purgeOldFiles(dir, 0 * time.Hour, nil) + err = purgeOldFiles(dir, 0 * time.Hour) if (err != nil) { t.Fatal(err) } @@ -96,4 +77,11 @@ func TestPurgeOldFiles (t *testing.T) { if _, err := os.Stat(subdir); !errors.Is(err, os.ErrNotExist) { t.Error("should have deleted this directory") } + + if _, err := os.Stat(sympath); !errors.Is(err, os.ErrNotExist) { // Symlink can be deleted, but not its target. + t.Error("should have deleted the symlink") + } + if _, err := os.Stat(target); errors.Is(err, os.ErrNotExist) { + t.Error("should not have deleted the symlink target") + } }