Skip to content

Commit 516010e

Browse files
committed
Simplify/fix MkdirAll usage
This subtle bug keeps lurking in because error checking for `Mkdir()` and `MkdirAll()` is slightly different wrt to `EEXIST`/`IsExist`: - for `Mkdir()`, `IsExist` error should (usually) be ignored (unless you want to make sure directory was not there before) as it means "the destination directory was already there" - for `MkdirAll()`, `IsExist` error should NEVER be ignored. Mostly, this commit just removes ignoring the IsExist error, as it should not be ignored. Also, there are a couple of cases then IsExist is handled as "directory already exist" which is wrong. As a result, some code that never worked as intended is now removed. NOTE that `idtools.MkdirAndChown()` behaves like `os.MkdirAll()` rather than `os.Mkdir()` -- so its description is amended accordingly, and its usage is handled as such (i.e. IsExist error is not ignored). For more details, a quote from my runc commit 6f82d4b (July 2015): TL;DR: check for IsExist(err) after a failed MkdirAll() is both redundant and wrong -- so two reasons to remove it. Quoting MkdirAll documentation: > MkdirAll creates a directory named path, along with any necessary > parents, and returns nil, or else returns an error. If path > is already a directory, MkdirAll does nothing and returns nil. This means two things: 1. If a directory to be created already exists, no error is returned. 2. If the error returned is IsExist (EEXIST), it means there exists a non-directory with the same name as MkdirAll need to use for directory. Example: we want to MkdirAll("a/b"), but file "a" (or "a/b") already exists, so MkdirAll fails. The above is a theory, based on quoted documentation and my UNIX knowledge. 3. In practice, though, current MkdirAll implementation [1] returns ENOTDIR in most of cases described in #2, with the exception when there is a race between MkdirAll and someone else creating the last component of MkdirAll argument as a file. In this very case MkdirAll() will indeed return EEXIST. Because of #1, IsExist check after MkdirAll is not needed. Because of #2 and #3, ignoring IsExist error is just plain wrong, as directory we require is not created. It's cleaner to report the error now. Note this error is all over the tree, I guess due to copy-paste, or trying to follow the same usage pattern as for Mkdir(), or some not quite correct examples on the Internet. [1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 2aa13f8 commit 516010e

File tree

14 files changed

+19
-40
lines changed

14 files changed

+19
-40
lines changed

