Bug fix and authoring
[src/app-framework-main.git] / src / wgt.c
index dd889d5..db16129 100644 (file)
--- a/src/wgt.c
+++ b/src/wgt.c
@@ -1,6 +1,8 @@
 /*
  Copyright 2015 IoT.bzh
 
+ author: José Bollo <jose.bollo@iot.bzh>
+
  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
 #include "wgt.h"
 
 struct wgt {
+       int refcount;
        int rootfd;
        int nrlocales;
        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;
@@ -62,7 +66,8 @@ static int validsubpath(const char *subpath)
                default:
                        while(subpath[i] && subpath[i] != '/')
                                i++;
-                       l++;
+                       if (l >= 0)
+                               l++;
                case '/':
                        break;
                }
@@ -70,10 +75,20 @@ static int validsubpath(const char *subpath)
        return l >= 0;
 }
 
+static const char *normalsubpath(const char *subpath)
+{
+       while(*subpath == '/')
+               subpath++;
+       return validsubpath(subpath) ? subpath : NULL;
+}
+
 struct wgt *wgt_create()
 {
        struct wgt *wgt = malloc(sizeof * wgt);
-       if (wgt) {
+       if (!wgt)
+               errno = ENOMEM;
+       else {
+               wgt->refcount = 1;
                wgt->rootfd = -1;
                wgt->nrlocales = 0;
                wgt->locales = NULL;
@@ -96,9 +111,16 @@ void wgt_locales_reset(struct wgt *wgt)
                free(wgt->locales[--wgt->nrlocales]);
 }
 
-void wgt_destroy(struct wgt *wgt)
+void wgt_addref(struct wgt *wgt)
 {
-       if (wgt) {
+       assert(wgt);
+       wgt->refcount++;
+}
+
+void wgt_unref(struct wgt *wgt)
+{
+       assert(wgt);
+       if (!--wgt->refcount) {
                wgt_disconnect(wgt);
                wgt_locales_reset(wgt);
                free(wgt->locales);
@@ -106,24 +128,39 @@ void wgt_destroy(struct wgt *wgt)
        }
 }
 
-int wgt_connect(struct wgt *wgt, const char *pathname)
+int wgt_connectat(struct wgt *wgt, int dirfd, const char *pathname)
 {
        int rfd;
 
        assert(wgt);
 
-       rfd = AT_FDCWD;
-       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 0;
 }
 
+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);
@@ -134,7 +171,9 @@ int wgt_has(struct wgt *wgt, const char *filename)
 {
        assert(wgt);
        assert(wgt_is_connected(wgt));
-       if (!validsubpath(filename)) {
+
+       filename = normalsubpath(filename);
+       if (!filename) {
                errno = EINVAL;
                return -1;
        }
@@ -145,7 +184,8 @@ int wgt_open_read(struct wgt *wgt, const char *filename)
 {
        assert(wgt);
        assert(wgt_is_connected(wgt));
-       if (!validsubpath(filename)) {
+       filename = normalsubpath(filename);
+       if (!filename) {
                errno = EINVAL;
                return -1;
        }
@@ -213,7 +253,8 @@ static const char *localize(struct wgt *wgt, const char *filename, char path[PAT
 {
        int i;
 
-       if (!validsubpath(filename)) {
+       filename = normalsubpath(filename);
+       if (!filename) {
                errno = EINVAL;
                return NULL;
        }
@@ -266,3 +307,27 @@ int wgt_locales_open_read(struct wgt *wgt, const char *filename)
 }
 
 
+#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
+