From 919d08347a22bfe1034a5da40f2d767ccfcdba97 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 9 Aug 2019 15:38:32 +0200 Subject: [PATCH] Use loopback types and constants from x/sys/unix to implement pkg/loopback without cgo Signed-off-by: Tobias Klauser --- pkg/loopback/attach_loopback.go | 10 +++--- pkg/loopback/ioctl.go | 18 +++++------ pkg/loopback/loop_wrapper.go | 54 ++++++++------------------------- pkg/loopback/loopback.go | 2 +- 4 files changed, 28 insertions(+), 56 deletions(-) diff --git a/pkg/loopback/attach_loopback.go b/pkg/loopback/attach_loopback.go index 94feb8fc7d..61c40e07e6 100644 --- a/pkg/loopback/attach_loopback.go +++ b/pkg/loopback/attach_loopback.go @@ -1,4 +1,4 @@ -// +build linux,cgo +// +build linux package loopback // import "github.com/docker/docker/pkg/loopback" @@ -116,10 +116,10 @@ func AttachLoopDevice(sparseName string) (loop *os.File, err error) { } // Set the status of the loopback device - loopInfo := &loopInfo64{ - loFileName: stringToLoopName(loopFile.Name()), - loOffset: 0, - loFlags: LoFlagsAutoClear, + loopInfo := &unix.LoopInfo64{ + File_name: stringToLoopName(loopFile.Name()), + Offset: 0, + Flags: LoFlagsAutoClear, } if err := ioctlLoopSetStatus64(loopFile.Fd(), loopInfo); err != nil { diff --git a/pkg/loopback/ioctl.go b/pkg/loopback/ioctl.go index 612fd00abe..ab4f6e55c7 100644 --- a/pkg/loopback/ioctl.go +++ b/pkg/loopback/ioctl.go @@ -1,4 +1,4 @@ -// +build linux,cgo +// +build linux package loopback // import "github.com/docker/docker/pkg/loopback" @@ -17,32 +17,32 @@ func ioctlLoopCtlGetFree(fd uintptr) (int, error) { } func ioctlLoopSetFd(loopFd, sparseFd uintptr) error { - return unix.IoctlSetInt(int(loopFd), LoopSetFd, int(sparseFd)) + return unix.IoctlSetInt(int(loopFd), unix.LOOP_SET_FD, int(sparseFd)) } -func ioctlLoopSetStatus64(loopFd uintptr, loopInfo *loopInfo64) error { - if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, LoopSetStatus64, uintptr(unsafe.Pointer(loopInfo))); err != 0 { +func ioctlLoopSetStatus64(loopFd uintptr, loopInfo *unix.LoopInfo64) error { + if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_SET_STATUS64, uintptr(unsafe.Pointer(loopInfo))); err != 0 { return err } return nil } func ioctlLoopClrFd(loopFd uintptr) error { - if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, LoopClrFd, 0); err != 0 { + if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_CLR_FD, 0); err != 0 { return err } return nil } -func ioctlLoopGetStatus64(loopFd uintptr) (*loopInfo64, error) { - loopInfo := &loopInfo64{} +func ioctlLoopGetStatus64(loopFd uintptr) (*unix.LoopInfo64, error) { + loopInfo := &unix.LoopInfo64{} - if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, LoopGetStatus64, uintptr(unsafe.Pointer(loopInfo))); err != 0 { + if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(loopInfo))); err != 0 { return nil, err } return loopInfo, nil } func ioctlLoopSetCapacity(loopFd uintptr, value int) error { - return unix.IoctlSetInt(int(loopFd), LoopSetCapacity, value) + return unix.IoctlSetInt(int(loopFd), unix.LOOP_SET_CAPACITY, value) } diff --git a/pkg/loopback/loop_wrapper.go b/pkg/loopback/loop_wrapper.go index 7206bfb950..d68551c2e9 100644 --- a/pkg/loopback/loop_wrapper.go +++ b/pkg/loopback/loop_wrapper.go @@ -1,52 +1,24 @@ -// +build linux,cgo +// +build linux package loopback // import "github.com/docker/docker/pkg/loopback" -/* -#include // FIXME: present only for defines, maybe we can remove it? - -#ifndef LOOP_CTL_GET_FREE - #define LOOP_CTL_GET_FREE 0x4C82 -#endif - -#ifndef LO_FLAGS_PARTSCAN - #define LO_FLAGS_PARTSCAN 8 -#endif - -*/ -import "C" - -type loopInfo64 struct { - loDevice uint64 /* ioctl r/o */ - loInode uint64 /* ioctl r/o */ - loRdevice uint64 /* ioctl r/o */ - loOffset uint64 - loSizelimit uint64 /* bytes, 0 == max available */ - loNumber uint32 /* ioctl r/o */ - loEncryptType uint32 - loEncryptKeySize uint32 /* ioctl w/o */ - loFlags uint32 /* ioctl r/o */ - loFileName [LoNameSize]uint8 - loCryptName [LoNameSize]uint8 - loEncryptKey [LoKeySize]uint8 /* ioctl w/o */ - loInit [2]uint64 -} +import "golang.org/x/sys/unix" // IOCTL consts const ( - LoopSetFd = C.LOOP_SET_FD - LoopCtlGetFree = C.LOOP_CTL_GET_FREE - LoopGetStatus64 = C.LOOP_GET_STATUS64 - LoopSetStatus64 = C.LOOP_SET_STATUS64 - LoopClrFd = C.LOOP_CLR_FD - LoopSetCapacity = C.LOOP_SET_CAPACITY + LoopSetFd = unix.LOOP_SET_FD + LoopCtlGetFree = unix.LOOP_CTL_GET_FREE + LoopGetStatus64 = unix.LOOP_GET_STATUS64 + LoopSetStatus64 = unix.LOOP_SET_STATUS64 + LoopClrFd = unix.LOOP_CLR_FD + LoopSetCapacity = unix.LOOP_SET_CAPACITY ) // LOOP consts. const ( - LoFlagsAutoClear = C.LO_FLAGS_AUTOCLEAR - LoFlagsReadOnly = C.LO_FLAGS_READ_ONLY - LoFlagsPartScan = C.LO_FLAGS_PARTSCAN - LoKeySize = C.LO_KEY_SIZE - LoNameSize = C.LO_NAME_SIZE + LoFlagsAutoClear = unix.LO_FLAGS_AUTOCLEAR + LoFlagsReadOnly = unix.LO_FLAGS_READ_ONLY + LoFlagsPartScan = unix.LO_FLAGS_PARTSCAN + LoKeySize = unix.LO_KEY_SIZE + LoNameSize = unix.LO_NAME_SIZE ) diff --git a/pkg/loopback/loopback.go b/pkg/loopback/loopback.go index 03e335d097..40fde96621 100644 --- a/pkg/loopback/loopback.go +++ b/pkg/loopback/loopback.go @@ -16,7 +16,7 @@ func getLoopbackBackingFile(file *os.File) (uint64, uint64, error) { logrus.Errorf("Error get loopback backing file: %s", err) return 0, 0, ErrGetLoopbackBackingFile } - return loopInfo.loDevice, loopInfo.loInode, nil + return loopInfo.Device, loopInfo.Inode, nil } // SetCapacity reloads the size for the loopback device.