Skip to content

Commit

Permalink
Chcon,Relabel: do process the root entry last
Browse files Browse the repository at this point in the history
Commit 8501cfd does not guarantee that the root entry is processed
last -- due to parallel nature of the pwalk it one of the last to be
processed, but might be not the last one.

Rewrite the code to process the root entry after all the others
are done.

Remove the test for root entry being last as the last = p assignment
is racy, and this is now very obvious from the code that the root entry
is indeed the last one.

Fixes: 8501cfd
Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Oct 6, 2021
1 parent 3e29a7d commit b43035c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
8 changes: 5 additions & 3 deletions pkg/pwalk/pwalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func WalkN(root string, walkFn WalkFunc, num int) error {
return err
}
if len(p) == rootLen {
// Don't add root entry just yet.
// Root entry is processed separately below.
rootEntry = &walkArgs{path: p, info: &info}
return nil
}
Expand All @@ -78,8 +78,6 @@ func WalkN(root string, walkFn WalkFunc, num int) error {
}
})
if err == nil {
// Finally, add the root entry.
files <- rootEntry
close(files)
}
wg.Done()
Expand All @@ -102,6 +100,10 @@ func WalkN(root string, walkFn WalkFunc, num int) error {

wg.Wait()

if err == nil {
err = walkFn(rootEntry.path, *rootEntry.info, nil)
}

return err
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/pwalk/pwalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ func TestWalk(t *testing.T) {
}
defer os.RemoveAll(dir)

var last string
err = WalkN(dir,
func(p string, _ os.FileInfo, _ error) error {
last = p
func(_ string, _ os.FileInfo, _ error) error {
atomic.AddUint32(&count, 1)
return nil
},
Expand All @@ -37,9 +35,6 @@ func TestWalk(t *testing.T) {
if count != uint32(total) {
t.Errorf("File count mismatch: found %d, expected %d", count, total)
}
if last != dir {
t.Errorf("Last entry: want %q, got %q", dir, last)
}

t.Logf("concurrency: %d, files found: %d\n", concurrency, count)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/pwalkdir/pwalkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func WalkN(root string, walkFn fs.WalkDirFunc, num int) error {
return err
}
if len(p) == rootLen {
// Don't add root entry just yet.
// Root entry is processed separately below.
rootEntry = &walkArgs{path: p, entry: entry}
return nil
}
Expand All @@ -79,8 +79,6 @@ func WalkN(root string, walkFn fs.WalkDirFunc, num int) error {
}
})
if err == nil {
// Finally, add the root entry.
files <- rootEntry
close(files)
}
wg.Done()
Expand All @@ -103,6 +101,10 @@ func WalkN(root string, walkFn fs.WalkDirFunc, num int) error {

wg.Wait()

if err == nil {
err = walkFn(rootEntry.path, rootEntry.entry, nil)
}

return err
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/pwalkdir/pwalkdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ func TestWalkDir(t *testing.T) {
}
defer os.RemoveAll(dir)

var last string
err = WalkN(dir,
func(p string, _ fs.DirEntry, _ error) error {
last = p
func(_ string, _ fs.DirEntry, _ error) error {
atomic.AddUint32(&count, 1)
return nil
},
Expand All @@ -41,9 +39,6 @@ func TestWalkDir(t *testing.T) {
if count != uint32(total) {
t.Errorf("File count mismatch: found %d, expected %d", count, total)
}
if last != dir {
t.Errorf("Last entry: want %q, got %q", dir, last)
}

t.Logf("concurrency: %d, files found: %d\n", concurrency, count)
}
Expand Down

0 comments on commit b43035c

Please sign in to comment.