本文整理匯總了Golang中syscall.Fstat函數的典型用法代碼示例。如果您正苦於以下問題:Golang Fstat函數的具體用法?Golang Fstat怎麽用?Golang Fstat使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Fstat函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Equal
// Equal determines if two network handles refer to the same network
// namespace. This is done by comparing the device and inode that the
// file descripors point to.
func (ns NsHandle) Equal(other NsHandle) bool {
var s1, s2 syscall.Stat_t
if err := syscall.Fstat(int(ns), &s1); err != nil {
return false
}
if err := syscall.Fstat(int(other), &s2); err != nil {
return false
}
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
}
示例2: getHandle
func getHandle(fn string) (*handle, error) {
f, err := os.OpenFile(fn, O_PATH, 0)
if err != nil {
return nil, errors.Wrapf(err, "failed to open %v with O_PATH", fn)
}
var stat syscall.Stat_t
if err := syscall.Fstat(int(f.Fd()), &stat); err != nil {
f.Close()
return nil, errors.Wrapf(err, "failed to stat handle %v", f.Fd())
}
h := &handle{
f: f,
name: fn,
dev: stat.Dev,
ino: stat.Ino,
}
// check /proc just in case
if _, err := os.Stat(h.procPath()); err != nil {
f.Close()
return nil, errors.Wrapf(err, "couldn't stat %v", h.procPath())
}
return h, nil
}
示例3: NewNetNSProbe
func NewNetNSProbe(g *graph.Graph, n *graph.Node, runPath ...string) (*NetNSProbe, error) {
if uid := os.Geteuid(); uid != 0 {
return nil, errors.New("NetNS probe has to be run as root")
}
path := "/var/run/netns"
if len(runPath) > 0 && runPath[0] != "" {
path = runPath[0]
}
rootNs, err := netns.Get()
if err != nil {
return nil, errors.New("Failed to get root namespace")
}
defer rootNs.Close()
var stats syscall.Stat_t
if err := syscall.Fstat(int(rootNs), &stats); err != nil {
return nil, errors.New("Failed to stat root namespace")
}
return &NetNSProbe{
Graph: g,
Root: n,
nsnlProbes: make(map[string]*NetNsNetLinkTopoUpdater),
pathToNetNS: make(map[string]*NetNs),
runPath: path,
rootNsDev: stats.Dev,
}, nil
}
示例4: getFileSize
func (h *Handle) getFileSize() (uint64, error) {
var stat syscall.Stat_t
if err := syscall.Fstat(int(h.file.Fd()), &stat); err != nil {
return 0, osErrorToFuseError(err)
}
return uint64(stat.Size), nil
}
示例5: getBlkSize
func getBlkSize(f *os.File) (uint32, error) {
var stat syscall.Stat_t
if err := syscall.Fstat(int(f.Fd()), &stat); err != nil {
return 0, osErrorToFuseError(err)
}
return uint32(stat.Blksize), nil
}
示例6: String
// String shows the file descriptor number and its dev and inode.
func (ns NsHandle) String() string {
var s syscall.Stat_t
if err := syscall.Fstat(int(ns), &s); err != nil {
return fmt.Sprintf("NS(%d: unknown)", ns)
}
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino)
}
示例7: 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
}
示例8: NewLock
// NewLock opens a new lock on a file without acquisition
func NewLock(path string, lockType LockType) (*FileLock, error) {
l := &FileLock{path: path, fd: -1}
mode := syscall.O_RDONLY | syscall.O_CLOEXEC
if lockType == Dir {
mode |= syscall.O_DIRECTORY
}
lfd, err := syscall.Open(l.path, mode, 0)
if err != nil {
if err == syscall.ENOENT {
err = ErrNotExist
} else if err == syscall.EACCES {
err = ErrPermission
}
return nil, err
}
l.fd = lfd
var stat syscall.Stat_t
err = syscall.Fstat(lfd, &stat)
if err != nil {
return nil, err
}
// Check if the file is a regular file
if lockType == RegFile && !(stat.Mode&syscall.S_IFMT == syscall.S_IFREG) {
return nil, ErrNotRegular
}
return l, nil
}
示例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: 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
}
示例11: Stat
// Stat returns the Dir structure describing file.
// It returns the Dir and an error, if any.
func (file *File) Stat() (dir *Dir, err Error) {
var stat syscall.Stat_t
e := syscall.Fstat(file.fd, &stat)
if e != 0 {
return nil, &PathError{"stat", file.name, Errno(e)}
}
return dirFromStat(file.name, new(Dir), &stat, &stat), nil
}
示例12: isatty
// isatty reports whether fd is a tty.
// Actually it reports whether fd is a character device, which is close enough.
func isatty(fd int) bool {
var st syscall.Stat_t
err := syscall.Fstat(fd, &st)
if err != nil {
return false
}
return st.Mode&syscall.S_IFMT == syscall.S_IFCHR
}
示例13: Stat
// Stat returns the FileInfo structure describing file.
// If there is an error, it will be of type *PathError.
func (f *File) Stat() (fi FileInfo, err error) {
var stat syscall.Stat_t
err = syscall.Fstat(f.fd, &stat)
if err != nil {
return nil, &PathError{"stat", f.name, err}
}
return fileInfoFromStat(&stat, f.name), nil
}
示例14: Stat
// Stat returns the FileInfo structure describing file.
// It returns the FileInfo and an error, if any.
func (file *File) Stat() (fi *FileInfo, err Error) {
var stat syscall.Stat_t
e := syscall.Fstat(file.fd, &stat)
if e != 0 {
return nil, &PathError{"stat", file.name, Errno(e)}
}
return fileInfoFromStat(file.name, new(FileInfo), &stat, &stat), nil
}
示例15: open
func open(path string) (*os.File, error) {
var f *os.File
var err error
for {
f, err = os.OpenFile(path,
syscall.O_RDWR|syscall.O_CREAT|syscall.O_EXCL, 0644)
if err != nil {
if !os.IsExist(err) {
return nil, err
}
f, err = os.OpenFile(path, syscall.O_RDWR, 0644)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
}
err = syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &syscall.Flock_t{
Type: syscall.F_WRLCK,
})
if err != nil {
f.Close()
return nil, err
}
st1 := syscall.Stat_t{}
err = syscall.Fstat(int(f.Fd()), &st1) // ffs
if err != nil {
f.Close()
return nil, err
}
st2 := syscall.Stat_t{}
err = syscall.Stat(path, &st2)
if err != nil {
f.Close()
if os.IsNotExist(err) {
continue
}
return nil, err
}
if st1.Ino != st2.Ino {
f.Close()
continue
}
break
}
return f, nil
}