Improve error handling during container file copy removal
Based on feedback at #1365 Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
committed by
Christian Goll
parent
9e91b1c19a
commit
5860334a89
@@ -82,8 +82,10 @@ func runContainedCmd(cmd *cobra.Command, containerName string, args []string) (e
|
||||
wwlog.Verbose("Running contained command: %s", childArgs)
|
||||
retVal := childCommandFunc(cmd, childArgs)
|
||||
for _, cpyFile := range filesToCpy {
|
||||
if err := cpyFile.removeFromContainer(containerName); err != nil {
|
||||
wwlog.Warn("couldn't remove file: %s", err)
|
||||
if cpyFile.shouldRemoveFromContainer(containerName) {
|
||||
if err := cpyFile.removeFromContainer(containerName); err != nil {
|
||||
wwlog.Warn("couldn't remove file: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return retVal
|
||||
@@ -183,37 +185,49 @@ type copyFile struct {
|
||||
modTime time.Time
|
||||
}
|
||||
|
||||
func (this *copyFile) containerDest(containerName string) string {
|
||||
return path.Join(container.RootFsDir(containerName), this.fileName)
|
||||
}
|
||||
|
||||
func (this *copyFile) copyToContainer(containerName string) error {
|
||||
containerDest := path.Join(container.RootFsDir(containerName), this.fileName)
|
||||
containerDest := this.containerDest(containerName)
|
||||
if _, err := os.Stat(path.Dir(containerDest)); err != nil {
|
||||
return fmt.Errorf("destination directory doesn't exist: %s", err)
|
||||
return err
|
||||
} else if _, err := os.Stat(containerDest); err == nil {
|
||||
return fmt.Errorf("file already exists in container: %s", this.fileName)
|
||||
return err
|
||||
} else if _, err := os.Stat(this.src); err != nil {
|
||||
return fmt.Errorf("source doesn't exist: %s", err)
|
||||
return err
|
||||
} else if err := util.CopyFile(this.src, containerDest); err != nil {
|
||||
return fmt.Errorf("couldn't copy files into container: %w", err)
|
||||
return err
|
||||
} else if stat, err := os.Stat(containerDest); err != nil {
|
||||
return err
|
||||
} else {
|
||||
// we can ignore error as the file was copied
|
||||
stat, _ := os.Stat(containerDest)
|
||||
this.modTime = stat.ModTime()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *copyFile) removeFromContainer(containerName string) error {
|
||||
containerDest := path.Join(container.RootFsDir(containerName), this.fileName)
|
||||
func (this *copyFile) shouldRemoveFromContainer(containerName string) bool {
|
||||
containerDest := this.containerDest(containerName)
|
||||
if this.modTime.IsZero() {
|
||||
return fmt.Errorf("not previously copied: %s", this.fileName)
|
||||
wwlog.Debug("file was not previously copied: %s", this.fileName)
|
||||
return false
|
||||
} else if destStat, err := os.Stat(containerDest); err != nil {
|
||||
return fmt.Errorf("no longer present: %s", err)
|
||||
wwlog.Verbose("file is no longer present: %s (%s)", this.fileName, err)
|
||||
return false
|
||||
} else if destStat.ModTime() == this.modTime {
|
||||
return os.Remove(containerDest)
|
||||
wwlog.Verbose("don't remove modified file:", this.fileName)
|
||||
return false
|
||||
} else {
|
||||
return fmt.Errorf("modified since copy: %s", this.fileName)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (this *copyFile) removeFromContainer(containerName string) error {
|
||||
containerDest := this.containerDest(containerName)
|
||||
return os.Remove(containerDest)
|
||||
}
|
||||
|
||||
/*
|
||||
Check the objects we want to copy in, instead of mounting
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user