Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion fs/du.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@

package fs

import "context"
import (
"context"
"os"
)

func handleWalkError(root, path string, err error) error {
if err != nil && path != root && os.IsNotExist(err) {
return nil
}
return err
}

// Usage of disk information
type Usage struct {
Expand Down
20 changes: 20 additions & 0 deletions fs/du_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ func TestUsage(t *testing.T) {
}
}

func TestHandleWalkError(t *testing.T) {
t.Parallel()

root := filepath.Join("root")
missing := filepath.Join(root, "removed")
notExistErr := &os.PathError{Op: "lstat", Path: missing, Err: os.ErrNotExist}

if err := handleWalkError(root, missing, notExistErr); err != nil {
t.Fatalf("expected missing child to be ignored, got %v", err)
}
if err := handleWalkError(root, root, notExistErr); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected missing root error, got %v", err)
}

permissionErr := &os.PathError{Op: "open", Path: missing, Err: os.ErrPermission}
if err := handleWalkError(root, missing, permissionErr); !errors.Is(err, os.ErrPermission) {
t.Fatalf("expected permission error, got %v", err)
}
}

// createSparseFile creates a sparse file filled with random
// bytes for data parts
// The parse alternate data length, hole length, data length, ....
Expand Down
2 changes: 1 addition & 1 deletion fs/du_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func diskUsage(ctx context.Context, roots ...string) (Usage, error) {
for _, root := range roots {
if err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
return handleWalkError(root, path, err)
}

select {
Expand Down
2 changes: 1 addition & 1 deletion fs/du_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func diskUsage(ctx context.Context, roots ...string) (Usage, error) {
for _, root := range roots {
if err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
return handleWalkError(root, path, err)
}

select {
Expand Down
Loading