Diff

Differences From Artifact [4ed6fa78a6]:

To Artifact [9a50e0345b]:


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
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




+














+
+

-

+







#define FUSE_USE_VERSION 26

#include <sys/types.h>
#include <sqlite3.h>
#include <pthread.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <fuse.h>
#include <tcl.h>

#define APPFS_CACHEDIR "/tmp/appfs-cache"

#define APPFS_DEBUG(x...) { fprintf(stderr, "[debug] %s:%i:%s: ", __FILE__, __LINE__, __func__); fprintf(stderr, x); fprintf(stderr, "\n"); }

static pthread_key_t interpKey;

struct appfs_thread_data {
	Tcl_Interp *interp;
	sqlite3 *db;
	const char *cachedir;
};

struct appfs_thread_data globalThread;

typedef enum {
	APPFS_OS_UNKNOWN,
	APPFS_OS_ALL,
163
164
165
166
167
168
169













































170
171
172
173
174
175
176
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







			return("arm");
		case APPFS_CPU_UNKNOWN:
			return("unknown");
	}

	return("unknown");
}

static Tcl_Interp *appfs_create_TclInterp(const char *cachedir) {
	Tcl_Interp *interp;
	int tcl_ret;

	interp = Tcl_CreateInterp();
	if (interp == NULL) {
		fprintf(stderr, "Unable to create Tcl Interpreter.  Aborting.\n");

		return(NULL);
	}

	tcl_ret = Tcl_Init(interp);
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl.  Aborting.\n");

		return(NULL);
	}

	tcl_ret = Tcl_Eval(interp, ""
#include "appfs.tcl.h"
	"");
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS script.  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

		return(NULL);
	}

	if (Tcl_SetVar(interp, "::appfs::cachedir", cachedir, TCL_GLOBAL_ONLY) == NULL) {
		fprintf(stderr, "Unable to set cache directory.  This should never fail.\n");

		return(NULL);
	}

	tcl_ret = Tcl_Eval(interp, "::appfs::init");
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS script (::appfs::init).  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

		return(NULL);
	}

	return(interp);
}

static int appfs_Tcl_Eval(Tcl_Interp *interp, int objc, const char *cmd, ...) {
	Tcl_Obj **objv;
	const char *arg;
	va_list argp;
	int retval;
	int i;
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
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







+
+
+
+



-
+
+


+
+
+
+
+
+
+
+
+
-
+

-
+

-
+


-
+



+


+
+
+
+

+
+
+
-
+

-
+




-
+




-
+
+


+
+
+
+
+
+
+
-
+

-
+

-
+


-
+








	for (i = 0; i < objc; i++) {
		Tcl_DecrRefCount(objv[i]);
	}

	ckfree((void *) objv);

	if (retval != TCL_OK) {
		APPFS_DEBUG("Tcl command failed, ::errorInfo contains: %s\n", Tcl_GetVar(interp, "::errorInfo", 0));
	}

	return(retval);
}

static int appfs_update_index(const char *hostname) {
static void appfs_update_index(const char *hostname) {
	Tcl_Interp *interp;
	int tcl_ret;

	APPFS_DEBUG("Enter: hostname = %s", hostname);

	interp = pthread_getspecific(interpKey);
	if (interp == NULL) {
		interp = appfs_create_TclInterp(globalThread.cachedir);

		pthread_setspecific(interpKey, interp);
	}

	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 2, "::appfs::getindex", hostname);
	tcl_ret = appfs_Tcl_Eval(interp, 2, "::appfs::getindex", hostname);
	if (tcl_ret != TCL_OK) {
		APPFS_DEBUG("Call to ::appfs::getindex failed: %s", Tcl_GetStringResult(globalThread.interp));
		APPFS_DEBUG("Call to ::appfs::getindex failed: %s", Tcl_GetStringResult(interp));

		return(-1);
		return;
	}

	return(0);
	return;
}

