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
|
|
|
|
|
2012-02-16 16:04:29 -07:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
2011-11-01 19:49:08 -06:00
|
|
|
|
2009-05-15 15:11:24 -06:00
|
|
|
// MkdirAll creates a directory named path,
|
|
|
|
// along with any necessary parents, and returns nil,
|
|
|
|
// or else returns an error.
|
2017-12-13 08:45:09 -07:00
|
|
|
// The permission bits perm (before umask) are used for all
|
2009-05-15 15:11:24 -06:00
|
|
|
// directories that MkdirAll creates.
|
|
|
|
// If path is already a directory, MkdirAll does nothing
|
|
|
|
// and returns nil.
|
2012-01-19 16:45:18 -07:00
|
|
|
func MkdirAll(path string, perm FileMode) error {
|
2014-10-06 13:49:33 -06:00
|
|
|
// Fast path: if we can tell whether path is a directory or file, 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 {
|
2011-11-30 10:04:16 -07:00
|
|
|
if dir.IsDir() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
2014-10-06 13:49:33 -06:00
|
|
|
// Slow path: make sure parent exists and then call Mkdir for path.
|
2009-12-15 16:40:16 -07:00
|
|
|
i := len(path)
|
2011-05-28 21:03:49 -06:00
|
|
|
for i > 0 && IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
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
|
2011-05-28 21:03:49 -06:00
|
|
|
for j > 0 && !IsPathSeparator(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 {
|
2018-01-28 14:19:13 -07:00
|
|
|
// Create parent.
|
2018-02-19 14:26:01 -07:00
|
|
|
err = MkdirAll(fixRootDirectory(path[: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 13:49:33 -06:00
|
|
|
// Parent now exists; invoke Mkdir and use its result.
|
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)
|
2011-11-30 10:04:16 -07:00
|
|
|
if err1 == nil && dir.IsDir() {
|
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
|
|
|
}
|
2018-11-21 16:06:42 -07:00
|
|
|
|
2019-01-29 17:36:25 -07:00
|
|
|
// RemoveAll removes path and any children it contains.
|
|
|
|
// It removes everything it can but returns the first error
|
|
|
|
// it encounters. If the path does not exist, RemoveAll
|
|
|
|
// returns nil (no error).
|
2019-03-01 03:04:14 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2019-01-29 17:36:25 -07:00
|
|
|
func RemoveAll(path string) error {
|
|
|
|
return removeAll(path)
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:06:42 -07:00
|
|
|
// endsWithDot reports whether the final component of path is ".".
|
|
|
|
func endsWithDot(path string) bool {
|
|
|
|
if path == "." {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(path) >= 2 && path[len(path)-1] == '.' && IsPathSeparator(path[len(path)-2]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|