-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement user and group methods for unix
- Loading branch information
Showing
11 changed files
with
137 additions
and
89 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
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
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 |
---|---|---|
@@ -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) | ||
} |
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
Oops, something went wrong.