@@ -8,11 +8,13 @@ import (
8
8
"io"
9
9
"io/ioutil"
10
10
"log"
11
+ "math/rand"
11
12
"net/http"
12
13
"os"
13
14
"path/filepath"
14
15
"regexp"
15
16
"runtime"
17
+ "strconv"
16
18
"strings"
17
19
"syscall"
18
20
"time"
@@ -559,16 +561,16 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
559
561
return
560
562
}
561
563
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 )
564
566
if os .IsNotExist (err ) {
565
567
// the error is caused by a missing directory, create it and retry
566
568
mkdirErr := os .MkdirAll (filepath .Dir (path ), h .opt .DirMode )
567
569
if mkdirErr != nil {
568
570
log .Print (mkdirErr )
569
571
} else {
570
572
// try again
571
- tf , err = ioutil . TempFile ( filepath . Dir ( path ), tmpFn )
573
+ tf , err = tempFile ( tmpFn , h . opt . FileMode )
572
574
}
573
575
}
574
576
if err != nil {
@@ -660,6 +662,19 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
660
662
h .sendMetric (objectType , BlobWrite , uint64 (written ))
661
663
}
662
664
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
+
663
678
func syncDir (dirname string ) error {
664
679
if runtime .GOOS == "windows" {
665
680
// syncing a directory is not possible on windows
0 commit comments