auto delete folders
parent
684dab3a56
commit
b84606cd71
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"jannex/log-manager/modules/cache"
|
||||
"jannex/log-manager/modules/config"
|
||||
"jannex/log-manager/modules/structs"
|
||||
|
@ -213,6 +214,27 @@ func CheckForDeletableLogs() {
|
|||
Logs: []string{"I " + utils.GetTime() + "LogManager: Deleted log file " + path + " because it was older than " + strconv.Itoa(daysToKeepLogs) + " days."},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Check if the directory is empty.
|
||||
isEmpty, err := isDirEmpty(path)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error checking %s: %v\n", path, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if isEmpty {
|
||||
fmt.Printf("Deleting empty directory: %s\n", path)
|
||||
|
||||
if err := os.Remove(path); err != nil {
|
||||
fmt.Printf("Error deleting %s: %v\n", path, err)
|
||||
}
|
||||
|
||||
AddLog(structs.LogBody{
|
||||
Type: "system",
|
||||
Logs: []string{"I " + utils.GetTime() + "LogManager: Deleted empty log folder " + path},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -222,3 +244,27 @@ func CheckForDeletableLogs() {
|
|||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func isDirEmpty(dirPath string) (bool, error) {
|
||||
dir, err := os.Open(dirPath)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer dir.Close()
|
||||
|
||||
_, err = dir.Readdirnames(1) // Try to read an entry from the directory.
|
||||
|
||||
if err == nil {
|
||||
// There is at least one entry in the directory; it's not empty.
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
// The directory is empty.
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue