Skip to content

Commit 7be891c

Browse files
author
Mrunal Patel
authored
Merge pull request #3 from wking/mode-type-comparison
fileutils: Use ModeType as the mask for mode checks
2 parents 7d4729f + 8bad3ca commit 7be891c

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

fileutils.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ func CopyFile(source string, dest string) error {
2222

2323
uid := int(st.Uid)
2424
gid := int(st.Gid)
25+
modeType := si.Mode()&os.ModeType
2526

2627
// Handle symlinks
27-
if si.Mode()&os.ModeSymlink != 0 {
28+
if modeType == os.ModeSymlink {
2829
target, err := os.Readlink(source)
2930
if err != nil {
3031
return err
@@ -35,15 +36,14 @@ func CopyFile(source string, dest string) error {
3536
}
3637

3738
// Handle device files
38-
if st.Mode&syscall.S_IFMT == syscall.S_IFBLK || st.Mode&syscall.S_IFMT == syscall.S_IFCHR {
39+
if modeType == os.ModeDevice {
3940
devMajor := int64(major(uint64(st.Rdev)))
4041
devMinor := int64(minor(uint64(st.Rdev)))
41-
mode := uint32(si.Mode() & 07777)
42-
if st.Mode&syscall.S_IFMT == syscall.S_IFBLK {
43-
mode |= syscall.S_IFBLK
44-
}
45-
if st.Mode&syscall.S_IFMT == syscall.S_IFCHR {
42+
mode := uint32(si.Mode() & os.ModePerm)
43+
if si.Mode()&os.ModeCharDevice != 0 {
4644
mode |= syscall.S_IFCHR
45+
} else {
46+
mode |= syscall.S_IFBLK
4747
}
4848
if err := syscall.Mknod(dest, mode, int(mkdev(devMajor, devMinor))); err != nil {
4949
return err
@@ -76,7 +76,7 @@ func CopyFile(source string, dest string) error {
7676
}
7777

7878
// Chmod the file
79-
if !(si.Mode()&os.ModeSymlink == os.ModeSymlink) {
79+
if !(modeType == os.ModeSymlink) {
8080
if err := os.Chmod(dest, si.Mode()); err != nil {
8181
return err
8282
}

0 commit comments

Comments
 (0)