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


Golang user.LookupId函数代码示例

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


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

示例1: ls

func ls(filename string, longFormat bool) {
	f, err := os.OpenFile(filename, os.O_RDONLY, 0)
	if f == nil {
		fmt.Fprintf(os.Stderr, "ls: cannot access %s: %s\n", filename,
			err.Error())
		os.Exit(1)
	}
	files, err := f.Readdir(-1)
	if files == nil {
		fmt.Fprintf(os.Stderr, "ls: could not get files in %s: %s\n",
			filename, err.Error())
		os.Exit(1)
	}
	for j := 0; j < len(files); j++ {
		if longFormat {
			timeLayout := "Jan 2 15:04"
			gid := files[j].Sys().(*syscall.Stat_t).Gid
			uid := files[j].Sys().(*syscall.Stat_t).Uid
			group, _ := user.LookupId(strconv.Itoa(int(gid)))
			user, _ := user.LookupId(strconv.Itoa(int(uid)))

			fmt.Printf("%s\t%s\t%s\t%d\t%s\t%s\n", files[j].Mode(), group.Username, user.Username, files[j].Size(), files[j].ModTime().Format(timeLayout), files[j].Name())
		} else {

			fmt.Printf("%s\n", files[j].Name())
		}
	}
	f.Close()
}
开发者ID:risent,项目名称:coreutils,代码行数:29,代码来源:ls.go

示例2: dir2Dir

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
}
开发者ID:KevinMGranger,项目名称:go9p,代码行数:35,代码来源:ufs.go

示例3: dir2Dir

func dir2Dir(path string, d os.FileInfo, dotu bool, upool Users) (*Dir, error) {
	if r := recover(); r != nil {
		fmt.Print("stat failed: ", r)
		return nil, &os.PathError{"dir2Dir", path, nil}
	}
	sysif := d.Sys()
	if sysif == nil {
		return nil, &os.PathError{"dir2Dir: sysif is nil", path, nil}
	}
	sysMode := sysif.(*syscall.Stat_t)

	dir := new(ufsDir)
	dir.Qid = *dir2Qid(d)
	dir.Mode = dir2Npmode(d, dotu)
	dir.Atime = uint32(0 /*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, nil
	}

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

	// 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
	}

	/* For Akaros, we use the Muid as the link value. */
	if *Akaros && (d.Mode()&os.ModeSymlink != 0) {
		dir.Muid, err = os.Readlink(path)
		if err == nil {
			dir.Mode |= DMSYMLINK
		}
	}
	return &dir.Dir, nil
}
开发者ID:wwaites,项目名称:go9p,代码行数:49,代码来源:ufs.go

示例4: newBaseFileInfo

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")
}
开发者ID:eadmund,项目名称:cypherback,代码行数:28,代码来源:backup_set.go

示例5: Uid2User

func (up *osUsers) Uid2User(uid int) User {
	u, err := user.LookupId(strconv.Itoa(uid))
	if err != nil {
		return nil
	}
	return newUser(u)
}
开发者ID:hugelgupf,项目名称:ninep,代码行数:7,代码来源:osusers.go

示例6: uidToUserName

func uidToUserName(uid uint32) string {
	u, err := user.LookupId(fmt.Sprintf("%d", uid))
	if err != nil {
		return ""
	}
	return u.Username
}
开发者ID:ttacon,项目名称:cluefs,代码行数:7,代码来源:user.go

示例7: LookupId

// LookupId looks up a user by userid.
func LookupId(uid string) (*User, error) {
	u, err := user.LookupId(uid)
	if err != nil {
		return nil, err
	}
	return lookupGroup(u)
}
开发者ID:carriercomm,项目名称:mantle,代码行数:8,代码来源:user.go

示例8: mkKindiDir

func mkKindiDir(path string) (string, error) {
	var name string

	if len(path) == 0 || path == "~/.kindi" {
		uid := syscall.Getuid()
		uid_str := strconv.Itoa(uid)
		u, err := user.LookupId(uid_str)
		if err != nil {
			return "", err
		}
		if e, g := uid_str, u.Uid; e != g {
			return "", fmt.Errorf("expected Uid of %d; got %d", e, g)
		}
		fi, err := os.Stat(u.HomeDir)
		if err != nil || !fi.IsDir() {
			return "", fmt.Errorf("expected a valid HomeDir; stat(%q): err=%v, IsDirectory=%v", err, fi.IsDir())
		}

		name = filepath.Join(u.HomeDir, ".kindi")
	} else {
		name = path
	}

	err := os.Mkdir(name, 0700)
	if err != nil {
		if pe, ok := err.(*os.PathError); ok && pe.Err == syscall.EEXIST {
			return name, nil
		}
		return "", err
	}
	return name, nil
}
开发者ID:uwedeportivo,项目名称:kindi2,代码行数:32,代码来源:main.go

示例9: main

func main() {
	fmt.Println("Current User")
	// Lookup current user, and print out info
	u, err := user.Current()
	if err != nil {
		panic(err)
	}
	fmt.Println(u)

	fmt.Println("root User by name")
	// Lookup root user by name, and print out info
	username, err := user.Lookup("root")
	if err != nil {
		panic(err)
	}
	fmt.Println(username)

	fmt.Println("root User by UID 0")
	// Lookup root user by user name, and print out info
	// Careful here - as the type is not what you expect it to be
	u, err = user.LookupId("0")
	if err != nil {
		panic(err)
	}
	fmt.Println(u)

}
开发者ID:cmceniry,项目名称:g4sa-tutorial,代码行数:27,代码来源:lookup.go

示例10: parseline

