Skip to content

Commit f2ddae9

Browse files
committed
uucore::entries: Make Passwd::locate and Group::locate thread-safe
1 parent fe286fa commit f2ddae9

File tree

6 files changed

+121
-135
lines changed

6 files changed

+121
-135
lines changed

.vscode/cspell.dictionaries/workspace.wordlist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ getgrgid
182182
getgrnam
183183
getgrouplist
184184
getgroups
185+
getpwent
185186
getpwnam
186187
getpwuid
187188
getuid

src/uu/chown/src/chown.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
183183

184184
let uid = if !user.is_empty() {
185185
Some(match Passwd::locate(user) {
186-
Ok(u) => u.uid(), // We have been able to get the uid
186+
Ok(u) => u.uid, // We have been able to get the uid
187187
Err(_) =>
188188
// we have NOT been able to find the uid
189189
// but we could be in the case where we have user.group
@@ -208,7 +208,7 @@ fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
208208
Some(
209209
Group::locate(group)
210210
.map_err(|_| USimpleError::new(1, format!("invalid group: {}", spec.quote())))?
211-
.gid(),
211+
.gid,
212212
)
213213
} else {
214214
None

src/uu/id/src/id.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
245245
// GNU's `id` does not support the flags: -p/-P/-A.
246246
if matches.is_present(options::OPT_PASSWORD) {
247247
// BSD's `id` ignores all but the first specified user
248-
pline(possible_pw.map(|v| v.uid()));
248+
pline(possible_pw.as_ref().map(|v| v.uid));
249249
return Ok(());
250250
};
251251
if matches.is_present(options::OPT_HUMAN_READABLE) {
@@ -259,7 +259,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
259259
return Ok(());
260260
}
261261

262-
let (uid, gid) = possible_pw.map(|p| (p.uid(), p.gid())).unwrap_or((
262+
let (uid, gid) = possible_pw.as_ref().map(|p| (p.uid, p.gid)).unwrap_or((
263263
if state.rflag { getuid() } else { geteuid() },
264264
if state.rflag { getgid() } else { getegid() },
265265
));
@@ -302,7 +302,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
302302

303303
let groups = entries::get_groups_gnu(Some(gid)).unwrap();
304304
let groups = if state.user_specified {
305-
possible_pw.map(|p| p.belongs_to()).unwrap()
305+
possible_pw.as_ref().map(|p| p.belongs_to()).unwrap()
306306
} else {
307307
groups.clone()
308308
};
@@ -453,7 +453,7 @@ pub fn uu_app() -> App<'static, 'static> {
453453

454454
fn pretty(possible_pw: Option<Passwd>) {
455455
if let Some(p) = possible_pw {
456-
print!("uid\t{}\ngroups\t", p.name());
456+
print!("uid\t{}\ngroups\t", p.name);
457457
println!(
458458
"{}",
459459
p.belongs_to()
@@ -466,18 +466,18 @@ fn pretty(possible_pw: Option<Passwd>) {
466466
let login = cstr2cow!(getlogin() as *const _);
467467
let rid = getuid();
468468
if let Ok(p) = Passwd::locate(rid) {
469-
if login == p.name() {
469+
if login == p.name {
470470
println!("login\t{}", login);
471471
}
472-
println!("uid\t{}", p.name());
472+
println!("uid\t{}", p.name);
473473
} else {
474474
println!("uid\t{}", rid);
475475
}
476476

477477
let eid = getegid();
478478
if eid == rid {
479479
if let Ok(p) = Passwd::locate(eid) {
480-
println!("euid\t{}", p.name());
480+
println!("euid\t{}", p.name);
481481
} else {
482482
println!("euid\t{}", eid);
483483
}
@@ -486,7 +486,7 @@ fn pretty(possible_pw: Option<Passwd>) {
486486
let rid = getgid();
487487
if rid != eid {
488488
if let Ok(g) = Group::locate(rid) {
489-
println!("euid\t{}", g.name());
489+
println!("euid\t{}", g.name);
490490
} else {
491491
println!("euid\t{}", rid);
492492
}
@@ -511,16 +511,16 @@ fn pline(possible_uid: Option<uid_t>) {
511511

512512
println!(
513513
"{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
514-
pw.name(),
515-
pw.user_passwd(),
516-
pw.uid(),
517-
pw.gid(),
518-
pw.user_access_class(),
519-
pw.passwd_change_time(),
520-
pw.expiration(),
521-
pw.user_info(),
522-
pw.user_dir(),
523-
pw.user_shell()
514+
pw.name,
515+
pw.user_passwd,
516+
pw.uid,
517+
pw.gid,
518+
pw.user_access_class,
519+
pw.passwd_change_time,
520+
pw.expiration,
521+
pw.user_info,
522+
pw.user_dir,
523+
pw.user_shell
524524
);
525525
}
526526

@@ -531,13 +531,7 @@ fn pline(possible_uid: Option<uid_t>) {
531531

532532
println!(
533533
"{}:{}:{}:{}:{}:{}:{}",
534-
pw.name(),
535-
pw.user_passwd(),
536-
pw.uid(),
537-
pw.gid(),
538-
pw.user_info(),
539-
pw.user_dir(),
540-
pw.user_shell()
534+
pw.name, pw.user_passwd, pw.uid, pw.gid, pw.user_info, pw.user_dir, pw.user_shell
541535
);
542536
}
543537

src/uu/pinky/src/pinky.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ impl Pinky {
267267

268268
if self.include_fullname {
269269
if let Ok(pw) = Passwd::locate(ut.user().as_ref()) {
270-
let mut gecos = pw.user_info().into_owned();
270+
let mut gecos = pw.user_info;
271271
if let Some(n) = gecos.find(',') {
272272
gecos.truncate(n + 1);
273273
}
274-
print!(" {:<19.19}", gecos.replace("&", &pw.name().capitalize()));
274+
print!(" {:<19.19}", gecos.replace("&", &pw.name.capitalize()));
275275
} else {
276276
print!(" {:19}", " ???");
277277
}
@@ -333,21 +333,21 @@ impl Pinky {
333333
for u in &self.names {
334334
print!("Login name: {:<28}In real life: ", u);
335335
if let Ok(pw) = Passwd::locate(u.as_str()) {
336-
println!(" {}", pw.user_info().replace("&", &pw.name().capitalize()));
336+
println!(" {}", pw.user_info.replace("&", &pw.name.capitalize()));
337337
if self.include_home_and_shell {
338-
print!("Directory: {:<29}", pw.user_dir());
339-
println!("Shell: {}", pw.user_shell());
338+
print!("Directory: {:<29}", pw.user_dir);
339+
println!("Shell: {}", pw.user_shell);
340340
}
341341
if self.include_project {
342-
let mut p = PathBuf::from(pw.user_dir().as_ref());
342+
let mut p = PathBuf::from(&pw.user_dir);
343343
p.push(".project");
344344
if let Ok(f) = File::open(p) {
345345
print!("Project: ");
346346
read_to_console(f);
347347
}
348348
}
349349
if self.include_plan {
350-
let mut p = PathBuf::from(pw.user_dir().as_ref());
350+
let mut p = PathBuf::from(&pw.user_dir);
351351
p.push(".plan");
352352
if let Ok(f) = File::open(p) {
353353
println!("Plan:");

0 commit comments

Comments
 (0)