]> Cypherpunks.ru repositories - gocheese.git/commitdiff
TempFile is not friendly with umask. Replace with simpler one
authorSergey Matveev <stargrave@stargrave.org>
Wed, 4 Dec 2019 13:39:57 +0000 (16:39 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 4 Dec 2019 13:40:44 +0000 (16:40 +0300)
gocheese.go
tmp.go [new file with mode: 0644]

index 2245d5cafc5743f6b1a6a73d9464497437c0c5cb..9b74ee7d5cf729b6f6f0faf09fb108b26ca896bc 100644 (file)
@@ -154,7 +154,7 @@ func refreshDir(w http.ResponseWriter, r *http.Request, dir, filenameGet string)
                        }
                        defer resp.Body.Close()
                        hasher := sha256.New()
-                       dst, err := ioutil.TempFile(filepath.Join(*root, dir), "")
+                       dst, err := TempFile(filepath.Join(*root, dir))
                        if err != nil {
                                http.Error(w, err.Error(), http.StatusInternalServerError)
                                return false
@@ -322,7 +322,7 @@ func serveUpload(w http.ResponseWriter, r *http.Request) {
                        http.Error(w, err.Error(), http.StatusInternalServerError)
                        return
                }
-               dst, err = ioutil.TempFile(dirPath, "")
+               dst, err = TempFile(dirPath)
                if err != nil {
                        http.Error(w, err.Error(), http.StatusInternalServerError)
                        return
diff --git a/tmp.go b/tmp.go
new file mode 100644 (file)
index 0000000..876e23d
--- /dev/null
+++ b/tmp.go
@@ -0,0 +1,32 @@
+/*
+GoCheese -- Python private package repository and caching proxy
+Copyright (C) 2019 Sergey Matveev <stargrave@stargrave.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 3 of the License.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package main
+
+import (
+       "os"
+       "path/filepath"
+       "strconv"
+       "time"
+)
+
+func TempFile(dir string) (*os.File, error) {
+       // Assume that probability of suffix collision is negligible
+       suffix := strconv.FormatInt(time.Now().UnixNano()+int64(os.Getpid()), 16)
+       name := filepath.Join(dir, "nncp"+suffix)
+       return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
+}