func Getfilecon(path string) (string, error) { var scon []byte cnt, err := syscall.Getxattr(path, xattrNameSelinux, scon) scon = make([]byte, cnt) cnt, err = syscall.Getxattr(path, xattrNameSelinux, scon) return string(scon), err }
// gets an attribute value as bytes func (this FileXattribsNS) Get(tag string) ([]byte, error) { if size, err := syscall.Getxattr(this.File.Name(), this.namespace+ssep+tag, nil); err == nil { buf := make([]byte, size) if read, err := syscall.Getxattr(this.File.Name(), this.namespace+ssep+tag, buf); err == nil { return buf[:read], nil } else { return nil, err } } else { return nil, err } }
// Retrieve extended attribute data associated with path. func Getxattr(path, name string) ([]byte, error) { name = userPrefix + name // find size. size, err := syscall.Getxattr(path, name, nil) if err != nil { return nil, &XAttrError{"getxattr", path, name, err} } buf := make([]byte, size) // Read into buffer of that size. read, err := syscall.Getxattr(path, name, buf) if err != nil { return nil, &XAttrError{"getxattr", path, name, err} } return buf[:read], nil }
func listFlags(path string) (err error) { exists := pathExists(path) if !exists { log.Printf("%s does not exist, cannot check PaX flags.\n", path) return } supported, err := isXattrSupported() if err != nil { return } if supported { var flags []byte sz, err := syscall.Getxattr(path, "user.pax.flags", flags) if err != nil { log.Println(err) } log.Println(sz) log.Println(flags) } else { args := []string{"-v", path} log.Println(args) exec.Command("/sbin/paxctl").Run() fmt.Fprintln(os.Stdout) out, err := exec.Command("/sbin/paxctl", args...).Output() if err != nil { log.Println(err) } log.Printf("%s\n", out) } return }
// gets splits which are aligned with the nearest linebreak to a file boundary func GetFileSplits(filePath string) ([]Split, error) { // open file and get size f, err := os.Open(filePath) if err != nil { return nil, fmt.Errorf("Error opening %s for read : %s", filePath, err) } defer f.Close() stat, err := os.Stat(filePath) if err != nil { return nil, fmt.Errorf("Error getting stat for file %s : %s", filePath, err) } fileSize := stat.Size() // list blocks blocLocBytes := make([]byte, 64*1024, 64*1024) bytesLen, err := syscall.Getxattr(filePath, "user.mfs.blockLocs", blocLocBytes) if err != nil { return nil, err } blocLocStr := string(blocLocBytes[:bytesLen]) lines := strings.Split(blocLocStr, "\n") ret := make([]Split, 0, len(lines)) // find first linebreak after each to make splits for _, line := range lines { if line == "" { continue } lineSplits := strings.Split(line, "\t") if len(lineSplits) < 3 { return nil, fmt.Errorf("Error, improperly formatted blockLocs line (less than 3 elements): %s", line) } blockStartPos, err := strconv.ParseInt(lineSplits[0], 10, 64) if err != nil { return nil, fmt.Errorf("Error, blockStartPos not a number! %s : %s", err, line) } blockEndPos, err := strconv.ParseInt(lineSplits[1], 10, 64) if err != nil { return nil, fmt.Errorf("Error, blockEndPos not a number! %s : %s", err, line) } if blockStartPos > 0 { blockStartPos, err = nextLineBreak(f, blockStartPos) if err != nil { return nil, err } } if blockEndPos < fileSize { blockEndPos, err = nextLineBreak(f, blockEndPos) if err != nil { return nil, err } } blockHosts := strings.Split(lineSplits[2], ",") if len(blockHosts) != 0 { ret = append(ret, Split{filePath, blockHosts[0], blockStartPos, blockEndPos}) } } return ret, nil }
func TestDefaultXAttr(t *testing.T) { dir := testutil.TempDir() defer os.RemoveAll(dir) root := &xattrNode{ Node: nodefs.NewDefaultNode(), } opts := nodefs.NewOptions() opts.Debug = testutil.VerboseTest() s, _, err := nodefs.MountRoot(dir, root, opts) if err != nil { t.Fatalf("MountRoot: %v", err) } go s.Serve() if err := s.WaitMount(); err != nil { t.Fatal("WaitMount", err) } defer s.Unmount() var data [1024]byte sz, err := syscall.Getxattr(filepath.Join(dir, "child"), "attr", data[:]) if err != nil { t.Fatalf("Getxattr: %v", err) } else if val := string(data[:sz]); val != "value" { t.Fatalf("got %v, want 'value'", val) } }
// updates an attribute, returns error if attribute not pre-existing. func (this *FileXattribsNS) Update(tag string, data []byte) error { if _, err := syscall.Getxattr(this.File.Name(), this.namespace+ssep+tag, nil); err == nil { return this.Set(tag, data) } else { return err } }
// GetXAttr returns the extended attribute value from the path. func GetXAttr(path, name string) ([]byte, error) { var attr = make([]byte, XAttrValueBufferSize) size, err := syscall.Getxattr(path, name, attr) if err != nil { return attr, err } return attr[0:size], nil }
func (e *EncodedAttr) ReadXAttr(path string) (hash []byte) { b := make([]byte, 64) val, errno := syscall.Getxattr(path, _TERM_XATTR, b) if errno == nil { // TODO needs test. return e.Decode(b[:val]) } return nil }
func (constor *Constor) isdeleted(pathl string) bool { var inobyte []byte inobyte = make([]byte, 100, 100) if _, err := syscall.Getxattr(pathl, DELXATTR, inobyte); err == nil { return true } else { return false } }
func (r *RealFileReader) GetAttr(n string) ([]byte, error) { tmp := make([]byte, 256) sz, err := syscall.Getxattr(r.path, n, tmp) if err != nil { return nil, err } else { return tmp[:sz], nil } }
// based on libacl's acl_get_file func getType(path, attr string) (ACL, error) { buf := bufpool.Get().([]byte) defer func() { bufpool.Put(buf) }() sz, err := syscall.Getxattr(path, attr, buf) if sz == -1 && err == syscall.ERANGE { sz, err = syscall.Getxattr(path, attr, nil) if sz <= 0 { return nil, err } buf = make([]byte, sz) sz, err = syscall.Getxattr(path, attr, buf) } switch { case sz > 0: return aclFromXattr(buf[:sz]) case err == syscall.ENODATA: // TODO(joshlf): acl_get_file also checks for ENOATTR, // but it's not defined in syscall? fi, err := os.Stat(path) if err != nil { return nil, err } if attr == aclEADefault { if fi.IsDir() { return nil, nil } return nil, syscall.EACCES } else { return FromUnix(fi.Mode()), nil } default: return nil, err } }
func setOrHasXattr(path, n, v string) error { if err := syscall.Setxattr(path, n, []byte(v), 1); err == nil { return nil } else if !os.IsExist(err) { return err } tab := make([]byte, 256) sz, err := syscall.Getxattr(path, n, tab) if err != nil { return err } if bytes.Equal([]byte(v), tab[:sz]) { return nil } else { return errors.New("XATTR mismatch") } }
func TestXAttrCaching(t *testing.T) { wd, _ := ioutil.TempDir("", "unionfs") defer os.RemoveAll(wd) os.Mkdir(wd+"/mnt", 0700) err := os.Mkdir(wd+"/rw", 0700) if err != nil { t.Fatalf("Mkdir failed: %v", err) } rwFS := pathfs.NewLoopbackFileSystem(wd + "/rw") roFS := &TestFS{ FileSystem: pathfs.NewDefaultFileSystem(), } ufs, err := NewUnionFs([]pathfs.FileSystem{rwFS, NewCachingFileSystem(roFS, entryTtl)}, testOpts) if err != nil { t.Fatalf("NewUnionFs: %v", err) } opts := &nodefs.Options{ EntryTimeout: entryTtl / 2, AttrTimeout: entryTtl / 2, NegativeTimeout: entryTtl / 2, } pathfs := pathfs.NewPathNodeFs(ufs, &pathfs.PathNodeFsOptions{ClientInodes: true}) server, conn, err := nodefs.MountRoot(wd+"/mnt", pathfs.Root(), opts) if err != nil { t.Fatalf("MountNodeFileSystem failed: %v", err) } server.SetDebug(VerboseTest()) conn.SetDebug(VerboseTest()) pathfs.SetDebug(VerboseTest()) defer server.Unmount() go server.Serve() if fi, err := os.Lstat(wd + "/mnt"); err != nil || !fi.IsDir() { t.Fatalf("root not readable: %v, %v", err, fi) } buf := make([]byte, 1024) n, err := syscall.Getxattr(wd+"/mnt/file", "user.attr", buf) if err != nil { t.Fatalf("Getxattr: %v", err) } want := "\x2a" got := string(buf[:n]) if got != want { t.Fatalf("Got %q want %q", got, err) } time.Sleep(entryTtl / 2) n, err = syscall.Getxattr(wd+"/mnt/file", "user.attr", buf) if err != nil { t.Fatalf("Getxattr: %v", err) } got = string(buf[:n]) if got != want { t.Fatalf("Got %q want %q", got, err) } server.Unmount() if roFS.xattrRead != 1 { t.Errorf("got xattrRead=%d, want 1", roFS.xattrRead) } }
// Darwin doesn't have support for syscall.Getxattr() so we pull it into its own file and implement it by hand on Darwin. func Getxattr(path string, attr string, dest []byte) (sz int, err error) { return syscall.Getxattr(path, attr, dest) }