mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #11350 from delftswa2014/utils-directory-windows
Makes `directory` pkg compilable on Windows.
This commit is contained in:
commit
6374c12fbb
3 changed files with 64 additions and 19 deletions
|
@ -1,49 +1,54 @@
|
|||
package directory
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Size of an empty directory should be 0
|
||||
func TestSizeEmpty(t *testing.T) {
|
||||
var dir string
|
||||
var err error
|
||||
if err = os.Mkdir("/tmp/testSizeEmptyDirectory", 0777); err != nil {
|
||||
if dir, err = ioutil.TempDir(os.TempDir(), "testSizeEmptyDirectory"); err != nil {
|
||||
t.Fatalf("failed to create directory: %s", err)
|
||||
}
|
||||
|
||||
var size int64
|
||||
if size, _ = Size("/tmp/testSizeEmptyDirectory"); size != 0 {
|
||||
if size, _ = Size(dir); size != 0 {
|
||||
t.Fatalf("empty directory has size: %d", size)
|
||||
}
|
||||
}
|
||||
|
||||
// Size of a directory with one empty file should be 0
|
||||
func TestSizeEmptyFile(t *testing.T) {
|
||||
var dir string
|
||||
var err error
|
||||
if err = os.Mkdir("/tmp/testSizeEmptyFile", 0777); err != nil {
|
||||
if dir, err = ioutil.TempDir(os.TempDir(), "testSizeEmptyFile"); err != nil {
|
||||
t.Fatalf("failed to create directory: %s", err)
|
||||
}
|
||||
|
||||
if _, err = os.Create("/tmp/testSizeEmptyFile/file"); err != nil {
|
||||
var file *os.File
|
||||
if file, err = ioutil.TempFile(dir, "file"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
}
|
||||
|
||||
var size int64
|
||||
if size, _ = Size("/tmp/testSizeEmptyFile"); size != 0 {
|
||||
if size, _ = Size(file.Name()); size != 0 {
|
||||
t.Fatalf("directory with one file has size: %d", size)
|
||||
}
|
||||
}
|
||||
|
||||
// Size of a directory with one 5-byte file should be 5
|
||||
func TestSizeNonemptyFile(t *testing.T) {
|
||||
var dir string
|
||||
var err error
|
||||
if err = os.Mkdir("/tmp/testSizeNonemptyFile", 0777); err != nil {
|
||||
if dir, err = ioutil.TempDir(os.TempDir(), "testSizeNonemptyFile"); err != nil {
|
||||
t.Fatalf("failed to create directory: %s", err)
|
||||
}
|
||||
|
||||
var file *os.File
|
||||
if file, err = os.Create("/tmp/testSizeNonemptyFile/file"); err != nil {
|
||||
if file, err = ioutil.TempFile(dir, "file"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
}
|
||||
|
||||
|
@ -51,33 +56,41 @@ func TestSizeNonemptyFile(t *testing.T) {
|
|||
file.Write(d)
|
||||
|
||||
var size int64
|
||||
if size, _ = Size("/tmp/testSizeNonemptyFile"); size != 5 {
|
||||
if size, _ = Size(file.Name()); size != 5 {
|
||||
t.Fatalf("directory with one 5-byte file has size: %d", size)
|
||||
}
|
||||
}
|
||||
|
||||
// Size of a directory with one empty directory should be 0
|
||||
func TestSizeNestedDirectoryEmpty(t *testing.T) {
|
||||
var dir string
|
||||
var err error
|
||||
if err = os.MkdirAll("/tmp/testSizeNestedDirectoryEmpty/nested", 0777); err != nil {
|
||||
if dir, err = ioutil.TempDir(os.TempDir(), "testSizeNestedDirectoryEmpty"); err != nil {
|
||||
t.Fatalf("failed to create directory: %s", err)
|
||||
}
|
||||
if dir, err = ioutil.TempDir(dir, "nested"); err != nil {
|
||||
t.Fatalf("failed to create nested directory: %s", err)
|
||||
}
|
||||
|
||||
var size int64
|
||||
if size, _ = Size("/tmp/testSizeNestedDirectoryEmpty"); size != 0 {
|
||||
if size, _ = Size(dir); size != 0 {
|
||||
t.Fatalf("directory with one empty directory has size: %d", size)
|
||||
}
|
||||
}
|
||||
|
||||
// Test directory with 1 file and 1 empty directory
|
||||
func TestSizeFileAndNestedDirectoryEmpty(t *testing.T) {
|
||||
var dir string
|
||||
var err error
|
||||
if err = os.MkdirAll("/tmp/testSizeFileAndNestedDirectoryEmpty/nested", 0777); err != nil {
|
||||
if dir, err = ioutil.TempDir(os.TempDir(), "testSizeFileAndNestedDirectoryEmpty"); err != nil {
|
||||
t.Fatalf("failed to create directory: %s", err)
|
||||
}
|
||||
if dir, err = ioutil.TempDir(dir, "nested"); err != nil {
|
||||
t.Fatalf("failed to create nested directory: %s", err)
|
||||
}
|
||||
|
||||
var file *os.File
|
||||
if file, err = os.Create("/tmp/testSizeFileAndNestedDirectoryEmpty/file"); err != nil {
|
||||
if file, err = ioutil.TempFile(dir, "file"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
}
|
||||
|
||||
|
@ -85,20 +98,24 @@ func TestSizeFileAndNestedDirectoryEmpty(t *testing.T) {
|
|||
file.Write(d)
|
||||
|
||||
var size int64
|
||||
if size, _ = Size("/tmp/testSizeFileAndNestedDirectoryEmpty"); size != 6 {
|
||||
if size, _ = Size(dir); size != 6 {
|
||||
t.Fatalf("directory with 6-byte file and empty directory has size: %d", size)
|
||||
}
|
||||
}
|
||||
|
||||
// Test directory with 1 file and 1 non-empty directory
|
||||
func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
|
||||
var dir, dirNested string
|
||||
var err error
|
||||
if err = os.MkdirAll("/tmp/testSizeFileAndNestedDirectoryEmpty/nested", 0777); err != nil {
|
||||
if dir, err = ioutil.TempDir(os.TempDir(), "TestSizeFileAndNestedDirectoryNonempty"); err != nil {
|
||||
t.Fatalf("failed to create directory: %s", err)
|
||||
}
|
||||
if dirNested, err = ioutil.TempDir(dir, "nested"); err != nil {
|
||||
t.Fatalf("failed to create nested directory: %s", err)
|
||||
}
|
||||
|
||||
var file *os.File
|
||||
if file, err = os.Create("/tmp/testSizeFileAndNestedDirectoryEmpty/file"); err != nil {
|
||||
if file, err = ioutil.TempFile(dir, "file"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
}
|
||||
|
||||
|
@ -106,15 +123,15 @@ func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
|
|||
file.Write(data)
|
||||
|
||||
var nestedFile *os.File
|
||||
if nestedFile, err = os.Create("/tmp/testSizeFileAndNestedDirectoryEmpty/nested/file"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
if nestedFile, err = ioutil.TempFile(dirNested, "file"); err != nil {
|
||||
t.Fatalf("failed to create file in nested directory: %s", err)
|
||||
}
|
||||
|
||||
nestedData := []byte{100, 111, 99, 107, 101, 114}
|
||||
nestedFile.Write(nestedData)
|
||||
|
||||
var size int64
|
||||
if size, _ = Size("/tmp/testSizeFileAndNestedDirectoryEmpty"); size != 12 {
|
||||
t.Fatalf("directory with 6-byte file and empty directory has size: %d", size)
|
||||
if size, _ = Size(dir); size != 12 {
|
||||
t.Fatalf("directory with 6-byte file and nested directory with 6-byte file has size: %d", size)
|
||||
}
|
||||
}
|
||||
|
|
28
pkg/directory/directory_windows.go
Normal file
28
pkg/directory/directory_windows.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// +build windows
|
||||
|
||||
package directory
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Size walks a directory tree and returns its total size in bytes.
|
||||
func Size(dir string) (size int64, err error) {
|
||||
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e error) error {
|
||||
// Ignore directory sizes
|
||||
if fileInfo == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
s := fileInfo.Size()
|
||||
if fileInfo.IsDir() || s == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
size += s
|
||||
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
Loading…
Reference in a new issue