From: Sergey Matveev Date: Wed, 4 Dec 2019 13:39:57 +0000 (+0300) Subject: TempFile is not friendly with umask. Replace with simpler one X-Git-Tag: v2.0.0~11 X-Git-Url: http://www.git.cypherpunks.ru/?p=gocheese.git;a=commitdiff_plain;h=2b83ac5db8551f70614672b2d326272b35d064d3 TempFile is not friendly with umask. Replace with simpler one --- diff --git a/gocheese.go b/gocheese.go index 2245d5c..9b74ee7 100644 --- a/gocheese.go +++ b/gocheese.go @@ -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 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 + +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 . +*/ + +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)) +}