本文整理汇总了Golang中syscall.Stat函数的典型用法代码示例。如果您正苦于以下问题:Golang Stat函数的具体用法?Golang Stat怎么用?Golang Stat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Stat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetFileUser
func SetFileUser(filename, username string, run bool) (bool, error) {
userData, err := user.Lookup(username)
if err != nil {
return false, err
}
uid, err := strconv.Atoi(userData.Uid)
if err != nil {
return false, err
}
var stat syscall.Stat_t
err = syscall.Stat(filename, &stat)
if err != nil {
return false, err
}
if int(stat.Uid) == uid {
return false, nil
}
if run {
err = os.Chown(filename, uid, int(stat.Gid))
if err != nil {
return false, err
}
}
return true, nil
}
示例2: getDevnumForDevice
func getDevnumForDevice(name string) (devnum uint64, err error) {
var stat syscall.Stat_t
if err = syscall.Stat(name, &stat); err != nil {
return 0, err
}
return stat.Rdev, nil
}
示例3: fixStdioPermissions
// fixStdioPermissions fixes the permissions of PID 1's STDIO within the container to the specified user.
// The ownership needs to match because it is created outside of the container and needs to be
// localized.
func fixStdioPermissions(u *user.ExecUser) error {
var null syscall.Stat_t
if err := syscall.Stat("/dev/null", &null); err != nil {
return err
}
for _, fd := range []uintptr{
os.Stdin.Fd(),
os.Stderr.Fd(),
os.Stdout.Fd(),
} {
var s syscall.Stat_t
if err := syscall.Fstat(int(fd), &s); err != nil {
return err
}
// skip chown of /dev/null if it was used as one of the STDIO fds.
if s.Rdev == null.Rdev {
continue
}
// Operation not permitted error on unprivileged container
//if err := syscall.Fchown(int(fd), u.Uid, u.Gid); err != nil {
// return err
//}
}
return nil
}
示例4: isDir
func isDir(fullpath string) (bool, error) {
var st syscall.Stat_t
if err := syscall.Stat(fullpath, &st); err != nil {
return false, osErrorToFuseError(err)
}
return st.Mode&syscall.S_IFMT == syscall.S_IFDIR, nil
}
示例5: Stat
// Stat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*Stat_t, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
}
示例6: readTree
func readTree(base, root string) (map[string]node, error) {
tree := make(map[string]node)
dirInfos, err := ioutil.ReadDir(base)
if err != nil {
return nil, fmt.Errorf("Couldn't read directory entries for %q: %v", base, err)
}
for _, info := range dirInfos {
s := &syscall.Stat_t{}
if err := syscall.Stat(filepath.Join(base, info.Name()), s); err != nil {
return nil, fmt.Errorf("Can't stat file %q: %v", filepath.Join(base, info.Name()), err)
}
tree[filepath.Join(root, info.Name())] = node{int(s.Uid), int(s.Gid)}
if info.IsDir() {
// read the subdirectory
subtree, err := readTree(filepath.Join(base, info.Name()), filepath.Join(root, info.Name()))
if err != nil {
return nil, err
}
for path, nodeinfo := range subtree {
tree[path] = nodeinfo
}
}
}
return tree, nil
}
示例7: watchDaemon
func watchDaemon(mdbFileName string, mdbChannel chan<- *mdb.Mdb,
logger *log.Logger) {
var lastStat syscall.Stat_t
var lastMdb *mdb.Mdb
for ; ; time.Sleep(time.Second) {
var stat syscall.Stat_t
if err := syscall.Stat(mdbFileName, &stat); err != nil {
logger.Printf("Error stating file: %s\t%s\n", mdbFileName, err)
continue
}
stat.Atim = lastStat.Atim
if stat != lastStat {
mdb := loadFile(mdbFileName, logger)
if mdb == nil {
continue
}
compareStartTime := time.Now()
if lastMdb == nil || !compare(lastMdb, mdb) {
if lastMdb != nil {
mdbCompareTimeDistribution.Add(time.Since(compareStartTime))
}
mdbChannel <- mdb
lastMdb = mdb
}
lastStat = stat
}
}
}
示例8: TestUtimesNano
// Setting nanoseconds should work for dates after 1970
func TestUtimesNano(t *testing.T) {
tc := NewTestCase(t)
defer tc.Cleanup()
path := tc.mountFile
err := ioutil.WriteFile(path, []byte("xyz"), 0600)
if err != nil {
t.Fatal(err)
}
ts := make([]syscall.Timespec, 2)
// atime
ts[0].Sec = 1
ts[0].Nsec = 2
// mtime
ts[1].Sec = 3
ts[1].Nsec = 4
err = syscall.UtimesNano(path, ts)
if err != nil {
t.Fatal(err)
}
var st syscall.Stat_t
err = syscall.Stat(path, &st)
if err != nil {
t.Fatal(err)
}
if st.Atim != ts[0] {
t.Errorf("Wrong atime: %v, want: %v", st.Atim, ts[0])
}
if st.Mtim != ts[1] {
t.Errorf("Wrong mtime: %v, want: %v", st.Mtim, ts[1])
}
}
示例9: fixStdioPermissions
// fixStdioPermissions fixes the permissions of PID 1's STDIO within the container to the specified user.
// The ownership needs to match because it is created outside of the container and needs to be
// localized.
func fixStdioPermissions(u *user.ExecUser) error {
var null syscall.Stat_t
if err := syscall.Stat("/dev/null", &null); err != nil {
return err
}
for _, fd := range []uintptr{
os.Stdin.Fd(),
os.Stderr.Fd(),
os.Stdout.Fd(),
} {
var s syscall.Stat_t
if err := syscall.Fstat(int(fd), &s); err != nil {
return err
}
// Skip chown of /dev/null if it was used as one of the STDIO fds.
if s.Rdev == null.Rdev {
continue
}
// We only change the uid owner (as it is possible for the mount to
// prefer a different gid, and there's no reason for us to change it).
// The reason why we don't just leave the default uid=X mount setup is
// that users expect to be able to actually use their console. Without
// this code, you couldn't effectively run as a non-root user inside a
// container and also have a console set up.
if err := syscall.Fchown(int(fd), u.Uid, int(s.Gid)); err != nil {
return err
}
}
return nil
}
示例10: watchDaemon
func watchDaemon(mdbFileName string, mdbChannel chan *Mdb, logger *log.Logger) {
var lastStat syscall.Stat_t
var lastMdb *Mdb
for ; ; time.Sleep(time.Second) {
var stat syscall.Stat_t
err := syscall.Stat(mdbFileName, &stat)
if err != nil {
logger.Printf("Error stating file: %s\t%s\n", mdbFileName, err)
continue
}
if stat != lastStat {
file, err := os.Open(mdbFileName)
if err != nil {
logger.Printf("Error opening file\t%s\n", err)
continue
}
decoder := json.NewDecoder(file)
var mdb Mdb
err = decoder.Decode(&mdb.Machines)
if err != nil {
logger.Printf("Error decoding\t%s\n", err)
continue
}
sort.Sort(&mdb)
if lastMdb == nil || !compare(lastMdb, &mdb) {
mdbChannel <- &mdb
lastMdb = &mdb
}
lastStat = stat
}
}
}
示例11: touch
func touch(filename string, nacc, nmod int64) (errcnt int) {
var st syscall.Stat_t
var ut syscall.Utimbuf
if e := syscall.Stat(filename, &st); e != 0 {
if e == syscall.ENOENT {
if *cflag {
errcnt++
return
}
var fd int
defer syscall.Close(fd)
if fd, e = syscall.Creat(filename, 0666); e != 0 {
fmt.Fprintf(os.Stderr, "touch: can not create %s\n", filename)
errcnt += 1
return
}
if e = syscall.Fstat(fd, &st); e != 0 {
fmt.Fprintf(os.Stderr, "touch: can't stat %s\n", filename)
errcnt += 1
return
}
} else {
fmt.Fprintf(os.Stderr, "touch: can't stat %s\n", filename)
errcnt += 1
return
}
}
if *aflag {
ut.Actime = nacc
} else {
ut.Actime = st.Atim.Sec
}
if *mflag {
ut.Modtime = nmod
} else {
ut.Modtime = st.Mtim.Sec
}
if nulltime {
if e := syscall.Utime(filename, nil); e != 0 {
fmt.Fprintf(os.Stderr, "touch: unable to touch %s", filename)
errcnt += 1
}
} else {
if e := syscall.Utime(filename, &ut); e != 0 {
fmt.Fprintf(os.Stderr, "touch: unable to touch %s", filename)
errcnt += 1
}
}
return
}
示例12: GetFileSize
func GetFileSize(path string) int64 {
var st syscall.Stat_t
err := syscall.Stat(path, &st)
if err != nil {
return -1
}
return st.Size
}
示例13: Stat
func Stat(path string, statbuf *Stat_t) error {
var rawStatbuf syscall.Stat_t
if err := syscall.Stat(path, &rawStatbuf); err != nil {
return err
}
convertStat(statbuf, &rawStatbuf)
return nil
}
示例14: Owner
// Owner returns uid and gid of a file
func Owner(path string) (uid uint32, gid uint32, err error) {
s := syscall.Stat_t{}
err = syscall.Stat(path, &s)
if err != nil {
return 0, 0, err
}
return s.Uid, s.Gid, nil
}
示例15: GetDevnumForFile
func GetDevnumForFile(name string) (devnum uint64, err error) {
var stat syscall.Stat_t
err = syscall.Stat(name, &stat)
if err != nil {
return 0, err
}
return stat.Dev, nil
}