1 // GoCheese -- Python private package repository and caching proxy
2 // Copyright (C) 2019-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 NoSync = os.Getenv("GOCHEESE_NO_SYNC") == "1"
31 func TempFile(dir string) (*os.File, error) {
32 tmp, err := os.CreateTemp(dir, "gocheese")
36 err = os.Chmod(tmp.Name(), os.FileMode(0666&^UmaskCur))
44 func DirSync(dirPath string) error {
48 fd, err := os.Open(dirPath)
60 func WriteFileSync(dirPath, filePath string, data []byte, mtime time.Time) error {
61 dst, err := TempFile(dirPath)
65 if _, err = dst.Write(data); err != nil {
71 if err = dst.Sync(); err != nil {
78 if err = os.Chtimes(dst.Name(), mtime, mtime); err != nil {
81 if err = os.Rename(dst.Name(), filePath); err != nil {
84 return DirSync(dirPath)
87 func mkdirForPkg(w http.ResponseWriter, r *http.Request, pkgName string) bool {
88 path := filepath.Join(Root, pkgName)
89 if _, err := os.Stat(path); os.IsNotExist(err) {
90 if err = os.Mkdir(path, os.FileMode(0777)); err != nil {
91 log.Println("error", r.RemoteAddr, "mkdir", pkgName, err)
92 http.Error(w, err.Error(), http.StatusInternalServerError)
95 log.Println(r.RemoteAddr, "mkdir", pkgName)