From f02ee7386a87d1316352b492fa83722da30d6f60 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Fri, 5 Jan 2018 17:16:06 +0100 Subject: [PATCH] Auto create data/ subdirs on demand Closes #40 --- README.md | 4 ---- handlers.go | 11 +++++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fa69edd..8611601 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,6 @@ Rest Server requires Go 1.7 or higher to build. The only tested compiler is the The required version of restic backup client to use with Rest Server is [v0.7.1](https://github.com/restic/restic/releases/tag/v0.7.1) or higher. -If you have a local repository created with an older version of restic client, which you would now like to serve via Rest Server, you need to first create missing subdirectories in the data directory. Run this simple one-liner in the repository directory: - -```for i in {0..255}; do mkdir -p $(printf "data/%02x" $i); done``` - ## Installation ### From source diff --git a/handlers.go b/handlers.go index 25903db..09b4956 100644 --- a/handlers.go +++ b/handlers.go @@ -367,6 +367,17 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) { } tf, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600) + if os.IsNotExist(err) { + // the error is caused by a missing directory, create it and retry + mkdirErr := os.MkdirAll(filepath.Dir(path), 0700) + if mkdirErr != nil { + log.Print(mkdirErr) + } else { + // try again + tf, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600) + } + } + if err != nil { if Config.Debug { log.Print(err)