Merge pull request #171 from MichaelEischer/fix-file-permissions

Honour repo.FileMode permissions
This commit is contained in:
Alexander Neumann 2022-02-10 19:51:57 +01:00 committed by GitHub
commit e9900b7a00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,11 +8,13 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"strconv"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -559,8 +561,8 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
return return
} }
tmpFn := objectID + ".rest-server-temp" tmpFn := filepath.Join(filepath.Dir(path), objectID+".rest-server-temp")
tf, err := ioutil.TempFile(filepath.Dir(path), tmpFn) tf, err := tempFile(tmpFn, h.opt.FileMode)
if os.IsNotExist(err) { if os.IsNotExist(err) {
// the error is caused by a missing directory, create it and retry // the error is caused by a missing directory, create it and retry
mkdirErr := os.MkdirAll(filepath.Dir(path), h.opt.DirMode) mkdirErr := os.MkdirAll(filepath.Dir(path), h.opt.DirMode)
@ -568,7 +570,7 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
log.Print(mkdirErr) log.Print(mkdirErr)
} else { } else {
// try again // try again
tf, err = ioutil.TempFile(filepath.Dir(path), tmpFn) tf, err = tempFile(tmpFn, h.opt.FileMode)
} }
} }
if err != nil { if err != nil {
@ -660,6 +662,19 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
h.sendMetric(objectType, BlobWrite, uint64(written)) h.sendMetric(objectType, BlobWrite, uint64(written))
} }
// tempFile implements a custom version of ioutil.TempFile which allows modifying the file permissions
func tempFile(fn string, perm os.FileMode) (f *os.File, err error) {
for i := 0; i < 10; i++ {
name := fn + strconv.FormatInt(rand.Int63(), 10)
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)
if os.IsExist(err) {
continue
}
break
}
return
}
func syncDir(dirname string) error { func syncDir(dirname string) error {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// syncing a directory is not possible on windows // syncing a directory is not possible on windows