static const char *appfs_getfile(const char *hostname, const char *sha1) {
	Tcl_Interp *interp;
	char *retval;
	int tcl_ret;

	interp = pthread_getspecific(interpKey);
	if (interp == NULL) {
		interp = appfs_create_TclInterp(globalThread.cachedir);

		pthread_setspecific(interpKey, interp);
	}

	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 3, "::appfs::download", hostname, sha1);
	tcl_ret = appfs_Tcl_Eval(interp, 3, "::appfs::download", hostname, sha1);
	if (tcl_ret != TCL_OK) {
		APPFS_DEBUG("Call to ::appfs::download failed: %s", Tcl_GetStringResult(globalThread.interp));
		APPFS_DEBUG("Call to ::appfs::download failed: %s", Tcl_GetStringResult(interp));

		return(NULL);
	}

	retval = strdup(Tcl_GetStringResult(globalThread.interp));
	retval = strdup(Tcl_GetStringResult(interp));

	return(retval);
}

static int appfs_update_manifest(const char *hostname, const char *sha1) {
static void appfs_update_manifest(const char *hostname, const char *sha1) {
	Tcl_Interp *interp;
	int tcl_ret;

	interp = pthread_getspecific(interpKey);
	if (interp == NULL) {
		interp = appfs_create_TclInterp(globalThread.cachedir);

		pthread_setspecific(interpKey, interp);
	}

	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 3, "::appfs::getpkgmanifest", hostname, sha1);
	tcl_ret = appfs_Tcl_Eval(interp, 3, "::appfs::getpkgmanifest", hostname, sha1);
	if (tcl_ret != TCL_OK) {
		APPFS_DEBUG("Call to ::appfs::getpkgmanifest failed: %s", Tcl_GetStringResult(globalThread.interp));
		APPFS_DEBUG("Call to ::appfs::getpkgmanifest failed: %s", Tcl_GetStringResult(interp));

		return(-1);
		return;
	}

	return(0);
	return;
}


#define appfs_free_list_type(id, type) static void appfs_free_list_ ## id(type *head) { \
	type *obj, *next; \
	for (obj = head; obj; obj = next) { \
		next = obj->_next; \
324
325
326
327
328
329
330
331

332
333
334
335
336
337

338
339
340
341
342
343
344
345
346
347
402
403
404
405
406
407
408

409
410
411
412
413
414

415



416
417
418
419
420
421
422







-
+





-
+
-
-
-








	return(0);
}

static struct appfs_package *appfs_getindex(const char *hostname, int *package_count_p) {
	struct appfs_package *head = NULL;
	char *sql;
	int index_ret, sqlite_ret;
	int sqlite_ret;

	if (package_count_p == NULL) {
		return(NULL);
	}

	index_ret = appfs_update_index(hostname);
	appfs_update_index(hostname);
	if (index_ret != 0) {
		return(NULL);
	}

	sql = sqlite3_mprintf("SELECT package, version, sha1, os, cpuArch, isLatest FROM packages WHERE hostname = %Q;", hostname);
	if (sql == NULL) {
		APPFS_DEBUG("Call to sqlite3_mprintf failed.");

		return(NULL);
	}
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
458
459
460
461
462
463
464

465
466
467
468
469
470

471





472



473
474
475
476
477
478
479







-
+





-
+
-
-
-
-
-
+
-
-
-







	return(0);
	
}

static struct appfs_children *appfs_getchildren(const char *hostname, const char *package_hash, const char *path, int *children_count_p) {
	struct appfs_children *head = NULL;
	char *sql;
	int index_ret, sqlite_ret, manifest_ret;
	int sqlite_ret;

	if (children_count_p == NULL) {
		return(NULL);
	}

	index_ret = appfs_update_index(hostname);
	appfs_update_index(hostname);
	if (index_ret != 0) {
		return(NULL);
	}

	manifest_ret = appfs_update_manifest(hostname, package_hash);
	appfs_update_manifest(hostname, package_hash);
	if (manifest_ret != 0) {
		return(NULL);
	}

	sql = sqlite3_mprintf("SELECT file_name FROM files WHERE package_sha1 = %Q AND file_directory = %Q;", package_hash, path);
	if (sql == NULL) {
		APPFS_DEBUG("Call to sqlite3_mprintf failed.");

		return(NULL);
	}
434
435
436
437
438
439
440
441

442
443

444
445
446
447
448
449
450
451
452
453
502
503
504
505
506
507
508

509
510

511



512
513
514
515
516
517
518







-
+

-
+
-
-
-








