Merge pull request #5045 from wzshiming/fix/file-not-closed

FIx file is not closed
This commit is contained in:
Phil Estes 2021-02-18 12:45:19 -05:00 committed by GitHub
commit d30a6c005f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}