From 75330cd1c4637371a4bb8dd4363ba7060e279cab Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 27 Jun 2024 00:48:07 +0300 Subject: [PATCH 1/2] [fsutil] Add method 'GetModeOctal' --- .github/ISSUE_TEMPLATE/config.yml | 9 ------- CHANGELOG.md | 4 +++ ek.go | 2 +- fsutil/examples_test.go | 9 +++++++ fsutil/fs.go | 45 ++++++++++++++++++++++++++++--- fsutil/fs_test.go | 12 +++++++++ 6 files changed, 68 insertions(+), 13 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 325838a9..3ba13e0c 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,10 +1 @@ blank_issues_enabled: false - -contact_links: - - name: Security Policies and Procedures - url: https://github.com/essentialkaos/.github/blob/master/SECURITY.md - about: Security procedures and general policies for all ESSENTIAL KAOS projects. - - - name: Contributing Guidelines - url: https://github.com/essentialkaos/contributing-guidelines/blob/master/CONTRIBUTING.md - about: Contributing Guidelines for all ESSENTIAL KAOS projects diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c03bd6b..7df73b5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### [12.129.0](https://kaos.sh/ek/12.129.0) + +- `[fsutil]` Added method `GetModeOctal` + ### [12.128.0](https://kaos.sh/ek/12.128.0) - `[pager]` Disable `PAGER` environment variable usage by default diff --git a/ek.go b/ek.go index a8550edd..420a991e 100644 --- a/ek.go +++ b/ek.go @@ -21,7 +21,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.128.0" +const VERSION = "12.129.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/fsutil/examples_test.go b/fsutil/examples_test.go index 4b66f4aa..d88dc676 100644 --- a/fsutil/examples_test.go +++ b/fsutil/examples_test.go @@ -279,6 +279,15 @@ func ExampleGetMode() { } } +func ExampleGetModeOctal() { + target := "/home/john/test.txt" + mode := GetModeOctal(target) + + if mode != "" { + fmt.Printf("File mode: %s\n", mode) + } +} + func ExampleCopyFile() { target := "/home/john/test.txt" err := CopyFile(target, "/home/bob/test.txt", 0644) diff --git a/fsutil/fs.go b/fsutil/fs.go index cf64beda..446aeff6 100644 --- a/fsutil/fs.go +++ b/fsutil/fs.go @@ -15,6 +15,7 @@ import ( "errors" "fmt" "os" + "strconv" "strings" "syscall" "time" @@ -670,12 +671,23 @@ func GetMode(path string) os.FileMode { path = PATH.Clean(path) - return os.FileMode(getMode(path) & 0777) + return getMode(path) & 0777 +} + +// GetModeOctal returns file mode bits in octal form (like 0644) +func GetModeOctal(path string) string { + if path == "" { + return "" + } + + path = PATH.Clean(path) + + return getModeOctal(path) } // ////////////////////////////////////////////////////////////////////////////////// // -func getMode(path string) uint32 { +func getMode(path string) os.FileMode { var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -684,7 +696,34 @@ func getMode(path string) uint32 { return 0 } - return uint32(stat.Mode) + return os.FileMode(stat.Mode) +} + +func getModeOctal(path string) string { + var stat = &syscall.Stat_t{} + + err := syscall.Stat(path, stat) + + if err != nil { + return "" + } + + m := strconv.FormatUint(uint64(stat.Mode&0777), 8) + s := 0 + + if stat.Mode&syscall.S_ISVTX != 0 { + s += 1 + } + + if stat.Mode&syscall.S_ISGID != 0 { + s += 2 + } + + if stat.Mode&syscall.S_ISUID != 0 { + s += 4 + } + + return strconv.Itoa(s) + m } func isReadableStat(stat *syscall.Stat_t, uid int, gids []int) bool { diff --git a/fsutil/fs_test.go b/fsutil/fs_test.go index 12c4a9f3..cabe3308 100644 --- a/fsutil/fs_test.go +++ b/fsutil/fs_test.go @@ -630,6 +630,18 @@ func (s *FSSuite) TestGetMode(c *check.C) { c.Assert(GetMode(tmpFile), check.Equals, os.FileMode(0764)) } +func (s *FSSuite) TestGetModeOctal(c *check.C) { + tmpDir := c.MkDir() + tmpFile := tmpDir + "/test.file" + + c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 13631988), check.IsNil) + + os.Chmod(tmpFile, 13631988) + + c.Assert(GetModeOctal(""), check.Equals, "") + c.Assert(GetModeOctal(tmpFile), check.Equals, "7764") +} + func (s *FSSuite) TestCountLines(c *check.C) { tmpDir := c.MkDir() tmpFile := tmpDir + "/test.file" From 243bcf5de6571d6f16d42e1f6d5f625a767aac11 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 27 Jun 2024 00:50:49 +0300 Subject: [PATCH 2/2] [fsutil] Update Windows stubs --- fsutil/fs_windows.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fsutil/fs_windows.go b/fsutil/fs_windows.go index 9d3c5253..4b90f703 100644 --- a/fsutil/fs_windows.go +++ b/fsutil/fs_windows.go @@ -230,6 +230,12 @@ func GetMode(path string) os.FileMode { return 0 } +// ❗ GetModeOctal returns file mode bits in octal form (like 0644) +func GetModeOctal(path string) string { + panic("UNSUPPORTED") + return "" +} + // ❗ GetTimes returns time of access, modification, and creation at once func GetTimes(path string) (time.Time, time.Time, time.Time, error) { panic("UNSUPPORTED")