Ejemplo n.º 1
0
func fileStatFromInfoOs(fi os.FileInfo, flags *uint32, fileStat *FileStat) {
	if statt, ok := fi.Sys().(*syscall.Stat_t); ok {
		*flags |= ssh_FILEXFER_ATTR_UIDGID
		fileStat.UID = statt.Uid
		fileStat.GID = statt.Gid
	}
}
Ejemplo n.º 2
0
// We have a dummy version of this call in posix.go.
// Windows does not implement the syscall.Stat_t type we
// need, but the *nixes do. We use this in util.AddToArchive
// to set owner/group on files being added to a tar archive.
func GetOwnerAndGroup(finfo os.FileInfo, header *tar.Header) {
	systat := finfo.Sys().(*syscall.Stat_t)
	if systat != nil {
		header.Uid = int(systat.Uid)
		header.Gid = int(systat.Gid)
	}
}
Ejemplo n.º 3
0
func (t *GcsfuseTest) UidAndGidFlags() {
	var err error
	var fi os.FileInfo

	// Mount, setting the flags. Make sure to set the directory mode such that we
	// can actually see the contents.
	args := []string{
		"--uid", "1719",
		"--gid", "2329",
		"--dir-mode", "555",
		canned.FakeBucketName,
		t.dir,
	}

	err = t.runGcsfuse(args)
	AssertEq(nil, err)
	defer unmount(t.dir)

	// Stat contents.
	fi, err = os.Lstat(path.Join(t.dir, canned.TopLevelFile))
	AssertEq(nil, err)
	ExpectEq(1719, fi.Sys().(*syscall.Stat_t).Uid)
	ExpectEq(2329, fi.Sys().(*syscall.Stat_t).Gid)

	fi, err = os.Lstat(path.Join(t.dir, canned.TopLevelDir))
	AssertEq(nil, err)
	ExpectEq(1719, fi.Sys().(*syscall.Stat_t).Uid)
	ExpectEq(2329, fi.Sys().(*syscall.Stat_t).Gid)
}
Ejemplo n.º 4
0
Archivo: find.go Proyecto: kiyor/gfind
func (f *File) getInfo(path string) error {
	var fstat os.FileInfo
	var err error
	f.Path = path
	fstat, err = os.Stat(path)
	if err != nil {
		fstat, err = os.Lstat(path)
		if err != nil {
			return err
		}
	}
	f.FileInfo = fstat
	f.Relpath = path[len(rootdir):]
	// 	f.Relpath, err = filepath.Rel(rootdir, f.Path)
	// 	if err != nil {
	// 		log.Fatalln(err.Error())
	// 		return
	// 	}
	f.Stat = fstat.Sys().(*syscall.Stat_t)
	f.getExt()

	if !f.IsDir() && !f.IsLink() {
		f.IsFile = true
	}
	return nil
}
Ejemplo n.º 5
0
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
	stat, ok := toStatT(fi.Sys())
	if !ok {
		return nil
	}

	node.Inode = uint64(stat.ino())

	node.fillTimes(stat)

	var err error

	if err = node.fillUser(stat); err != nil {
		return errors.Annotate(err, "fillExtra")
	}

	switch node.Type {
	case "file":
		node.Size = uint64(stat.size())
		node.Links = uint64(stat.nlink())
	case "dir":
	case "symlink":
		node.LinkTarget, err = os.Readlink(path)
	case "dev":
		node.Device = uint64(stat.rdev())
	case "chardev":
		node.Device = uint64(stat.rdev())
	case "fifo":
	case "socket":
	default:
		err = fmt.Errorf("invalid node type %q", node.Type)
	}

	return err
}
Ejemplo n.º 6
0
func fetchFileDetailsJSON(m *FileDetailsJSON, fi os.FileInfo) {
	if !*excludeSize {
		m.Size = fi.Size()
	}
	if !*excludeMtime {
		tmp := fi.ModTime()
		m.Mtime = tmp.Format(*timeFormat)
	}
	stat := fi.Sys().(*syscall.Stat_t)
	if !*excludeUid {
		m.Uid = stat.Uid
	}
	if !*excludeGid {
		m.Gid = stat.Gid
	}
	if !*excludeInode {
		m.Inode = stat.Ino
	}
	if !*excludeAtime {
		tmp := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
		m.Atime = tmp.Format(*timeFormat)
	}
	if !*excludeCtime {
		tmp := time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
		m.Ctime = tmp.Format(*timeFormat)
	}
	/*
	   if 1==0 {
	       log.Print("Number of links: ",stat.Nlink)
	   }
	*/
}
Ejemplo n.º 7
0
func New(fi os.FileInfo) *ExtraStat {
	return &ExtraStat{
		AccessTime:  time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0),
		ModTime:     fi.ModTime(),
		CreatedTime: fi.ModTime(),
	}
}
Ejemplo n.º 8
0
func fi2fo(fi os.FileInfo, p string) FileObj {
	st, ok := fi.Sys().(*syscall.Stat_t)
	if !ok {
		panic(fmt.Sprintf("fen: type should be *syscall.Stat_t, %T instead", st))
	}
	return FileObj{Name: p, Atim: st.Atim, Mtim: st.Mtim, Ctim: st.Ctim}
}
Ejemplo n.º 9
0
// Returns an open reader for the next file, along with a FileSummary.
// Returns io.EOF when it reaches the last file.
// The caller is responsible for closing the reader.
func (iter *FileSystemIterator) Next() (io.ReadCloser, *FileSummary, error) {
	iter.index += 1
	if iter.index >= len(iter.files) {
		return nil, nil, io.EOF
	}
	filePath := iter.files[iter.index]
	var stat os.FileInfo
	var err error
	if stat, err = os.Stat(filePath); os.IsNotExist(err) {
		return nil, nil, fmt.Errorf("File '%s' does not exist.", filePath)
	}
	fileMode := stat.Mode()
	fs := &FileSummary{
		RelPath:       strings.Replace(filePath, iter.rootPath+string(os.PathSeparator), "", 1),
		AbsPath:       filePath,
		Mode:          fileMode,
		Size:          stat.Size(),
		ModTime:       stat.ModTime(),
		IsDir:         stat.IsDir(),
		IsRegularFile: fileMode.IsRegular(),
	}
	systat := stat.Sys().(*syscall.Stat_t)
	if systat != nil {
		fs.Uid = int(systat.Uid)
		fs.Gid = int(systat.Gid)
	}
	file, err := os.Open(filePath)
	if err != nil {
		return nil, fs, fmt.Errorf("Cannot read file '%s': %v", filePath, err)
	}
	return file, fs, nil
}
Ejemplo n.º 10
0
func getFileOwner(info os.FileInfo) (uid, gid string) {

	uid = fmt.Sprint(info.Sys().(*syscall.Stat_t).Uid)
	gid = fmt.Sprint(info.Sys().(*syscall.Stat_t).Gid)

	return uid, gid
}
Ejemplo n.º 11
0
// getFileUserAndGroupId returns owner user and group ids from given FileInfo.
func getFileUserAndGroupId(fi os.FileInfo) (uid uint, gid uint, err error) { // {{{
	if st, ok := fi.Sys().(*syscall.Stat_t); ok {
		return uint(st.Uid), uint(st.Gid), nil
	}
	err = errors.New("Stat failed on: " + fi.Name())
	return
} // }}}
Ejemplo n.º 12
0
func (m *MetadataEncoder) File(path string, fi os.FileInfo, mime string,
	digest *[sha256.Size]byte) error {

	_, err := m.e.Encode(TypeFile)
	if err != nil {
		return err
	}

	if digest == nil {
		digest = &[sha256.Size]byte{}
	}
	stat, ok := fi.Sys().(*syscall.Stat_t)
	if !ok {
		stat = &syscall.Stat_t{
			Uid: 0xffffffff,
			Gid: 0xffffffff,
		}
	}
	_, err = m.e.Encode(File{
		Name:     path,
		Mode:     fi.Mode(),
		Owner:    int(stat.Uid),
		Group:    int(stat.Gid),
		Size:     fi.Size(),
		Modified: fi.ModTime(),

		MimeType: mime,
		Digest:   *digest,
	})
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 13
0
func (m *MetadataEncoder) Dir(path string, fi os.FileInfo) error {
	_, err := m.e.Encode(TypeDir)
	if err != nil {
		return err
	}

	stat, ok := fi.Sys().(*syscall.Stat_t)
	if !ok {
		stat = &syscall.Stat_t{
			Uid: 0xffffffff,
			Gid: 0xffffffff,
		}
	}
	_, err = m.e.Encode(Dir{
		Name:     path,
		Mode:     fi.Mode(),
		Owner:    int(stat.Uid),
		Group:    int(stat.Gid),
		Modified: fi.ModTime(),
	})
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 14
0
func (v *containerVolume) addFile(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	relPath := path[len(filepath.Dir(v.hostPath))+1:] // relative to volume parent directory (<docker>/vfs/dir/)
	th, err := tar.FileInfoHeader(info, relPath)
	if err != nil {
		return err
	}
	th.Name = relPath
	if si, ok := info.Sys().(*syscall.Stat_t); ok {
		th.Uid = int(si.Uid)
		th.Gid = int(si.Gid)
	}

	if err := v.tw.WriteHeader(th); err != nil {
		return err
	}

	if !info.Mode().IsDir() {
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		n, err := io.Copy(v.tw, file)
		if err != nil {
			return err
		}
		v.size = v.size + uint(n)
	}
	return nil
}
Ejemplo n.º 15
0
func getTimespec(fi os.FileInfo) (t timespec) {
	stat := fi.Sys().(*syscall.Win32FileAttributeData)
	t.atime.v = time.Unix(0, stat.LastAccessTime.Nanoseconds())
	t.mtime.v = time.Unix(0, stat.LastWriteTime.Nanoseconds())
	t.btime.v = time.Unix(0, stat.CreationTime.Nanoseconds())
	return t
}
Ejemplo n.º 16
0
func (node *Node) isNewer(path string, fi os.FileInfo) bool {
	if node.Type != "file" {
		debug.Log("node.isNewer", "node %v is newer: not file", path)
		return true
	}

	tpe := nodeTypeFromFileInfo(fi)
	if node.Name != fi.Name() || node.Type != tpe {
		debug.Log("node.isNewer", "node %v is newer: name or type changed", path)
		return true
	}

	extendedStat := fi.Sys().(*syscall.Stat_t)
	inode := extendedStat.Ino
	size := uint64(extendedStat.Size)

	if node.ModTime != fi.ModTime() ||
		node.ChangeTime != changeTime(extendedStat) ||
		node.Inode != uint64(inode) ||
		node.Size != size {
		debug.Log("node.isNewer", "node %v is newer: timestamp, size or inode changed", path)
		return true
	}

	debug.Log("node.isNewer", "node %v is not newer", path)
	return false
}
Ejemplo n.º 17
0
func (i *dockerInput) addFile(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}
	th, err := tar.FileInfoHeader(info, path)
	if err != nil {
		return err
	}
	th.Name = path[1:] //filepath.Join(cpath, path)
	if si, ok := info.Sys().(*syscall.Stat_t); ok {
		th.Uid = int(si.Uid)
		th.Gid = int(si.Gid)
	}

	if err := i.tw.WriteHeader(th); err != nil {
		return err
	}

	if !info.Mode().IsDir() {
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		if _, err := io.Copy(i.tw, file); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 18
0
// Check if the given file was renamed. If file is known but with different path,
// renamed will be set true and previous will be set to the previously known file path.
// Otherwise renamed will be false.
func (p *Prospector) isFileRenamed(file string, info os.FileInfo) string {
	// NOTE(driskell): What about using golang's func os.SameFile(fi1, fi2 FileInfo) bool instead?
	stat := info.Sys().(*syscall.Stat_t)

	for kf, ki := range p.prospectorList {
		if kf == file {
			continue
		}
		ks := ki.Fileinfo.Sys().(*syscall.Stat_t)
		if stat.Dev == ks.Dev && stat.Ino == ks.Ino {
			return kf
		}
	}

	// Now check the missingfiles
	for kf, ki := range p.missingFiles {
		ks := ki.Sys().(*syscall.Stat_t)
		if stat.Dev == ks.Dev && stat.Ino == ks.Ino {
			return kf
		}
	}

	// NOTE(ruflin): should instead an error be returned if not previous file?
	return ""
}
Ejemplo n.º 19
0
// Check if the given file was renamed. If file is known but with different path,
// renamed will be set true and previous will be set to the previously known file path.
// Otherwise renamed will be false.
func (p *Prospector) getPreviousFile(file string, info os.FileInfo) string {
	// TODO: To implement this properly the file state of the previous file is required.
	// For more details see how crawler implements it
	stat := info.Sys().(*syscall.Stat_t)

	for kf, ki := range p.prospectorList {
		if kf == file {
			continue
		}
		ks := ki.Fileinfo.Sys().(*syscall.Stat_t)
		if stat.Dev == ks.Dev && stat.Ino == ks.Ino {
			return kf
		}
	}

	// Now check the missingfiles
	for kf, ki := range p.missingFiles {
		ks := ki.Sys().(*syscall.Stat_t)
		if stat.Dev == ks.Dev && stat.Ino == ks.Ino {
			return kf
		}
	}

	// NOTE(ruflin): should instead an error be returned if not previous file?
	return ""
}
Ejemplo n.º 20
0
func update(p string, f os.FileInfo, err error) error {
	// Check the file mode.
	if f.IsDir() {
		m := os.FileMode(*cliChmodDir)
		if f.Mode().Perm() != m.Perm() {
			err := os.Chmod(p, m)
			if err != nil {
				log.Printf("Failed to chmod directory: %s", p)
			}
			log.Printf("Updated chmod directory: %s", p)
		}
	} else {
		m := os.FileMode(*cliChmodFile)
		if f.Mode().Perm() != m.Perm() {
			err := os.Chmod(p, m)
			if err != nil {
				log.Printf("Failed to chmod file: %s", p)
			}
			log.Printf("Updated chmod file: %s", p)
		}
	}

	// Check the file owner.
	fu := f.Sys().(*syscall.Stat_t).Uid
	fg := f.Sys().(*syscall.Stat_t).Gid
	if int(fu) != uid && int(fg) != gid {
		err := os.Chown(p, uid, gid)
		if err != nil {
			log.Printf("Failed to chown file: %s", p)
		}
		log.Printf("Updated chown file: %s", p)
	}

	return nil
}
Ejemplo n.º 21
0
func (rfs *ReverseFS) inoAwareStat(relPlainPath string) (*fuse.Attr, fuse.Status) {
	absPath, err := rfs.abs(relPlainPath, nil)
	if err != nil {
		return nil, fuse.ToStatus(err)
	}
	var fi os.FileInfo
	if relPlainPath == "" {
		// Look through symlinks for the root dir
		fi, err = os.Stat(absPath)
	} else {
		fi, err = os.Lstat(absPath)
	}
	if err != nil {
		return nil, fuse.ToStatus(err)
	}
	st := fi.Sys().(*syscall.Stat_t)
	// The file has hard links. We have to give it a stable inode number so
	// tar or rsync can find them.
	if fi.Mode().IsRegular() && st.Nlink > 1 {
		di := fusefrontend.DevInoFromStat(st)
		rfs.inoMapLock.Lock()
		stableIno := rfs.inoMap[di]
		if stableIno == 0 {
			rfs.inoMap[di] = rfs.inoGen.next()
		}
		rfs.inoMapLock.Unlock()
		st.Ino = stableIno
	} else {
		st.Ino = rfs.inoGen.next()
	}
	a := &fuse.Attr{}
	a.FromStat(st)
	return a, fuse.OK
}
Ejemplo n.º 22
0
func getTimespec(fi os.FileInfo) (t timespec) {
	stat := fi.Sys().(*syscall.Stat_t)
	t.atime.v = timespecToTime(stat.Atim)
	t.mtime.v = timespecToTime(stat.Mtim)
	t.ctime.v = timespecToTime(stat.Ctim)
	return t
}
Ejemplo n.º 23
0
Archivo: vufs.go Proyecto: postfix/vufs
func dir2Dir(s string, d os.FileInfo, upool p.Users) (*p.Dir, error) {
	sysif := d.Sys()
	if sysif == nil {
		return nil, &os.PathError{"dir2Dir", s, nil}
	}
	var sysMode *syscall.Stat_t
	switch t := sysif.(type) {
	case *syscall.Stat_t:
		sysMode = t
	default:
		return nil, &os.PathError{"dir2Dir: sysif has wrong type", s, nil}
	}

	dir := new(p.Dir)
	dir.Qid = *dir2Qid(d)
	dir.Mode = dir2Npmode(d)
	dir.Atime = uint32(atime(sysMode).Unix())
	dir.Mtime = uint32(d.ModTime().Unix())
	dir.Length = uint64(d.Size())
	dir.Name = s[strings.LastIndex(s, "/")+1:]

	uid, gid, err := path2UserGroup(s, upool)
	if err != nil {
		return nil, err
	}
	dir.Uid, dir.Gid = uid, gid

	return dir, nil
}
Ejemplo n.º 24
0
func ToStatT(f os.FileInfo) *syscall.Stat_t {
	s, _ := f.Sys().(*syscall.Stat_t)
	if s != nil {
		return s
	}
	return nil
}
Ejemplo n.º 25
0
func getuid(info os.FileInfo) (int64, error) {
	stat_t, ok := info.Sys().(*syscall.Stat_t)
	if !ok {
		return 0, StatError
	}
	return int64(stat_t.Uid), nil
}
Ejemplo n.º 26
0
func dir2Dir(path string, d os.FileInfo, dotu bool, upool p.Users) *p.Dir {
	sysMode := d.Sys().(*syscall.Stat_t)

	dir := new(Dir)
	dir.Qid = *dir2Qid(d)
	dir.Mode = dir2Npmode(d, dotu)
	dir.Atime = uint32(atime(sysMode).Unix())
	dir.Mtime = uint32(d.ModTime().Unix())
	dir.Length = uint64(d.Size())
	dir.Name = path[strings.LastIndex(path, "/")+1:]

	if dotu {
		dir.dotu(path, d, upool, sysMode)
		return &dir.Dir
	}

	unixUid := int(sysMode.Uid)
	unixGid := int(sysMode.Gid)
	dir.Uid = strconv.Itoa(unixUid)
	dir.Gid = strconv.Itoa(unixGid)
	dir.Muid = "none"

	// BUG(akumar): LookupId will never find names for
	// groups, as it only operates on user ids.
	u, err := user.LookupId(dir.Uid)
	if err == nil {
		dir.Uid = u.Username
	}
	g, err := user.LookupId(dir.Gid)
	if err == nil {
		dir.Gid = g.Username
	}

	return &dir.Dir
}
Ejemplo n.º 27
0
func (b *BackupSet) newBaseFileInfo(path string, info os.FileInfo) baseFileInfo {
	stat := info.Sys()
	switch stat := stat.(type) {
	case *syscall.Stat_t:
		user, err := user.LookupId(fmt.Sprintf("%d", stat.Uid))
		var userName string
		if err != nil {
			userName = ""
		} else {
			userName = user.Username
		}
		aTime := time.Unix(stat.Atim.Sec, stat.Atim.Nsec)
		cTime := time.Unix(stat.Ctim.Sec, stat.Ctim.Nsec)
		mTime := time.Unix(stat.Mtim.Sec, stat.Mtim.Nsec)
		return baseFileInfo{name: path,
			mode:     info.Mode(), //os.FileMode(stat.Mode),
			uid:      stat.Uid,
			gid:      stat.Gid,
			userName: userName,
			aTime:    aTime,
			cTime:    cTime,
			mTime:    mTime}
	default:
		return baseFileInfo{name: path,
			mode: info.Mode()}
	}
	panic("Can't get here")
}
Ejemplo n.º 28
0
func getUserName(fi os.FileInfo) (string, error) {
	var rv C.int
	var pwd C.struct_passwd
	var pwdres *C.struct_passwd
	var bufSize C.long
	var result string

	bufSize = 1024
	buf := C.malloc(C.size_t(bufSize))
	defer C.free(buf)

	uid := fi.Sys().(*syscall.Stat_t).Uid

	rv = C.mygetpwuid_r(C.int(uid), &pwd, (*C.char)(buf), C.size_t(bufSize), &pwdres)
	if rv != 0 {
		return "", errors.New("Could not read username")
	}

	if pwdres != nil {
		result = C.GoString(pwd.pw_name)
	} else {
		return "", errors.New("Could not convert username")
	}

	return result, nil
}
Ejemplo n.º 29
0
func getGroupName(fi os.FileInfo) (string, error) {
	var rv C.int
	var grp C.struct_group
	var grpres *C.struct_group
	var bufSize C.long
	var result string

	bufSize = 1024
	buf := C.malloc(C.size_t(bufSize))
	defer C.free(buf)

	gid := fi.Sys().(*syscall.Stat_t).Gid

	rv = C.mygetgrgid_r(C.int(gid), &grp, (*C.char)(buf), C.size_t(bufSize), &grpres)
	if rv != 0 {
		return "", errors.New("Could not read groupname")
	}

	if grpres != nil {
		result = C.GoString(grp.gr_name)
	} else {
		return "", errors.New("Could not convert groupname")
	}
	return result, nil
}
Ejemplo n.º 30
0
func getCtime(fi os.FileInfo) int64 {
	var result int64

	stat := fi.Sys().(*syscall.Stat_t)
	result = time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec)).Unix()

	return result
}