mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #44251 from thaJeztah/pkg_dir_cleanup
pkg/directory: remove unused MoveToSubdir() utility, and some refactoring
This commit is contained in:
commit
d006242d73
4 changed files with 18 additions and 80 deletions
|
@ -1,24 +1,8 @@
|
||||||
package directory // import "github.com/docker/docker/pkg/directory"
|
package directory // import "github.com/docker/docker/pkg/directory"
|
||||||
|
|
||||||
import (
|
import "context"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MoveToSubdir moves all contents of a directory to a subdirectory underneath the original path
|
// Size walks a directory tree and returns its total size in bytes.
|
||||||
func MoveToSubdir(oldpath, subdir string) error {
|
func Size(ctx context.Context, dir string) (int64, error) {
|
||||||
infos, err := os.ReadDir(oldpath)
|
return calcSize(ctx, dir)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, info := range infos {
|
|
||||||
if info.Name() != subdir {
|
|
||||||
oldName := filepath.Join(oldpath, info.Name())
|
|
||||||
newName := filepath.Join(oldpath, subdir, info.Name())
|
|
||||||
if err := os.Rename(oldName, newName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,6 @@ package directory // import "github.com/docker/docker/pkg/directory"
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -144,51 +141,6 @@ func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test migration of directory to a subdir underneath itself
|
|
||||||
func TestMoveToSubdir(t *testing.T) {
|
|
||||||
var outerDir, subDir string
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if outerDir, err = os.MkdirTemp(os.TempDir(), "TestMoveToSubdir"); err != nil {
|
|
||||||
t.Fatalf("failed to create directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if subDir, err = os.MkdirTemp(outerDir, "testSub"); err != nil {
|
|
||||||
t.Fatalf("failed to create subdirectory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// write 4 temp files in the outer dir to get moved
|
|
||||||
filesList := []string{"a", "b", "c", "d"}
|
|
||||||
for _, fName := range filesList {
|
|
||||||
if file, err := os.Create(filepath.Join(outerDir, fName)); err != nil {
|
|
||||||
t.Fatalf("couldn't create temp file %q: %v", fName, err)
|
|
||||||
} else {
|
|
||||||
file.WriteString(fName)
|
|
||||||
file.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = MoveToSubdir(outerDir, filepath.Base(subDir)); err != nil {
|
|
||||||
t.Fatalf("Error during migration of content to subdirectory: %v", err)
|
|
||||||
}
|
|
||||||
// validate that the files were moved to the subdirectory
|
|
||||||
infos, err := os.ReadDir(subDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(infos) != 4 {
|
|
||||||
t.Fatalf("Should be four files in the subdir after the migration: actual length: %d", len(infos))
|
|
||||||
}
|
|
||||||
var results []string
|
|
||||||
for _, info := range infos {
|
|
||||||
results = append(results, info.Name())
|
|
||||||
}
|
|
||||||
sort.Strings(results)
|
|
||||||
if !reflect.DeepEqual(filesList, results) {
|
|
||||||
t.Fatalf("Results after migration do not equal list of files: expected: %v, got: %v", filesList, results)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test a non-existing directory
|
// Test a non-existing directory
|
||||||
func TestSizeNonExistingDirectory(t *testing.T) {
|
func TestSizeNonExistingDirectory(t *testing.T) {
|
||||||
if _, err := Size(context.Background(), "/thisdirectoryshouldnotexist/TestSizeNonExistingDirectory"); err == nil {
|
if _, err := Size(context.Background(), "/thisdirectoryshouldnotexist/TestSizeNonExistingDirectory"); err == nil {
|
||||||
|
|
|
@ -10,14 +10,15 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Size walks a directory tree and returns its total size in bytes.
|
// calcSize walks a directory tree and returns its total size in bytes.
|
||||||
func Size(ctx context.Context, dir string) (size int64, err error) {
|
func calcSize(ctx context.Context, dir string) (int64, error) {
|
||||||
|
var size int64
|
||||||
data := make(map[uint64]struct{})
|
data := make(map[uint64]struct{})
|
||||||
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
err := filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if dir does not exist, Size() returns the error.
|
|
||||||
// if dir/x disappeared while walking, Size() ignores dir/x.
|
// if dir/x disappeared while walking, Size() ignores dir/x.
|
||||||
if os.IsNotExist(err) && d != dir {
|
// if dir does not exist, Size() returns the error.
|
||||||
|
if d != dir && os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
@ -51,5 +52,5 @@ func Size(ctx context.Context, dir string) (size int64, err error) {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return
|
return size, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,14 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Size walks a directory tree and returns its total size in bytes.
|
// calcSize walks a directory tree and returns its total calcSize in bytes.
|
||||||
func Size(ctx context.Context, dir string) (size int64, err error) {
|
func calcSize(ctx context.Context, dir string) (int64, error) {
|
||||||
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
var size int64
|
||||||
|
err := filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if dir does not exist, Size() returns the error.
|
|
||||||
// if dir/x disappeared while walking, Size() ignores dir/x.
|
// if dir/x disappeared while walking, Size() ignores dir/x.
|
||||||
if os.IsNotExist(err) && d != dir {
|
// if dir does not exist, Size() returns the error.
|
||||||
|
if d != dir && os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
@ -38,5 +39,5 @@ func Size(ctx context.Context, dir string) (size int64, err error) {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return
|
return size, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue