Remove use of deprecated readdir_r
[src/app-framework-main.git] / src / afm-db.c
index 034617b..a15255b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- Copyright 2015 IoT.bzh
+ Copyright 2015, 2016, 2017 IoT.bzh
 
  author: José Bollo <jose.bollo@iot.bzh>
 
@@ -231,7 +231,7 @@ static int enumentries(struct enumdata *data, int (*callto)(struct enumdata *))
        DIR *dir;
        int rc;
        char *beg;
-       struct dirent entry, *e;
+       struct dirent *e;
        size_t len;
 
        /* opens the directory */
@@ -244,24 +244,28 @@ static int enumentries(struct enumdata *data, int (*callto)(struct enumdata *))
        *beg++ = '/';
 
        /* enumerate entries */
-       rc = readdir_r(dir, &entry, &e);
-       while (!rc && e) {
-               if (entry.d_name[0] != '.' || (entry.d_name[1]
-                       && (entry.d_name[1] != '.' || entry.d_name[2]))) {
+       for(;;) {
+               errno = 0;
+               e = readdir(dir);
+               if (!e) {
+                       rc = !errno - 1;
+                       break;
+               }
+               if (e->d_name[0] != '.' || (e->d_name[1]
+                       && (e->d_name[1] != '.' || e->d_name[2]))) {
                        /* prepare callto */
-                       len = strlen(entry.d_name);
+                       len = strlen(e->d_name);
                        if (beg + len >= data->path + sizeof data->path) {
                                errno = ENAMETOOLONG;
                                return -1;
                        }
-                       data->length = (int)(stpcpy(beg, entry.d_name)
+                       data->length = (int)(stpcpy(beg, e->d_name)
                                                                - data->path);
                        /* call the function */
                        rc = callto(data);
                        if (rc)
                                break;
                }       
-               rc = readdir_r(dir, &entry, &e);
        }
        closedir(dir);
        return rc;
@@ -482,10 +486,31 @@ struct json_object *afm_db_application_list(struct afm_db *afdb)
  */
 struct json_object *afm_db_get_application(struct afm_db *afdb, const char *id)
 {
+       int i;
        struct json_object *result;
-       if (!afm_db_ensure_applications(afdb) && json_object_object_get_ex(
-                               afdb->applications.direct, id, &result))
+
+       if (afm_db_ensure_applications(afdb))
+               return NULL;
+
+       /* search case sensitively */
+       if (json_object_object_get_ex( afdb->applications.direct, id, &result))
                return json_object_get(result);
+
+       /* fallback to a case insensitive search */
+       i = json_object_array_length(afdb->applications.pubarr);
+       while (i) {
+               result = json_object_array_get_idx(afdb->applications.pubarr, --i);
+               if (result
+                 && json_object_object_get_ex(result, "id", &result)
+                 && !strcasecmp(id, json_object_get_string(result))) {
+                       if (json_object_object_get_ex( afdb->applications.direct, 
+                                                      json_object_get_string(result),
+                                                      &result))
+                               return json_object_get(result);
+                       else
+                               return NULL;
+               }
+       }
        return NULL;
 }