Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add backup flag to an intvalue, if enable, it will do backup and keep… #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"text/template"
Expand Down Expand Up @@ -86,6 +87,7 @@ var (
tlsKey string
users map[string]string
version bool
backup int
build string
)

Expand All @@ -106,6 +108,7 @@ func init() {
flag.StringVar(&auth, "auth", "basic", "Enable HTTP Basic Authentication.")
flag.BoolVar(&genHtpass, "gen", false, "Generate a .htpasswd file or add a new entry to an existing file.")
flag.BoolVar(&version, "v", false, "Show version and exit.")
flag.IntVar(&backup, "backup", 0, "Keep last backup number when flag set enable")
flag.Parse()

// These are OpenBSD specific protections used to prevent unnecessary file access.
Expand Down Expand Up @@ -144,6 +147,12 @@ func logger(f http.HandlerFunc) http.HandlerFunc {
r.ContentLength,
)
f(w, r)

if backup > 0 && r.Method == "PUT" {
path := davDir + "/backup/"
name := r.URL.Path
backupFiles(path, name)
}
}
}

Expand Down Expand Up @@ -426,3 +435,58 @@ func main() {
log.Fatalln(s.Serve(lis))
}
}

type ByModTime []os.FileInfo

func (fis ByModTime) Len() int {
return len(fis)
}

func (fis ByModTime) Swap(i, j int) {
fis[i], fis[j] = fis[j], fis[i]
}

func (fis ByModTime) Less(i, j int) bool {
return fis[i].ModTime().Before(fis[j].ModTime())
}

func sortFile(path string) (fis ByModTime) {
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
fis, err = f.Readdir(-1)
if err != nil {
fmt.Println(err)
}
defer f.Close()

sort.Sort(ByModTime(fis))
return
}

func backupFiles(path, name string) error {
_, fErr := os.Stat(path)
if os.IsNotExist(fErr) {
log.Printf("creating %q\n", path)
wErr := os.Mkdir(path, 755)
if wErr != nil {
return wErr
}
}

timestamp := time.Now().Format("20060102.150405")
filename := path + name + "." + timestamp
files := sortFile(path)
if len(files) > backup {
for k, _ := range files[backup:] {
err := os.Remove(path + files[k].Name())
if err != nil {
log.Fatal(err)
}
}
}
input, err := ioutil.ReadFile(path + ".." + name)
ioutil.WriteFile(filename, input, 0644)
return nil
}