Skip to content

Commit

Permalink
No need for the responses subdirectory, purge protection.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Apr 10, 2024
1 parent a1c679d commit e9ac2e8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 57 deletions.
38 changes: 11 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 2 additions & 9 deletions purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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()) {
Expand Down
30 changes: 9 additions & 21 deletions purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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")
}
}

0 comments on commit e9ac2e8

Please sign in to comment.