ggstd/os/user/lookup.rs
1// Copyright 2023 The rust-ggstd authors. All rights reserved.
2// Copyright 2011 The Go Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6// import "sync"
7
8// pub(super) const USER_FILE: &'static str = "/etc/passwd";
9// pub(super) const GROUP_FILE: &'static str = "/etc/group";
10
11// var colon = []byte{':'}
12
13/// Current returns the current user.
14///
15/// The first call will cache the current user information.
16/// Subsequent calls will return the cached value and will not reflect
17/// changes to the current user.
18pub fn current() -> std::io::Result<super::User> {
19 // cache.Do(fn() { cache.u, cache.err = current() })
20 // if cache.err != nil {
21 // return nil, cache.err
22 // }
23 // u := *cache.u // copy
24 // return &u, nil
25 super::current_internal()
26}
27
28// // cache of the current user
29// var cache struct {
30// sync.Once
31// u *User
32// err error
33// }
34
35// // Lookup looks up a user by username. If the user cannot be found, the
36// // returned error is of type UnknownUserError.
37// fn Lookup(username string) (*User, error) {
38// if u, err := current(); err == nil && u.Username == username {
39// return u, err
40// }
41// return lookupUser(username)
42// }
43
44// // LookupId looks up a user by userid. If the user cannot be found, the
45// // returned error is of type UnknownUserIdError.
46// fn LookupId(uid string) (*User, error) {
47// if u, err := current(); err == nil && u.Uid == uid {
48// return u, err
49// }
50// return lookupUserId(uid)
51// }
52
53// // LookupGroup looks up a group by name. If the group cannot be found, the
54// // returned error is of type UnknownGroupError.
55// fn LookupGroup(name string) (*Group, error) {
56// return lookupGroup(name)
57// }
58
59// // LookupGroupId looks up a group by groupid. If the group cannot be found, the
60// // returned error is of type UnknownGroupIdError.
61// fn LookupGroupId(gid string) (*Group, error) {
62// return lookupGroupId(gid)
63// }
64
65// // GroupIds returns the list of group IDs that the user is a member of.
66// fn (u *User) GroupIds() ([]string, error) {
67// return listGroups(u)
68// }