140 lines
2.5 KiB
Go
140 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"syscall"
|
|
)
|
|
|
|
func alignRight(i int, s string) string {
|
|
for len(s) <= i {
|
|
s = " " + s
|
|
}
|
|
return s
|
|
}
|
|
|
|
func fsOnly(dir string) bool {
|
|
var fs syscall.Statfs_t
|
|
err := syscall.Statfs(dir, &fs)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return false
|
|
}
|
|
if fs.Fsid != config.dirMount.Fsid {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func sizeConv(size int64) string {
|
|
if size >= 1024*1024*1024*1024 {
|
|
return fmt.Sprintf("%.1fT", float64(size)/(1024*1024*1024*1024))
|
|
} else if size >= 1024*1024*1024 {
|
|
return fmt.Sprintf("%.1fG", float64(size)/(1024*1024*1024))
|
|
} else if size >= 1024*1024 {
|
|
return fmt.Sprintf("%.1fM", float64(size)/(1024*1024))
|
|
} else if size >= 1024 {
|
|
return fmt.Sprintf("%.1fK", float64(size)/(1024))
|
|
}
|
|
return fmt.Sprintf("%dB", size)
|
|
}
|
|
|
|
func sizeRevConv(size string) int64 {
|
|
var n int64 = 0
|
|
var i int
|
|
for i = 0; i < len(size); i++ {
|
|
if size[i] >= '0' && size[i] <= '9' {
|
|
n = n*10 + int64(size[i]-'0')
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
if len(size) > i {
|
|
switch size[i] {
|
|
case 'K':
|
|
n *= 1024
|
|
case 'M':
|
|
n *= 1024 * 1024
|
|
case 'G':
|
|
n *= 1024 * 1024 * 1024
|
|
case 'T':
|
|
n *= 1024 * 1024 * 1024 * 1024
|
|
}
|
|
return n
|
|
}
|
|
return n
|
|
}
|
|
|
|
func sortItems(items []Item) []Item {
|
|
sort.Slice(items, func(i, j int) bool {
|
|
sizeI := int64(0)
|
|
if items[i].isDir {
|
|
sizeI = items[i].dir.size
|
|
} else {
|
|
sizeI = items[i].file.size
|
|
}
|
|
|
|
sizeJ := int64(0)
|
|
if items[j].isDir {
|
|
sizeJ = items[j].dir.size
|
|
} else {
|
|
sizeJ = items[j].file.size
|
|
}
|
|
|
|
if sizeI == sizeJ {
|
|
var nameI, nameJ string
|
|
if items[i].isDir {
|
|
nameI = items[i].dir.path
|
|
} else {
|
|
nameI = items[i].file.name
|
|
}
|
|
if items[j].isDir {
|
|
nameJ = items[j].dir.path
|
|
} else {
|
|
nameJ = items[j].file.name
|
|
}
|
|
return nameI < nameJ
|
|
}
|
|
return sizeI > sizeJ
|
|
})
|
|
|
|
return items
|
|
}
|
|
|
|
// Prints
|
|
|
|
func printerr(err error) {
|
|
if !config.silent && (config.verbose || config.noninter) {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
func printConfig() {
|
|
fmt.Println("Verbose:", config.verbose)
|
|
fmt.Println("Non-interactive:", config.noninter)
|
|
fmt.Println("FS Only:", config.fsOnly)
|
|
fmt.Println("Directory size min:", sizeConv(config.dmin))
|
|
fmt.Println("Top N:", config.top)
|
|
fmt.Println("Threads:", config.threads)
|
|
fmt.Println("Directory:", config.dir)
|
|
}
|
|
|
|
// Conversions
|
|
|
|
func atoi(s string) int {
|
|
var n int = 0
|
|
for _, c := range s {
|
|
n = n*10 + int(c-'0')
|
|
}
|
|
return n
|
|
}
|
|
|
|
func itoa(n int64) string {
|
|
var s string
|
|
for n > 0 {
|
|
s = string('0'+n%10) + s
|
|
n = n / 10
|
|
}
|
|
return s
|
|
}
|