Skip to content

Commit

Permalink
get device numbers for block and char device files
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Carlson committed Jun 17, 2018
1 parent 04299b8 commit f1e2653
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
### 2018-06-16
- Fix the calculation of kB, MB, GB, etc. which have graduations of 1024, not 1000.
- Add more precision to the file sizes.
- For block and char devices, show major and minor device numbers, instead of simply displaying 0 for file size.

### 2018-01-22

- Fix permission string generation to produce the same output as `ls -l`.
Expand Down
6 changes: 5 additions & 1 deletion ls-go.go
Expand Up @@ -201,7 +201,11 @@ func listFiles(parentDir string, items *[]os.FileInfo, forceDotfiles bool) {
}

if *args.bytes {
displayItem.display += sizeString(fileInfo.Size())
if fileInfo.Mode()&os.ModeDevice != 0 {
displayItem.display += deviceNumbers(path.Join(absPath, fileInfo.Name()))
} else {
displayItem.display += sizeString(fileInfo.Size())
}
}

if *args.mdate {
Expand Down
14 changes: 14 additions & 0 deletions ls-unix.go
Expand Up @@ -6,7 +6,12 @@ import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
"syscall"

"github.com/willf/pad"
"golang.org/x/sys/unix"
)

func getOwnerAndGroup(fileInfo *os.FileInfo) (string, string) {
Expand All @@ -19,3 +24,12 @@ func getOwnerAndGroup(fileInfo *os.FileInfo) (string, string) {
check(err)
return owner.Username, group.Name
}

func deviceNumbers(absPath string) string {
stat := syscall.Stat_t{}
err := syscall.Stat(absPath, &stat)
check(err)
major := strconv.FormatInt(int64(unix.Major(uint64(stat.Rdev))), 10)
minor := strconv.FormatInt(int64(unix.Minor(uint64(stat.Rdev))), 10)
return pad.Left(strings.Join([]string{major, minor}, ","), 7, " ") + " " + Reset
}
14 changes: 14 additions & 0 deletions ls-windows.go
Expand Up @@ -4,8 +4,13 @@ package main

import (
"os"
"strconv"
"strings"
"syscall"
"unsafe"

"github.com/willf/pad"
"golang.org/x/sys/windows"
)

var (
Expand Down Expand Up @@ -49,3 +54,12 @@ func getOwnerAndGroup(fileInfo *os.FileInfo) (string, string) {
}
return uid, gid
}

func deviceNumbers(absPath string) string {
stat := syscall.Stat_t{}
err := syscall.Stat(absPath, &stat)
check(err)
major := strconv.FormatInt(int64(windows.Major(uint64(stat.Rdev))), 10)
minor := strconv.FormatInt(int64(windows.Minor(uint64(stat.Rdev))), 10)
return pad.Left(strings.Join([]string{major, minor}, ","), 7, " ") + " " + Reset
}

0 comments on commit f1e2653

Please sign in to comment.