2019-04-26 03:39:49 +08:00
|
|
|
// +build linux darwin
|
|
|
|
|
|
|
|
package parse
|
|
|
|
|
|
|
|
import (
|
2019-10-22 09:59:15 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-07-18 16:42:09 +08:00
|
|
|
|
2020-03-31 21:56:18 +08:00
|
|
|
"github.com/containers/storage/pkg/unshare"
|
2019-09-13 05:44:50 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
|
|
|
"github.com/pkg/errors"
|
2019-04-26 03:39:49 +08:00
|
|
|
)
|
|
|
|
|
2021-02-09 07:06:30 +08:00
|
|
|
func DeviceFromPath(device string) ([]devices.Device, error) {
|
|
|
|
var devs []devices.Device
|
2019-09-13 05:44:50 +08:00
|
|
|
src, dst, permissions, err := Device(device)
|
|
|
|
if err != nil {
|
2019-10-22 09:59:15 +08:00
|
|
|
return nil, err
|
2019-09-13 05:44:50 +08:00
|
|
|
}
|
2020-01-17 23:48:59 +08:00
|
|
|
if unshare.IsRootless() && src != dst {
|
|
|
|
return nil, errors.Errorf("Renaming device %s to %s is not supported in rootless containers", src, dst)
|
2019-09-13 05:44:50 +08:00
|
|
|
}
|
2019-10-22 09:59:15 +08:00
|
|
|
srcInfo, err := os.Stat(src)
|
2019-09-13 05:44:50 +08:00
|
|
|
if err != nil {
|
2019-10-22 09:59:15 +08:00
|
|
|
return nil, errors.Wrapf(err, "error getting info of source device %s", src)
|
2019-09-13 05:44:50 +08:00
|
|
|
}
|
2019-10-22 09:59:15 +08:00
|
|
|
|
|
|
|
if !srcInfo.IsDir() {
|
|
|
|
|
|
|
|
dev, err := devices.DeviceFromPath(src, permissions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "%s is not a valid device", src)
|
|
|
|
}
|
|
|
|
dev.Path = dst
|
|
|
|
devs = append(devs, *dev)
|
|
|
|
return devs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If source device is a directory
|
|
|
|
srcDevices, err := devices.GetDevices(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error getting source devices from directory %s", src)
|
|
|
|
}
|
|
|
|
for _, d := range srcDevices {
|
|
|
|
d.Path = filepath.Join(dst, filepath.Base(d.Path))
|
2021-02-09 07:06:30 +08:00
|
|
|
d.Permissions = devices.Permissions(permissions)
|
2019-10-22 09:59:15 +08:00
|
|
|
devs = append(devs, *d)
|
|
|
|
}
|
|
|
|
return devs, nil
|
2019-09-13 05:44:50 +08:00
|
|
|
}
|