X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwgt.c;h=db16129dc05ece83f1bfec9b3ae8b8308c30a377;hb=997dbadce8ee4a3345b5a9074dcf60010c5781bc;hp=9c3cd6eee6468d515e30e891bd9b309427e8d82e;hpb=38cccfcf9cb02b5a470dd4de31c528e1d106a100;p=src%2Fapp-framework-main.git diff --git a/src/wgt.c b/src/wgt.c index 9c3cd6e..db16129 100644 --- a/src/wgt.c +++ b/src/wgt.c @@ -1,6 +1,8 @@ /* Copyright 2015 IoT.bzh + author: José Bollo + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -36,6 +38,7 @@ struct wgt { 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; @@ -63,7 +66,8 @@ static int validsubpath(const char *subpath) default: while(subpath[i] && subpath[i] != '/') i++; - l++; + if (l >= 0) + l++; case '/': break; } @@ -130,12 +134,10 @@ int wgt_connectat(struct wgt *wgt, int dirfd, const char *pathname) 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; @@ -147,6 +149,18 @@ int wgt_connect(struct wgt *wgt, const char *pathname) 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); @@ -293,3 +307,27 @@ int wgt_locales_open_read(struct wgt *wgt, const char *filename) } +#if defined(TEST_wgt_validsubpath) +#include +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 +