1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg
Kir Kolyshkin 516010e92d 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 <kolyshkin@gmail.com>
2017-11-27 17:32:12 -08:00
..
aaparser apparmor: make pkg/aaparser work on read-only root 2017-05-18 00:05:13 +10:00
archive Remove solaris files 2017-10-24 15:39:34 -04:00
authorization Update libcontainerd to use containerd 1.0 2017-10-20 07:11:37 -07:00
broadcaster
chrootarchive Merge pull request #35285 from crosbymichael/solaris 2017-10-25 15:14:04 +02:00
containerfs pkg/package: remove promise package 2017-09-21 17:56:45 -07:00
devicemapper devmapper gd: disable for static build 2017-09-17 22:04:31 -07:00
directory Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
discovery Add gosimple linter 2017-09-12 12:09:59 -04:00
dmesg devmapper: show dmesg if mount fails 2017-09-17 22:04:31 -07:00
filenotify Update logrus to v1.0.1 2017-07-31 13:16:46 -07:00
fileutils Remove solaris files 2017-10-24 15:39:34 -04:00
fsutils [project] change syscall to /x/sys/unix|windows 2017-07-11 08:00:32 -04:00
homedir Split homedir files by operating system 2017-07-10 14:10:43 +01:00
idtools Simplify/fix MkdirAll usage 2017-11-27 17:32:12 -08:00
ioutils Fix golint errors. 2017-08-18 14:23:44 -04:00
jsonmessage Move RFC3339NanoFixed to a more appropriate package. 2017-09-25 16:07:24 -04:00
locker pkg/locker: add benchmarks 2017-10-02 11:20:21 -07:00
longpath
loopback Add gosimple linter 2017-09-12 12:09:59 -04:00
mount Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
namesgenerator Merge pull request #35250 from joppich/patch-1 2017-11-09 06:21:02 -08:00
parsers Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
pidfile [project] change syscall to /x/sys/unix|windows 2017-07-11 08:00:32 -04:00
platform Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
plugingetter Fixup some issues with plugin refcounting 2017-10-21 15:17:57 -04:00
plugins Add ineffassign linter 2017-09-08 18:23:21 -04:00
pools Add gosimple linter 2017-09-12 12:09:59 -04:00
progress Update the stream formatter to display custom unit numbers. 2017-05-11 11:13:11 -07:00
pubsub Make plugin emit strongly typed, consumable events 2017-07-06 14:26:06 -04:00
reexec Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
signal Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
stdcopy Spelling fixes 2017-07-03 13:13:09 -07:00
streamformatter Add goimports to linters. 2017-08-21 18:15:08 -04:00
stringid pkg: remove random package 2017-05-08 17:02:02 -07:00
symlink [project] change syscall to /x/sys/unix|windows 2017-07-11 08:00:32 -04:00
sysinfo Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
system idtools.MkdirAs*: error out if dir exists as file 2017-11-27 13:25:44 -08:00
tailfile Change reading order of tailfile 2016-11-13 20:08:51 -08:00
tarsum Add ineffassign linter 2017-09-08 18:23:21 -04:00
term Remove solaris build tag and `contrib/mkimage/solaris 2017-11-02 00:01:46 +00:00
truncindex Fix non thread-safe Iteration around go-patricia 2017-01-09 17:06:50 +00:00
urlutil Move IsGitTransport() to gitutils 2017-06-26 10:07:04 -07:00
useragent
README.md Rename a few docker to moby 2017-10-25 13:56:12 +02:00

pkg/ is a collection of utility packages used by the Moby project without being specific to its internals.

Utility packages are kept separate from the moby core codebase to keep it as small and concise as possible. If some utilities grow larger and their APIs stabilize, they may be moved to their own repository under the Moby organization, to facilitate re-use by other projects. However that is not the priority.

The directory pkg is named after the same directory in the camlistore project. Since Brad is a core Go maintainer, we thought it made sense to copy his methods for organizing Go code :) Thanks Brad!

Because utility packages are small and neatly separated from the rest of the codebase, they are a good place to start for aspiring maintainers and contributors. Get in touch if you want to help maintain them!