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 // If path exists, stop with success or error.
21 dir
, err
:= Stat(path
)
26 return &PathError
{"mkdir", path
, syscall
.ENOTDIR
}
29 // Doesn't already exist; make sure parent does.
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 // Now parent exists, try to create.
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.
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.
92 // Remove contents & return first error.
95 names
, err1
:= fd
.Readdirnames(100)
96 for _
, name
:= range names
{
97 err1
:= RemoveAll(path
+ string(PathSeparator
) + name
)
105 // If Readdirnames returned an error, use it.
114 // Close directory, because windows won't remove opened directory.