daemon/checkpoint.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ func getCheckpointDir(checkDir, checkpointID, ctrName, ctrID, ctrCheckpointDir s
3434
err2 = fmt.Errorf("checkpoint with name %s already exists for container %s", checkpointID, ctrName)
3535
case err != nil && os.IsNotExist(err):
3636
err2 = os.MkdirAll(checkpointAbsDir, 0700)
37-
if os.IsExist(err2) {
38-
err2 = nil
39-
}
4037
case err != nil:
4138
err2 = err
4239
case err == nil:

daemon/daemon.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,22 +675,22 @@ func NewDaemon(config *config.Config, registryService registry.Service, containe
675675
}
676676

677677
daemonRepo := filepath.Join(config.Root, "containers")
678-
if err := idtools.MkdirAllAndChown(daemonRepo, 0700, rootIDs); err != nil && !os.IsExist(err) {
678+
if err := idtools.MkdirAllAndChown(daemonRepo, 0700, rootIDs); err != nil {
679679
return nil, err
680680
}
681681

682682
// Create the directory where we'll store the runtime scripts (i.e. in
683683
// order to support runtimeArgs)
684684
daemonRuntimes := filepath.Join(config.Root, "runtimes")
685-
if err := system.MkdirAll(daemonRuntimes, 0700, ""); err != nil && !os.IsExist(err) {
685+
if err := system.MkdirAll(daemonRuntimes, 0700, ""); err != nil {
686686
return nil, err
687687
}
688688
if err := d.loadRuntimes(); err != nil {
689689
return nil, err
690690
}
691691

692692
if runtime.GOOS == "windows" {
693-
if err := system.MkdirAll(filepath.Join(config.Root, "credentialspecs"), 0, ""); err != nil && !os.IsExist(err) {
693+
if err := system.MkdirAll(filepath.Join(config.Root, "credentialspecs"), 0, ""); err != nil {
694694
return nil, err
695695
}
696696
}

daemon/daemon_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ func (daemon *Daemon) initCgroupsPath(path string) error {
15141514

15151515
func maybeCreateCPURealTimeFile(sysinfoPresent bool, configValue int64, file string, path string) error {
15161516
if sysinfoPresent && configValue != 0 {
1517-
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
1517+
if err := os.MkdirAll(path, 0755); err != nil {
15181518
return err
15191519
}
15201520
if err := ioutil.WriteFile(filepath.Join(path, file), []byte(strconv.FormatInt(configValue, 10)), 0700); err != nil {

daemon/daemon_windows.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package daemon
33
import (
44
"context"
55
"fmt"
6-
"os"
76
"path/filepath"
87
"strings"
98

@@ -485,7 +484,7 @@ func setupRemappedRoot(config *config.Config) (*idtools.IDMappings, error) {
485484
func setupDaemonRoot(config *config.Config, rootDir string, rootIDs idtools.IDPair) error {
486485
config.Root = rootDir
487486
// Create the root directory if it doesn't exists
488-
if err := system.MkdirAllWithACL(config.Root, 0, system.SddlAdministratorsLocalSystem); err != nil && !os.IsExist(err) {
487+
if err := system.MkdirAllWithACL(config.Root, 0, system.SddlAdministratorsLocalSystem); err != nil {
489488
return err
490489
}
491490
return nil

daemon/graphdriver/aufs/aufs.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,8 @@ func Init(root string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
122122
if err != nil {
123123
return nil, err
124124
}
125-
// Create the root aufs driver dir and return
126-
// if it already exists
127-
// If not populate the dir structure
125+
// Create the root aufs driver dir
128126
if err := idtools.MkdirAllAndChown(root, 0700, idtools.IDPair{UID: rootUID, GID: rootGID}); err != nil {
129-
if os.IsExist(err) {
130-
return a, nil
131-
}
132127
return nil, err
133128
}
134129

daemon/graphdriver/devmapper/deviceset.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (devices *DeviceSet) ensureImage(name string, size int64) (string, error) {
268268
if err != nil {
269269
return "", err
270270
}
271-
if err := idtools.MkdirAllAndChown(dirname, 0700, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
271+
if err := idtools.MkdirAllAndChown(dirname, 0700, idtools.IDPair{UID: uid, GID: gid}); err != nil {
272272
return "", err
273273
}
274274

@@ -1697,10 +1697,10 @@ func (devices *DeviceSet) initDevmapper(doInit bool) (retErr error) {
16971697
if err != nil {
16981698
return err
16991699
}
1700-
if err := idtools.MkdirAndChown(devices.root, 0700, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
1700+
if err := idtools.MkdirAndChown(devices.root, 0700, idtools.IDPair{UID: uid, GID: gid}); err != nil {
17011701
return err
17021702
}
1703-
if err := os.MkdirAll(devices.metadataDir(), 0700); err != nil && !os.IsExist(err) {
1703+
if err := os.MkdirAll(devices.metadataDir(), 0700); err != nil {
17041704
return err
17051705
}
17061706

daemon/graphdriver/devmapper/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
189189
}
190190

191191
// Create the target directories if they don't exist
192-
if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
192+
if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
193193
d.ctr.Decrement(mp)
194194
return nil, err
195195
}
@@ -204,7 +204,7 @@ func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
204204
return nil, err
205205
}
206206

207-
if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
207+
if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
208208
d.ctr.Decrement(mp)
209209
d.DeviceSet.UnmountDevice(id, mp)
210210
return nil, err

daemon/graphdriver/overlay/overlay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
136136
return nil, err
137137
}
138138
// Create the driver home dir
139-
if err := idtools.MkdirAllAndChown(home, 0700, idtools.IDPair{rootUID, rootGID}); err != nil && !os.IsExist(err) {
139+
if err := idtools.MkdirAllAndChown(home, 0700, idtools.IDPair{rootUID, rootGID}); err != nil {
140140
return nil, err
141141
}
142142

daemon/graphdriver/overlay2/overlay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
171171
return nil, err
172172
}
173173
// Create the driver home dir
174-
if err := idtools.MkdirAllAndChown(path.Join(home, linkDir), 0700, idtools.IDPair{rootUID, rootGID}); err != nil && !os.IsExist(err) {
174+
if err := idtools.MkdirAllAndChown(path.Join(home, linkDir), 0700, idtools.IDPair{rootUID, rootGID}); err != nil {
175175
return nil, err
176176
}
177177

pkg/idtools/idtools.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ func MkdirAllAndChown(path string, mode os.FileMode, owner IDPair) error {
4242
}
4343

4444
// MkdirAndChown creates a directory and then modifies ownership to the requested uid/gid.
45-
// If the directory already exists, this function still changes ownership
45+
// If the directory already exists, this function still changes ownership.
46+
// Note that unlike os.Mkdir(), this function does not return IsExist error
47+
// in case path already exists.
4648
func MkdirAndChown(path string, mode os.FileMode, owner IDPair) error {
4749
return mkdirAs(path, mode, owner.UID, owner.GID, false, true)
4850
}

pkg/idtools/idtools_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func mkdirAs(path string, mode os.FileMode, ownerUID, ownerGID int, mkAll, chown
3131
stat, err := system.Stat(path)
3232
if err == nil {
3333
if !stat.IsDir() {
34-
return &os.PathError{"mkdir", path, syscall.ENOTDIR}
34+
return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
3535
}
3636
if !chownExisting {
3737
return nil
@@ -58,7 +58,7 @@ func mkdirAs(path string, mode os.FileMode, ownerUID, ownerGID int, mkAll, chown
5858
paths = append(paths, dirPath)
5959
}
6060
}
61-
if err := system.MkdirAll(path, mode, ""); err != nil && !os.IsExist(err) {
61+
if err := system.MkdirAll(path, mode, ""); err != nil {
6262
return err
6363
}
6464
} else {

pkg/idtools/idtools_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// Platforms such as Windows do not support the UID/GID concept. So make this
1212
// just a wrapper around system.MkdirAll.
1313
func mkdirAs(path string, mode os.FileMode, ownerUID, ownerGID int, mkAll, chownExisting bool) error {
14-
if err := system.MkdirAll(path, mode, ""); err != nil && !os.IsExist(err) {
14+
if err := system.MkdirAll(path, mode, ""); err != nil {
1515
return err
1616
}
1717
return nil

volume/local/local.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,6 @@ func (r *Root) Name() string {
139139
return volume.DefaultDriverName
140140
}
141141

142-
type alreadyExistsError struct {
143-
path string
144-
}
145-
146-
func (e alreadyExistsError) Error() string {
147-
return "local volume already exists under " + e.path
148-
}
149-
150-
func (e alreadyExistsError) Conflict() {}
151-
152142
type systemError struct {
153143
err error
154144
}
@@ -181,9 +171,6 @@ func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error
181171

182172
path := r.DataPath(name)
183173
if err := idtools.MkdirAllAndChown(path, 0755, r.rootIDs); err != nil {
184-
if os.IsExist(err) {
185-
return nil, alreadyExistsError{filepath.Dir(path)}
186-
}
187174
return nil, errors.Wrapf(systemError{err}, "error while creating volume path '%s'", path)
188175
}
189176

volume/volume.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ func (m *MountPoint) Setup(mountLabel string, rootIDs idtools.IDPair, checkFun f
192192
return "", fmt.Errorf("Unable to setup mount point, neither source nor volume defined")
193193
}
194194

195-
// system.MkdirAll() produces an error if m.Source exists and is a file (not a directory),
196195
if m.Type == mounttypes.TypeBind {
197196
// Before creating the source directory on the host, invoke checkFun if it's not nil. One of
198197
// the use case is to forbid creating the daemon socket as a directory if the daemon is in

0 commit comments

Comments
 (0)