	return(0);
}

static char *appfs_lookup_package_hash(const char *hostname, const char *package, appfs_os_t os, appfs_cpuArch_t cpuArch, const char *version) {
	char *sql;
	char *retval = NULL;
	int index_ret, sqlite_ret;
	int sqlite_ret;

	index_ret = appfs_update_index(hostname);
	appfs_update_index(hostname);
	if (index_ret != 0) {
		return(NULL);
	}

	sql = sqlite3_mprintf("SELECT sha1 FROM packages WHERE hostname = %Q AND package = %Q AND os = %Q AND cpuArch = %Q AND version = %Q;",
		hostname,
		package,
		appfs_convert_os_toString(os),
		appfs_convert_cpuArch_toString(cpuArch),
		version
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
550
551
588
589
590
591
592
593
594

595
596
597
598
599
600

601





602



603
604
605
606
607
608
609







-
+





-
+
-
-
-
-
-
+
-
-
-







	/* Until this is used, prevent the compiler from complaining */
	source = source;
}

static int appfs_getfileinfo(const char *hostname, const char *package_hash, const char *_path, struct appfs_pathinfo *pathinfo) {
	char *directory, *file, *path;
	char *sql;
	int index_ret, sqlite_ret, manifest_ret;
	int sqlite_ret;

	if (pathinfo == NULL) {
		return(-EIO);
	}

	index_ret = appfs_update_index(hostname);
	appfs_update_index(hostname);
	if (index_ret != 0) {
		return(-EIO);
	}

	manifest_ret = appfs_update_manifest(hostname, package_hash);
	appfs_update_manifest(hostname, package_hash);
	if (manifest_ret != 0) {
		return(-EIO);
	}

	path = strdup(_path);
	directory = path;
	file = strrchr(path, '/');
	if (file == NULL) {
		file = path;
		directory = "";
1006
1007
1008
1009
1010
1011
1012
1013

1014
1015
1016

1017
1018
1019
1020
1021

1022
1023
1024


1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1064
1065
1066
1067
1068
1069
1070

1071
1072


1073

1074



1075



1076
1077
























1078
1079
1080
1081
1082
1083
1084







-
+

-
-
+
-

-
-
-
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







	.read		= appfs_fuse_read
};
#endif

int main(int argc, char **argv) {
	const char *cachedir = APPFS_CACHEDIR;
	char dbfilename[1024];
	int tcl_ret, snprintf_ret, sqlite_ret;
	int pthread_ret, snprintf_ret, sqlite_ret;

	globalThread.interp = Tcl_CreateInterp();
	if (globalThread.interp == NULL) {
	globalThread.cachedir = cachedir;
		fprintf(stderr, "Unable to create Tcl Interpreter.  Aborting.\n");

		return(1);
	}

	pthread_ret = pthread_key_create(&interpKey, NULL);
	tcl_ret = Tcl_Init(globalThread.interp);
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl.  Aborting.\n");
	if (pthread_ret != 0) {
		fprintf(stderr, "Unable to create TSD key for Tcl.  Aborting.\n");

		return(1);
	}

	tcl_ret = Tcl_Eval(globalThread.interp, ""
#include "appfs.tcl.h"
	"");
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS script.  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(globalThread.interp));

		return(1);
	}

	if (Tcl_SetVar(globalThread.interp, "::appfs::cachedir", cachedir, TCL_GLOBAL_ONLY) == NULL) {
		fprintf(stderr, "Unable to set cache directory.  This should never fail.\n");

		return(1);
	}

	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 1, "::appfs::init");
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS script (::appfs::init).  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(globalThread.interp));

		return(1);
	}

	snprintf_ret = snprintf(dbfilename, sizeof(dbfilename), "%s/%s", cachedir, "cache.db");
	if (snprintf_ret >= sizeof(dbfilename)) {
		fprintf(stderr, "Unable to set database filename.  Aborting.\n");