]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: allow to close dirfd
authorKarel Zak <kzak@redhat.com>
Wed, 17 Oct 2018 11:18:25 +0000 (13:18 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 7 Dec 2018 11:32:58 +0000 (12:32 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/path.h
lib/path.c

index 7ab9779a574cce765caacb01596a3a5261dec628..b34aa366ef051df80fb15cc188dcde1908c23b83 100644 (file)
@@ -40,6 +40,8 @@ void *ul_path_get_dialect(struct path_cxt *pc);
 
 int ul_path_set_enoent_redirect(struct path_cxt *pc, int (*func)(struct path_cxt *, const char *, int *));
 int ul_path_get_dirfd(struct path_cxt *pc);
+void ul_path_close_dirfd(struct path_cxt *pc);
+int ul_path_isopen_dirfd(struct path_cxt *pc);
 
 char *ul_path_get_abspath(struct path_cxt *pc, char *buf, size_t bufsz, const char *path, ...)
                                __attribute__ ((__format__ (__printf__, 4, 5)));
index 9cc2e3e2eae523cd139a8cdf26a9599c046804a9..d36fe41e8d9f68d0c09a7168b842cf5b35fc2f3b 100644 (file)
@@ -94,8 +94,7 @@ void ul_unref_path(struct path_cxt *pc)
                DBG(CXT, ul_debugobj(pc, "dealloc"));
                if (pc->dialect)
                        pc->free_dialect(pc);
-               if (pc->dir_fd >= 0)
-                       close(pc->dir_fd);
+               ul_path_close_dirfd(pc);
                free(pc->dir_path);
                free(pc->prefix);
                free(pc);
@@ -212,6 +211,23 @@ int ul_path_get_dirfd(struct path_cxt *pc)
        return pc->dir_fd;
 }
 
+/* Note that next ul_path_get_dirfd() will reopen the directory */
+void ul_path_close_dirfd(struct path_cxt *pc)
+{
+       assert(pc);
+
+       if (pc->dir_fd >= 0) {
+               DBG(CXT, ul_debugobj(pc, "closing dir: '%s'", pc->dir_path));
+               close(pc->dir_fd);
+               pc->dir_fd = -1;
+       }
+}
+
+int ul_path_isopen_dirfd(struct path_cxt *pc)
+{
+       return pc && pc->dir_fd >= 0;
+}
+
 static const char *ul_path_mkpath(struct path_cxt *pc, const char *path, va_list ap)
 {
        int rc = vsnprintf(pc->path_buffer, sizeof(pc->path_buffer), path, ap);
@@ -301,7 +317,7 @@ int ul_path_open(struct path_cxt *pc, int flags, const char *path)
 
        if (!pc) {
                fd = open(path, flags);
-               DBG(CXT, ul_debug("opening '%s'", path));
+               DBG(CXT, ul_debug("opening '%s' [no context]", path));
        } else {
                int fdx;
                int dir = ul_path_get_dirfd(pc);