Compare commits

..
3 Commits
Author SHA1 Message Date
ge 5fc58acaef remove deprecations, minor changes 2026-10-30 02:48:41 +03:00
ge bdaaaaae58 all: rename functions, deprecate old ones
Rename get_by_* to user_by_* and get_all to users. users() now can return errors.
2026-03-18 15:46:31 +03:00
ge 326d93714a Revert "all: rename module to pwdb"
This reverts commit 2d9ec0bf59.

The name `pwd` is still better, since it matches the name of the corresponding C header file.
2026-03-18 15:32:52 +03:00
5 changed files with 43 additions and 39 deletions

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
# Access to the UNIX Password Database # 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. database.
See [passwd(5)](https://man7.org/linux/man-pages/man5/passwd.5.html), See [passwd(5)](https://man7.org/linux/man-pages/man5/passwd.5.html),
+23 -19
View File
@@ -1,24 +1,24 @@
@[has_globals] @[has_globals]
module pwdb module pwd
import sync import sync
$if windows { $if windows {
$compile_error('pwdb: MS Windows is not supported') $compile_error('pwd: MS Windows is not supported')
} }
#include <errno.h> #include <errno.h>
#include <pwd.h> #include <pwd.h>
__global pwdb_mutex &sync.Mutex __global pwd__mutex__ &sync.Mutex
fn init() { fn init() {
pwdb_mutex = sync.new_mutex() pwd__mutex__ = sync.new_mutex()
} }
fn cleanup() { fn cleanup() {
pwdb_mutex.destroy() pwd__mutex__.destroy()
unsafe { free(pwdb_mutex) } unsafe { free(pwd__mutex__) }
} }
struct C.passwd { struct C.passwd {
@@ -74,26 +74,26 @@ fn make_error(name string, uid int) IError {
return error_with_code(c_error_number_str(err), err) return error_with_code(c_error_number_str(err), err)
} }
// get_by_uid returns the passwd database entry by user ID. // user_by_uid returns the passwd database entry by user ID.
// If the entry is not found, the EntryNotFoundError error will be returned. // If the entry is not found, the EntryNotFoundError error will be returned.
pub fn get_by_uid(uid int) !Passwd { pub fn user_by_uid(uid int) !Passwd {
pwdb_mutex.lock() pwd__mutex__.lock()
defer { defer {
pwdb_mutex.unlock() pwd__mutex__.unlock()
} }
pw := C.getpwuid(uid) pw := C.getpwuid(i32(uid))
if isnil(pw) { if isnil(pw) {
return make_error('', uid) return make_error('', uid)
} }
return make_passwd(pw) return make_passwd(pw)
} }
// get_by_uid returns the passwd database entry by user name. // user_by_name returns the passwd database entry by user name.
// If the entry is not found, the EntryNotFoundError error will be returned. // If the entry is not found, the EntryNotFoundError error will be returned.
pub fn get_by_name(name string) !Passwd { pub fn user_by_name(name string) !Passwd {
pwdb_mutex.lock() pwd__mutex__.lock()
defer { defer {
pwdb_mutex.unlock() pwd__mutex__.unlock()
} }
pw := C.getpwnam(&char(name.str)) pw := C.getpwnam(&char(name.str))
if isnil(pw) { if isnil(pw) {
@@ -102,17 +102,21 @@ pub fn get_by_name(name string) !Passwd {
return make_passwd(pw) return make_passwd(pw)
} }
// get_all returns all entries from passwd database in arbitrary order. // users returns all entries from passwd database in arbitrary order.
pub fn get_all() []Passwd { pub fn users() ![]Passwd {
mut pwds := []Passwd{} mut pwds := []Passwd{}
pwdb_mutex.lock() pwd__mutex__.lock()
C.setpwent() C.setpwent()
defer { defer {
C.endpwent() C.endpwent()
pwdb_mutex.unlock() pwd__mutex__.unlock()
} }
for { for {
pw := C.getpwent() pw := C.getpwent()
err := make_error('', -1)
if err.code() != 0 {
return err
}
if isnil(pw) { if isnil(pw) {
break break
} }
+17
View File
@@ -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
View File
@@ -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
}
+2 -2
View File
@@ -1,7 +1,7 @@
Module { Module {
name: 'pwdb' name: 'pwd'
description: 'Access to the UNIX password database' description: 'Access to the UNIX password database'
version: '0.1.0' version: '0.3.0'
license: 'Unlicense' license: 'Unlicense'
dependencies: [] dependencies: []
} }