当前位置: 首页>>代码示例>>Golang>>正文


Golang os.Lchown函数代码示例

本文整理汇总了Golang中os.Lchown函数的典型用法代码示例。如果您正苦于以下问题:Golang Lchown函数的具体用法?Golang Lchown怎么用?Golang Lchown使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Lchown函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: createDevices

func createDevices(rootDir string, uid, gid int) error {
	nullDir := fp.Join(rootDir, os.DevNull)
	if err := osutil.Mknod(nullDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {
		return err
	}

	if err := os.Lchown(nullDir, uid, gid); err != nil {
		log.Debugf("Failed to lchown %s: %s\n", nullDir, err)
		return err
	}

	zeroDir := fp.Join(rootDir, "/dev/zero")
	if err := osutil.Mknod(zeroDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {
		return err
	}

	if err := os.Lchown(zeroDir, uid, gid); err != nil {
		log.Debugf("Failed to lchown %s: %s\n", zeroDir, err)
		return err
	}

	for _, f := range []string{"/dev/random", "/dev/urandom"} {
		randomDir := fp.Join(rootDir, f)
		if err := osutil.Mknod(randomDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+9); err != nil {
			return err
		}

		if err := os.Lchown(randomDir, uid, gid); err != nil {
			log.Debugf("Failed to lchown %s: %s\n", randomDir, err)
			return err
		}
	}

	return nil
}
开发者ID:yuuki,项目名称:droot,代码行数:35,代码来源:run.go

示例2: createDevices

func createDevices(rootDir string, uid, gid int) error {
	nullDir := fp.Join(rootDir, os.DevNull)
	if err := osutil.Mknod(nullDir, syscall.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {
		return err
	}

	if err := os.Lchown(nullDir, uid, gid); err != nil {
		return errwrap.Wrapff(err, "Failed to lchown %s: {{err}}", nullDir)
	}

	zeroDir := fp.Join(rootDir, "/dev/zero")
	if err := osutil.Mknod(zeroDir, syscall.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {
		return err
	}

	if err := os.Lchown(zeroDir, uid, gid); err != nil {
		return errwrap.Wrapff(err, "Failed to lchown %s:", zeroDir)
	}

	for _, f := range []string{"/dev/random", "/dev/urandom"} {
		randomDir := fp.Join(rootDir, f)
		if err := osutil.Mknod(randomDir, syscall.S_IFCHR|uint32(os.FileMode(0666)), 1*256+9); err != nil {
			return err
		}

		if err := os.Lchown(randomDir, uid, gid); err != nil {
			return errwrap.Wrapff(err, "Failed to lchown %s: {{err}}", randomDir)
		}
	}

	return nil
}
开发者ID:varung,项目名称:droot,代码行数:32,代码来源:run.go

示例3: NewUidShiftingFilePermEditor

func NewUidShiftingFilePermEditor(uidRange *uid.UidRange) (FilePermissionsEditor, error) {
	if os.Geteuid() != 0 {
		return func(_ string, _, _ int, _ byte, _ os.FileInfo) error {
			// The files are owned by the current user on creation.
			// If we do nothing, they will remain so.
			return nil
		}, nil
	}

	return func(path string, uid, gid int, typ byte, fi os.FileInfo) error {
		shiftedUid, shiftedGid, err := uidRange.ShiftRange(uint32(uid), uint32(gid))
		if err != nil {
			return err
		}
		if err := os.Lchown(path, int(shiftedUid), int(shiftedGid)); err != nil {
			return err
		}

		// lchown(2) says that, depending on the linux kernel version, it
		// can change the file's mode also if executed as root. So call
		// os.Chmod after it.
		if typ != tar.TypeSymlink {
			if err := os.Chmod(path, fi.Mode()); err != nil {
				return err
			}
		}
		return nil
	}, nil
}
开发者ID:rhencke,项目名称:acbuild,代码行数:29,代码来源:tar.go

示例4: main

func main() {
	// Create a hard link
	// You will have two file names that point to the same contents
	// Changing the contents of one will change the other
	// Deleting/renaming one will not affect the other
	err := os.Link("original.txt", "original_also.txt")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("creating sym")
	// Create a symlink
	err = os.Symlink("original.txt", "original_sym.txt")
	if err != nil {
		log.Fatal(err)
	}

	// Lstat will return file info, but if it is actually
	// a symlink, it will return info about the symlink.
	// It will not follow the link and give information
	// about the real file
	// Symlinks do not work in Windows
	fileInfo, err := os.Lstat("original_sym.txt")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Link info: %+v", fileInfo)

	// Change ownership of a symlink only
	// and not the file it points to
	err = os.Lchown("original_sym.txt", os.Getuid(), os.Getgid())
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:tjyang,项目名称:reference,代码行数:35,代码来源:links.go

示例5: SetSymlinkUser

func SetSymlinkUser(filename, username string, live 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.Lstat(filename, &stat)
	if err != nil {
		return false, err
	}
	if int(stat.Uid) == uid {
		return false, nil
	}
	if live {
		err = os.Lchown(filename, uid, int(stat.Gid))
		if err != nil {
			return false, err
		}
	}
	return true, nil
}
开发者ID:RegalSkeppet,项目名称:prov,代码行数:25,代码来源:symlinkfile.go

示例6: Set

func (f *StatData) Set(which Field) (needswriting bool, e os.Error) {
	stat, e := Stat(f.Name)
	if e != nil {
		_, ispatherror := e.(*os.PathError) // *os.PathError means file doesn't exist (which is okay)
		if e != nil && !ispatherror {
			return false, e
		}
		return true, nil
	}
	if stat.Mode != f.Mode {
		return true, os.Remove(f.Name)
	}
	if Perms&which != 0 && stat.Perms != f.Perms {
		e = os.Chmod(f.Name, f.Perms)
		if e != nil {
			return false, e
		}
	} else {
		f.Perms = stat.Perms
	}
	// Set the ownership if needed...
	if Gid&which == 0 {
		f.Gid = stat.Gid
	}
	if Uid&which == 0 {
		f.Uid = stat.Uid
	}
	if Uid&which != 0 || Gid&which != 0 {
		e = os.Lchown(f.Name, f.Uid, f.Gid)
		if e != nil {
			return false, e
		}
	}
	return false, nil
}
开发者ID:andradeandrey,项目名称:goadmin,代码行数:35,代码来源:file.go

示例7: EnsureFileOwner

func EnsureFileOwner(destPath string, owner string, groupName string) (bool, error) {
	changed := false
	stat, err := os.Lstat(destPath)
	if err != nil {
		return changed, fmt.Errorf("error getting file stat for %q: %v", destPath, err)
	}

	user, err := LookupUser(owner) //user.Lookup(owner)
	if err != nil {
		return changed, fmt.Errorf("error looking up user %q: %v", owner, err)
	}

	group, err := LookupGroup(groupName)
	if err != nil {
		return changed, fmt.Errorf("error looking up group %q: %v", groupName, err)
	}

	if int(stat.Sys().(*syscall.Stat_t).Uid) == user.Uid && int(stat.Sys().(*syscall.Stat_t).Gid) == group.Gid {
		return changed, nil
	}

	glog.Infof("Changing file owner/group for %q to %s:%s", destPath, owner, group)
	err = os.Lchown(destPath, user.Uid, group.Gid)
	if err != nil {
		return changed, fmt.Errorf("error setting file owner/group for %q: %v", destPath, err)
	}
	changed = true

	return changed, nil
}
开发者ID:crohling,项目名称:kops,代码行数:30,代码来源:files.go

示例8: fixPermissions

func fixPermissions(source, destination string, uid, gid int) error {
	// The copied root permission should not be changed for previously existing
	// directories.
	s, err := os.Stat(destination)
	if err != nil && !os.IsNotExist(err) {
		return err
	}
	fixRootPermission := (err != nil) || !s.IsDir()

	// We Walk on the source rather than on the destination because we don't
	// want to change permissions on things we haven't created or modified.
	return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error {
		// Do not alter the walk root itself as it potentially existed before.
		if !fixRootPermission && (source == fullpath) {
			return nil
		}
		// Path is prefixed by source: substitute with destination instead.
		cleaned, err := filepath.Rel(source, fullpath)
		if err != nil {
			return err
		}
		fullpath = path.Join(destination, cleaned)
		return os.Lchown(fullpath, uid, gid)
	})
}
开发者ID:ncultra,项目名称:docker,代码行数:25,代码来源:internals.go

示例9: fixPermissions

func fixPermissions(source, destination string, uid, gid int, destExisted bool) error {
	// If the destination didn't already exist, or the destination isn't a
	// directory, then we should Lchown the destination. Otherwise, we shouldn't
	// Lchown the destination.
	destStat, err := os.Stat(destination)
	if err != nil {
		// This should *never* be reached, because the destination must've already
		// been created while untar-ing the context.
		return err
	}
	doChownDestination := !destExisted || !destStat.IsDir()

	// We Walk on the source rather than on the destination because we don't
	// want to change permissions on things we haven't created or modified.
	return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error {
		// Do not alter the walk root iff. it existed before, as it doesn't fall under
		// the domain of "things we should chown".
		if !doChownDestination && (source == fullpath) {
			return nil
		}

		// Path is prefixed by source: substitute with destination instead.
		cleaned, err := filepath.Rel(source, fullpath)
		if err != nil {
			return err
		}

		fullpath = path.Join(destination, cleaned)
		return os.Lchown(fullpath, uid, gid)
	})
}
开发者ID:hgschmie,项目名称:docker,代码行数:31,代码来源:internals.go

示例10: lchown

func lchown(name string, uid, gid int) error {
	p, err := winPath(name)
	if err != nil {
		return newPathError("lchown", name, err)
	}
	return os.Lchown(p, uid, gid)
}
开发者ID:jianqiu,项目名称:bosh-agent,代码行数:7,代码来源:fs_windows.go

示例11: restoreMetadata

func (node Node) restoreMetadata(path string) error {
	var err error

	err = os.Lchown(path, int(node.UID), int(node.GID))
	if err != nil {
		return errors.Annotate(err, "Lchown")
	}

	if node.Type != "symlink" {
		err = os.Chmod(path, node.Mode)
		if err != nil {
			return errors.Annotate(err, "Chmod")
		}
	}

	if node.Type != "dir" {
		err = node.RestoreTimestamps(path)
		if err != nil {
			debug.Log("Node.restoreMetadata", "error restoring timestamps for dir %v: %v", path, err)
			return err
		}
	}

	return nil
}
开发者ID:klauspost,项目名称:restic,代码行数:25,代码来源:node.go

示例12: doUidshiftIntoContainer

func (set *IdmapSet) doUidshiftIntoContainer(dir string, testmode bool, how string) error {
	convert := func(path string, fi os.FileInfo, err error) (e error) {
		uid, gid, err := getOwner(path)
		if err != nil {
			return err
		}
		var newuid, newgid int
		switch how {
		case "in":
			newuid, newgid = set.ShiftIntoNs(uid, gid)
		case "out":
			newuid, newgid = set.ShiftFromNs(uid, gid)
		}
		if testmode {
			fmt.Printf("I would shift %q to %d %d\n", path, newuid, newgid)
		} else {
			err = os.Lchown(path, int(newuid), int(newgid))
			if err == nil {
				m := fi.Mode()
				if m&os.ModeSymlink == 0 {
					err = os.Chmod(path, m)
					if err != nil {
						fmt.Printf("Error resetting mode on %q, continuing\n", path)
					}
				}
			}
		}
		return nil
	}

	if !PathExists(dir) {
		return fmt.Errorf("No such file or directory: %q", dir)
	}
	return filepath.Walk(dir, convert)
}
开发者ID:Ramzec,项目名称:lxd,代码行数:35,代码来源:idmapset.go

示例13: CopyDirectory

// CopyDirectory copies the files under the source directory
// to dest directory. The dest directory is created if it
// does not exist.
func CopyDirectory(source string, dest string) error {
	fi, err := os.Stat(source)
	if err != nil {
		return err
	}

	// Get owner.
	st, ok := fi.Sys().(*syscall.Stat_t)
	if !ok {
		return fmt.Errorf("could not convert to syscall.Stat_t")
	}

	// We have to pick an owner here anyway.
	if err := MkdirAllNewAs(dest, fi.Mode(), int(st.Uid), int(st.Gid)); err != nil {
		return err
	}

	return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// Get the relative path
		relPath, err := filepath.Rel(source, path)
		if err != nil {
			return nil
		}

		if info.IsDir() {
			// Skip the source directory.
			if path != source {
				// Get the owner.
				st, ok := info.Sys().(*syscall.Stat_t)
				if !ok {
					return fmt.Errorf("could not convert to syscall.Stat_t")
				}

				uid := int(st.Uid)
				gid := int(st.Gid)

				if err := os.Mkdir(filepath.Join(dest, relPath), info.Mode()); err != nil {
					return err
				}

				if err := os.Lchown(filepath.Join(dest, relPath), uid, gid); err != nil {
					return err
				}
			}
			return nil
		}

		// Copy the file.
		if err := CopyFile(path, filepath.Join(dest, relPath)); err != nil {
			return err
		}

		return nil
	})
}
开发者ID:curtiszimmerman,项目名称:runc,代码行数:62,代码来源:fileutils.go

示例14: fixPermissions

func fixPermissions(destination string, uid, gid int) error {
	return filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if err := os.Lchown(path, uid, gid); err != nil && !os.IsNotExist(err) {
			return err
		}
		return nil
	})
}
开发者ID:andrew2king,项目名称:docker,代码行数:8,代码来源:build.go

