Create directories before key setup, not on startup

This commit is contained in:
Zlatko Čalušić 2016-12-27 01:35:45 +01:00
parent 75c1eae7f2
commit 061d31829d
2 changed files with 32 additions and 28 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
@ -21,6 +22,31 @@ func isHashed(dir string) bool {
return dir == "data"
}
func createDirectories(c *Context) {
log.Println("Creating repository directories")
dirs := []string{
"data",
"index",
"keys",
"locks",
"snapshots",
"tmp",
}
for _, d := range dirs {
if err := os.MkdirAll(filepath.Join(c.path, d), 0700); err != nil {
log.Fatal(err)
}
}
for i := 0; i < 256; i++ {
if err := os.MkdirAll(filepath.Join(c.path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
log.Fatal(err)
}
}
}
// AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
// stored in f and returns the http.HandlerFunc.
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
@ -172,6 +198,12 @@ func SaveBlob(c *Context) http.HandlerFunc {
dir := vars[1]
name := vars[2]
if dir == "keys" {
if _, err := os.Stat("keys"); err != nil && os.IsNotExist(err) {
createDirectories(c)
}
}
tmp := filepath.Join(c.path, "tmp", name)
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)