mirror of
https://github.com/gechandesu/pwd.git
synced 2026-05-13 19:26:49 +03:00
Compare commits
2 Commits
2d9ec0bf59
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| bdaaaaae58 | |||
| 326d93714a |
@@ -1,6 +1,6 @@
|
||||
# Access to the UNIX Password Database
|
||||
|
||||
`pwdb` module provides thread-safe access to the UNIX user account and password
|
||||
`pwd` module provides thread-safe access to the UNIX user account and password
|
||||
database.
|
||||
|
||||
See [passwd(5)](https://man7.org/linux/man-pages/man5/passwd.5.html),
|
||||
|
||||
+37
-13
@@ -1,24 +1,24 @@
|
||||
@[has_globals]
|
||||
module pwdb
|
||||
module pwd
|
||||
|
||||
import sync
|
||||
|
||||
$if windows {
|
||||
$compile_error('pwdb: MS Windows is not supported')
|
||||
$compile_error('pwd: MS Windows is not supported')
|
||||
}
|
||||
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
|
||||
__global pwdb_mutex &sync.Mutex
|
||||
__global pwd_mutex &sync.Mutex
|
||||
|
||||
fn init() {
|
||||
pwdb_mutex = sync.new_mutex()
|
||||
pwd_mutex = sync.new_mutex()
|
||||
}
|
||||
|
||||
fn cleanup() {
|
||||
pwdb_mutex.destroy()
|
||||
unsafe { free(pwdb_mutex) }
|
||||
pwd_mutex.destroy()
|
||||
unsafe { free(pwd_mutex) }
|
||||
}
|
||||
|
||||
struct C.passwd {
|
||||
@@ -76,10 +76,17 @@ fn make_error(name string, uid int) IError {
|
||||
|
||||
// get_by_uid returns the passwd database entry by user ID.
|
||||
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||
@[deprecated: 'use user_by_uid() instead']
|
||||
pub fn get_by_uid(uid int) !Passwd {
|
||||
pwdb_mutex.lock()
|
||||
return user_by_uid(uid)!
|
||||
}
|
||||
|
||||
// user_by_uid returns the passwd database entry by user ID.
|
||||
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||
pub fn user_by_uid(uid int) !Passwd {
|
||||
pwd_mutex.lock()
|
||||
defer {
|
||||
pwdb_mutex.unlock()
|
||||
pwd_mutex.unlock()
|
||||
}
|
||||
pw := C.getpwuid(uid)
|
||||
if isnil(pw) {
|
||||
@@ -88,12 +95,19 @@ pub fn get_by_uid(uid int) !Passwd {
|
||||
return make_passwd(pw)
|
||||
}
|
||||
|
||||
// get_by_uid returns the passwd database entry by user name.
|
||||
// get_by_name returns the passwd database entry by user name.
|
||||
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||
@[deprecated: 'use user_by_name() instead']
|
||||
pub fn get_by_name(name string) !Passwd {
|
||||
pwdb_mutex.lock()
|
||||
return user_by_name(name)!
|
||||
}
|
||||
|
||||
// user_by_name returns the passwd database entry by user name.
|
||||
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||
pub fn user_by_name(name string) !Passwd {
|
||||
pwd_mutex.lock()
|
||||
defer {
|
||||
pwdb_mutex.unlock()
|
||||
pwd_mutex.unlock()
|
||||
}
|
||||
pw := C.getpwnam(&char(name.str))
|
||||
if isnil(pw) {
|
||||
@@ -103,16 +117,26 @@ pub fn get_by_name(name string) !Passwd {
|
||||
}
|
||||
|
||||
// get_all returns all entries from passwd database in arbitrary order.
|
||||
@[deprecated: 'use users() instead']
|
||||
pub fn get_all() []Passwd {
|
||||
return users() or { []Passwd{} }
|
||||
}
|
||||
|
||||
// users returns all entries from passwd database in arbitrary order.
|
||||
pub fn users() ![]Passwd {
|
||||
mut pwds := []Passwd{}
|
||||
pwdb_mutex.lock()
|
||||
pwd_mutex.lock()
|
||||
C.setpwent()
|
||||
defer {
|
||||
C.endpwent()
|
||||
pwdb_mutex.unlock()
|
||||
pwd_mutex.unlock()
|
||||
}
|
||||
for {
|
||||
pw := C.getpwent()
|
||||
err := make_error('', -1)
|
||||
if err.code() != 0 {
|
||||
return err
|
||||
}
|
||||
if isnil(pw) {
|
||||
break
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// vtest build: !windows
|
||||
import pwd
|
||||
|
||||
fn test_user_by_uid() {
|
||||
pw := pwd.user_by_uid(0)!
|
||||
assert pw.name == 'root'
|
||||
}
|
||||
|
||||
fn test_user_by_name() {
|
||||
pw := pwd.user_by_name('root')!
|
||||
assert pw.uid == 0
|
||||
}
|
||||
|
||||
fn test_users() {
|
||||
pws := pwd.users()!
|
||||
assert pws.len > 0
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
// vtest build: !windows
|
||||
import pwdb
|
||||
|
||||
fn test_get_by_uid() {
|
||||
pw := pwdb.get_by_uid(0)!
|
||||
assert pw.name == 'root'
|
||||
}
|
||||
|
||||
fn test_get_by_name() {
|
||||
pw := pwdb.get_by_name('root')!
|
||||
assert pw.uid == 0
|
||||
}
|
||||
|
||||
fn test_get_all() {
|
||||
pws := pwdb.get_all()
|
||||
assert pws.len > 0
|
||||
}
|
||||
Reference in New Issue
Block a user