mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
2aa13f86f0
Standard golang's `os.MkdirAll()` function returns "not a directory" error in case a directory to be created already exists but is not a directory (e.g. a file). Our own `idtools.MkdirAs*()` functions do not replicate the behavior. This is a bug since all `Mkdir()`-like functions are expected to ensure the required directory exists and is indeed a directory, and return an error otherwise. As the code is using our in-house `system.Stat()` call returning a type which is incompatible with that of golang's `os.Stat()`, I had to amend the `system` package with `IsDir()`. A test case is also provided. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
// +build !windows
|
|
|
|
package system
|
|
|
|
import (
|
|
"syscall"
|
|
)
|
|
|
|
// StatT type contains status of a file. It contains metadata
|
|
// like permission, owner, group, size, etc about a file.
|
|
type StatT struct {
|
|
mode uint32
|
|
uid uint32
|
|
gid uint32
|
|
rdev uint64
|
|
size int64
|
|
mtim syscall.Timespec
|
|
}
|
|
|
|
// Mode returns file's permission mode.
|
|
func (s StatT) Mode() uint32 {
|
|
return s.mode
|
|
}
|
|
|
|
// UID returns file's user id of owner.
|
|
func (s StatT) UID() uint32 {
|
|
return s.uid
|
|
}
|
|
|
|
// GID returns file's group id of owner.
|
|
func (s StatT) GID() uint32 {
|
|
return s.gid
|
|
}
|
|
|
|
// Rdev returns file's device ID (if it's special file).
|
|
func (s StatT) Rdev() uint64 {
|
|
return s.rdev
|
|
}
|
|
|
|
// Size returns file's size.
|
|
func (s StatT) Size() int64 {
|
|
return s.size
|
|
}
|
|
|
|
// Mtim returns file's last modification time.
|
|
func (s StatT) Mtim() syscall.Timespec {
|
|
return s.mtim
|
|
}
|
|
|
|
// IsDir reports whether s describes a directory.
|
|
func (s StatT) IsDir() bool {
|
|
return s.mode&syscall.S_IFDIR != 0
|
|
}
|
|
|
|
// Stat takes a path to a file and returns
|
|
// a system.StatT type pertaining to that file.
|
|
//
|
|
// Throws an error if the file does not exist
|
|
func Stat(path string) (*StatT, error) {
|
|
s := &syscall.Stat_t{}
|
|
if err := syscall.Stat(path, s); err != nil {
|
|
return nil, err
|
|
}
|
|
return fromStatT(s)
|
|
}
|