aboutsummaryrefslogtreecommitdiffstats
path: root/src/afm-db.c
blob: 8cf3b3799e46a7d06e31c70912828215c4274888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 Copyright 2015, 2016 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

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>

#include <json-c/json.h>

#include "utils-json.h"
#include "wgt-info.h"
#include "afm-db.h"

/*
 * The json object recorded by application widget
 * has the following contents:
 *  {
 *    id: STRING, the application identifier without version info
 *    path: STRING, the path of the root directory for the application
 *    content: STRING, the relative path to the entryu point of the application
 *    type: STRING, the mime type describing the type 'content'
 *    bindings: ARRAY, array of bindings
 *      [
 *        STRING, path to the binding
 *      ]
 *    public: OBJECT, public content describing the application widget
 *      {  
 *        id: STRING, the application identifier "idaver"
 *        version: STRING, the full version of the application
 *        width: INTEGER, the width indication or 0
 *        height: INTEGER, the height indication or 0
 *        name: STRING, the name of the application
 *        description: STRING, the description of the application
 *        shortname: STRING, the short name of the application
 *        author: STRING, the author of the application
 *      }
 */

/*
 * The structure afm_apps records the data about applications
 * for several accesses.
 */
struct afm_apps {
	struct json_object *pubarr; /* array of the public data of apps */
	struct json_object *direct; /* hash of applications by their "idaver" */
	struct json_object *byapp;  /* hash of applications by their id */
};

/*
 * Two types of directory are handled:
 *  - root directories: contains subdirectories appid/version
 *         containing the applications
 *  - application directories: it contains an application
 */
enum dir_type {
	type_root, /* type for root directory */
	type_app   /* type for application directory */
};

/*
 * Structure for recording a path to application(s)
 * in the list of directories.
 */
struct afm_db_dir {
	struct afm_db_dir *next; /* link to the next item of the list */
	enum dir_type type;      /* the type of the path */
	char path[1];            /* the path of the directory */
};

/*
 * The structure afm_db records the applications
 * for a set of directories recorded as a linked list
 */
struct afm_db {
	int refcount;               /* count of references to the structure */
	struct afm_db_dir *dirhead; /* first directory of the set */
	struct afm_db_dir *dirtail; /* last directory of the set */
	struct afm_apps applications; /* the data about applications */
};

/*
 * The structure enumdata records data used when enumerating
 * application directories of a root directory.
 */
struct enumdata {
	char path[PATH_MAX];   /* "current" computed path */
	int length;            /* length of path */
	struct afm_apps apps;  /* */
};

/*
 * Release the data of the afm_apps object 'apps'.
 */
static void apps_put(struct afm_apps *apps)
{
	json_object_put(apps->pubarr);
	json_object_put(apps->direct);
	json_object_put(apps->byapp);
}

/*
 * Adds the application widget 'desc' of the directory 'path' to the
 * afm_apps object 'apps'.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 */
static int addwgt(struct afm_apps *apps, const char *path,
						const struct wgt_desc *desc)
{
	const struct wgt_desc_feature *feat;
	struct json_object *priv, *pub, *bya, *plugs, *str;

	/* create the application structure */
	priv = json_object_new_object();
	if (!priv)
		return -1;

	pub = j_add_new_object(priv, "public");
	if (!pub)
		goto error;

	plugs = j_add_new_array(priv, "bindings");
	if (!plugs)
		goto error;

	if(!j_add_string(priv, "id", desc->id)
	|| !j_add_string(priv, "path", path)
	|| !j_add_string(priv, "content", desc->content_src)
	|| !j_add_string(priv, "type", desc->content_type)
	|| !j_add_string(pub, "id", desc->idaver)
	|| !j_add_string(pub, "version", desc->version)
	|| !j_add_integer(pub, "width", desc->width)
	|| !j_add_integer(pub, "height", desc->height)
	|| !j_add_string(pub, "name", desc->name)
	|| !j_add_string(pub, "description", desc->description)
	|| !j_add_string(pub, "shortname", desc->name_short)
	|| !j_add_string(pub, "author", desc->author))
		goto error;

	/* extract bindings from features */
	feat = desc->features;
	while (feat) {
		static const char prefix[] = FWK_PREFIX_BINDING;
		if (!memcmp(feat->name, prefix, sizeof prefix - 1)) {
			str = json_object_new_string (
					feat->name + sizeof prefix - 1);
			if (str == NULL)
				goto error;
			if (json_object_array_add(plugs, str)) {
				json_object_put(str);
				goto error;
			}
		}
		feat = feat->next;
	}

	/* record the application structure */
	if (!j_add(apps->direct, desc->idaver, priv))
		goto error;

	if (json_object_array_add(apps->pubarr, pub))
		goto error;
	json_object_get(pub);

	if (!json_object_object_get_ex(apps->byapp, desc->id, &bya)) {
		bya = j_add_new_object(apps->byapp, desc->id);
		if (!bya)
			goto error;
	}

	if (!j_add(bya, desc->version, priv))
		goto error;
	json_object_get(priv);
	return 0;

error:
	json_object_put(priv);
	return -1;
}

