func initDebugLogger() { debugfile := os.Getenv("DEBUG_LOG") if debugfile == "" { return } fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile) f, err := fs.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600) if err == nil { _, err = f.Seek(2, 0) if err != nil { fmt.Fprintf(os.Stderr, "unable to seek to the end of %v: %v\n", debugfile, err) os.Exit(3) } } if err != nil && os.IsNotExist(errors.Cause(err)) { f, err = fs.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600) } if err != nil { fmt.Fprintf(os.Stderr, "unable to open debug log file: %v\n", err) os.Exit(2) } opts.logger = log.New(f, "", log.LstdFlags) }
// SaveBlob returns a http.HandlerFunc that saves a blob to the repository. func SaveBlob(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := strings.Split(r.RequestURI, "/") dir := vars[1] name := vars[2] path := filepath.Join(c.path, dir, name) tmp := path + "_tmp" tf, err := fs.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600) if err != nil { http.Error(w, "500 internal server error", 500) return } if _, err := io.Copy(tf, r.Body); err != nil { http.Error(w, "400 bad request", 400) tf.Close() os.Remove(tmp) return } if err := tf.Close(); err != nil { http.Error(w, "500 internal server error", 500) } if err := os.Rename(tmp, path); err != nil { http.Error(w, "500 internal server error", 500) return } w.Write([]byte("200 ok")) } }
func (node Node) createFileAt(path string, repo Repository) error { f, err := fs.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600) defer f.Close() if err != nil { return errors.Wrap(err, "OpenFile") } var buf []byte for _, id := range node.Content { size, err := repo.LookupBlobSize(id, DataBlob) if err != nil { return err } buf = buf[:cap(buf)] if uint(len(buf)) < size { buf = make([]byte, size) } n, err := repo.LoadBlob(DataBlob, id, buf) if err != nil { return err } buf = buf[:n] _, err = f.Write(buf) if err != nil { return errors.Wrap(err, "Write") } } return nil }