Last active
November 23, 2017 19:07
-
-
Save ik5/fa359cf2b6eb015e14e0 to your computer and use it in GitHub Desktop.
example of calculating quota of a directory, given a known maxiumum size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
) | |
// | |
const ( | |
PartitionSize = 1024 * 1024 * 1024 * 2 | |
RootPath = "/tmp/test/" | |
) | |
func min(a, b int64) int64 { | |
if a > b { | |
return b | |
} | |
return a | |
} | |
func calculate(usedSize, availSize, totalSize int64) int64 { | |
return min(totalSize, availSize) - usedSize | |
} | |
func getDirUsage(root string) int64 { | |
var size int64 | |
var err error | |
err = filepath.Walk(root, func(path string, info os.FileInfo, e error) error { | |
if e != nil { | |
fmt.Printf("Error: %v\n", e) | |
return e | |
} | |
/* | |
fmt.Printf("Path: %s\n", path) | |
fmt.Printf("\tName: %s\n", info.Name()) | |
fmt.Printf("\tSize: %d\n", info.Size()) | |
fmt.Printf("\tMode: %d\n", info.Mode()) | |
fmt.Printf("\tModTime: %v\n", info.ModTime()) | |
fmt.Printf("\tIsDir: %v\n", info.IsDir()) | |
fmt.Printf("\tSys: %v\n\n", info.Sys()) | |
*/ | |
if info.Mode().IsRegular() { | |
size = size + info.Size() | |
} | |
return nil | |
}) | |
if err != nil { | |
fmt.Printf("Error: %v\n", err) | |
} | |
return size | |
} | |
func main() { | |
fmt.Printf("Calculating %s\n", RootPath) | |
used := getDirUsage(RootPath) | |
avail := PartitionSize - used | |
quota := calculate(used, avail, PartitionSize) | |
fmt.Printf("Size: %d, Used: %d, Available Size: %d, quota: %d\n", PartitionSize, used, avail, quota) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment