char *ul_path_get_abspath(struct path_cxt *pc, char *buf, size_t bufsz, const char *path, ...)
__attribute__ ((__format__ (__printf__, 4, 5)));
+int ul_path_stat(struct path_cxt *pc, struct stat *sb, const char *path);
int ul_path_access(struct path_cxt *pc, int mode, const char *path);
int ul_path_accessf(struct path_cxt *pc, int mode, const char *path, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));
assert(pc);
if (pc->dir_fd >= 0) {
- DBG(CXT, ul_debugobj(pc, "closing dir: '%s'", pc->dir_path));
+ DBG(CXT, ul_debugobj(pc, "closing dir"));
close(pc->dir_fd);
pc->dir_fd = -1;
}
int ul_path_access(struct path_cxt *pc, int mode, const char *path)
{
- int dir, rc;
-
- dir = ul_path_get_dirfd(pc);
- if (dir < 0)
- return dir;
+ int rc;
- DBG(CXT, ul_debugobj(pc, "access: '%s'", path));
- rc = faccessat(dir, path, mode, 0);
+ if (!pc) {
+ rc = access(path, mode);
+ DBG(CXT, ul_debug("access '%s' [no context, rc=%d]", path, rc));
+ } else {
+ int dir = ul_path_get_dirfd(pc);
+ if (dir < 0)
+ return dir;
+ if (*path == '/')
+ path++;
- if (rc && errno == ENOENT
- && pc->redirect_on_enoent
- && pc->redirect_on_enoent(pc, path, &dir) == 0)
rc = faccessat(dir, path, mode, 0);
+
+ if (rc && errno == ENOENT
+ && pc->redirect_on_enoent
+ && pc->redirect_on_enoent(pc, path, &dir) == 0)
+ rc = faccessat(dir, path, mode, 0);
+
+ DBG(CXT, ul_debugobj(pc, "access: '%s' [rc=%d]", path, rc));
+ }
return rc;
}
return !p ? -errno : ul_path_access(pc, mode, p);
}
+int ul_path_stat(struct path_cxt *pc, struct stat *sb, const char *path)
+{
+ int rc;
+
+ if (!pc) {
+ rc = stat(path, sb);
+ DBG(CXT, ul_debug("stat '%s' [no context, rc=%d]", path, rc));
+ } else {
+ int dir = ul_path_get_dirfd(pc);
+ if (dir < 0)
+ return dir;
+ if (*path == '/')
+ path++;
+
+ rc = fstatat(dir, path, sb, 0);
+
+ if (rc && errno == ENOENT
+ && pc->redirect_on_enoent
+ && pc->redirect_on_enoent(pc, path, &dir) == 0)
+ rc = fstatat(dir, path, sb, 0);
+
+ DBG(CXT, ul_debugobj(pc, "stat '%s' [rc=%d]", path, rc));
+ }
+ return rc;
+}
+
int ul_path_open(struct path_cxt *pc, int flags, const char *path)
{
int fd;
if (dir < 0)
return dir;
+ if (*path == '/')
+ path++;
+
fdx = fd = openat(dir, path, flags);
if (fd < 0 && errno == ENOENT
if (dirfd < 0)
return dirfd;
+ if (*path == '/')
+ path++;
+
return readlinkat(dirfd, path, buf, bufsiz);
}