/*
 * Adds the application widget in the directory 'path' to the
 * afm_apps object 'apps'.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 */
static int addapp(struct afm_apps *apps, const char *path)
{
	int rc;
	struct wgt_info *info;

	/* connect to the widget */
	info = wgt_info_createat(AT_FDCWD, path, 0, 1, 0);
	if (info == NULL) {
		if (errno == ENOENT)
			return 0; /* silently ignore bad directories */
		return -1;
	}
	/* adds the widget */
	rc = addwgt(apps, path, wgt_info_desc(info));
	wgt_info_unref(info);
	return rc;
}

/*
 * Enumerate the directories designated by 'data' and call the
 * function 'callto' for each of them.
 */
static int enumentries(struct enumdata *data, int (*callto)(struct enumdata *))
{
	DIR *dir;
	int rc;
	char *beg;
	struct dirent entry, *e;
	size_t len;

	/* opens the directory */
	dir = opendir(data->path);
	if (!dir)
		return -1;

	/* prepare appending entry names */
	beg = data->path + data->length;
	*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]))) {
			/* prepare callto */
			len = strlen(entry.d_name);
			if (beg + len >= data->path + sizeof data->path) {
				errno = ENAMETOOLONG;
				return -1;
			}
			data->length = (int)(stpcpy(beg, entry.d_name)
								- data->path);
			/* call the function */
			rc = callto(data);
			if (rc)
				break;
		}	
		rc = readdir_r(dir, &entry, &e);
	}
	closedir(dir);
	return rc;
}

/*
 * called for each version directory.
 */
static int recordapp(struct enumdata *data)
{
	return addapp(&data->apps, data->path);
}

/*
 * called for each application directory.
 * enumerate directories of the existing versions.
 */
static int enumvers(struct enumdata *data)
{
	int rc = enumentries(data, recordapp);
	return !rc || errno != ENOTDIR ? 0 : rc;
}

/*
 * Adds the directory of 'path' and 'type' to the afm_db object 'afdb'.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 * Possible errno values: ENOMEM, ENAMETOOLONG
 */
static int add_dir(struct afm_db *afdb, const char *path, enum dir_type type)
{
	struct afm_db_dir *dir;
	size_t len;

	assert(afdb);

	/* check size */
	len = strlen(path);
	if (len >= PATH_MAX) {
		errno = ENAMETOOLONG;
		return -1;
	}

	/* avoiding duplications */
	dir = afdb->dirhead;
	while(dir != NULL && (strcmp(dir->path, path) || dir->type != type))
		dir = dir ->next;
	if (dir != NULL)
		return 0;

	/* allocates the structure */
	dir = malloc(strlen(path) + sizeof * dir);
	if (dir == NULL) {
		errno = ENOMEM;
		return -1;
	}

	/* add it at tail */
	dir->next = NULL;
	dir->type = type;
	strcpy(dir->path, path);
	if (afdb->dirtail == NULL)
		afdb->dirhead = dir;
	else
		afdb->dirtail->next = dir;
	afdb->dirtail = dir;
	return 0;
}

/*
 * Creates an afm_db object and returns it with one reference added.
 * Return NULL with errno = ENOMEM if memory exhausted.
 */
struct afm_db *afm_db_create()
{
	struct afm_db *afdb = malloc(sizeof * afdb);
	if (afdb == NULL)
		errno = ENOMEM;
	else {
		afdb->refcount = 1;
		afdb->dirhead = NULL;
		afdb->dirtail = NULL;
		afdb->applications.pubarr = NULL;
		afdb->applications.direct = NULL;
		afdb->applications.byapp = NULL;
	}
	return afdb;
}

/*
 * Adds a reference to an existing afm_db.
 */
void afm_db_addref(struct afm_db *afdb)
{
	assert(afdb);
	afdb->refcount++;
}

/*
 * Removes a reference to an existing afm_db object.
 * Removes the objet if there no more reference to it.
 */