func (d DATA) parseline(line string) {
	data := d.val
	l := strings.Fields(line)
	if len(l) < 3 {
		return
	}
	recv, err := strconv.ParseFloat(l[len(l)-1], 64)
	if err != nil {
		return
	}
	sent, err := strconv.ParseFloat(l[len(l)-2], 64)
	if err != nil {
		return
	}
	processCol := strings.Join(l[0:len(l)-2], "_")
	processdata := strings.Split(processCol, "/")
	if len(processdata) < 3 {
		return
	}
	pname := strings.Join(processdata[0:len(processdata)-2], "/")
	if strings.Index(pname, ":") != -1 && strings.Index(pname, "-") != -1 {
		pname = strings.Split(pname, "-")[0]
	}
	ud, err := user.LookupId(processdata[len(processdata)-1])
	if err != nil {
		ud = &user.User{"-1", "-1", "dontknow", "dontknow", "/home/dontknow"}
	}
	data[pname] = pt{
		data[pname].sent + sent,
		data[pname].recv + recv,
		data[pname].users.add(ud.Username),
	}
}
开发者ID:yujinqiu,项目名称:nethogs-parser,代码行数:33,代码来源:hogs.go

示例11: HasDirDiff

func HasDirDiff(path, username, groupname string, mode os.FileMode) (bool, error) {
	fi, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			return true, nil
		}
		return false, err
	}
	if !fi.IsDir() {
		return false, errors.New("not a directory")
	}
	if (fi.Mode() - os.ModeDir) != mode {
		return true, nil
	}

	uid := fi.Sys().(*syscall.Stat_t).Uid
	gid := fi.Sys().(*syscall.Stat_t).Gid
	u, err := user.LookupId(fmt.Sprint(uid))
	if err != nil {
		return false, err
	}
	g, err := group.LookupId(fmt.Sprint(gid))
	if err != nil {
		return false, err
	}
	if u.Username != username {
		return true, nil
	}
	if g.Name != groupname {
		return true, nil
	}
	return false, nil
}
开发者ID:nyushi,项目名称:barrow,代码行数:33,代码来源:diff.go

示例12: switchnsGit

func switchnsGit(cmd *cobra.Command, args []string) {
	var u *user.User
	var err error
	var repoId git.RepoIdentifier

	uid := os.Getuid()
	originalCommand := os.Getenv("SSH_ORIGINAL_COMMAND")

	if u, err = user.LookupId(strconv.Itoa(uid)); err != nil {
		os.Exit(2)
	}

	if uid != 0 {
		if !isValidGitCommand(originalCommand, !git_rw) {
			os.Exit(2)
		}
		if repoId, err = git.NewIdentifierFromUser(u); err != nil {
			os.Exit(2)
		}
		env := []string{fmt.Sprintf("HOME=%s", repoId.RepositoryPathFor())}
		runCommand("geard-githost", []string{"/usr/bin/git-shell", "-c", originalCommand}, env)
	} else {
		fmt.Println("Cannot switch into any git repo as root user")
		os.Exit(2)
	}
}
开发者ID:roacobb,项目名称:geard,代码行数:26,代码来源:main.go

示例13: IsUIDInAny

// IsUIDInAny checks whether the given user belongs to any of the
// given groups
func IsUIDInAny(uid uint32, groups ...string) bool {
	usr, err := user.LookupId(strconv.FormatUint(uint64(uid), 10))
	if err != nil {
		return false
	}

	gid, err := strconv.ParseUint(usr.Gid, 10, 32)
	if err != nil {
		return false
	}

	// XXX cache the Getgrnam calls for a second or so?
	for _, groupname := range groups {
		group, err := Getgrnam(groupname)
		if err != nil {
			continue
		}

		if group.Gid == uint(gid) {
			return true
		}

		for _, member := range group.Mem {
			if member == usr.Username {
				return true
			}
		}
	}

	return false
}
开发者ID:dholbach,项目名称:snappy,代码行数:33,代码来源:group.go

示例14: printLong

// printLong prints the item in the long format.
func (i listItem) printLong() error {
	uid, gid := -1, -1
	if sys, ok := i.info.Sys().(*syscall.Stat_t); ok {
		uid = int(sys.Uid)
		gid = int(sys.Gid)
	}

	userStr := strconv.Itoa(uid)
	var errs errors
	if u, err := user.LookupId(userStr); err != nil {
		errs = append(errs, err)
	} else {
		userStr = u.Username
	}

	size := i.info.Size()
	time := i.info.ModTime().Format("Jan 2 15:04")
	mode := i.info.Mode().String()
	name := i.pathName()
	if _, err := fmt.Println(mode, userStr, gid, size, time, name); err != nil {
		errs = append(errs, err)
	}

	if errs == nil {
		return nil
	}
	return errs
}
开发者ID:vivounicorn,项目名称:eaburns.cmds,代码行数:29,代码来源:main.go

示例15: User

func (opt *UserOption) User() (userinfo *user.User, err error) {
	nvs := strings.TrimSpace(opt.Value)
	if nvs == "" {
		// special case: empty string is the current euid.
		return nil, EmptyUserSet
	}
	// attempt to map this as a number first, in case a numeric UID
	// was provided.
	_, err = strconv.Atoi(nvs)
	if err != nil {
		switch err.(type) {
		case *strconv.NumError:
			// not a number.  do a user table lookup.
			userinfo, err = user.Lookup(nvs)
			if err != nil {
				return nil, err
			}
			return userinfo, nil
		default:
			return nil, err
		}
	}
	userinfo, err = user.LookupId(nvs)
	return userinfo, err
}
开发者ID:kuroneko,项目名称:configureit,代码行数:25,代码来源:user.go


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