Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(std/fs): add support to parse home directory from /etc/passwd #23295

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jobansd
Copy link

@jobansd jobansd commented Mar 18, 2025

Implemented the TODO in get_app_data_dir.zig that suggests parsing the home directory from /etc/passwd.

Changes

  • Added getHomeDirFromPasswd() function, which implements the parsing logic.
  • Changed the return type of getAppDataDir() from GetAppDataDirError![]u8 to ![]u8 to encompass errors from fs.openFileAbsolute() and reader.read()
  • GetAppDataDirError retained in return type, updated the getHomeDirFromPasswd() function call to catch and return error.AppDataDirUnavailable instead (suggested by @squeek502).
  • Added declaration for getuid() to c.zig.
  • Added wrapper function for getuid() to posix.zig.
  • Changed std.os.linux.getuid() to std.posix.getuid() in get_app_data_dir.zig.

Implemented the TODO in `get_app_data_dir.zig` that suggests parsing the home directory from `/etc/passwd`.
@squeek502
Copy link
Collaborator

IMO the GetAppDataDirError error set should be retained, and the getHomeDirFromPasswd calls should be changed to

getHomeDirFromPasswd() catch return error.AppDataDirUnavailable

so something like

const home_dir = posix.getenv("HOME") orelse (getHomeDirFromPasswd() catch return error.AppDataDirUnavailable);

or

const home_dir = posix.getenv("HOME") orelse blk: {
    break :blk getHomeDirFromPasswd() catch return error.AppDataDirUnavailable;
};

Restored `GetAppDataDirError` error set in return type.
@jobansd
Copy link
Author

jobansd commented Mar 19, 2025

@squeek502, thanks for the suggestion, the change has been pushed.

@jobansd jobansd force-pushed the feature/read-home-dir-from-passwd branch from 0d2af2a to 53d2777 Compare March 19, 2025 16:44
jobansd added 2 commits March 19, 2025 13:59
Moved `std.os.linux.getuid()` call out of the while loop to reduce invocations and created variable `currentUid` to store the value.
Changed fields in ReaderState enum to snake_case.
Replaced `std.os.linux.getuid()` with `std.posix.getuid()` and added supporting changes to enhance portability of `getHomeDirFromPasswd()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants