char **locales;
};
+/* a valid subpath is a relative path not looking deeper than root using .. */
static int validsubpath(const char *subpath)
{
int l = 0, i = 0;
default:
while(subpath[i] && subpath[i] != '/')
i++;
- l++;
+ if (l >= 0)
+ l++;
case '/':
break;
}
assert(wgt);
- rfd = dirfd;
- if (pathname) {
- rfd = openat(rfd, pathname, O_PATH|O_DIRECTORY);
- if (rfd < 0)
- return rfd;
- }
+ rfd = (pathname && *pathname) ? openat(dirfd, pathname, O_PATH|O_DIRECTORY) : dup(dirfd);
+ if (rfd < 0)
+ return rfd;
+
if (wgt->rootfd >= 0)
close(wgt->rootfd);
wgt->rootfd = rfd;
return wgt_connectat(wgt, AT_FDCWD, pathname);
}
+struct wgt *wgt_createat(int dirfd, const char *pathname)
+{
+ struct wgt *wgt = wgt_create();
+ if (wgt) {
+ if (wgt_connectat(wgt, dirfd, pathname)) {
+ wgt_unref(wgt);
+ wgt = NULL;
+ }
+ }
+ return wgt;
+}
+
int wgt_is_connected(struct wgt *wgt)
{
assert(wgt);
}
+#if defined(TEST_wgt_validsubpath)
+#include <stdio.h>
+void t(const char *subpath, int validity) {
+ printf("%s -> %d = %d, %s\n", subpath, validity, validsubpath(subpath), validsubpath(subpath)==validity ? "ok" : "NOT OK");
+}
+int main() {
+ t("/",0);
+ t("..",0);
+ t(".",1);
+ t("../a",0);
+ t("a/..",1);
+ t("a/../////..",0);
+ t("a/../b/..",1);
+ t("a/b/c/..",1);
+ t("a/b/c/../..",1);
+ t("a/b/c/../../..",1);
+ t("a/b/c/../../../.",1);
+ t("./..a/././..b/..c/./.././.././../.",1);
+ t("./..a/././..b/..c/./.././.././.././..",0);
+ t("./..a//.//./..b/..c/./.././/./././///.././.././a/a/a/a/a",1);
+ return 0;
+}
+#endif
+