Skip to content

Commit

Permalink
Merge pull request #483 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.129.0
  • Loading branch information
andyone authored Jun 26, 2024
2 parents fb36f19 + 243bcf5 commit 589b0c9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
9 changes: 0 additions & 9 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.128.0"
const VERSION = "12.129.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
9 changes: 9 additions & 0 deletions fsutil/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 42 additions & 3 deletions fsutil/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions fsutil/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions fsutil/fs_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 589b0c9

Please sign in to comment.