Skip to content

Commit

Permalink
implement user and group methods for unix
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Nov 10, 2023
1 parent fe0a202 commit 81d426d
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 89 deletions.
14 changes: 10 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,15 @@ func (file File) ListDirRecursiveChan(cancel <-chan error, patterns ...string) (
return files, errs
}

func (file File) User() string {
func (file File) User() (string, error) {
if file == "" {
return "", ErrEmptyPath
}
fileSystem, path := file.ParseRawURI()
if fs, ok := fileSystem.(UserFileSystem); ok {
return fs.User(path)
}
return ""
return "", NewErrUnsupported(fileSystem, "User")
}

func (file File) SetUser(user string) error {
Expand All @@ -616,12 +619,15 @@ func (file File) SetUser(user string) error {
return NewErrUnsupported(fileSystem, "SetUser")
}

func (file File) Group() string {
func (file File) Group() (string, error) {
if file == "" {
return "", ErrEmptyPath
}
fileSystem, path := file.ParseRawURI()
if fs, ok := fileSystem.(GroupFileSystem); ok {
return fs.Group(path)
}
return ""
return "", NewErrUnsupported(fileSystem, "Group")
}

func (file File) SetGroup(group string) error {
Expand Down
4 changes: 2 additions & 2 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ type ExistsFileSystem interface {
type UserFileSystem interface {
FileSystem

User(filePath string) string
User(filePath string) (string, error)
SetUser(filePath string, user string) error
}

type GroupFileSystem interface {
FileSystem

Group(filePath string) string
Group(filePath string) (string, error)
SetGroup(filePath string, group string) error
}

Expand Down
8 changes: 0 additions & 8 deletions httpfs/httpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,6 @@ func (f *HTTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callba
return fs.NewErrUnsupported(f, "ListDirInfo")
}

func (f *HTTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
return fs.NewErrUnsupported(f, "ListDirInfoRecursive")
}

func (f *HTTPFileSystem) ListDirMax(ctx context.Context, dirPath string, max int, patterns []string) ([]fs.File, error) {
return nil, fs.NewErrUnsupported(f, "ListDirMax")
}

func (f *HTTPFileSystem) ReadAll(ctx context.Context, filePath string) (data []byte, err error) {
if ctx.Err() != nil {
return nil, ctx.Err()
Expand Down
8 changes: 4 additions & 4 deletions invalidfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ func (InvalidFileSystem) SetPermissions(filePath string, perm Permissions) error
return ErrInvalidFileSystem
}

func (InvalidFileSystem) User(filePath string) string {
return ""
func (InvalidFileSystem) User(filePath string) (string, error) {
return "", ErrInvalidFileSystem
}

func (InvalidFileSystem) SetUser(filePath string, user string) error {
return ErrInvalidFileSystem
}

func (InvalidFileSystem) Group(filePath string) string {
return ""
func (InvalidFileSystem) Group(filePath string) (string, error) {
return "", ErrInvalidFileSystem
}

func (InvalidFileSystem) SetGroup(filePath string, group string) error {
Expand Down
12 changes: 6 additions & 6 deletions listdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Test_listDirMaxImpl(t *testing.T) {
bg := context.Background()
ctx := context.Background()
errCtx, cancel := context.WithCancel(context.Background())
cancel()

Expand Down Expand Up @@ -37,11 +37,11 @@ func Test_listDirMaxImpl(t *testing.T) {
wantFiles []File
wantErr bool
}{
{name: "-1", args: args{ctx: bg, max: -1, listDir: list("1", "2", "3")}, wantFiles: []File{"1", "2", "3"}},
{name: "-1 no files", args: args{ctx: bg, max: -1, listDir: list()}, wantFiles: nil},
{name: "0", args: args{ctx: bg, max: 0, listDir: list("1", "2", "3")}, wantFiles: nil},
{name: "1", args: args{ctx: bg, max: 1, listDir: list("1", "2", "3")}, wantFiles: []File{"1"}},
{name: "2", args: args{ctx: bg, max: 2, listDir: list("1", "2", "3")}, wantFiles: []File{"1", "2"}},
{name: "-1", args: args{ctx: ctx, max: -1, listDir: list("1", "2", "3")}, wantFiles: []File{"1", "2", "3"}},
{name: "-1 no files", args: args{ctx: ctx, max: -1, listDir: list()}, wantFiles: nil},
{name: "0", args: args{ctx: ctx, max: 0, listDir: list("1", "2", "3")}, wantFiles: nil},
{name: "1", args: args{ctx: ctx, max: 1, listDir: list("1", "2", "3")}, wantFiles: []File{"1"}},
{name: "2", args: args{ctx: ctx, max: 2, listDir: list("1", "2", "3")}, wantFiles: []File{"1", "2"}},

{name: "context error", args: args{ctx: errCtx, max: -1, listDir: list("1", "2", "3")}, wantErr: true},
}
Expand Down
24 changes: 0 additions & 24 deletions localfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,30 +410,6 @@ func (local *LocalFileSystem) SetPermissions(filePath string, perm Permissions)
return os.Chmod(filePath, mode)
}

func (local *LocalFileSystem) User(filePath string) string {
filePath = expandTilde(filePath)

panic("not implemented")
}

func (local *LocalFileSystem) SetUser(filePath string, user string) error {
filePath = expandTilde(filePath)

panic("not implemented")
}

func (local *LocalFileSystem) Group(filePath string) string {
filePath = expandTilde(filePath)

panic("not implemented")
}

func (local *LocalFileSystem) SetGroup(filePath string, group string) error {
filePath = expandTilde(filePath)

panic("not implemented")
}

func (local *LocalFileSystem) Touch(filePath string, perm []Permissions) error {
if filePath == "" {
return ErrEmptyPath
Expand Down
87 changes: 87 additions & 0 deletions localfilesystem_unix.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,98 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package fs

import (
"fmt"
"os"
"os/user"
"strconv"
"syscall"
)

const localRoot = `/`

var extraDirPermissions Permissions = AllExecute

func hasLocalFileAttributeHidden(path string) (bool, error) {
return false, nil
}

func (local *LocalFileSystem) User(filePath string) (string, error) {
if filePath == "" {
return "", ErrEmptyPath
}
filePath = expandTilde(filePath)

info, err := os.Stat(filePath)
if err != nil {
return "", err
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return "", NewErrUnsupported(local, "User")
}
u, err := user.LookupId(fmt.Sprint(stat.Uid))
if err != nil {
return "", err
}
return u.Username, nil
}

func (local *LocalFileSystem) SetUser(filePath string, username string) error {
if filePath == "" {
return ErrEmptyPath
}
filePath = expandTilde(filePath)

u, err := user.Lookup(username)
if err != nil {
return err
}
uid, err := strconv.Atoi(u.Uid)
if err != nil {
return err
}
return os.Chown(filePath, uid, -1)
}

func (local *LocalFileSystem) Group(filePath string) (string, error) {
if filePath == "" {
return "", ErrEmptyPath
}
filePath = expandTilde(filePath)

info, err := os.Stat(filePath)
if err != nil {
return "", err
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return "", NewErrUnsupported(local, "Group")
}
g, err := user.LookupGroupId(fmt.Sprint(stat.Gid))
if err != nil {
return "", err
}
return g.Name, nil
}

func (local *LocalFileSystem) SetGroup(filePath string, group string) error {
filePath = expandTilde(filePath)

if filePath == "" {
return ErrEmptyPath
}
filePath = expandTilde(filePath)

g, err := user.LookupGroup(group)
if err != nil {
return err
}
gid, err := strconv.Atoi(g.Gid)
if err != nil {
return err
}
return os.Chown(filePath, -1, gid)
}
21 changes: 6 additions & 15 deletions memfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

var (
_ fullyFeaturedFileSystem = new(MemFileSystem)
_ FileSystem = new(MemFileSystem)

// memFileNode implements io/fs.FileInfo
_ iofs.FileInfo = new(memFileInfo)
Expand Down Expand Up @@ -217,8 +217,7 @@ func (fs *MemFileSystem) MakeAllDirs(dirPath string, perm []Permissions) error {
// }
// parentDir.Dir[pathParts[len(pathParts)-1]] = fs.newMemFileInfo(f, modified)

panic("todo")

panic("TODO")
}

func (fs *MemFileSystem) IsReadOnly() bool {
Expand Down Expand Up @@ -410,28 +409,20 @@ func (*MemFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback
return nil
}

func (*MemFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error {
return nil
}

func (*MemFileSystem) ListDirMax(ctx context.Context, dirPath string, n int, patterns []string) (files []File, err error) {
return nil, nil
}

func (*MemFileSystem) SetPermissions(filePath string, perm Permissions) error {
return nil
}

func (*MemFileSystem) User(filePath string) string {
return ""
func (*MemFileSystem) User(filePath string) (string, error) {
return "", nil
}

func (*MemFileSystem) SetUser(filePath string, user string) error {
return nil
}

func (*MemFileSystem) Group(filePath string) string {
return ""
func (*MemFileSystem) Group(filePath string) (string, error) {
return "", nil
}

func (*MemFileSystem) SetGroup(filePath string, group string) error {
Expand Down
4 changes: 0 additions & 4 deletions readonlybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ func (*ReadOnlyBase) SetPermissions(filePath string, perm Permissions) error {
return ErrReadOnlyFileSystem
}

func (*ReadOnlyBase) User(filePath string) string { return "" }

func (*ReadOnlyBase) SetUser(filePath string, user string) error {
return ErrReadOnlyFileSystem
}

func (*ReadOnlyBase) Group(filePath string) string { return "" }

func (*ReadOnlyBase) SetGroup(filePath string, group string) error {
return ErrReadOnlyFileSystem
}
Expand Down
Loading

0 comments on commit 81d426d

Please sign in to comment.