Skip to content

Commit

Permalink
archive: use Mkdev, Major and Minor functions from golang.org/x/sys/unix
Browse files Browse the repository at this point in the history
Now that golang.org/x/sys/unix provides the Mkdev, Major and Minor
functions for every OS, use them instead of the locally defined version
which uses the Linux specific device major/minor encoding.

This also means that the device number should now be properly encoded on
e.g. Darwin, FreeBSD or Solaris.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
tklauser committed Oct 2, 2017
1 parent 727fd59 commit f01b139
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions archive/tar_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,13 @@ func setHeaderForSpecialDevice(hdr *tar.Header, name string, fi os.FileInfo) err
// Currently go does not fill in the major/minors
if s.Mode&syscall.S_IFBLK != 0 ||
s.Mode&syscall.S_IFCHR != 0 {
hdr.Devmajor = int64(major(uint64(s.Rdev)))
hdr.Devminor = int64(minor(uint64(s.Rdev)))
hdr.Devmajor = int64(unix.Major(uint64(s.Rdev)))
hdr.Devminor = int64(unix.Minor(uint64(s.Rdev)))
}

return nil
}

func major(device uint64) uint64 {
return (device >> 8) & 0xfff
}

func minor(device uint64) uint64 {
return (device & 0xff) | ((device >> 12) & 0xfff00)
}

func mkdev(major int64, minor int64) uint32 {
return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
}

func open(p string) (*os.File, error) {
return os.Open(p)
}
Expand Down Expand Up @@ -103,7 +91,7 @@ func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
mode |= unix.S_IFIFO
}

return unix.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor)))
return unix.Mknod(path, mode, int(unix.Mkdev(uint32(hdr.Devmajor), uint32(hdr.Devminor))))
}

func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
Expand Down

0 comments on commit f01b139

Please sign in to comment.