2009-05-15 15:11:24 -06: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-02-16 16:04:29 -07:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"syscall"
|
|
|
|
)
|
2011-11-01 19:49:08 -06:00
|
|
|
|
2009-05-15 15:11:24 -06: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-19 16:45:18 -07:00
|
|
|
func MkdirAll(path string, perm FileMode) error {
|
2009-05-15 15:11:24 -06:00
|
|
|
// If path exists, stop with success or error.
|
2010-12-09 16:43:45 -07:00
|
|
|
dir, err := Stat(path)
|
2009-05-15 15:11:24 -06:00
|
|
|
if err == nil {
|
2011-11-30 10:04:16 -07:00
|
|
|
if dir.IsDir() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
2012-02-16 16:04:29 -07:00
|
|
|
return &PathError{"mkdir", path, syscall.ENOTDIR}
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't already exist; make sure parent does.
|
2009-12-15 16:40:16 -07:00
|
|
|
i := len(path)
|
2011-05-28 21:03:49 -06:00
|
|
|
for i > 0 && IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
2009-11-09 13:07:39 -07:00
|
|
|
i--
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
j := i
|
2011-05-28 21:03:49 -06:00
|
|
|
for j > 0 && !IsPathSeparator(path[j-1]) { // Scan backward over element.
|
2009-11-09 13:07:39 -07:00
|
|
|
j--
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
2011-04-04 13:45:03 -06:00
|
|
|
if j > 1 {
|
2009-05-15 15:11:24 -06:00
|
|
|
// Create parent
|
2009-12-15 16:40:16 -07:00
|
|
|
err = MkdirAll(path[0:j-1], perm)
|
2009-05-15 15:11:24 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return err
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now parent exists, try to create.
|
2009-12-15 16:40:16 -07:00
|
|
|
err = Mkdir(path, perm)
|
2009-05-15 15:11:24 -06:00
|
|
|
if err != nil {
|
|
|
|
// Handle arguments like "foo/." by
|
|
|
|
// double-checking that directory doesn't exist.
|
2009-12-15 16:40:16 -07:00
|
|
|
dir, err1 := Lstat(path)
|
2011-11-30 10:04:16 -07:00
|
|
|
if err1 == nil && dir.IsDir() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return err
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return nil
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes path and any children it contains.
|
|
|
|
// It removes everything it can but returns the first error
|
2009-08-05 15:18:54 -06:00
|
|
|
// it encounters. If the path does not exist, RemoveAll
|
|
|
|
// returns nil (no error).
|
2011-11-01 19:49:08 -06:00
|
|
|
func RemoveAll(path string) error {
|
2009-05-15 15:11:24 -06:00
|
|
|
// Simple case: if Remove works, we're done.
|
2009-12-15 16:40:16 -07:00
|
|
|
err := Remove(path)
|
2014-09-18 12:48:47 -06:00
|
|
|
if err == nil || IsNotExist(err) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, is this a directory we need to recurse into?
|
2009-12-15 16:40:16 -07:00
|
|
|
dir, serr := Lstat(path)
|
2009-08-05 15:18:54 -06:00
|
|
|
if serr != nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-08-05 15:18:54 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return serr
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
2011-11-30 10:04:16 -07:00
|
|
|
if !dir.IsDir() {
|
2009-05-15 15:11:24 -06:00
|
|
|
// Not a directory; return the error from Remove.
|
2009-11-09 13:07:39 -07:00
|
|
|
return err
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Directory.
|
2011-04-05 00:42:14 -06:00
|
|
|
fd, err := Open(path)
|
2009-05-15 15:11:24 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return err
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove contents & return first error.
|
2009-12-15 16:40:16 -07:00
|
|
|
err = nil
|
2009-05-15 15:11:24 -06:00
|
|
|
for {
|
2009-12-15 16:40:16 -07:00
|
|
|
names, err1 := fd.Readdirnames(100)
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, name := range names {
|
2011-05-28 21:03:49 -06:00
|
|
|
err1 := RemoveAll(path + string(PathSeparator) + name)
|
2009-06-25 21:24:55 -06:00
|
|
|
if err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = err1
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
}
|
2011-11-01 19:49:08 -06:00
|
|
|
if err1 == io.EOF {
|
2011-05-16 10:26:16 -06:00
|
|
|
break
|
|
|
|
}
|
2009-05-15 15:11:24 -06:00
|
|
|
// If Readdirnames returned an error, use it.
|
2009-06-25 21:24:55 -06:00
|
|
|
if err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = err1
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
if len(names) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 20:35:34 -06:00
|
|
|
// Close directory, because windows won't remove opened directory.
|
|
|
|
fd.Close()
|
|
|
|
|
2009-05-15 15:11:24 -06:00
|
|
|
// Remove directory.
|
2009-12-15 16:40:16 -07:00
|
|
|
err1 := Remove(path)
|
2014-09-18 12:48:47 -06:00
|
|
|
if err1 == nil || IsNotExist(err1) {
|
|
|
|
return nil
|
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = err1
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return err
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|