From 4fd91c3f377cf0a65d16226a517f803a080502ee Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Fri, 7 Oct 2022 18:47:14 -0400 Subject: [PATCH] daemon: refactor isOnlineFSOperationPermitted It is only applicable to Windows so it does not need to be called from platform-generic code. Fix locking in the Windows implementation. Signed-off-by: Cory Snider --- daemon/archive.go | 20 -------------------- daemon/archive_unix.go | 6 ------ daemon/archive_windows.go | 24 ++++++++++++++++++++++-- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/daemon/archive.go b/daemon/archive.go index c11ec16a4f..4be02b25c6 100644 --- a/daemon/archive.go +++ b/daemon/archive.go @@ -16,11 +16,6 @@ func (daemon *Daemon) ContainerCopy(name string, res string) (io.ReadCloser, err return nil, err } - // Make sure an online file-system operation is permitted. - if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil { - return nil, errdefs.System(err) - } - data, err := daemon.containerCopy(ctr, res) if err == nil { return data, nil @@ -40,11 +35,6 @@ func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *types.C return nil, err } - // Make sure an online file-system operation is permitted. - if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil { - return nil, errdefs.System(err) - } - stat, err = daemon.containerStatPath(ctr, path) if err == nil { return stat, nil @@ -65,11 +55,6 @@ func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io return nil, nil, err } - // Make sure an online file-system operation is permitted. - if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil { - return nil, nil, errdefs.System(err) - } - content, stat, err = daemon.containerArchivePath(ctr, path) if err == nil { return content, stat, nil @@ -93,11 +78,6 @@ func (daemon *Daemon) ContainerExtractToDir(name, path string, copyUIDGID, noOve return err } - // Make sure an online file-system operation is permitted. - if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil { - return errdefs.System(err) - } - err = daemon.containerExtractToDir(ctr, path, copyUIDGID, noOverwriteDirNonDir, content) if err == nil { return nil diff --git a/daemon/archive_unix.go b/daemon/archive_unix.go index ea190b3a24..412d369f4c 100644 --- a/daemon/archive_unix.go +++ b/daemon/archive_unix.go @@ -332,9 +332,3 @@ func checkIfPathIsInAVolume(container *container.Container, absPath string) (boo } return toVolume, nil } - -// isOnlineFSOperationPermitted returns an error if an online filesystem operation -// is not permitted. -func (daemon *Daemon) isOnlineFSOperationPermitted(container *container.Container) error { - return nil -} diff --git a/daemon/archive_windows.go b/daemon/archive_windows.go index 6b6f03fa45..ba6e17c98f 100644 --- a/daemon/archive_windows.go +++ b/daemon/archive_windows.go @@ -23,6 +23,11 @@ func (daemon *Daemon) containerStatPath(container *container.Container, path str container.Lock() defer container.Unlock() + // Make sure an online file-system operation is permitted. + if err := daemon.isOnlineFSOperationPermitted(container); err != nil { + return nil, err + } + if err = daemon.Mount(container); err != nil { return nil, err } @@ -60,6 +65,11 @@ func (daemon *Daemon) containerArchivePath(container *container.Container, path } }() + // Make sure an online file-system operation is permitted. + if err := daemon.isOnlineFSOperationPermitted(container); err != nil { + return nil, nil, err + } + if err = daemon.Mount(container); err != nil { return nil, nil, err } @@ -142,6 +152,11 @@ func (daemon *Daemon) containerExtractToDir(container *container.Container, path container.Lock() defer container.Unlock() + // Make sure an online file-system operation is permitted. + if err := daemon.isOnlineFSOperationPermitted(container); err != nil { + return err + } + if err = daemon.Mount(container); err != nil { return err } @@ -260,6 +275,11 @@ func (daemon *Daemon) containerCopy(container *container.Container, resource str } }() + // Make sure an online file-system operation is permitted. + if err := daemon.isOnlineFSOperationPermitted(container); err != nil { + return nil, err + } + if err := daemon.Mount(container); err != nil { return nil, err } @@ -327,9 +347,9 @@ func checkIfPathIsInAVolume(container *container.Container, absPath string) (boo // is not permitted (such as stat or for copying). Running Hyper-V containers // cannot have their file-system interrogated from the host as the filter is // loaded inside the utility VM, not the host. -// IMPORTANT: The container lock must NOT be held when calling this function. +// IMPORTANT: The container lock MUST be held when calling this function. func (daemon *Daemon) isOnlineFSOperationPermitted(container *container.Container) error { - if !container.IsRunning() { + if !container.Running { return nil }