feat: add `fsext` function to get owner of directory

Andrey Nering created

Change summary

internal/fsext/owner_others.go  | 24 ++++++++++++++++++++++++
internal/fsext/owner_windows.go |  9 +++++++++
2 files changed, 33 insertions(+)

Detailed changes

internal/fsext/owner_others.go 🔗

@@ -0,0 +1,24 @@
+//go:build !windows
+
+package fsext
+
+import (
+	"os"
+	"syscall"
+)
+
+// Owner retrieves the user ID of the owner of the file or directory at the
+// specified path.
+func Owner(path string) (int, error) {
+	info, err := os.Stat(path)
+	if err != nil {
+		return 0, err
+	}
+	var uid int
+	if stat, ok := info.Sys().(*syscall.Stat_t); ok {
+		uid = int(stat.Uid)
+	} else {
+		uid = os.Getuid()
+	}
+	return uid, nil
+}

internal/fsext/owner_windows.go 🔗

@@ -0,0 +1,9 @@
+//go:build windows
+
+package fsext
+
+// Owner retrieves the user ID of the owner of the file or directory at the
+// specified path.
+func Owner(path string) (int, error) {
+	return -1, nil
+}