From 23ac4f84dd414f8a623ad747d3e74049dcff9527 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 14 Mar 2015 14:29:32 +0100 Subject: [PATCH] Set file-mode in repository read-only (Closes #48) --- backend/local.go | 17 ++++++++++++++--- backend/sftp.go | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/backend/local.go b/backend/local.go index fc76a1363..56f8f6a1f 100644 --- a/backend/local.go +++ b/backend/local.go @@ -196,15 +196,26 @@ func (b *Local) tempFile() (*os.File, error) { // Rename temp file to final name according to type and ID. func (b *Local) renameFile(file *os.File, t Type, id ID) error { - newname := b.filename(t, id) + filename := b.filename(t, id) oldname := file.Name() if t == Data || t == Tree { // create directories if necessary, ignore errors - os.MkdirAll(filepath.Dir(newname), dirMode) + os.MkdirAll(filepath.Dir(filename), dirMode) } - return os.Rename(oldname, newname) + err := os.Rename(oldname, filename) + if err != nil { + return err + } + + // set mode to read-only + fi, err := os.Stat(filename) + if err != nil { + return err + } + + return os.Chmod(filename, fi.Mode()&os.FileMode(^uint32(0111))) } // Construct directory for given Type. diff --git a/backend/sftp.go b/backend/sftp.go index f722f664b..8ec68fde7 100644 --- a/backend/sftp.go +++ b/backend/sftp.go @@ -302,17 +302,28 @@ func (r *SFTP) mkdirAll(dir string, mode os.FileMode) error { // Rename temp file to final name according to type and ID. func (r *SFTP) renameFile(oldname string, t Type, id ID) error { - newname := r.filename(t, id) + filename := r.filename(t, id) // create directories if necessary if t == Data || t == Tree { - err := r.mkdirAll(filepath.Dir(newname), dirMode) + err := r.mkdirAll(filepath.Dir(filename), dirMode) if err != nil { return err } } - return r.c.Rename(oldname, newname) + err := r.c.Rename(oldname, filename) + if err != nil { + return err + } + + // set mode to read-only + fi, err := r.c.Lstat(filename) + if err != nil { + return err + } + + return r.c.Chmod(filename, fi.Mode()&os.FileMode(^uint32(0111))) } // Construct directory for given Type.