示例15: mkdirAll

func mkdirAll(path string, hdr Metadata) (stack []string, topMTime time.Time, err error) {
	// Following code derives from the golang standard library, so you can consider it BSDish if you like.
	// Our changes are licensed under Apache for the sake of overall license simplicity of the project.
	// Ref: https://github.com/golang/go/blob/883bc6ed0ea815293fe6309d66f967ea60630e87/src/os/path.go#L12

	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
	dir, err := os.Stat(path)
	if err == nil {
		if dir.IsDir() {
			return nil, dir.ModTime(), nil
		}
		return nil, dir.ModTime(), &os.PathError{"mkdir", path, syscall.ENOTDIR}
	}

	// Slow path: make sure parent exists and then call Mkdir for path.
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}

	if j > 1 {
		// Create parent
		stack, topMTime, err = mkdirAll(path[0:j-1], hdr)
		if err != nil {
			return stack, topMTime, err
		}
	}

	// Parent now exists; invoke Mkdir and use its result.
	err = os.Mkdir(path, 0755)
	if err != nil {
		// Handle arguments like "foo/." by
		// double-checking that directory doesn't exist.
		dir, err1 := os.Lstat(path)
		if err1 == nil && dir.IsDir() {
			return stack, topMTime, nil
		}
		return stack, topMTime, err
	}
	stack = append(stack, path)

	// Apply standardizations.
	if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
		return stack, topMTime, err
	}
	if err := os.Chmod(path, hdr.FileMode()); err != nil {
		return stack, topMTime, err
	}
	// Except for time, because as usual with dirs, that requires walking backwards again at the end.
	// That'll be done one function out.

	return stack, topMTime, nil
}
开发者ID:kkroening,项目名称:repeatr,代码行数:58,代码来源:emplacements.go


注:本文中的os.Lchown函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。