Code

Automatically prefix all log messages with the plugin name (if available).
[sysdb.git] / src / utils / os.c
index e975f96e743b6510102426d6450dbe9270690e1c..653d88d05585ffe217343e1e2a1adbbea04296d6 100644 (file)
 #include <errno.h>
 
 #include <sys/types.h>
+#include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
 
 #include <dirent.h>
 
@@ -48,6 +51,7 @@
 #include <libgen.h>
 #include <netdb.h>
 #include <pwd.h>
+#include <time.h>
 
 /*
  * public API
@@ -74,7 +78,7 @@ sdb_get_homedir(void)
 
        if (status || (! result)) {
                char errbuf[1024];
-               sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory "
+               sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
                                "for user %s: %s", username,
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                free(username);
@@ -84,6 +88,29 @@ sdb_get_homedir(void)
        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)
 {
@@ -145,26 +172,25 @@ sdb_remove_all(const char *pathname)
                        return -1;
 
                while (42) {
-                       struct dirent de;
-                       struct dirent *res = NULL;
+                       struct dirent *de;
+                       char filename[strlen(pathname) + sizeof(de->d_name) + 2];
 
-                       char filename[strlen(pathname) + sizeof(de.d_name) + 2];
+                       errno = 0;
+                       de = readdir(d);
+                       if (! de) {
+                               if (errno == 0)
+                                       break;
 
-                       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'))))
+                       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);
+                                       "%s/%s", pathname, de->d_name);
                        if (sdb_remove_all(filename)) {
                                closedir(d);
                                return -1;
@@ -296,7 +322,7 @@ sdb_resolve(int network, const char *address, struct addrinfo **res)
 
        if (address) {
                host = address;
-               port = strchr(host, ':');
+               port = strrchr(host, ':');
                if (port) {
                        *port = '\0';
                        ++port;