fd.c: Introduce para_chdir().
authorAndre Noll <maan@systemlinux.org>
Mon, 1 Oct 2007 21:30:33 +0000 (23:30 +0200)
committerAndre Noll <maan@systemlinux.org>
Mon, 1 Oct 2007 21:30:33 +0000 (23:30 +0200)
Also, fix a bug in para_open() which would return -1 instead of
a proper error code.

fd.c

diff --git a/fd.c b/fd.c
index 398bf2ce818345a6cc3d98648953fa05096a8b48..e5aa59f59382967ffd9c54e7c7a4199246bde1f1 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -194,7 +194,29 @@ int para_open(const char *path, int flags, mode_t mode)
                break;
        };
        PARA_ERROR_LOG("failed to open %s: %s\n", path, strerror(errno));
                break;
        };
        PARA_ERROR_LOG("failed to open %s: %s\n", path, strerror(errno));
-       return ret;
+       return -E_OPEN;
+}
+
+/**
+ * Wrapper for chdir(2).
+ *
+ * \param path the specified directory.
+ *
+ * \return Positive on success, negative on errors.
+ */
+int para_chdir(const char *path)
+{
+       int ret = chdir(path);
+
+       if (ret >= 0)
+               return 1;
+       switch (errno) {
+       case ENOENT:
+               return -E_NOENT;
+       case EACCES:
+               return -E_CHDIR_PERM;
+       };
+       return -E_CHDIR;
 }
 
 /**
 }
 
 /**
@@ -231,8 +253,8 @@ int para_opendir(const char *dirname, DIR **dir, int *cwd)
                        return ret;
                *cwd = ret;
        }
                        return ret;
                *cwd = ret;
        }
-       ret = -E_CHDIR;
-       if (chdir(dirname) < 0)
+       ret = para_chdir(dirname);
+       if (ret < 0)
                goto close_cwd;
        ret = -E_OPENDIR;
        *dir = opendir(".");
                goto close_cwd;
        ret = -E_OPENDIR;
        *dir = opendir(".");