1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-24 00:37:28 +00:00

Set file-mode in repository read-only (Closes #48)

This commit is contained in:
Alexander Neumann 2015-03-14 14:29:32 +01:00
parent 248878022c
commit 23ac4f84dd
2 changed files with 28 additions and 6 deletions

View file

@ -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.

View file

@ -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.