Skip to content

Commit 5efd718

Browse files
authored
sdk: use uint32 for uid,gid (#1648)
1 parent ab46af0 commit 5efd718

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

cmd/gateway_noop.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import (
2626

2727
func cmdGateway() *cli.Command {
2828
return &cli.Command{
29-
Name: "gateway",
30-
Category: "SERVICE",
31-
Usage: "Start an S3-compatible gateway (not included)",
29+
Name: "gateway",
30+
Category: "SERVICE",
31+
Usage: "Start an S3-compatible gateway (not included)",
3232
Description: `This feature is not included. If you want it, recompile juicefs without "nogateway" flag`,
3333
Action: func(*cli.Context) error {
3434
return errors.New("not supported")

sdk/java/libjfs/guid.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,49 @@ import (
2525
)
2626

2727
type pwent struct {
28-
id int
28+
id uint32
2929
name string
3030
}
3131

3232
type mapping struct {
3333
sync.Mutex
3434
salt string
35-
usernames map[string]int
36-
userIDs map[int]string
37-
groups map[string]int
38-
groupIDs map[int]string
35+
usernames map[string]uint32
36+
userIDs map[uint32]string
37+
groups map[string]uint32
38+
groupIDs map[uint32]string
3939
}
4040

4141
func newMapping(salt string) *mapping {
4242
m := &mapping{
4343
salt: salt,
44-
usernames: make(map[string]int),
45-
userIDs: make(map[int]string),
46-
groups: make(map[string]int),
47-
groupIDs: make(map[int]string),
44+
usernames: make(map[string]uint32),
45+
userIDs: make(map[uint32]string),
46+
groups: make(map[string]uint32),
47+
groupIDs: make(map[uint32]string),
4848
}
4949
m.update(genAllUids(), genAllGids())
5050
return m
5151
}
5252

53-
func (m *mapping) genGuid(name string) int {
53+
func (m *mapping) genGuid(name string) uint32 {
5454
digest := md5.Sum([]byte(m.salt + name + m.salt))
5555
a := binary.LittleEndian.Uint64(digest[0:8])
5656
b := binary.LittleEndian.Uint64(digest[8:16])
57-
return int(uint32(a ^ b))
57+
return uint32(a ^ b)
5858
}
5959

60-
func (m *mapping) lookupUser(name string) int {
60+
func (m *mapping) lookupUser(name string) uint32 {
6161
m.Lock()
6262
defer m.Unlock()
63-
var id int
63+
var id uint32
6464
if id, ok := m.usernames[name]; ok {
6565
return id
6666
}
6767
u, _ := user.Lookup(name)
6868
if u != nil {
69-
id, _ = strconv.Atoi(u.Uid)
69+
id_, _ := strconv.ParseUint(u.Uid, 10, 32)
70+
id = uint32(id_)
7071
} else {
7172
id = m.genGuid(name)
7273
}
@@ -75,33 +76,34 @@ func (m *mapping) lookupUser(name string) int {
7576
return id
7677
}
7778

78-
func (m *mapping) lookupGroup(name string) int {
79+
func (m *mapping) lookupGroup(name string) uint32 {
7980
m.Lock()
8081
defer m.Unlock()
81-
var id int
82+
var id uint32
8283
if id, ok := m.groups[name]; ok {
8384
return id
8485
}
8586
g, _ := user.LookupGroup(name)
8687
if g == nil {
8788
id = m.genGuid(name)
8889
} else {
89-
id, _ = strconv.Atoi(g.Gid)
90+
id_, _ := strconv.ParseUint(g.Gid, 10, 32)
91+
id = uint32(id_)
9092
}
9193
m.groups[name] = id
9294
m.groupIDs[id] = name
9395
return 0
9496
}
9597

96-
func (m *mapping) lookupUserID(id int) string {
98+
func (m *mapping) lookupUserID(id uint32) string {
9799
m.Lock()
98100
defer m.Unlock()
99101
if name, ok := m.userIDs[id]; ok {
100102
return name
101103
}
102-
u, _ := user.LookupId(strconv.Itoa(id))
104+
u, _ := user.LookupId(strconv.Itoa(int(id)))
103105
if u == nil {
104-
u = &user.User{Username: strconv.Itoa(id)}
106+
u = &user.User{Username: strconv.Itoa(int(id))}
105107
}
106108
name := u.Username
107109
if len(name) > 49 {
@@ -112,15 +114,15 @@ func (m *mapping) lookupUserID(id int) string {
112114
return name
113115
}
114116

115-
func (m *mapping) lookupGroupID(id int) string {
117+
func (m *mapping) lookupGroupID(id uint32) string {
116118
m.Lock()
117119
defer m.Unlock()
118120
if name, ok := m.groupIDs[id]; ok {
119121
return name
120122
}
121-
g, _ := user.LookupGroupId(strconv.Itoa(id))
123+
g, _ := user.LookupGroupId(strconv.Itoa(int(id)))
122124
if g == nil {
123-
g = &user.Group{Name: strconv.Itoa(id)}
125+
g = &user.Group{Name: strconv.Itoa(int(id))}
124126
}
125127
name := g.Name
126128
if len(name) > 49 {

sdk/java/libjfs/guid_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func genAllUids() []pwent {
4242
}
4343
name := C.GoString(p.pw_name)
4444
if name != "root" {
45-
uids = append(uids, pwent{int(p.pw_uid), name})
45+
uids = append(uids, pwent{uint32(p.pw_uid), name})
4646
}
4747
}
4848
return uids
@@ -61,7 +61,7 @@ func genAllGids() []pwent {
6161
}
6262
name := C.GoString(p.gr_name)
6363
if name != "root" {
64-
gids = append(gids, pwent{int(p.gr_gid), name})
64+
gids = append(gids, pwent{uint32(p.gr_gid), name})
6565
}
6666
}
6767
return gids

sdk/java/libjfs/guid_windows.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func genAllUids() []pwent {
4242
name := fields[len(fields)-2]
4343
sid := fields[len(fields)-1]
4444
ps := strings.Split(sid, "-")
45-
auth, _ := strconv.Atoi(ps[2])
45+
auth, _ := strconv.ParseUint(ps[2], 10, 32)
4646
count := len(ps) - 3
47-
var subAuth int
47+
var subAuth uint64
4848
if count > 0 {
49-
subAuth, _ = strconv.Atoi(ps[3])
49+
subAuth, _ = strconv.ParseUint(ps[3], 10, 32)
5050
}
51-
rid, _ := strconv.Atoi(ps[len(ps)-1])
52-
var uid int
51+
rid, _ := strconv.ParseUint(ps[len(ps)-1], 10, 32)
52+
var uid uint64
5353
if auth == 5 {
5454
if count == 1 {
5555
// "SYSTEM" S-1-5-18 <=> uid/gid: 18
@@ -72,7 +72,7 @@ func genAllUids() []pwent {
7272
uid = 0x60000*subAuth + rid
7373
}
7474
if uid > 0 {
75-
uids = append(uids, pwent{uid, name})
75+
uids = append(uids, pwent{uint32(uid), name})
7676
logger.Tracef("found account %s -> %d (%s)", name, uid, sid)
7777
}
7878
}
@@ -101,14 +101,14 @@ func genAllGids() []pwent {
101101
name := strings.TrimSpace(line[nameIndex : sidIndex-1])
102102
sid := strings.TrimSpace(line[sidIndex:])
103103
ps := strings.Split(sid, "-")
104-
auth, _ := strconv.Atoi(ps[2])
104+
auth, _ := strconv.ParseUint(ps[2], 10, 32)
105105
count := len(ps) - 3
106-
var subAuth int
106+
var subAuth uint64
107107
if count > 0 {
108-
subAuth, _ = strconv.Atoi(ps[3])
108+
subAuth, _ = strconv.ParseUint(ps[3], 10, 32)
109109
}
110-
rid, _ := strconv.Atoi(ps[len(ps)-1])
111-
var gid int
110+
rid, _ := strconv.ParseUint(ps[len(ps)-1], 10, 32)
111+
var gid uint64
112112
if auth == 5 {
113113
if count == 1 {
114114
// "SYSTEM" S-1-5-18 <=> uid/gid: 18
@@ -131,7 +131,7 @@ func genAllGids() []pwent {
131131
gid = 0x60000*subAuth + rid
132132
}
133133
if gid > 0 {
134-
gids = append(gids, pwent{gid, name})
134+
gids = append(gids, pwent{uint32(gid), name})
135135
logger.Tracef("found group %s -> %d (%s)", name, gid, sid)
136136
}
137137
}

sdk/java/libjfs/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ func (w *wrapper) lookupGids(groups string) []uint32 {
186186
func (w *wrapper) uid2name(uid uint32) string {
187187
name := w.superuser
188188
if uid > 0 {
189-
name = w.m.lookupUserID(int(uid))
189+
name = w.m.lookupUserID(uid)
190190
}
191191
return name
192192
}
193193

194194
func (w *wrapper) gid2name(gid uint32) string {
195195
group := w.supergroup
196196
if gid > 0 {
197-
group = w.m.lookupGroupID(int(gid))
197+
group = w.m.lookupGroupID(gid)
198198
}
199199
return group
200200
}
@@ -539,8 +539,8 @@ func jfs_update_uid_grouping(h uintptr, uidstr *C.char, grouping *C.char) {
539539
continue
540540
}
541541
username := strings.TrimSpace(fields[0])
542-
uid, _ := strconv.Atoi(strings.TrimSpace(fields[1]))
543-
uids = append(uids, pwent{uid, username})
542+
uid, _ := strconv.ParseUint(strings.TrimSpace(fields[1]), 10, 32)
543+
uids = append(uids, pwent{uint32(uid), username})
544544
}
545545

546546
var buffer bytes.Buffer
@@ -559,8 +559,8 @@ func jfs_update_uid_grouping(h uintptr, uidstr *C.char, grouping *C.char) {
559559
continue
560560
}
561561
gname := strings.TrimSpace(fields[0])
562-
gid, _ := strconv.Atoi(strings.TrimSpace(fields[1]))
563-
gids = append(gids, pwent{gid, gname})
562+
gid, _ := strconv.ParseUint(strings.TrimSpace(fields[1]), 10, 32)
563+
gids = append(gids, pwent{uint32(gid), gname})
564564
if len(fields) > 2 {
565565
for _, user := range strings.Split(fields[len(fields)-1], ",") {
566566
if strings.TrimSpace(user) == w.user {

0 commit comments

Comments
 (0)