2024-07-30 14:43:54 -04:00
|
|
|
// Package filemode does things io/fs doesn't do
|
2024-07-27 19:37:01 -04:00
|
|
|
package filemode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse parses a string of 3 or 4 numbers as a *NIX filesystem permission.
|
|
|
|
//
|
2024-07-30 14:43:54 -04:00
|
|
|
// "0777" or "777" -> fs.FileMode(0777)
|
2024-07-27 19:37:01 -04:00
|
|
|
//
|
2024-07-30 14:43:54 -04:00
|
|
|
// "0644" or "644" -> fs.FileMode(0644)
|
|
|
|
func Parse(input string) (fs.FileMode, error) {
|
|
|
|
const simplemodelen = 3
|
|
|
|
if input == "" {
|
2024-07-27 19:37:01 -04:00
|
|
|
return fs.FileMode(0), nil
|
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
if len(input) == simplemodelen {
|
|
|
|
input = "0" + input
|
2024-07-27 19:37:01 -04:00
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
md, e := strconv.ParseUint(input, 8, 64)
|
2024-07-27 19:37:01 -04:00
|
|
|
if e != nil {
|
|
|
|
return 0, e
|
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
|
2024-07-27 19:37:01 -04:00
|
|
|
return fs.FileMode(md), nil
|
|
|
|
}
|