void afm_db_unref(struct afm_db *afdb)
{
	struct afm_db_dir *dir;

	assert(afdb);
	if (!--afdb->refcount) {
		/* no more reference, clean the memory used by the object */
		apps_put(&afdb->applications);
		while (afdb->dirhead != NULL) {
			dir = afdb->dirhead;
			afdb->dirhead = dir->next;
			free(dir);
		}
		free(afdb);
	}
}

/*
 * Adds the root directory of 'path' to the afm_db object 'afdb'.
 * Be aware that no check is done on the directory of 'path' that will
 * only be used within calls to the function 'afm_db_update_applications'.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 * Possible errno values: ENOMEM, ENAMETOOLONG
 */
int afm_db_add_root(struct afm_db *afdb, const char *path)
{
	return add_dir(afdb, path, type_root);
}

/*
 * Adds the application directory of 'path' to the afm_db object 'afdb'.
 * Be aware that no check is done on the directory of 'path' that will
 * only be used within calls to the function 'afm_db_update_applications'.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 * Possible errno values: ENOMEM, ENAMETOOLONG
 */
int afm_db_add_application(struct afm_db *afdb, const char *path)
{
	return add_dir(afdb, path, type_app);
}

/*
 * Regenerate the list of applications of the afm_bd object 'afdb'.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 */
int afm_db_update_applications(struct afm_db *afdb)
{
	int rc;
	struct enumdata edata;
	struct afm_apps oldapps;
	struct afm_db_dir *dir;

	/* create the result */
	edata.apps.pubarr = json_object_new_array();
	edata.apps.direct = json_object_new_object();
	edata.apps.byapp = json_object_new_object();
	if (edata.apps.pubarr == NULL || edata.apps.direct == NULL
						|| edata.apps.byapp == NULL) {
		errno = ENOMEM;
		goto error;
	}
	/* for each directory of afdb */
	for (dir = afdb->dirhead ; dir != NULL ; dir = dir->next) {
		if (dir->type == type_root) {
			edata.length = (int)(stpcpy(edata.path, dir->path)
								- edata.path);
			assert(edata.length < (int)sizeof edata.path);
			/* enumerate the applications */
			rc = enumentries(&edata, enumvers);
			if (rc)
				goto error;
		} else {
			rc = addapp(&edata.apps, dir->path);
		}
	}
	/* commit the result */
	oldapps = afdb->applications;
	afdb->applications = edata.apps;
	apps_put(&oldapps);
	return 0;

error:
	apps_put(&edata.apps);
	return -1;
}

/*
 * Ensure that applications of the afm_bd object 'afdb' are listed.
 * Returns 0 in case of success.
 * Returns -1 and set errno in case of error
 */
int afm_db_ensure_applications(struct afm_db *afdb)
{
	return afdb->applications.pubarr ? 0 : afm_db_update_applications(afdb);
}

/*
 * Get the list of the applications public data of the afm_db object 'afdb'.
 * The list is returned as a JSON-array that must be released using
 * 'json_object_put'.
 * Returns NULL in case of error.
 */
struct json_object *afm_db_application_list(struct afm_db *afdb)
{
	return afm_db_ensure_applications(afdb) ? NULL
			: json_object_get(afdb->applications.pubarr);
}

/*
 * Get the private data of the applications of 'id' in the afm_db object 'afdb'.
 * It returns a JSON-object that must be released using 'json_object_put'.
 * Returns NULL in case of error.
 */
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))
		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;
}

/*
 * Get the public data of the applications of 'id' in the afm_db object 'afdb'.
 * It returns a JSON-object that must be released using 'json_object_put'.
 * Returns NULL in case of error.
 */
struct json_object *afm_db_get_application_public(struct afm_db *afdb,
							const char *id)
{
	struct json_object *result;
	struct json_object *priv = afm_db_get_application(afdb, id);
	if (priv == NULL)
		return NULL;
	if (json_object_object_get_ex(priv, "public", &result))
		json_object_get(result);
	else
		result = NULL;
	json_object_put(priv);
	return result;
}




#if defined(TESTAPPFWK)
#include <stdio.h>
int main()
{
struct afm_db *afdb = afm_db_create();
afm_db_add_root(afdb,FWK_APP_DIR);
afm_db_update_applications(afdb);
printf("array = %s\n", json_object_to_json_string_ext(afdb->applications.pubarr, 3));
printf("direct = %s\n", json_object_to_json_string_ext(afdb->applications.direct, 3));
printf("byapp = %s\n", json_object_to_json_string_ext(afdb->applications.byapp, 3));
return 0;
}
#endif