Retro68/gcc/libgo/go/os/path.go

80 lines
2.0 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00: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
import (
"syscall"
)
// MkdirAll creates a directory named path,
// along with any necessary parents, and returns nil,
// or else returns an error.
2018-12-28 15:30:48 +00:00
// The permission bits perm (before umask) are used for all
2012-03-27 23:13:14 +00:00
// directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing
// and returns nil.
func MkdirAll(path string, perm FileMode) error {
2015-08-28 15:33:40 +00:00
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
2012-03-27 23:13:14 +00:00
dir, err := Stat(path)
if err == nil {
if dir.IsDir() {
return nil
}
return &PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
// Slow path: make sure parent exists and then call Mkdir for path.
2012-03-27 23:13:14 +00:00
i := len(path)
for i > 0 && IsPathSeparator(path[i-1]) { // Skip trailing path separator.
i--
}
j := i
for j > 0 && !IsPathSeparator(path[j-1]) { // Scan backward over element.
j--
}
if j > 1 {
2019-06-02 15:48:37 +00:00
// Create parent.
err = MkdirAll(fixRootDirectory(path[:j-1]), perm)
2012-03-27 23:13:14 +00:00
if err != nil {
return err
}
}
2015-08-28 15:33:40 +00:00
// Parent now exists; invoke Mkdir and use its result.
2012-03-27 23:13:14 +00:00
err = Mkdir(path, perm)
if err != nil {
// Handle arguments like "foo/." by
// double-checking that directory doesn't exist.
dir, err1 := Lstat(path)
if err1 == nil && dir.IsDir() {
return nil
}
return err
}
return nil
}
// 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
2012-03-27 23:13:14 +00:00
// returns nil (no error).
2019-06-02 15:48:37 +00:00
// If there is an error, it will be of type *PathError.
2012-03-27 23:13:14 +00:00
func RemoveAll(path string) error {
2019-06-02 15:48:37 +00:00
return removeAll(path)
}
2012-03-27 23:13:14 +00:00
2019-06-02 15:48:37 +00:00
// endsWithDot reports whether the final component of path is ".".
func endsWithDot(path string) bool {
if path == "." {
return true
2015-08-28 15:33:40 +00:00
}
2019-06-02 15:48:37 +00:00
if len(path) >= 2 && path[len(path)-1] == '.' && IsPathSeparator(path[len(path)-2]) {
return true
2012-03-27 23:13:14 +00:00
}
2019-06-02 15:48:37 +00:00
return false
2012-03-27 23:13:14 +00:00
}