1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 // MkdirAll creates a directory named path,
9 // along with any necessary parents, and returns nil,
10 // or else returns an error.
11 // The permission bits perm are used for all
12 // directories that MkdirAll creates.
13 // If path is already a directory, MkdirAll does nothing
15 func MkdirAll(path
string, perm
uint32) Error
{
16 // If path exists, stop with success or error.
17 dir
, err
:= Lstat(path
)
19 if dir
.IsDirectory() {
22 return &PathError
{"mkdir", path
, ENOTDIR
}
25 // Doesn't already exist; make sure parent does.
27 for i
> 0 && path
[i
-1] == '/' { // Skip trailing slashes.
32 for j
> 0 && path
[j
-1] != '/' { // Scan backward over element.
38 err
= MkdirAll(path
[0:j
-1], perm
)
44 // Now parent exists, try to create.
45 err
= Mkdir(path
, perm
)
47 // Handle arguments like "foo/." by
48 // double-checking that directory doesn't exist.
49 dir
, err1
:= Lstat(path
)
50 if err1
== nil && dir
.IsDirectory() {
58 // RemoveAll removes path and any children it contains.
59 // It removes everything it can but returns the first error
60 // it encounters. If the path does not exist, RemoveAll
61 // returns nil (no error).
62 func RemoveAll(path
string) Error
{
63 // Simple case: if Remove works, we're done.
69 // Otherwise, is this a directory we need to recurse into?
70 dir
, serr
:= Lstat(path
)
72 if serr
, ok
:= serr
.(*PathError
); ok
&& serr
.Error
== ENOENT
{
77 if !dir
.IsDirectory() {
78 // Not a directory; return the error from Remove.
83 fd
, err
:= Open(path
, O_RDONLY
, 0)
88 // Remove contents & return first error.
91 names
, err1
:= fd
.Readdirnames(100)
92 for _
, name
:= range names
{
93 err1
:= RemoveAll(path
+ "/" + name
)
98 // If Readdirnames returned an error, use it.
107 // Close directory, because windows won't remove opened directory.