archive: keep timestamps when extracting

This commit is contained in:
Matthew Penner 2021-09-01 09:54:41 -06:00
parent 3b5e042ccc
commit 5cd43dd4c9
No known key found for this signature in database
GPG Key ID: 73E0E4F42955DDEF
6 changed files with 31 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"os" "os"
"time"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
@ -152,12 +153,15 @@ func (s *Server) RestoreBackup(b backup.BackupInterface, reader io.ReadCloser) (
// Attempt to restore the backup to the server by running through each entry // Attempt to restore the backup to the server by running through each entry
// in the file one at a time and writing them to the disk. // in the file one at a time and writing them to the disk.
s.Log().Debug("starting file writing process for backup restoration") s.Log().Debug("starting file writing process for backup restoration")
err = b.Restore(s.Context(), reader, func(file string, r io.Reader, mode fs.FileMode) error { err = b.Restore(s.Context(), reader, func(file string, r io.Reader, mode fs.FileMode, atime, mtime time.Time) error {
s.Events().Publish(DaemonMessageEvent, "(restoring): "+file) s.Events().Publish(DaemonMessageEvent, "(restoring): "+file)
if err := s.Filesystem().Writefile(file, r); err != nil { if err := s.Filesystem().Writefile(file, r); err != nil {
return err return err
} }
return s.Filesystem().Chmod(file, mode) if err := s.Filesystem().Chmod(file, mode); err != nil {
return err
}
return s.Filesystem().Chtimes(file, atime, mtime)
}) })
return errors.WithStackIf(err) return errors.WithStackIf(err)

View File

@ -8,6 +8,7 @@ import (
"io/fs" "io/fs"
"os" "os"
"path" "path"
"time"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
@ -26,7 +27,7 @@ const (
// RestoreCallback is a generic restoration callback that exists for both local // RestoreCallback is a generic restoration callback that exists for both local
// and remote backups allowing the files to be restored. // and remote backups allowing the files to be restored.
type RestoreCallback func(file string, r io.Reader, mode fs.FileMode) error type RestoreCallback func(file string, r io.Reader, mode fs.FileMode, atime, mtime time.Time) error
// noinspection GoNameStartsWithPackageName // noinspection GoNameStartsWithPackageName
type BackupInterface interface { type BackupInterface interface {

View File

@ -88,7 +88,7 @@ func (b *LocalBackup) Restore(ctx context.Context, _ io.Reader, callback Restore
if f.IsDir() { if f.IsDir() {
return nil return nil
} }
return callback(filesystem.ExtractNameFromArchive(f), f, f.Mode()) return callback(filesystem.ExtractNameFromArchive(f), f, f.Mode(), f.ModTime(), f.ModTime())
} }
}) })
} }

View File

@ -116,7 +116,7 @@ func (s *S3Backup) Restore(ctx context.Context, r io.Reader, callback RestoreCal
return err return err
} }
if header.Typeflag == tar.TypeReg { if header.Typeflag == tar.TypeReg {
if err := callback(header.Name, tr, header.FileInfo().Mode()); err != nil { if err := callback(header.Name, tr, header.FileInfo().Mode(), header.AccessTime, header.ModTime); err != nil {
return err return err
} }
} }

View File

@ -136,6 +136,10 @@ func (fs *Filesystem) DecompressFile(dir string, file string) error {
if err := fs.Chmod(p, f.Mode()); err != nil { if err := fs.Chmod(p, f.Mode()); err != nil {
return wrapError(err, source) return wrapError(err, source)
} }
// Update the file modification time to the one set in the archive.
if err := fs.Chtimes(p, f.ModTime(), f.ModTime()); err != nil {
return wrapError(err, source)
}
return nil return nil
}) })
if err != nil { if err != nil {

View File

@ -528,3 +528,20 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) {
return out, nil return out, nil
} }
func (fs *Filesystem) Chtimes(path string, atime, mtime time.Time) error {
cleaned, err := fs.SafePath(path)
if err != nil {
return err
}
if fs.isTest {
return nil
}
if err := os.Chtimes(cleaned, atime, mtime); err != nil {
return err
}
return nil
}