-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ support listing file flags (#190)
* feat: ✨ add funcs to get file flags * feat: ✨ add flags Enabler * feat: ✨ support listing file flags
- Loading branch information
1 parent
23bfd53
commit 24d7828
Showing
8 changed files
with
129 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package content | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Equationzhao/g/internal/align" | ||
constval "github.com/Equationzhao/g/internal/global" | ||
"github.com/Equationzhao/g/internal/item" | ||
"github.com/Equationzhao/g/internal/osbased" | ||
) | ||
|
||
type FlagsEnabler struct{} | ||
|
||
func NewFlagsEnabler() *FlagsEnabler { | ||
return &FlagsEnabler{} | ||
} | ||
|
||
const ( | ||
Flags = constval.NameOfFlags | ||
) | ||
|
||
func (f FlagsEnabler) Enable() ContentOption { | ||
align.Register(Flags) | ||
return func(info *item.FileInfo) (string, string) { | ||
flags := osbased.CheckFlags(info) | ||
if len(flags) == 0 { | ||
return "-", Flags | ||
} | ||
return strings.Join(flags, ","), Flags | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package osbased | ||
|
||
import ( | ||
"os" | ||
"slices" | ||
"syscall" | ||
|
||
"github.com/Equationzhao/g/internal/item" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
var flags = map[int]string{ | ||
unix.UF_APPEND: "uappnd", | ||
unix.UF_COMPRESSED: "compressed", | ||
unix.UF_HIDDEN: "hidden", | ||
unix.UF_IMMUTABLE: "uchg", | ||
unix.UF_NODUMP: "nodump", | ||
unix.UF_OPAQUE: "opaque", | ||
|
||
// unix.UF_SETTABLE: "UF_SETTABLE", | ||
// unix.UF_TRACKED: "UF_TRACKED", | ||
// unix.UF_DATAVAULT: "UF_DATAVAULT", | ||
|
||
unix.SF_APPEND: "sappnd", | ||
unix.SF_ARCHIVED: "arch", | ||
unix.SF_DATALESS: "dataless", | ||
unix.SF_IMMUTABLE: "schg", | ||
unix.SF_RESTRICTED: "restricted", | ||
|
||
// unix.SF_SETTABLE: "SF_SETTABLE", | ||
// unix.SF_SUPPORTED: "SF_SUPPORTED", | ||
// unix.SF_SYNTHETIC: "SF_SYNTHETIC", | ||
// unix.SF_FIRMLINK: "SF_FIRMLINK", | ||
} | ||
|
||
func getFlags(filename string) uint32 { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return 0 | ||
} | ||
defer file.Close() | ||
|
||
fileInfo, err := file.Stat() | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
stat := fileInfo.Sys().(*syscall.Stat_t) | ||
return stat.Flags | ||
} | ||
|
||
func CheckFlags(i *item.FileInfo) []string { | ||
res := make([]string, 0, 8) | ||
f := getFlags(i.FullPath) | ||
for key, val := range flags { | ||
if f&uint32(key) != 0 { | ||
res = append(res, val) | ||
} | ||
} | ||
slices.Sort(res) | ||
return res | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package osbased | ||
|
||
import "github.com/Equationzhao/g/internal/item" | ||
|
||
func CheckFlags(_ *item.FileInfo) []string { | ||
return nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package osbased | ||
|
||
import "github.com/Equationzhao/g/internal/item" | ||
|
||
func CheckFlags(_ *item.FileInfo) []string { | ||
return nil | ||
} |