More archiver code cleanup; ref pterodactyl/panel#2438

This commit is contained in:
Dane Everitt 2020-10-03 19:13:41 -07:00
parent d9109cbf5a
commit e02c197585
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -81,33 +81,25 @@ func (a *Archiver) Archive() error {
files = append(files, f) files = append(files, f)
} }
stat, err := a.Stat() if err := a.DeleteIfExists(); err != nil {
if err != nil && !os.IsNotExist(err) {
return err return err
} }
// Check if the file exists.
if stat != nil {
if err := os.Remove(a.Path()); err != nil {
return err
}
}
return archiver.NewTarGz().Archive(files, a.Path()) return archiver.NewTarGz().Archive(files, a.Path())
} }
// DeleteIfExists deletes the archive if it exists. // DeleteIfExists deletes the archive if it exists.
func (a *Archiver) DeleteIfExists() error { func (a *Archiver) DeleteIfExists() error {
stat, err := a.Stat() if _, err := a.Stat(); err != nil {
if err != nil && !errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
return nil
}
return err return err
} }
// Check if the file exists. if err := os.Remove(a.Path()); err != nil {
if stat != nil { return errors.WithStack(err)
if err := os.Remove(a.Path()); err != nil {
return err
}
} }
return nil return nil