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
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2010-08-03 16:34:52 -06:00
|
|
|
func MkdirAll(path string, perm uint32) 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 {
|
|
|
|
if dir.IsDirectory() {
|
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 &PathError{"mkdir", path, 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)
|
|
|
|
for i > 0 && path[i-1] == '/' { // Skip trailing slashes.
|
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
|
|
|
|
for j > 0 && 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)
|
2009-05-15 15:11:24 -06:00
|
|
|
if err1 == nil && dir.IsDirectory() {
|
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).
|
2009-05-15 15:11:24 -06:00
|
|
|
func RemoveAll(path string) Error {
|
|
|
|
// Simple case: if Remove works, we're done.
|
2009-12-15 16:40:16 -07:00
|
|
|
err := Remove(path)
|
2009-05-15 15:11:24 -06:00
|
|
|
if err == nil {
|
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 {
|
|
|
|
if serr, ok := serr.(*PathError); ok && serr.Error == ENOENT {
|
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
|
|
|
}
|
|
|
|
if !dir.IsDirectory() {
|
|
|
|
// 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.
|
2009-12-15 16:40:16 -07:00
|
|
|
fd, err := Open(path, O_RDONLY, 0)
|
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 {
|
2009-12-15 16:40:16 -07:00
|
|
|
err1 := RemoveAll(path + "/" + 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
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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)
|
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
|
|
|
}
|