]> Cypherpunks.ru repositories - gocheese.git/blob - fileutils.go
Unify copyright comment format
[gocheese.git] / fileutils.go
1 // GoCheese -- Python private package repository and caching proxy
2 // Copyright (C) 2019-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
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.
7 //
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.
12 //
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/>.
15
16 package main
17
18 import (
19         "log"
20         "net/http"
21         "os"
22         "path/filepath"
23         "time"
24 )
25
26 var (
27         NoSync   = os.Getenv("GOCHEESE_NO_SYNC") == "1"
28         UmaskCur int
29 )
30
31 func TempFile(dir string) (*os.File, error) {
32         tmp, err := os.CreateTemp(dir, "gocheese")
33         if err != nil {
34                 return nil, err
35         }
36         err = os.Chmod(tmp.Name(), os.FileMode(0666&^UmaskCur))
37         if err != nil {
38                 tmp.Close()
39                 return nil, err
40         }
41         return tmp, nil
42 }
43
44 func DirSync(dirPath string) error {
45         if NoSync {
46                 return nil
47         }
48         fd, err := os.Open(dirPath)
49         if err != nil {
50                 return err
51         }
52         err = fd.Sync()
53         if err != nil {
54                 fd.Close()
55                 return err
56         }
57         return fd.Close()
58 }
59
60 func WriteFileSync(dirPath, filePath string, data []byte, mtime time.Time) error {
61         dst, err := TempFile(dirPath)
62         if err != nil {
63                 return err
64         }
65         if _, err = dst.Write(data); err != nil {
66                 os.Remove(dst.Name())
67                 dst.Close()
68                 return err
69         }
70         if !NoSync {
71                 if err = dst.Sync(); err != nil {
72                         os.Remove(dst.Name())
73                         dst.Close()
74                         return err
75                 }
76         }
77         dst.Close()
78         if err = os.Chtimes(dst.Name(), mtime, mtime); err != nil {
79                 return err
80         }
81         if err = os.Rename(dst.Name(), filePath); err != nil {
82                 return err
83         }
84         return DirSync(dirPath)
85 }
86
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)
93                         return false
94                 }
95                 log.Println(r.RemoteAddr, "mkdir", pkgName)
96         }
97         return true
98 }