diff --git a/src/utils/os.c b/src/utils/os.c
index b1e143a6a651d140e0ee10ce36fdff7f05da421b..2b6a51460a67f89122dbee5ec7d75d70e2b3fe82 100644 (file)
--- a/src/utils/os.c
+++ b/src/utils/os.c
#endif /* HAVE_CONFIG_H */
#include "utils/os.h"
+#include "utils/error.h"
#include <errno.h>
#include <sys/types.h>
+#include <sys/socket.h>
#include <sys/stat.h>
+#include <dirent.h>
+
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
+#include <netdb.h>
+#include <pwd.h>
/*
- * private helper functions
+ * public API
*/
-/*
- * Recursively create the directory 'pathname' using the specified 'mode'. If
- * 'enforce_mode' is true, the mode will be enforced even if the directory
- * exists already.
- */
-static int
-mkdir_rec(const char *pathname, mode_t mode, _Bool enforce_mode)
+char *
+sdb_get_homedir(void)
+{
+ char *username = sdb_get_current_user();
+
+ struct passwd pw_entry;
+ struct passwd *result = NULL;
+
+ char buf[4096];
+
+ int status;
+
+ if (username) {
+ memset(&pw_entry, 0, sizeof(pw_entry));
+ status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
+ }
+ else
+ status = -1;
+
+ if (status || (! result)) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory "
+ "for user %s: %s", username,
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ free(username);
+ return NULL;
+ }
+ free(username);
+ return strdup(result->pw_dir);
+} /* sdb_get_homedir */
+
+char *
+sdb_realpath(const char *path)
+{
+ if (! path)
+ return NULL;
+
+ if ((strlen(path) >= 2) && (path[0] == '~') && (path[1] == '/')) {
+ char *homedir = sdb_get_homedir();
+ char tmp[(homedir ? strlen(homedir) : 0) + strlen(path)];
+ char *ret;
+
+ if (! homedir)
+ return NULL;
+
+ snprintf(tmp, sizeof(tmp), "%s/%s", homedir, path + 2);
+ ret = realpath(tmp, NULL);
+ free(homedir);
+ return ret;
+ }
+
+ return realpath(path, NULL);
+} /* sdb_realpath */
+
+int
+sdb_mkdir_all(const char *pathname, mode_t mode)
{
struct stat st;
+ char *pathname_copy;
char *base_dir;
int status = 0;
errno = ENOTDIR;
return -1;
}
-
- if ((st.st_mode != mode) && enforce_mode)
- return chmod(pathname, mode);
return 0;
}
/* pathname exists but we cannot access it */
return -1;
- base_dir = strdup(pathname);
- if (! base_dir)
+ pathname_copy = strdup(pathname);
+ if (! pathname_copy)
return -1;
- base_dir = dirname(base_dir);
+ base_dir = dirname(pathname_copy);
- /* don't enforce the mode on parent directories */
- status = mkdir_rec(base_dir, mode, 0);
+ status = sdb_mkdir_all(base_dir, mode);
if (! status)
status = mkdir(pathname, mode);
- free(base_dir);
+ free(pathname_copy);
return status;
-} /* mkdir_rec */
+} /* sdb_mkdir_all */
-/*
- * public API
- */
+int
+sdb_remove_all(const char *pathname)
+{
+ struct stat st;
+
+ if ((! pathname) || (! *pathname)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ memset(&st, 0, sizeof(st));
+ if (stat(pathname, &st))
+ return -1;
+
+ if (S_ISDIR(st.st_mode)) {
+ DIR *d = opendir(pathname);
+
+ if (! d)
+ return -1;
+
+ while (42) {
+ struct dirent de;
+ struct dirent *res = NULL;
+
+ char filename[strlen(pathname) + sizeof(de.d_name) + 2];
+
+ memset(&de, 0, sizeof(de));
+ if (readdir_r(d, &de, &res)) {
+ closedir(d);
+ return -1;
+ }
+
+ if (! res)
+ break;
+
+ if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
+ || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
+ continue;
+
+ snprintf(filename, sizeof(filename),
+ "%s/%s", pathname, de.d_name);
+ if (sdb_remove_all(filename)) {
+ closedir(d);
+ return -1;
+ }
+ }
+ closedir(d);
+ }
+ return remove(pathname);
+} /* sdb_remove_all */
+
+char *
+sdb_get_current_user(void)
+{
+ struct passwd pw_entry;
+ struct passwd *result = NULL;
+
+ uid_t uid;
+
+ char buf[4096];
+ int status;
+
+ uid = geteuid();
+ memset(&pw_entry, 0, sizeof(pw_entry));
+ status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
+
+ if (status || (! result)) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ return NULL;
+ }
+ return strdup(result->pw_name);
+} /* sdb_get_current_user */
int
-sdb_mkdir_all(const char *pathname, mode_t mode)
+sdb_select(int fd, int type)
{
- return mkdir_rec(pathname, mode, 1);
-} /* sdb_mkdir_all */
+ fd_set fds;
+ fd_set *readfds = NULL;
+ fd_set *writefds = NULL;
+ fd_set *exceptfds = NULL;
+
+ if (fd < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ FD_ZERO(&fds);
+
+ switch (type) {
+ case SDB_SELECTIN:
+ readfds = &fds;
+ break;
+ case SDB_SELECTOUT:
+ writefds = &fds;
+ break;
+ case SDB_SELECTERR:
+ exceptfds = &fds;
+ break;
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+
+ FD_SET(fd, &fds);
+
+ while (42) {
+ int n;
+ errno = 0;
+ n = select(fd + 1, readfds, writefds, exceptfds, NULL);
+
+ if ((n < 0) && (errno != EINTR))
+ return n;
+ if (n > 0)
+ break;
+ }
+ return 0;
+} /* sdb_select */
+
+ssize_t
+sdb_write(int fd, size_t msg_len, const void *msg)
+{
+ const char *buf;
+ size_t len;
+
+ if ((fd < 0) || (msg_len && (! msg)))
+ return -1;
+ if (! msg_len)
+ return 0;
+
+ buf = msg;
+ len = msg_len;
+ while (len > 0) {
+ ssize_t status;
+
+ if (sdb_select(fd, SDB_SELECTOUT))
+ return -1;
+
+ errno = 0;
+ status = write(fd, buf, len);
+ if (status < 0) {
+ if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
+ continue;
+ if (errno == EINTR)
+ continue;
+
+ return status;
+ }
+
+ len -= (size_t)status;
+ buf += status;
+ }
+
+ return (ssize_t)msg_len;
+} /* sdb_write */
+
+int
+sdb_resolve(int network, const char *address, struct addrinfo **res)
+{
+ struct addrinfo ai_hints;
+ const char *host;
+ char *port;
+ int status;
+
+ if (! res) {
+ errno = EINVAL;
+ return EAI_SYSTEM;
+ }
+
+ if (address) {
+ host = address;
+ port = strrchr(host, ':');
+ if (port) {
+ *port = '\0';
+ ++port;
+ }
+ if (! *host)
+ host = NULL;
+ }
+ else {
+ host = NULL;
+ port = NULL;
+ }
+
+ memset(&ai_hints, 0, sizeof(ai_hints));
+ ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
+ if (network & SDB_NET_V4)
+ ai_hints.ai_family = AF_INET;
+ else if (network & SDB_NET_V6)
+ ai_hints.ai_family = AF_INET6;
+ else
+ ai_hints.ai_family = AF_UNSPEC;
+
+ if ((network & SDB_NET_IP) == SDB_NET_IP) {
+ ai_hints.ai_socktype = 0;
+ ai_hints.ai_protocol = 0;
+ }
+ else if (network & SDB_NET_TCP) {
+ ai_hints.ai_socktype = SOCK_STREAM;
+ ai_hints.ai_protocol = IPPROTO_TCP;
+ }
+ else if (network & SDB_NET_UDP) {
+ ai_hints.ai_socktype = SOCK_DGRAM;
+ ai_hints.ai_protocol = IPPROTO_UDP;
+ }
+
+ status = getaddrinfo(host, port, &ai_hints, res);
+ if (port) {
+ --port;
+ *port = ':';
+ }
+ return status;
+} /* sdb_resolve */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */