2010-12-03 04:34:57 +00: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 20:01:37 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"syscall"
|
|
|
|
)
|
2011-12-03 02:17:34 +00:00
|
|
|
|
2010-12-03 04:34:57 +00:00
|
|
|
// MkdirAll creates a directory named path,
|
|
|
|
// along with any necessary parents, and returns nil,
|
|
|
|
// or else returns an error.
|
2018-01-17 14:20:29 +00:00
|
|
|
// The permission bits perm (before umask) are used for all
|
2010-12-03 04:34:57 +00:00
|
|
|
// directories that MkdirAll creates.
|
|
|
|
// If path is already a directory, MkdirAll does nothing
|
|
|
|
// and returns nil.
|
2012-01-25 21:54:22 +00:00
|
|
|
func MkdirAll(path string, perm FileMode) error {
|
2015-01-15 00:27:56 +00:00
|
|
|
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
|
2011-01-21 18:19:03 +00:00
|
|
|
dir, err := Stat(path)
|
2010-12-03 04:34:57 +00:00
|
|
|
if err == nil {
|
2011-12-13 19:16:27 +00:00
|
|
|
if dir.IsDir() {
|
2010-12-03 04:34:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2012-03-02 20:01:37 +00:00
|
|
|
return &PathError{"mkdir", path, syscall.ENOTDIR}
|
2010-12-03 04:34:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:27:56 +00:00
|
|
|
// Slow path: make sure parent exists and then call Mkdir for path.
|
2010-12-03 04:34:57 +00:00
|
|
|
i := len(path)
|
2011-09-16 15:47:21 +00:00
|
|
|
for i > 0 && IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
2010-12-03 04:34:57 +00:00
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
j := i
|
2011-09-16 15:47:21 +00:00
|
|
|
for j > 0 && !IsPathSeparator(path[j-1]) { // Scan backward over element.
|
2010-12-03 04:34:57 +00:00
|
|
|
j--
|
|
|
|
}
|
|
|
|
|
2011-04-07 17:09:10 +00:00
|
|
|
if j > 1 {
|
2018-09-24 21:46:21 +00:00
|
|
|
// Create parent.
|
|
|
|
err = MkdirAll(fixRootDirectory(path[:j-1]), perm)
|
2010-12-03 04:34:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:27:56 +00:00
|
|
|
// Parent now exists; invoke Mkdir and use its result.
|
2010-12-03 04:34:57 +00: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 19:16:27 +00:00
|
|
|
if err1 == nil && dir.IsDir() {
|
2010-12-03 04:34:57 +00: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
|
2016-07-22 18:15:38 +00:00
|
|
|
// it encounters. If the path does not exist, RemoveAll
|
2010-12-03 04:34:57 +00:00
|
|
|
// returns nil (no error).
|
2011-12-03 02:17:34 +00:00
|
|
|
func RemoveAll(path string) error {
|
2010-12-03 04:34:57 +00:00
|
|
|
// Simple case: if Remove works, we're done.
|
|
|
|
err := Remove(path)
|
2015-01-15 00:27:56 +00:00
|
|
|
if err == nil || IsNotExist(err) {
|
2010-12-03 04:34:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, is this a directory we need to recurse into?
|
|
|
|
dir, serr := Lstat(path)
|
|
|
|
if serr != nil {
|
2012-03-02 20:01:37 +00:00
|
|
|
if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
|
2010-12-03 04:34:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return serr
|
|
|
|
}
|
2011-12-13 19:16:27 +00:00
|
|
|
if !dir.IsDir() {
|
2010-12-03 04:34:57 +00:00
|
|
|
// Not a directory; return the error from Remove.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove contents & return first error.
|
|
|
|
err = nil
|
|
|
|
for {
|
2018-09-24 21:46:21 +00:00
|
|
|
fd, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
if IsNotExist(err) {
|
|
|
|
// Already deleted by someone else.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2018-01-09 01:23:08 +00:00
|
|
|
}
|
2018-09-24 21:46:21 +00:00
|
|
|
|
|
|
|
const request = 1024
|
|
|
|
names, err1 := fd.Readdirnames(request)
|
|
|
|
|
|
|
|
// Removing files from the directory may have caused
|
|
|
|
// the OS to reshuffle it. Simply calling Readdirnames
|
|
|
|
// again may skip some entries. The only reliable way
|
|
|
|
// to avoid this is to close and re-open the
|
|
|
|
// directory. See issue 20841.
|
|
|
|
fd.Close()
|
|
|
|
|
2010-12-03 04:34:57 +00:00
|
|
|
for _, name := range names {
|
2011-09-16 15:47:21 +00:00
|
|
|
err1 := RemoveAll(path + string(PathSeparator) + name)
|
2010-12-03 04:34:57 +00:00
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 21:46:21 +00:00
|
|
|
|
2011-12-03 02:17:34 +00:00
|
|
|
if err1 == io.EOF {
|
2011-09-16 15:47:21 +00:00
|
|
|
break
|
|
|
|
}
|
2010-12-03 04:34:57 +00:00
|
|
|
// If Readdirnames returned an error, use it.
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-09-24 21:46:21 +00:00
|
|
|
// We don't want to re-open unnecessarily, so if we
|
|
|
|
// got fewer than request names from Readdirnames, try
|
|
|
|
// simply removing the directory now. If that
|
|
|
|
// succeeds, we are done.
|
|
|
|
if len(names) < request {
|
|
|
|
err1 := Remove(path)
|
|
|
|
if err1 == nil || IsNotExist(err1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// We got some error removing the
|
|
|
|
// directory contents, and since we
|
|
|
|
// read fewer names than we requested
|
|
|
|
// there probably aren't more files to
|
|
|
|
// remove. Don't loop around to read
|
|
|
|
// the directory again. We'll probably
|
|
|
|
// just get the same error.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-03 04:34:57 +00:00
|
|
|
|
|
|
|
// Remove directory.
|
|
|
|
err1 := Remove(path)
|
2015-01-15 00:27:56 +00:00
|
|
|
if err1 == nil || IsNotExist(err1) {
|
|
|
|
return nil
|
|
|
|
}
|
2010-12-03 04:34:57 +00:00
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|