os: implement MkdirAll
Этот коммит содержится в:
родитель
85ad157f3f
коммит
8416bb61d8
5 изменённых файлов: 262 добавлений и 0 удалений
61
src/os/path.go
Обычный файл
61
src/os/path.go
Обычный файл
|
@ -0,0 +1,61 @@
|
|||
// +build !baremetal,!js
|
||||
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// MkdirAll creates a directory named path,
|
||||
// along with any necessary parents, and returns nil,
|
||||
// or else returns an error.
|
||||
// The permission bits perm (before umask) are used for all
|
||||
// directories that MkdirAll creates.
|
||||
// If path is already a directory, MkdirAll does nothing
|
||||
// and returns nil.
|
||||
func MkdirAll(path string, perm FileMode) error {
|
||||
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
|
||||
dir, err := Stat(path)
|
||||
if err == nil {
|
||||
if dir.IsDir() {
|
||||
return nil
|
||||
}
|
||||
return &PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
|
||||
}
|
||||
|
||||
// Slow path: make sure parent exists and then call Mkdir for path.
|
||||
i := len(path)
|
||||
for i > 0 && IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
||||
i--
|
||||
}
|
||||
|
||||
j := i
|
||||
for j > 0 && !IsPathSeparator(path[j-1]) { // Scan backward over element.
|
||||
j--
|
||||
}
|
||||
|
||||
if j > 1 {
|
||||
// Create parent.
|
||||
err = MkdirAll(fixRootDirectory(path[:j-1]), perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Parent now exists; invoke Mkdir and use its result.
|
||||
err = Mkdir(path, perm)
|
||||
if err != nil {
|
||||
// Handle arguments like "foo/." by
|
||||
// double-checking that directory doesn't exist.
|
||||
dir, err1 := Lstat(path)
|
||||
if err1 == nil && dir.IsDir() {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
75
src/os/path_test.go
Обычный файл
75
src/os/path_test.go
Обычный файл
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package os_test
|
||||
|
||||
import (
|
||||
. "os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMkdirAll(t *testing.T) {
|
||||
tmpDir := TempDir()
|
||||
path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
|
||||
err := MkdirAll(path, 0777)
|
||||
if err != nil {
|
||||
t.Fatalf("MkdirAll %q: %s", path, err)
|
||||
}
|
||||
// TODO: revert to upstream code which uses RemoveAll
|
||||
defer Remove(tmpDir + "/_TestMkdirAll_/dir/dir2")
|
||||
defer Remove(tmpDir + "/_TestMkdirAll_/dir")
|
||||
defer Remove(tmpDir + "/_TestMkdirAll_")
|
||||
|
||||
// Already exists, should succeed.
|
||||
err = MkdirAll(path, 0777)
|
||||
if err != nil {
|
||||
t.Fatalf("MkdirAll %q (second time): %s", path, err)
|
||||
}
|
||||
|
||||
// Make file.
|
||||
fpath := path + "/file"
|
||||
f, err := Create(fpath)
|
||||
if err != nil {
|
||||
t.Fatalf("create %q: %s", fpath, err)
|
||||
}
|
||||
defer Remove(fpath)
|
||||
defer f.Close()
|
||||
|
||||
// Can't make directory named after file.
|
||||
err = MkdirAll(fpath, 0777)
|
||||
if err == nil {
|
||||
t.Fatalf("MkdirAll %q: no error", fpath)
|
||||
}
|
||||
perr, ok := err.(*PathError)
|
||||
if !ok {
|
||||
t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
|
||||
}
|
||||
if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
|
||||
t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
|
||||
}
|
||||
|
||||
// Can't make subdirectory of file.
|
||||
ffpath := fpath + "/subdir"
|
||||
err = MkdirAll(ffpath, 0777)
|
||||
if err == nil {
|
||||
t.Fatalf("MkdirAll %q: no error", ffpath)
|
||||
}
|
||||
perr, ok = err.(*PathError)
|
||||
if !ok {
|
||||
t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
|
||||
}
|
||||
if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
|
||||
t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
path := tmpDir + `\_TestMkdirAll_\dir\.\dir2\`
|
||||
err := MkdirAll(path, 0777)
|
||||
if err != nil {
|
||||
t.Fatalf("MkdirAll %q: %s", path, err)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,3 +34,43 @@ func basename(name string) string {
|
|||
|
||||
return name
|
||||
}
|
||||
|
||||
// splitPath returns the base name and parent directory.
|
||||
func splitPath(path string) (string, string) {
|
||||
// if no better parent is found, the path is relative from "here"
|
||||
dirname := "."
|
||||
|
||||
// Remove all but one leading slash.
|
||||
for len(path) > 1 && path[0] == '/' && path[1] == '/' {
|
||||
path = path[1:]
|
||||
}
|
||||
|
||||
i := len(path) - 1
|
||||
|
||||
// Remove trailing slashes.
|
||||
for ; i > 0 && path[i] == '/'; i-- {
|
||||
path = path[:i]
|
||||
}
|
||||
|
||||
// if no slashes in path, base is path
|
||||
basename := path
|
||||
|
||||
// Remove leading directory path
|
||||
for i--; i >= 0; i-- {
|
||||
if path[i] == '/' {
|
||||
if i == 0 {
|
||||
dirname = path[:1]
|
||||
} else {
|
||||
dirname = path[:i]
|
||||
}
|
||||
basename = path[i+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return dirname, basename
|
||||
}
|
||||
|
||||
func fixRootDirectory(p string) string {
|
||||
return p
|
||||
}
|
||||
|
|
|
@ -214,3 +214,14 @@ func fixLongPath(path string) string {
|
|||
}
|
||||
return string(pathbuf[:w])
|
||||
}
|
||||
|
||||
// fixRootDirectory fixes a reference to a drive's root directory to
|
||||
// have the required trailing slash.
|
||||
func fixRootDirectory(p string) string {
|
||||
if len(p) == len(`\\?\c:`) {
|
||||
if IsPathSeparator(p[0]) && IsPathSeparator(p[1]) && p[2] == '?' && IsPathSeparator(p[3]) && p[5] == ':' {
|
||||
return p + `\`
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
|
75
src/os/path_windows_test.go
Обычный файл
75
src/os/path_windows_test.go
Обычный файл
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package os_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFixLongPath(t *testing.T) {
|
||||
if os.CanUseLongPaths {
|
||||
return
|
||||
}
|
||||
// 248 is long enough to trigger the longer-than-248 checks in
|
||||
// fixLongPath, but short enough not to make a path component
|
||||
// longer than 255, which is illegal on Windows. (which
|
||||
// doesn't really matter anyway, since this is purely a string
|
||||
// function we're testing, and it's not actually being used to
|
||||
// do a system call)
|
||||
veryLong := "l" + strings.Repeat("o", 248) + "ng"
|
||||
for _, test := range []struct{ in, want string }{
|
||||
// Short; unchanged:
|
||||
{`C:\short.txt`, `C:\short.txt`},
|
||||
{`C:\`, `C:\`},
|
||||
{`C:`, `C:`},
|
||||
// The "long" substring is replaced by a looooooong
|
||||
// string which triggers the rewriting. Except in the
|
||||
// cases below where it doesn't.
|
||||
{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
|
||||
{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
|
||||
{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
|
||||
{`\\unc\path`, `\\unc\path`},
|
||||
{`long.txt`, `long.txt`},
|
||||
{`C:long.txt`, `C:long.txt`},
|
||||
{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
|
||||
{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
|
||||
{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
|
||||
} {
|
||||
in := strings.ReplaceAll(test.in, "long", veryLong)
|
||||
want := strings.ReplaceAll(test.want, "long", veryLong)
|
||||
if got := os.FixLongPath(in); got != want {
|
||||
got = strings.ReplaceAll(got, veryLong, "long")
|
||||
t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: bring back upstream version's TestMkdirAllLongPath once os.RemoveAll and t.TempDir implemented
|
||||
|
||||
func TestMkdirAllExtendedLength(t *testing.T) {
|
||||
// TODO: revert to upstream version once os.RemoveAll and t.TempDir implemented
|
||||
tmpDir := os.TempDir()
|
||||
|
||||
const prefix = `\\?\`
|
||||
if len(tmpDir) < 4 || tmpDir[:4] != prefix {
|
||||
fullPath, err := syscall.FullPath(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
|
||||
}
|
||||
tmpDir = prefix + fullPath
|
||||
}
|
||||
path := tmpDir + `\dir\`
|
||||
if err := os.MkdirAll(path, 0777); err != nil {
|
||||
t.Fatalf("MkdirAll(%q) failed: %v", path, err)
|
||||
}
|
||||
|
||||
path = path + `.\dir2`
|
||||
if err := os.MkdirAll(path, 0777); err == nil {
|
||||
t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
|
||||
}
|
||||
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче