]> Cypherpunks.ru repositories - gocheese.git/blobdiff - fileutils.go
Metadata, mtime support. Massive refactoring
[gocheese.git] / fileutils.go
index 03b852e011a2e9cbfcbd2af84e9fe3fd78f62f0f..b37b99ce4c79aa01c2e56534c9a2887767e0e6a8 100644 (file)
@@ -18,6 +18,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 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
+}