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)
}
stat, err := a.Stat()
if err != nil && !os.IsNotExist(err) {
if err := a.DeleteIfExists(); err != nil {
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())
}
// DeleteIfExists deletes the archive if it exists.
func (a *Archiver) DeleteIfExists() error {
stat, err := a.Stat()
if err != nil && !errors.Is(err, os.ErrNotExist) {
if _, err := a.Stat(); err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
// Check if the file exists.
if stat != nil {
if err := os.Remove(a.Path()); err != nil {
return err
}
return errors.WithStack(err)
}
return nil