Cleanup lchmod logic in archive
Move to single lchmod interface mirroring other implementations. Separate logic for freebsd which supports symlink no follow flag. Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
parent
7eceeb950b
commit
53835221f6
@ -393,9 +393,8 @@ func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is no LChmod, so ignore mode for symlink. Also, this
|
// call lchmod after lchown since lchown can modify the file mode
|
||||||
// must happen after chown, as that can modify the file mode
|
if err := lchmod(path, hdrInfo.Mode()); err != nil {
|
||||||
if err := handleLChmod(hdr, path, hdrInfo); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,11 @@
|
|||||||
|
|
||||||
package archive
|
package archive
|
||||||
|
|
||||||
import "golang.org/x/sys/unix"
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
// mknod wraps unix.Mknod. FreeBSD's unix.Mknod signature is different from
|
// mknod wraps unix.Mknod. FreeBSD's unix.Mknod signature is different from
|
||||||
// other Unix and Unix-like operating systems.
|
// other Unix and Unix-like operating systems.
|
||||||
@ -34,3 +38,11 @@ func lsetxattrCreate(link string, attr string, data []byte) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lchmod(path string, mode os.FileMode) error {
|
||||||
|
err := unix.Fchmodat(unix.AT_FDCWD, path, uint32(mode), unix.AT_SYMLINK_NOFOLLOW)
|
||||||
|
if err != nil {
|
||||||
|
err = &os.PathError{Op: "lchmod", Path: path, Err: err}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
@ -18,7 +18,11 @@
|
|||||||
|
|
||||||
package archive
|
package archive
|
||||||
|
|
||||||
import "golang.org/x/sys/unix"
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
// mknod wraps Unix.Mknod and casts dev to int
|
// mknod wraps Unix.Mknod and casts dev to int
|
||||||
func mknod(path string, mode uint32, dev uint64) error {
|
func mknod(path string, mode uint32, dev uint64) error {
|
||||||
@ -34,3 +38,18 @@ func lsetxattrCreate(link string, attr string, data []byte) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lchmod checks for symlink and changes the mode if not a symlink
|
||||||
|
func lchmod(path string, mode os.FileMode) error {
|
||||||
|
fi, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.Mode()&os.ModeSymlink == 0 {
|
||||||
|
if err := os.Chmod(path, mode); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -111,21 +111,6 @@ func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
|
|||||||
return mknod(path, mode, unix.Mkdev(uint32(hdr.Devmajor), uint32(hdr.Devminor)))
|
return mknod(path, mode, unix.Mkdev(uint32(hdr.Devmajor), uint32(hdr.Devminor)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
|
|
||||||
if hdr.Typeflag == tar.TypeLink {
|
|
||||||
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
|
|
||||||
if err := os.Chmod(path, hdrInfo.Mode()); err != nil && !os.IsNotExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if hdr.Typeflag != tar.TypeSymlink {
|
|
||||||
if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getxattr(path, attr string) ([]byte, error) {
|
func getxattr(path, attr string) ([]byte, error) {
|
||||||
b, err := sysx.LGetxattr(path, attr)
|
b, err := sysx.LGetxattr(path, attr)
|
||||||
if err == unix.ENOTSUP || err == sysx.ENODATA {
|
if err == unix.ENOTSUP || err == sysx.ENODATA {
|
||||||
|
@ -98,7 +98,7 @@ func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
|
func lchmod(path string, mode os.FileMode) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user