OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package os | 5 package os |
6 | 6 |
| 7 import "io" |
| 8 |
7 // MkdirAll creates a directory named path, | 9 // MkdirAll creates a directory named path, |
8 // along with any necessary parents, and returns nil, | 10 // along with any necessary parents, and returns nil, |
9 // or else returns an error. | 11 // or else returns an error. |
10 // The permission bits perm are used for all | 12 // The permission bits perm are used for all |
11 // directories that MkdirAll creates. | 13 // directories that MkdirAll creates. |
12 // If path is already a directory, MkdirAll does nothing | 14 // If path is already a directory, MkdirAll does nothing |
13 // and returns nil. | 15 // and returns nil. |
14 func MkdirAll(path string, perm uint32) Error { | 16 func MkdirAll(path string, perm uint32) error { |
15 // If path exists, stop with success or error. | 17 // If path exists, stop with success or error. |
16 dir, err := Stat(path) | 18 dir, err := Stat(path) |
17 if err == nil { | 19 if err == nil { |
18 if dir.IsDirectory() { | 20 if dir.IsDirectory() { |
19 return nil | 21 return nil |
20 } | 22 } |
21 return &PathError{"mkdir", path, ENOTDIR} | 23 return &PathError{"mkdir", path, ENOTDIR} |
22 } | 24 } |
23 | 25 |
24 // Doesn't already exist; make sure parent does. | 26 // Doesn't already exist; make sure parent does. |
(...skipping 26 matching lines...) Expand all Loading... |
51 } | 53 } |
52 return err | 54 return err |
53 } | 55 } |
54 return nil | 56 return nil |
55 } | 57 } |
56 | 58 |
57 // RemoveAll removes path and any children it contains. | 59 // RemoveAll removes path and any children it contains. |
58 // It removes everything it can but returns the first error | 60 // It removes everything it can but returns the first error |
59 // it encounters. If the path does not exist, RemoveAll | 61 // it encounters. If the path does not exist, RemoveAll |
60 // returns nil (no error). | 62 // returns nil (no error). |
61 func RemoveAll(path string) Error { | 63 func RemoveAll(path string) error { |
62 // Simple case: if Remove works, we're done. | 64 // Simple case: if Remove works, we're done. |
63 err := Remove(path) | 65 err := Remove(path) |
64 if err == nil { | 66 if err == nil { |
65 return nil | 67 return nil |
66 } | 68 } |
67 | 69 |
68 // Otherwise, is this a directory we need to recurse into? | 70 // Otherwise, is this a directory we need to recurse into? |
69 dir, serr := Lstat(path) | 71 dir, serr := Lstat(path) |
70 if serr != nil { | 72 if serr != nil { |
71 » » if serr, ok := serr.(*PathError); ok && (serr.Error == ENOENT ||
serr.Error == ENOTDIR) { | 73 » » if serr, ok := serr.(*PathError); ok && (serr.Err == ENOENT || s
err.Err == ENOTDIR) { |
72 return nil | 74 return nil |
73 } | 75 } |
74 return serr | 76 return serr |
75 } | 77 } |
76 if !dir.IsDirectory() { | 78 if !dir.IsDirectory() { |
77 // Not a directory; return the error from Remove. | 79 // Not a directory; return the error from Remove. |
78 return err | 80 return err |
79 } | 81 } |
80 | 82 |
81 // Directory. | 83 // Directory. |
82 fd, err := Open(path) | 84 fd, err := Open(path) |
83 if err != nil { | 85 if err != nil { |
84 return err | 86 return err |
85 } | 87 } |
86 | 88 |
87 // Remove contents & return first error. | 89 // Remove contents & return first error. |
88 err = nil | 90 err = nil |
89 for { | 91 for { |
90 names, err1 := fd.Readdirnames(100) | 92 names, err1 := fd.Readdirnames(100) |
91 for _, name := range names { | 93 for _, name := range names { |
92 err1 := RemoveAll(path + string(PathSeparator) + name) | 94 err1 := RemoveAll(path + string(PathSeparator) + name) |
93 if err == nil { | 95 if err == nil { |
94 err = err1 | 96 err = err1 |
95 } | 97 } |
96 } | 98 } |
97 » » if err1 == EOF { | 99 » » if err1 == io.EOF { |
98 break | 100 break |
99 } | 101 } |
100 // If Readdirnames returned an error, use it. | 102 // If Readdirnames returned an error, use it. |
101 if err == nil { | 103 if err == nil { |
102 err = err1 | 104 err = err1 |
103 } | 105 } |
104 if len(names) == 0 { | 106 if len(names) == 0 { |
105 break | 107 break |
106 } | 108 } |
107 } | 109 } |
108 | 110 |
109 // Close directory, because windows won't remove opened directory. | 111 // Close directory, because windows won't remove opened directory. |
110 fd.Close() | 112 fd.Close() |
111 | 113 |
112 // Remove directory. | 114 // Remove directory. |
113 err1 := Remove(path) | 115 err1 := Remove(path) |
114 if err == nil { | 116 if err == nil { |
115 err = err1 | 117 err = err1 |
116 } | 118 } |
117 return err | 119 return err |
118 } | 120 } |
OLD | NEW |