2010-12-03 05:34:57 +01:00
|
|
|
// 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
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"syscall"
|
|
|
|
)
|
2011-12-03 03:17:34 +01:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// MkdirAll creates a directory named path,
|
|
|
|
// along with any necessary parents, and returns nil,
|
|
|
|
// or else returns an error.
|
|
|
|
// The permission bits perm are used for all
|
|
|
|
// directories that MkdirAll creates.
|
|
|
|
// If path is already a directory, MkdirAll does nothing
|
|
|
|
// and returns nil.
|
2012-01-25 22:54:22 +01:00
|
|
|
func MkdirAll(path string, perm FileMode) error {
|
2015-01-15 01:27:56 +01:00
|
|
|
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
|
2011-01-21 19:19:03 +01:00
|
|
|
dir, err := Stat(path)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err == nil {
|
2011-12-13 20:16:27 +01:00
|
|
|
if dir.IsDir() {
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
return &PathError{"mkdir", path, syscall.ENOTDIR}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
// Slow path: make sure parent exists and then call Mkdir for path.
|
2010-12-03 05:34:57 +01:00
|
|
|
i := len(path)
|
2011-09-16 17:47:21 +02:00
|
|
|
for i > 0 && IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
2010-12-03 05:34:57 +01:00
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
j := i
|
2011-09-16 17:47:21 +02:00
|
|
|
for j > 0 && !IsPathSeparator(path[j-1]) { // Scan backward over element.
|
2010-12-03 05:34:57 +01:00
|
|
|
j--
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
if j > 1 {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Create parent
|
|
|
|
err = MkdirAll(path[0:j-1], perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
// Parent now exists; invoke Mkdir and use its result.
|
2010-12-03 05:34:57 +01:00
|
|
|
err = Mkdir(path, perm)
|
|
|
|
if err != nil {
|
|
|
|
// Handle arguments like "foo/." by
|
|
|
|
// double-checking that directory doesn't exist.
|
|
|
|
dir, err1 := Lstat(path)
|
2011-12-13 20:16:27 +01:00
|
|
|
if err1 == nil && dir.IsDir() {
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes path and any children it contains.
|
|
|
|
// It removes everything it can but returns the first error
|
|
|
|
// it encounters. If the path does not exist, RemoveAll
|
|
|
|
// returns nil (no error).
|
2011-12-03 03:17:34 +01:00
|
|
|
func RemoveAll(path string) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Simple case: if Remove works, we're done.
|
|
|
|
err := Remove(path)
|
2015-01-15 01:27:56 +01:00
|
|
|
if err == nil || IsNotExist(err) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, is this a directory we need to recurse into?
|
|
|
|
dir, serr := Lstat(path)
|
|
|
|
if serr != nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return serr
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
if !dir.IsDir() {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Not a directory; return the error from Remove.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directory.
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err := Open(path)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2015-01-15 01:27:56 +01:00
|
|
|
if IsNotExist(err) {
|
|
|
|
// Race. It was deleted between the Lstat and Open.
|
|
|
|
// Return nil per RemoveAll's docs.
|
|
|
|
return nil
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove contents & return first error.
|
|
|
|
err = nil
|
|
|
|
for {
|
|
|
|
names, err1 := fd.Readdirnames(100)
|
|
|
|
for _, name := range names {
|
2011-09-16 17:47:21 +02:00
|
|
|
err1 := RemoveAll(path + string(PathSeparator) + name)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
2011-12-03 03:17:34 +01:00
|
|
|
if err1 == io.EOF {
|
2011-09-16 17:47:21 +02:00
|
|
|
break
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
// If Readdirnames returned an error, use it.
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close directory, because windows won't remove opened directory.
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// Remove directory.
|
|
|
|
err1 := Remove(path)
|
2015-01-15 01:27:56 +01:00
|
|
|
if err1 == nil || IsNotExist(err1) {
|
|
|
|
return nil
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|