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.
12 // MkdirAll creates a directory named path,
13 // along with any necessary parents, and returns nil,
14 // or else returns an error.
15 // The permission bits perm are used for all
16 // directories that MkdirAll creates.
17 // If path is already a directory, MkdirAll does nothing
19 func MkdirAll(path
string, perm FileMode
) error
{
20 // Fast path: if we can tell whether path is a directory or file, stop with success or error.
21 dir
, err
:= Stat(path
)
26 return &PathError
{"mkdir", path
, syscall
.ENOTDIR
}
29 // Slow path: make sure parent exists and then call Mkdir for path.
31 for i
> 0 && IsPathSeparator(path
[i
-1]) { // Skip trailing path separator.
36 for j
> 0 && !IsPathSeparator(path
[j
-1]) { // Scan backward over element.
42 err
= MkdirAll(path
[0:j
-1], perm
)
48 // Parent now exists; invoke Mkdir and use its result.
49 err
= Mkdir(path
, perm
)
51 // Handle arguments like "foo/." by
52 // double-checking that directory doesn't exist.
53 dir
, err1
:= Lstat(path
)
54 if err1
== nil && dir
.IsDir() {
62 // RemoveAll removes path and any children it contains.
63 // It removes everything it can but returns the first error
64 // it encounters. If the path does not exist, RemoveAll
65 // returns nil (no error).
66 func RemoveAll(path
string) error
{
67 // Simple case: if Remove works, we're done.
69 if err
== nil ||
IsNotExist(err
) {
73 // Otherwise, is this a directory we need to recurse into?
74 dir
, serr
:= Lstat(path
)
76 if serr
, ok
:= serr
.(*PathError
); ok
&& (IsNotExist(serr
.Err
) || serr
.Err
== syscall
.ENOTDIR
) {
82 // Not a directory; return the error from Remove.
90 // Race. It was deleted between the Lstat and Open.
91 // Return nil per RemoveAll's docs.
97 // Remove contents & return first error.
100 names
, err1
:= fd
.Readdirnames(100)
101 for _
, name
:= range names
{
102 err1
:= RemoveAll(path
+ string(PathSeparator
) + name
)
110 // If Readdirnames returned an error, use it.
119 // Close directory, because windows won't remove opened directory.
124 if err1
== nil ||
IsNotExist(err1
) {