Skip to content

Commit e9900b7

Browse files
authored
Merge pull request #171 from MichaelEischer/fix-file-permissions
Honour repo.FileMode permissions
2 parents 959250f + 51ab8e9 commit e9900b7

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

repo/repo.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"io"
99
"io/ioutil"
1010
"log"
11+
"math/rand"
1112
"net/http"
1213
"os"
1314
"path/filepath"
1415
"regexp"
1516
"runtime"
17+
"strconv"
1618
"strings"
1719
"syscall"
1820
"time"
@@ -559,16 +561,16 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
559561
return
560562
}
561563

562-
tmpFn := objectID + ".rest-server-temp"
563-
tf, err := ioutil.TempFile(filepath.Dir(path), tmpFn)
564+
tmpFn := filepath.Join(filepath.Dir(path), objectID+".rest-server-temp")
565+
tf, err := tempFile(tmpFn, h.opt.FileMode)
564566
if os.IsNotExist(err) {
565567
// the error is caused by a missing directory, create it and retry
566568
mkdirErr := os.MkdirAll(filepath.Dir(path), h.opt.DirMode)
567569
if mkdirErr != nil {
568570
log.Print(mkdirErr)
569571
} else {
570572
// try again
571-
tf, err = ioutil.TempFile(filepath.Dir(path), tmpFn)
573+
tf, err = tempFile(tmpFn, h.opt.FileMode)
572574
}
573575
}
574576
if err != nil {
@@ -660,6 +662,19 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
660662
h.sendMetric(objectType, BlobWrite, uint64(written))
661663
}
662664

665+
// tempFile implements a custom version of ioutil.TempFile which allows modifying the file permissions
666+
func tempFile(fn string, perm os.FileMode) (f *os.File, err error) {
667+
for i := 0; i < 10; i++ {
668+
name := fn + strconv.FormatInt(rand.Int63(), 10)
669+
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)
670+
if os.IsExist(err) {
671+
continue
672+
}
673+
break
674+
}
675+
return
676+
}
677+
663678
func syncDir(dirname string) error {
664679
if runtime.GOOS == "windows" {
665680
// syncing a directory is not possible on windows

0 commit comments

Comments
 (0)