74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package database
|
|
|
|
import (
|
|
"eden-server/internal/utility"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func watchdog(env utility.Environment, db *gorm.DB) {
|
|
slog.Info("performing the watchdog cycle")
|
|
|
|
fsFiles, err := os.ReadDir(env.ContentDirectory)
|
|
if err != nil {
|
|
slog.Error("failed to read the content directory on the filesystem", "error", err)
|
|
return
|
|
}
|
|
|
|
dbFiles, err := GetFiles(db)
|
|
if err != nil {
|
|
slog.Error("failed to retrieve the files indexed from the database", "error", err)
|
|
return
|
|
}
|
|
|
|
// generate a set of filesystem contents
|
|
fsSet := make(map[string]struct{}) // cool name for the files that are (now) marked for annihilation
|
|
for _, f := range fsFiles {
|
|
// absolute path creation
|
|
fullPath := filepath.Join(env.ContentDirectory, f.Name())
|
|
fsSet[fullPath] = struct{}{}
|
|
}
|
|
|
|
// generate a set of Database contents
|
|
dbSet := make(map[string]File) // cool name for the files that are going to be deregistered from the database
|
|
for _, f := range dbFiles {
|
|
dbSet[f.FilePath] = f
|
|
}
|
|
|
|
// FS -> DB
|
|
// check for orphaned filesystem files
|
|
var fsPurgeScroll []string
|
|
for path := range fsSet {
|
|
if _, exists := dbSet[path]; !exists {
|
|
fsPurgeScroll = append(fsPurgeScroll, path)
|
|
}
|
|
}
|
|
|
|
// DB -> FS
|
|
// check stale database records
|
|
var dbPurgeScroll []File
|
|
for path, f := range dbSet {
|
|
if _, exists := fsSet[path]; !exists {
|
|
dbPurgeScroll = append(dbPurgeScroll, f)
|
|
}
|
|
}
|
|
|
|
if len(fsPurgeScroll) > 0 {
|
|
slog.Info("filesystem purge scroll is populated, engaging purge")
|
|
//filepath is stored in the slice, the filename or file object, see above
|
|
for _, fp := range fsPurgeScroll {
|
|
utility.RemoveFile(fp)
|
|
}
|
|
}
|
|
|
|
if len(dbPurgeScroll) > 0 {
|
|
slog.Info("database purge scroll is populated, engaging purge")
|
|
for _, f := range dbPurgeScroll {
|
|
DeregisterFile(db, f)
|
|
}
|
|
}
|
|
}
|