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" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -21,6 +22,31 @@ func isHashed(dir string) bool {
return dir == "data" 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 // AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
// stored in f and returns the http.HandlerFunc. // stored in f and returns the http.HandlerFunc.
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc { func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
@ -172,6 +198,12 @@ func SaveBlob(c *Context) http.HandlerFunc {
dir := vars[1] dir := vars[1]
name := vars[2] 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) tmp := filepath.Join(c.path, "tmp", name)
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600) tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)

28
main.go
View file

@ -2,7 +2,6 @@ package main
import ( import (
"flag" "flag"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -10,31 +9,6 @@ import (
"runtime/pprof" "runtime/pprof"
) )
func createDirectories(path string) {
log.Println("Creating repository directories")
dirs := []string{
"data",
"index",
"keys",
"locks",
"snapshots",
"tmp",
}
for _, d := range dirs {
if err := os.MkdirAll(filepath.Join(path, d), 0700); err != nil {
log.Fatal(err)
}
}
for i := 0; i < 256; i++ {
if err := os.MkdirAll(filepath.Join(path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
log.Fatal(err)
}
}
}
func setupRoutes(path string) *Router { func setupRoutes(path string) *Router {
context := &Context{path} context := &Context{path}
@ -72,8 +46,6 @@ func main() {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
createDirectories(*path)
router := setupRoutes(*path) router := setupRoutes(*path)
var handler http.Handler var handler http.Handler