summaryrefslogtreecommitdiffstats
path: root/src/appfwk.c
blob: 6dc7182fbc8f1a709ffed77eaa5e65c61473399b (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
/*
 Copyright 2015 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 <wgt.h>

struct stringset {
	int count;
	char **strings;
};
 

struct appfwk {
	int refcount;
	struct stringset paths;
};

struct appfwk *appfwk_create()
{
	struct appfwk *appfwk = malloc(sizeof * appfwk);
	if (!appfwk)
		errno = ENOMEM;
	else {
		appfwk->refcount = 1;
		appfwk->paths.count = 0;
		appfwk->paths.strings = NULL;
	}
	return appfwk;
}

void appfwk_addref(struct appfwk *appfwk)
{
	assert(appfwk);
	appfwk->refcount++;
}

void appfwk_unref(struct appfwk *appfwk)
{
	assert(appfwk);
	if (!--appfwk->refcount) {
		while (appfwk->paths.count)
			free(appfwk->paths.strings[--appfwk->paths.count]);
		free(appfwk);
	}
}

int appfwk_add_root(struct appfwk *appfwk, const char *path)
{
	int i, n;
	char *r, **roots;
	assert(appfwk);
	r = realpath(path, NULL);
	if (!r)
		return -1;

	/* avoiding duplications */
	n = appfwk->paths.count;
	roots = appfwk->paths.strings;
	for (i = 0 ; i < n ; i++)
		if (!strcmp(r, roots[i])) {
			free(r);
			return 0;
		}

	/* add */
	roots = realloc(roots, (n + 1) * sizeof(roots[0]));
	if (!roots) {
		free(r);
		errno = ENOMEM;
		return -1;
	}
	roots[n++] = r;
	appfwk->paths.strings = roots;
	appfwk->paths.count = n;
	return 0;
}

struct aea {
	char *path;
	char *appver;
	char *ver;
	const char *root;
	const char *appid;
	const char *version;
	int (*callback)(struct wgt *wgt, void *data);
	void *data;
};

struct appfwk_enumerate_applications_context {
	const char *dirpath;
	const char *appid;
	const char *version;
	
	void *data;
};

inline int testd(int dirfd, struct dirent *e)
{
	return e->d_name[0] != '.' || (e->d_name[1] && (e->d_name[1] != '.' || e->d_name[2]));
}

#include <stdio.h>
static int aea3(int dirfd, struct aea *aea)
{
	printf("aea3 %s *** %s *** %s\n", aea->path, aea->appver, aea->ver);
	return 0;
}

static int aea2(int dirfd, struct aea *aea)
{
	DIR *dir;
	int rc, fd;
	struct dirent entry, *e;

	dir = fdopendir(dirfd);
	if (!dir)
		return -1;

	aea->version = entry.d_name;

	rc = readdir_r(dir, &entry, &e);
	while (!rc && e) {
		if (testd(dirfd, &entry)) {
			fd = openat(dirfd, entry.d_name, O_DIRECTORY|O_RDONLY);
			if (fd >= 0) {
				strcpy(aea->ver, entry.d_name);
				rc = aea3(fd, aea);			
				close(fd);
			}
		}	
		rc = readdir_r(dir, &entry, &e);
	}
	closedir(dir);
	return rc;
}

static int aea1(int dirfd, struct aea *aea)
{
	DIR *dir;
	int rc, fd;
	struct dirent entry, *e;

	dir = fdopendir(dirfd);
	if (!dir)
		return -1;

	aea->appid = entry.d_name;

	rc = readdir_r(dir, &entry, &e);
	while (!rc && e) {
		if (testd(dirfd, &entry)) {
			fd = openat(dirfd, entry.d_name, O_DIRECTORY|O_RDONLY);
			if (fd >= 0) {
				aea->ver = stpcpy(aea->appver, entry.d_name);
				*aea->ver++ = '/';
				rc = aea2(fd, aea);			
				close(fd);
			}
		}	
		rc = readdir_r(dir, &entry, &e);
	}
	closedir(dir);
	return rc;
}

int appfwk_enumerate_applications(struct appfwk *appfwk, int (*callback)(struct wgt *wgt, void *data), void *data)
{
	int rc, iroot, nroots;
	char **roots;
	int fd;
	char buffer[PATH_MAX];
	struct aea aea;

	aea.callback = callback;
	aea.data = data;
	aea.path = buffer;

	nroots = appfwk->paths.count;
	roots = appfwk->paths.strings;
	for (iroot = 0 ; iroot < nroots ; iroot++) {
		aea.root = roots[iroot];
		fd = openat(AT_FDCWD, aea.root, O_DIRECTORY|O_RDONLY);
		if (fd >= 0) {
			aea.appver = stpcpy(buffer, aea.root);
			*aea.appver++ = '/';
			rc = aea1(fd, &aea);
			close(fd);
		}
	}
	return 0;
}
/*
	struct wgt *wgt;
		wgt = wgt_create();
		if (!wgt)
			return -1;
		wgt_unref(wgt);

	rfd = AT_FDCWD;
	if (pathname) {
		rfd = openat(rfd, pathname, O_PATH|O_DIRECTORY);
*/