From 5e4acc04363a9097cc9a2f29d93fd062fca1652b Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Thu, 18 Feb 2021 11:24:01 +0800 Subject: [PATCH] Fix file is not closed Signed-off-by: Shiming Zhang --- mount/losetup_linux.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mount/losetup_linux.go b/mount/losetup_linux.go index 88e0497da..4e9dcfb1f 100644 --- a/mount/losetup_linux.go +++ b/mount/losetup_linux.go @@ -72,7 +72,7 @@ func getFreeLoopDev() (uint32, error) { // setupLoopDev attaches the backing file to the loop device and returns // the file handle for the loop device. The caller is responsible for // closing the file handle. -func setupLoopDev(backingFile, loopDev string, param LoopParams) (*os.File, error) { +func setupLoopDev(backingFile, loopDev string, param LoopParams) (_ *os.File, retErr error) { // 1. Open backing file and loop device flags := os.O_RDWR if param.Readonly { @@ -85,12 +85,15 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) (*os.File, erro } defer back.Close() - // To make Autoclear (LO_FLAGS_AUTOCLEAR) work, this function intentionally - // doesn't close this file handle on defer. loop, err := os.OpenFile(loopDev, flags, 0) if err != nil { return nil, errors.Wrapf(err, "could not open loop device: %s", loopDev) } + defer func() { + if retErr != nil { + loop.Close() + } + }() // 2. Set FD if _, _, err = ioctl(loop.Fd(), unix.LOOP_SET_FD, back.Fd()); err != nil { @@ -127,10 +130,6 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) (*os.File, erro } } - // Cleanup loop fd and return error. - // If this function doesn't return this file handle, it must close the handle to - // prevent file descriptor leaks. - loop.Close() _, _, _ = ioctl(loop.Fd(), unix.LOOP_CLR_FD, 0) return nil, errors.Errorf("failed to set loop device info: %v", err) }