97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
|
package fsutils
|
||
|
|
||
|
import (
|
||
|
`reflect`
|
||
|
`strings`
|
||
|
)
|
||
|
|
||
|
/*
|
||
|
String returns a string representation (comparable to lsattr(1)) of an FsAttrs.
|
||
|
|
||
|
Not all flags are represented, as this aims for compatibility with e2fsprogs/lsattr output.
|
||
|
*/
|
||
|
func (f *FsAttrs) String() (s string) {
|
||
|
|
||
|
// Flags have their short name printed if set, otherwise a '-' placeholder is used.
|
||
|
// https://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git/tree/lib/e2p/pf.c
|
||
|
|
||
|
var refType reflect.Type
|
||
|
var refVal reflect.Value
|
||
|
var refField reflect.StructField
|
||
|
var fieldVal reflect.Value
|
||
|
var tagVal string
|
||
|
var sb strings.Builder
|
||
|
|
||
|
if f == nil {
|
||
|
s = strings.Repeat("-", len(linuxFsAttrsListOrder))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
refVal = reflect.ValueOf(*f)
|
||
|
refType = refVal.Type()
|
||
|
for _, fn := range linuxFsAttrsListOrder {
|
||
|
refField, _ = refType.FieldByName(fn)
|
||
|
tagVal = refField.Tag.Get("fsAttrShort")
|
||
|
if tagVal == "" || tagVal == "-" {
|
||
|
continue
|
||
|
}
|
||
|
fieldVal = refVal.FieldByName(fn)
|
||
|
if fieldVal.Bool() {
|
||
|
sb.WriteString(tagVal)
|
||
|
} else {
|
||
|
sb.WriteString("-")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
s = sb.String()
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
StringLong returns a more extensive/"human-friendly" representation (comparable to lsattr(1) wiih -l) of an Fsattrs.
|
||
|
|
||
|
Not all flags are represented, as this aims for compatibility with e2fsprogs/lsattr output.
|
||
|
*/
|
||
|
func (f *FsAttrs) StringLong() (s string) {
|
||
|
|
||
|
// The long names are separated via a commma then a space.
|
||
|
// If no attrs are set, the string "---" is used.
|
||
|
// https://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git/tree/lib/e2p/pf.c
|
||
|
|
||
|
var refType reflect.Type
|
||
|
var refVal reflect.Value
|
||
|
var refField reflect.StructField
|
||
|
var fieldVal reflect.Value
|
||
|
var tagVal string
|
||
|
var out []string
|
||
|
|
||
|
if f == nil {
|
||
|
s = strings.Repeat("-", 3)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
refVal = reflect.ValueOf(*f)
|
||
|
refType = refVal.Type()
|
||
|
for _, fn := range linuxFsAttrsListOrder {
|
||
|
refField, _ = refType.FieldByName(fn)
|
||
|
tagVal = refField.Tag.Get("fsAttrLong")
|
||
|
if tagVal == "" || tagVal == "-" {
|
||
|
continue
|
||
|
}
|
||
|
fieldVal = refVal.FieldByName(fn)
|
||
|
if fieldVal.Bool() {
|
||
|
out = append(out, tagVal)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if out == nil || len(out) == 0 {
|
||
|
s = strings.Repeat("-", 3)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
s = strings.Join(out, ", ")
|
||
|
|
||
|
return
|
||
|
}
|