Skip to content

Commit

Permalink
Fix: Correctly sync lower and upper etc for user files
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Aug 10, 2023
1 parent e324ad3 commit 4507f19
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
10 changes: 5 additions & 5 deletions core/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import (
"os/exec"
)

// MergeDiff merges the diff lines between source and dest
func MergeDiff(sourceFile, destFile string) error {
PrintVerbose("MergeDiff: merging %s -> %s", sourceFile, destFile)
// MergeDiff merges the diff lines between the first and second files into destination
func MergeDiff(firstFile, secondFile, destination string) error {
PrintVerbose("MergeDiff: merging %s + %s -> %s", firstFile, secondFile, destination)

// get the diff lines
diffLines, err := DiffFiles(sourceFile, destFile)
diffLines, err := DiffFiles(firstFile, secondFile)
if err != nil {
PrintVerbose("MergeDiff:err: %s", err)
return err
}

// write the diff to the destination
err = WriteDiff(destFile, diffLines)
err = WriteDiff(destination, diffLines)
if err != nil {
PrintVerbose("MergeDiff:err: %s", err)
return err
Expand Down
41 changes: 22 additions & 19 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func (s *ABSystem) CheckUpdate() (string, bool) {
return s.Registry.HasUpdate(s.CurImage.Digest)
}

// SyncLowerEtc syncs the lower etc dir (/.system/etc)
func (s *ABSystem) SyncLowerEtc(newEtc, presentUpperEtc string) error {
PrintVerbose("ABSystem.SyncLowerEtc: syncing /.system/etc -> %s", newEtc)
// MergeUserEtcFiles merges user-related files from the new lower etc (/.system/etc)
// with the old upper etc, if present, saving the result in the new upper etc.
func (s *ABSystem) MergeUserEtcFiles(oldUpperEtc, newLowerEtc, newUpperEtc string) error {
PrintVerbose("ABSystem.SyncLowerEtc: syncing /.system/etc -> %s", newLowerEtc)

etcFiles := []string{
"passwd",
Expand All @@ -125,26 +126,25 @@ func (s *ABSystem) SyncLowerEtc(newEtc, presentUpperEtc string) error {

for _, file := range etcFiles {
// Use file present in the immutable /etc if it exists. Otherwise, use the immutable one.
sourceFile := ""
_, err := os.Stat(presentUpperEtc + "/" + file)
_, err := os.Stat(oldUpperEtc + "/" + file)
if err != nil {
if os.IsNotExist(err) {
sourceFile = etcDir + "/" + file
if os.IsNotExist(err) { // No changes were made to the file from its image base, skip merge
continue
} else {
PrintVerbose("ABSystem.SyncLowerEtc:err(2): %s", err)
return err
}
} else {
sourceFile = presentUpperEtc + "/" + file
}

destFile := newEtc + "/" + file
firstFile := oldUpperEtc + "/" + file
secondFile := newLowerEtc + "/" + file
destination := newUpperEtc + "/" + file

// write the diff to the destination
err = MergeDiff(sourceFile, destFile)
if err != nil {
PrintVerbose("ABSystem.SyncLowerEtc:err(3): %s", err)
return err
// write the diff to the destination
err = MergeDiff(firstFile, secondFile, destination)
if err != nil {
PrintVerbose("ABSystem.SyncLowerEtc:err(3): %s", err)
return err
}
}
}

Expand Down Expand Up @@ -726,18 +726,21 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
PrintVerbose("ABSystem.RunOperation:err(8): %s", err)
return err
}
err = s.SyncLowerEtc(filepath.Join(systemNew, "/etc"), fmt.Sprintf("/var/lib/abroot/etc/%s", presentEtc.Label))
futureEtc, err := s.RootM.GetFuture()
if err != nil {
PrintVerbose("ABSystem.RunOperation:err(8.1): %s", err)
return err
}
oldUpperEtc := fmt.Sprintf("/var/lib/abroot/etc/%s", presentEtc.Label)
newUpperEtc := fmt.Sprintf("/var/lib/abroot/etc/%s", futureEtc.Label)

futureEtc, err := s.RootM.GetFuture()
err = s.MergeUserEtcFiles(oldUpperEtc, filepath.Join(systemNew, "/etc"), newUpperEtc)
if err != nil {
PrintVerbose("ABSystem.RunOperation:err(8.2): %s", err)
return err
}
err = s.SyncUpperEtc(fmt.Sprintf("/var/lib/abroot/etc/%s", futureEtc.Label))

err = s.SyncUpperEtc(newUpperEtc)
if err != nil {
PrintVerbose("ABSystem.RunOperation:err(8.3): %s", err)
return err
Expand Down

0 comments on commit 4507f19

Please sign in to comment.