summaryrefslogtreecommitdiffstats
path: root/passwd.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-02-29 18:35:55 -0500
committerTavian Barnes <tavianator@tavianator.com>2020-02-29 18:52:04 -0500
commit1c184e718f5745cf3a53a3c389814a792e73aa51 (patch)
tree0959d07a8e4ac507aefa4867156d144ce2eff1dd /passwd.h
parent7fbf673ae2db33d6e47386bf3169d4b48f0fc8b4 (diff)
downloadbfs-1c184e718f5745cf3a53a3c389814a792e73aa51.tar.xz
passwd: Cache the user/group tables
This is a significant optimization for conditions that need these tables: Benchmark #1: ./bfs ~/code/linux -nouser >/dev/null Time (mean ± σ): 232.0 ms ± 2.5 ms [User: 44.3 ms, System: 185.0 ms] Range (min … max): 228.7 ms … 238.7 ms 12 runs Benchmark #2: ./bfs-1.6 ~/code/linux -nouser >/dev/null Time (mean ± σ): 1.050 s ± 0.012 s [User: 544.2 ms, System: 500.0 ms] Range (min … max): 1.025 s … 1.063 s 10 runs Benchmark #3: find ~/code/linux -nouser >/dev/null Time (mean ± σ): 1.040 s ± 0.012 s [User: 533.6 ms, System: 500.6 ms] Range (min … max): 1.017 s … 1.054 s 10 runs Summary './bfs ~/code/linux -nouser >/dev/null' ran 4.48 ± 0.07 times faster than 'find ~/code/linux -nouser >/dev/null' 4.52 ± 0.07 times faster than './bfs-1.6 ~/code/linux -nouser >/dev/null'
Diffstat (limited to 'passwd.h')
-rw-r--r--passwd.h117
1 files changed, 117 insertions, 0 deletions
diff --git a/passwd.h b/passwd.h
new file mode 100644
index 0000000..a0b8c6b
--- /dev/null
+++ b/passwd.h
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * bfs *
+ * Copyright (C) 2020 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * Permission to use, copy, modify, and/or distribute this software for any *
+ * purpose with or without fee is hereby granted. *
+ * *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF *
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR *
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES *
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN *
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
+ ****************************************************************************/
+
+/**
+ * A caching wrapper for /etc/{passwd,group}.
+ */
+
+#ifndef BFS_PASSWD_H
+#define BFS_PASSWD_H
+
+#include <grp.h>
+#include <pwd.h>
+
+/**
+ * The user table.
+ */
+struct bfs_users;
+
+/**
+ * Parse the user table.
+ *
+ * @return
+ * The parsed user table, or NULL on failure.
+ */
+struct bfs_users *bfs_parse_users(void);
+
+/**
+ * Get a user entry by name.
+ *
+ * @param users
+ * The user table.
+ * @param name
+ * The username to look up.
+ * @return
+ * The matching user, or NULL if not found.
+ */
+const struct passwd *bfs_getpwnam(const struct bfs_users *users, const char *name);
+
+/**
+ * Get a user entry by ID.
+ *
+ * @param users
+ * The user table.
+ * @param uid
+ * The ID to look up.
+ * @return
+ * The matching user, or NULL if not found.
+ */
+const struct passwd *bfs_getpwuid(const struct bfs_users *users, uid_t uid);
+
+/**
+ * Free a user table.
+ *
+ * @param users
+ * The user table to free.
+ */
+void bfs_free_users(struct bfs_users *users);
+
+/**
+ * The group table.
+ */
+struct bfs_groups;
+
+/**
+ * Parse the group table.
+ *
+ * @return
+ * The parsed group table, or NULL on failure.
+ */
+struct bfs_groups *bfs_parse_groups(void);
+
+/**
+ * Get a group entry by name.
+ *
+ * @param groups
+ * The group table.
+ * @param name
+ * The group name to look up.
+ * @return
+ * The matching group, or NULL if not found.
+ */
+const struct group *bfs_getgrnam(const struct bfs_groups *groups, const char *name);
+
+/**
+ * Get a group entry by ID.
+ *
+ * @param groups
+ * The group table.
+ * @param uid
+ * The ID to look up.
+ * @return
+ * The matching group, or NULL if not found.
+ */
+const struct group *bfs_getgrgid(const struct bfs_groups *groups, gid_t gid);
+
+/**
+ * Free a group table.
+ *
+ * @param groups
+ * The group table to free.
+ */
+void bfs_free_groups(struct bfs_groups *groups);
+
+#endif // BFS_PASSWD_H