drivers/platformLChown: return early
This is purely aesthetical -- in case we can't get struct stat_t, return early. This improves readability and decreases the indentation. No functional change. Please review this with --ignore-space-change. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
parent
e517250392
commit
1e8bf20d6b
|
|
@ -12,66 +12,67 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func platformLChown(path string, info os.FileInfo, toHost, toContainer *idtools.IDMappings) error {
|
func platformLChown(path string, info os.FileInfo, toHost, toContainer *idtools.IDMappings) error {
|
||||||
sysinfo := info.Sys()
|
st, ok := info.Sys().(*syscall.Stat_t)
|
||||||
if st, ok := sysinfo.(*syscall.Stat_t); ok {
|
if !ok {
|
||||||
// Map an on-disk UID/GID pair from host to container
|
return nil
|
||||||
// using the first map, then back to the host using the
|
}
|
||||||
// second map. Skip that first step if they're 0, to
|
// Map an on-disk UID/GID pair from host to container
|
||||||
// compensate for cases where a parent layer should
|
// using the first map, then back to the host using the
|
||||||
// have had a mapped value, but didn't.
|
// second map. Skip that first step if they're 0, to
|
||||||
uid, gid := int(st.Uid), int(st.Gid)
|
// compensate for cases where a parent layer should
|
||||||
if toContainer != nil {
|
// have had a mapped value, but didn't.
|
||||||
pair := idtools.IDPair{
|
uid, gid := int(st.Uid), int(st.Gid)
|
||||||
UID: uid,
|
if toContainer != nil {
|
||||||
GID: gid,
|
pair := idtools.IDPair{
|
||||||
}
|
UID: uid,
|
||||||
mappedUID, mappedGID, err := toContainer.ToContainer(pair)
|
GID: gid,
|
||||||
if err != nil {
|
|
||||||
if (uid != 0) || (gid != 0) {
|
|
||||||
return fmt.Errorf("error mapping host ID pair %#v for %q to container: %v", pair, path, err)
|
|
||||||
}
|
|
||||||
mappedUID, mappedGID = uid, gid
|
|
||||||
}
|
|
||||||
uid, gid = mappedUID, mappedGID
|
|
||||||
}
|
}
|
||||||
if toHost != nil {
|
mappedUID, mappedGID, err := toContainer.ToContainer(pair)
|
||||||
pair := idtools.IDPair{
|
if err != nil {
|
||||||
UID: uid,
|
if (uid != 0) || (gid != 0) {
|
||||||
GID: gid,
|
return fmt.Errorf("error mapping host ID pair %#v for %q to container: %v", pair, path, err)
|
||||||
}
|
}
|
||||||
mappedPair, err := toHost.ToHost(pair)
|
mappedUID, mappedGID = uid, gid
|
||||||
if err != nil {
|
}
|
||||||
return fmt.Errorf("error mapping container ID pair %#v for %q to host: %v", pair, path, err)
|
uid, gid = mappedUID, mappedGID
|
||||||
}
|
}
|
||||||
uid, gid = mappedPair.UID, mappedPair.GID
|
if toHost != nil {
|
||||||
|
pair := idtools.IDPair{
|
||||||
|
UID: uid,
|
||||||
|
GID: gid,
|
||||||
|
}
|
||||||
|
mappedPair, err := toHost.ToHost(pair)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error mapping container ID pair %#v for %q to host: %v", pair, path, err)
|
||||||
|
}
|
||||||
|
uid, gid = mappedPair.UID, mappedPair.GID
|
||||||
|
}
|
||||||
|
if uid != int(st.Uid) || gid != int(st.Gid) {
|
||||||
|
stat, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%s: lstat(%q): %v", os.Args[0], path, err)
|
||||||
|
}
|
||||||
|
cap, err := system.Lgetxattr(path, "security.capability")
|
||||||
|
if err != nil && err != system.ErrNotSupportedPlatform {
|
||||||
|
return fmt.Errorf("%s: Lgetxattr(%q): %v", os.Args[0], path, err)
|
||||||
}
|
}
|
||||||
if uid != int(st.Uid) || gid != int(st.Gid) {
|
|
||||||
stat, err := os.Lstat(path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%s: lstat(%q): %v", os.Args[0], path, err)
|
|
||||||
}
|
|
||||||
cap, err := system.Lgetxattr(path, "security.capability")
|
|
||||||
if err != nil && err != system.ErrNotSupportedPlatform {
|
|
||||||
return fmt.Errorf("%s: Lgetxattr(%q): %v", os.Args[0], path, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make the change.
|
|
||||||
if err := syscall.Lchown(path, uid, gid); err != nil {
|
|
||||||
return fmt.Errorf("%s: chown(%q): %v", os.Args[0], path, err)
|
|
||||||
}
|
|
||||||
// Restore the SUID and SGID bits if they were originally set.
|
|
||||||
if (stat.Mode()&os.ModeSymlink == 0) && stat.Mode()&(os.ModeSetuid|os.ModeSetgid) != 0 {
|
|
||||||
if err := os.Chmod(path, stat.Mode()); err != nil {
|
|
||||||
return fmt.Errorf("%s: chmod(%q): %v", os.Args[0], path, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if cap != nil {
|
|
||||||
if err := system.Lsetxattr(path, "security.capability", cap, 0); err != nil {
|
|
||||||
return fmt.Errorf("%s: Lsetxattr(%q): %v", os.Args[0], path, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Make the change.
|
||||||
|
if err := syscall.Lchown(path, uid, gid); err != nil {
|
||||||
|
return fmt.Errorf("%s: chown(%q): %v", os.Args[0], path, err)
|
||||||
}
|
}
|
||||||
|
// Restore the SUID and SGID bits if they were originally set.
|
||||||
|
if (stat.Mode()&os.ModeSymlink == 0) && stat.Mode()&(os.ModeSetuid|os.ModeSetgid) != 0 {
|
||||||
|
if err := os.Chmod(path, stat.Mode()); err != nil {
|
||||||
|
return fmt.Errorf("%s: chmod(%q): %v", os.Args[0], path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cap != nil {
|
||||||
|
if err := system.Lsetxattr(path, "security.capability", cap, 0); err != nil {
|
||||||
|
return fmt.Errorf("%s: Lsetxattr(%q): %v", os.Args[0], path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue