本文整理汇总了Golang中os.FileMode类的典型用法代码示例。如果您正苦于以下问题:Golang FileMode类的具体用法?Golang FileMode怎么用?Golang FileMode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileMode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: copyDir
func copyDir(src, dst string, mode os.FileMode) error {
srcf, err := os.Open(src)
if err != nil {
return err
}
defer srcf.Close()
if mode&0500 == 0 {
// The source directory doesn't have write permission,
// so give the new directory write permission anyway
// so that we have permission to create its contents.
// We'll make the permissions match at the end.
mode |= 0500
}
if err := os.Mkdir(dst, mode.Perm()); err != nil {
return err
}
for {
names, err := srcf.Readdirnames(100)
for _, name := range names {
if err := Copy(filepath.Join(src, name), filepath.Join(dst, name)); err != nil {
return err
}
}
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("error reading directory %q: %v", src, err)
}
}
if err := os.Chmod(dst, mode.Perm()); err != nil {
return err
}
return nil
}
示例2: mode
// mode converts an os.FileMode to a go9p mode.
func mode(m os.FileMode, dotu bool) uint32 {
other := uint32(m & 0777)
if m.IsDir() {
other |= go9p.DMDIR
}
if !dotu {
return other
}
if m&os.ModeSymlink != 0 {
other |= go9p.DMSYMLINK
}
if m&os.ModeSocket != 0 {
other |= go9p.DMSOCKET
}
if m&os.ModeNamedPipe != 0 {
other |= go9p.DMNAMEDPIPE
}
if m&os.ModeDevice != 0 {
other |= go9p.DMDEVICE
}
if m&os.ModeSetuid != 0 {
other |= go9p.DMSETUID
}
if m&os.ModeSetgid != 0 {
other |= go9p.DMSETGID
}
return other
}
示例3: visit
func visit(path string, f os.FileInfo, err error) error {
var fMod os.FileMode
fMod = f.Mode()
if fMod.IsDir() {
fmt.Printf("Entering Folder: %s\n", f.Name())
return nil
}
fmt.Printf("Saving log: %s\n", path)
file, err := os.Open(path)
if err != nil {
fmt.Printf("trouble opening file: %s\n", err)
return nil
}
b := make([]byte, f.Size())
n, err := file.Read(b)
if int64(n) != f.Size() {
fmt.Printf("Error reading entire file: %s\n", err)
}
var finalkey = KEY_PREFIX + f.Name()
var kmsg KafkaMsg = KafkaMsg{
Topic: FolderTopic,
Key: finalkey,
Value: string(b),
}
sendMsg(kmsg)
return nil
}
示例4: Chmod
func (nd *Node) Chmod(newMode os.FileMode) error {
if nd.Size() > 0 && newMode.IsDir() {
return errors.New("File has size, cannot change to directory")
}
nd.mode = newMode
return nd.save()
}
示例5: put
// put single file
func put(args []string) error {
fs := flag.NewFlagSet("put", flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintln(os.Stderr, "create usage: gomote put [put-opts] <buildlet-name> <source or '-' for stdin> [destination]")
fs.PrintDefaults()
os.Exit(1)
}
modeStr := fs.String("mode", "", "Unix file mode (octal); default to source file mode")
fs.Parse(args)
if n := fs.NArg(); n < 2 || n > 3 {
fs.Usage()
}
bc, err := namedClient(fs.Arg(0))
if err != nil {
return err
}
var r io.Reader = os.Stdin
var mode os.FileMode = 0666
src := fs.Arg(1)
if src != "-" {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
r = f
if *modeStr == "" {
fi, err := f.Stat()
if err != nil {
return err
}
mode = fi.Mode()
}
}
if *modeStr != "" {
modeInt, err := strconv.ParseInt(*modeStr, 8, 64)
if err != nil {
return err
}
mode = os.FileMode(modeInt)
if !mode.IsRegular() {
return fmt.Errorf("bad mode: %v", mode)
}
}
dest := fs.Arg(2)
if dest == "" {
if src == "-" {
return errors.New("must specify destination file name when source is standard input")
}
dest = filepath.Base(src)
}
return bc.Put(r, dest, mode)
}
示例6: permToACL
// permToACL - convert perm to meaningful ACL
func permToACL(mode os.FileMode) BucketACL {
switch mode.Perm() {
case os.FileMode(0700):
return BucketACL("private")
case os.FileMode(0500):
return BucketACL("public-read")
case os.FileMode(0777):
return BucketACL("public-read-write")
default:
return BucketACL("private")
}
}
示例7: OpenFile
func (fs *memFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
fs.mu.Lock()
defer fs.mu.Unlock()
dir, frag, err := fs.find("open", name)
if err != nil {
return nil, err
}
var n *memFSNode
if dir == nil {
// We're opening the root.
if flag&(os.O_WRONLY|os.O_RDWR) != 0 {
return nil, os.ErrPermission
}
n, frag = &fs.root, "/"
} else {
n = dir.children[frag]
if flag&(os.O_SYNC|os.O_APPEND) != 0 {
// memFile doesn't support these flags yet.
return nil, os.ErrInvalid
}
if flag&os.O_CREATE != 0 {
if flag&os.O_EXCL != 0 && n != nil {
return nil, os.ErrExist
}
if n == nil {
n = &memFSNode{
mode: perm.Perm(),
}
dir.children[frag] = n
}
}
if n == nil {
return nil, os.ErrNotExist
}
if flag&(os.O_WRONLY|os.O_RDWR) != 0 && flag&os.O_TRUNC != 0 {
n.mu.Lock()
n.data = nil
n.mu.Unlock()
}
}
children := make([]os.FileInfo, 0, len(n.children))
for cName, c := range n.children {
children = append(children, c.stat(cName))
}
return &memFile{
n: n,
nameSnapshot: frag,
childrenSnapshot: children,
}, nil
}
示例8: categorizeEntry
func categorizeEntry(path string, mode os.FileMode, list *filelist.Lists) error {
switch {
case mode.IsDir():
list.Dirs = append(list.Dirs, path)
case mode.IsRegular():
list.Files = append(list.Files, path)
case isSymlink(mode):
list.Symlinks = append(list.Symlinks, path)
default:
return fmt.Errorf("unsupported file mode: %d (not a file, directory or symlink)", mode&os.ModeType)
}
return nil
}
示例9: WriteToFile
func (pb *Packbuilder) WriteToFile(name string, mode os.FileMode) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_write(pb.ptr, cname, C.uint(mode.Perm()), nil, nil)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
示例10: posixMode
// posixMode() returns the posix specific mode bits from Go's portable mode bits
//
// Copied from the syscallMode() function in file_posix.go in the Go source
func posixMode(i os.FileMode) (o uint32) {
o |= uint32(i.Perm())
if i&os.ModeSetuid != 0 {
o |= syscall.S_ISUID
}
if i&os.ModeSetgid != 0 {
o |= syscall.S_ISGID
}
if i&os.ModeSticky != 0 {
o |= syscall.S_ISVTX
}
return
}
示例11: syscallMode
// https://github.com/jnwhiteh/golang/blob/master/src/pkg/os/file_posix.go#L61
func syscallMode(i os.FileMode) (o uint32) {
o |= uint32(i.Perm())
if i&os.ModeSetuid != 0 {
o |= syscall.S_ISUID
}
if i&os.ModeSetgid != 0 {
o |= syscall.S_ISGID
}
if i&os.ModeSticky != 0 {
o |= syscall.S_ISVTX
}
// No mapping for Go's ModeTemporary (plan9 only).
return
}
示例12: FileMode
// Given a FileMode from the os package, get it's permission bits
func FileMode(fm os.FileMode) PermissionBits {
perm := PermissionBits(fm.Perm())
if fm&os.ModeSetuid != 0 {
perm.SetSetuid(true)
}
if fm&os.ModeSetgid != 0 {
perm.SetSetgid(true)
}
if fm&os.ModeSticky != 0 {
perm.SetSticky(true)
}
return perm
}
示例13: OpenFile
func (fs *memFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
var ret *memFile
err := fs.walk("open", name, func(dir *memFSNode, frag string, final bool) error {
if !final {
return nil
}
if frag == "" {
return os.ErrInvalid
}
if flag&(os.O_SYNC|os.O_APPEND) != 0 {
return os.ErrInvalid
}
n := dir.children[frag]
if flag&os.O_CREATE != 0 {
if flag&os.O_EXCL != 0 && n != nil {
return os.ErrExist
}
if n == nil {
n = &memFSNode{
name: frag,
mode: perm.Perm(),
}
dir.children[frag] = n
}
}
if n == nil {
return os.ErrNotExist
}
if flag&(os.O_WRONLY|os.O_RDWR) != 0 && flag&os.O_TRUNC != 0 {
n.mu.Lock()
n.data = nil
n.mu.Unlock()
}
children := make([]os.FileInfo, 0, len(n.children))
for _, c := range n.children {
children = append(children, c)
}
ret = &memFile{
n: n,
children: children,
}
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}
示例14: Command
func Command(rootfsPath string, uid, gid int, mode os.FileMode, recreate bool, paths ...string) *exec.Cmd {
flags := []string{
name,
"-rootfsPath", rootfsPath,
"-uid", strconv.Itoa(uid),
"-gid", strconv.Itoa(gid),
"-perm", strconv.Itoa(int(mode.Perm())),
}
if recreate {
flags = append(flags, "-recreate=true")
}
return reexec.Command(append(flags, paths...)...)
}
示例15: modeToType
func modeToType(mode os.FileMode) byte {
switch {
case mode.IsDir():
return 'd'
case mode.IsRegular():
return 'f'
case mode&os.ModeSymlink != 0:
return 'l'
case mode&os.ModeSocket != 0:
return 's'
case mode&os.ModeNamedPipe != 0:
return 'p'
case mode&os.ModeDevice != 0 && mode&os.ModeCharDevice != 0:
return 'C'
case mode&os.ModeDevice != 0 && mode&os.ModeCharDevice == 0:
return 'D'
default:
return 'u'
}
}