X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=fileutils.go;h=218b800156d086093dea82767ee351a3c0b289de;hb=9abd4f096961adf76de6fd3c5898a447a3ff81cf;hp=03b852e011a2e9cbfcbd2af84e9fe3fd78f62f0f;hpb=37facf3dde161d2df4d430896dc87ebb5740cc64;p=gocheese.git diff --git a/fileutils.go b/fileutils.go index 03b852e..218b800 100644 --- a/fileutils.go +++ b/fileutils.go @@ -18,6 +18,8 @@ along with this program. If not, see . package main import ( + "log" + "net/http" "os" "path/filepath" "strconv" @@ -49,7 +51,7 @@ func DirSync(dirPath string) error { return fd.Close() } -func WriteFileSync(dirPath, filePath string, data []byte) error { +func WriteFileSync(dirPath, filePath string, data []byte, mtime time.Time) error { dst, err := TempFile(dirPath) if err != nil { return err @@ -67,8 +69,24 @@ func WriteFileSync(dirPath, filePath string, data []byte) error { } } dst.Close() + if err = os.Chtimes(dst.Name(), mtime, mtime); err != nil { + return err + } if err = os.Rename(dst.Name(), filePath); err != nil { return err } return DirSync(dirPath) } + +func mkdirForPkg(w http.ResponseWriter, r *http.Request, pkgName string) bool { + path := filepath.Join(Root, pkgName) + if _, err := os.Stat(path); os.IsNotExist(err) { + if err = os.Mkdir(path, os.FileMode(0777)); err != nil { + log.Println("error", r.RemoteAddr, "mkdir", pkgName, err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return false + } + log.Println(r.RemoteAddr, "mkdir", pkgName) + } + return true +}