Skip to content

Commit

Permalink
Simplify RestoreResolvConf
Browse files Browse the repository at this point in the history
  • Loading branch information
upils committed Feb 23, 2024
1 parent 0a6b1ec commit 2d1b3aa
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,22 +480,23 @@ func BackupAndCopyResolvConf(chroot string) error {
// RestoreResolvConf restores the resolv.conf in the chroot from the
// version that was backed up by BackupAndCopyResolvConf
func RestoreResolvConf(chroot string) error {
if osutil.FileExists(filepath.Join(chroot, "etc", "resolv.conf.tmp")) {
if osutil.IsSymlink(filepath.Join(chroot, "etc", "resolv.conf")) {
// As per what live-build does, handle the case where some package
// in the install_packages phase converts resolv.conf into a
// symlink. In such case we don't restore our backup but instead
// remove it, leaving the symlink around.
backup := filepath.Join(chroot, "etc", "resolv.conf.tmp")
if err := osRemove(backup); err != nil {
return fmt.Errorf("Error removing file \"%s\": %s", backup, err.Error())
}
} else {
src := filepath.Join(chroot, "etc", "resolv.conf.tmp")
dest := filepath.Join(chroot, "etc", "resolv.conf")
if err := osRename(src, dest); err != nil {
return fmt.Errorf("Error moving file \"%s\" to \"%s\": %s", src, dest, err.Error())
}
if !osutil.FileExists(filepath.Join(chroot, "etc", "resolv.conf.tmp")) {
return nil
}
if osutil.IsSymlink(filepath.Join(chroot, "etc", "resolv.conf")) {
// As per what live-build does, handle the case where some package
// in the install_packages phase converts resolv.conf into a
// symlink. In such case we don't restore our backup but instead
// remove it, leaving the symlink around.
backup := filepath.Join(chroot, "etc", "resolv.conf.tmp")
if err := osRemove(backup); err != nil {
return fmt.Errorf("Error removing file \"%s\": %s", backup, err.Error())
}
} else {
src := filepath.Join(chroot, "etc", "resolv.conf.tmp")
dest := filepath.Join(chroot, "etc", "resolv.conf")
if err := osRename(src, dest); err != nil {
return fmt.Errorf("Error moving file \"%s\" to \"%s\": %s", src, dest, err.Error())
}
}
return nil
Expand Down

0 comments on commit 2d1b3aa

Please sign in to comment.