-
Notifications
You must be signed in to change notification settings - Fork 0
/
ziptools.c
647 lines (577 loc) · 19.7 KB
/
ziptools.c
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* -*- Mode: C; Character-encoding: utf-8; -*- */
/* Copyright (C) 2007-2019 beingmeta, inc.
Copyright (C) 2020-2022 beingmeta, LLC
This file is part of beingmeta's Kno platform and is copyright
and a valuable trade secret of beingmeta, inc.
*/
#ifndef _FILEINFO
#define _FILEINFO __FILE__
#endif
#include "kno/knosource.h"
#include "kno/lisp.h"
#include "kno/apply.h"
#include "kno/eval.h"
#include "kno/storage.h"
#include "kno/pools.h"
#include "kno/indexes.h"
#include "kno/frames.h"
#include "kno/numbers.h"
#include "kno/cprims.h"
#include <libu8/libu8io.h>
#include <libu8/u8pathfns.h>
#include <libu8/u8filefns.h>
#include <sys/types.h>
#include <zip.h>
static u8_condition ZipFileError=_("Zip file error");
KNO_EXPORT kno_lisp_type kno_zipfile_type;
kno_lisp_type kno_zipfile_type;
#define KNO_ZIPFILE_TYPE 0x4640ce0L
typedef struct KNO_ZIPFILE {
KNO_CONS_HEADER;
u8_string filename; int flags;
u8_mutex zipfile_lock; int closed;
struct zip *zip;} KNO_ZIPFILE;
typedef struct KNO_ZIPFILE *kno_zipfile;
/* Error messages */
static lispval znumerr(u8_context cxt,int zerrno,u8_string path)
{
u8_byte buf[1024];
zip_error_to_str(buf,1024,errno,zerrno);
return kno_err(ZipFileError,cxt,
u8_mkstring("(%s) %s",path,buf),
KNO_VOID);
}
static lispval ziperr(u8_context cxt,kno_zipfile zf,lispval irritant)
{
u8_string details=
u8_mkstring("(%s) %s",zf->filename,zip_strerror(zf->zip));
kno_seterr(ZipFileError,cxt,details,kno_incref(irritant));
return KNO_ERROR_VALUE;
}
static lispval zfilerr(u8_context cxt,kno_zipfile zf,struct zip_file *zfile,
lispval irritant)
{
u8_string details=
u8_mkstring("(%s) %s",zf->filename,zip_file_strerror(zfile));
zip_fclose(zfile);
return kno_err(ZipFileError,cxt,details,irritant);
}
/* Zip file utilities */
static void recycle_zipfile(struct KNO_RAW_CONS *c)
{
struct KNO_ZIPFILE *zf = (struct KNO_ZIPFILE *)c;
if (!(zf->closed)) zip_close(zf->zip);
zf->closed = 1;
u8_destroy_mutex(&(zf->zipfile_lock));
u8_free(zf->filename);
if (!(KNO_STATIC_CONSP(c))) u8_free(c);
}
static int unparse_zipfile(struct U8_OUTPUT *out,lispval x)
{
struct KNO_ZIPFILE *zf = (struct KNO_ZIPFILE *)x;
u8_printf(out,"#<ZIPFILE '%s'%s>",
zf->filename,((zf->closed)?(" closed"):""));
return 1;
}
static lispval zipreopen(struct KNO_ZIPFILE *zf,int locked)
{
if (!(zf->closed)) return KNO_FALSE;
else {
int errflag;
struct zip *zip;
if (!(locked)) u8_lock_mutex(&(zf->zipfile_lock));
if (!(zf->closed)) {
if (!(locked)) u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else zip = zip_open(zf->filename,zf->flags,&errflag);
if (!(zip)) {
lispval errval = znumerr("zipreopen",errflag,zf->filename);
if (!(locked)) u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}
else {
zf->zip = zip; zf->closed = 0;
if (!(locked)) u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_TRUE;}}
}
DEFC_PRIM("zipfile?",iszipfile_prim,
KNO_MAX_ARGS(1)|KNO_MIN_ARGS(1),
"**undocumented**",
{"arg",kno_any_type,KNO_VOID})
static lispval iszipfile_prim(lispval arg)
{
if (KNO_TYPEP(arg,kno_zipfile_type)) return KNO_TRUE;
else return KNO_FALSE;
}
/* Creating/opening zip files */
static lispval zipopen(u8_string path,int zflags,int oflags)
{
int errflag = 0, flags = zflags|oflags;
u8_string abspath = u8_abspath(path,NULL);
if ((!(flags&ZIP_CREATE))&&(!(u8_file_existsp(abspath)))) {
kno_seterr(kno_FileNotFound,"zipopen",abspath,KNO_VOID);
U8_CLEAR_ERRNO();
return KNO_ERROR_VALUE;}
else {
struct zip *zip = zip_open(abspath,flags,&errflag);
if (zip) {
struct KNO_ZIPFILE *zf = u8_alloc(struct KNO_ZIPFILE);
KNO_INIT_FRESH_CONS(zf,kno_zipfile_type);
u8_init_mutex(&(zf->zipfile_lock));
zf->filename = abspath; zf->flags = zflags; zf->closed = 0;
zf->zip = zip;
U8_CLEAR_ERRNO();
return LISP_CONS(zf);}
else {
U8_CLEAR_ERRNO();
return znumerr("open_zipfile",errflag,abspath);}}
}
DEFC_PRIM("zip/open",zipopen_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(1),
"**undocumented**",
{"filename",kno_string_type,KNO_VOID},
{"create",kno_any_type,KNO_FALSE})
static lispval zipopen_prim(lispval filename,lispval create)
{
if ((KNO_FALSEP(create))||(KNO_VOIDP(create)))
return zipopen(KNO_CSTRING(filename),ZIP_CHECKCONS,0);
else return zipopen(KNO_CSTRING(filename),ZIP_CHECKCONS,ZIP_CREATE);
}
DEFC_PRIM("zip/make",zipmake_prim,
KNO_MAX_ARGS(1)|KNO_MIN_ARGS(1),
"**undocumented**",
{"filename",kno_string_type,KNO_VOID})
static lispval zipmake_prim(lispval filename)
{
return zipopen(KNO_CSTRING(filename),0,ZIP_CREATE|ZIP_EXCL);
}
DEFC_PRIM("zip/filename",zipfilename_prim,
KNO_MAX_ARGS(1)|KNO_MIN_ARGS(1),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID})
static lispval zipfilename_prim(lispval zipfile)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
return kno_mkstring(zf->filename);
}
DEFC_PRIM("zip/close!",close_zipfile,
KNO_MAX_ARGS(1)|KNO_MIN_ARGS(1),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID})
static lispval close_zipfile(lispval zipfile)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
int retval;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else retval = zip_close(zf->zip);
if (retval) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("close_zipfile",zf,zipfile);}
else {
zf->closed = 1;
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_TRUE;}
}
DEFC_PRIM("zip/open?",zipfile_openp,
KNO_MAX_ARGS(1)|KNO_MIN_ARGS(1),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID})
static lispval zipfile_openp(lispval zipfile)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
if (zf->closed) return KNO_FALSE;
else return KNO_TRUE;
}
/* Adding to zip files */
static long long int zipadd
(struct KNO_ZIPFILE *zf,u8_string name,struct zip_source *zsource)
{
long long int index = zip_name_locate(zf->zip,name,0), retval = -1;
if (index<0) retval = index = zip_add(zf->zip,name,zsource);
else retval = zip_replace(zf->zip,index,zsource);
if (retval<0) return retval;
else return index;
}
DEFC_PRIM("zip/add!",zipadd_prim,
KNO_MAX_ARGS(5)|KNO_MIN_ARGS(3),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID},
{"filename",kno_string_type,KNO_VOID},
{"value",kno_any_type,KNO_VOID},
{"comment",kno_any_type,KNO_FALSE},
{"compress",kno_any_type,KNO_TRUE})
static lispval zipadd_prim(lispval zipfile,lispval filename,lispval value,
lispval comment,lispval compress)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(filename);
unsigned char *data = NULL; size_t datalen = 0;
struct zip_source *zsource;
long long int index = -1;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
if (KNO_STRINGP(value)) {
data = u8_strdup(KNO_CSTRING(value));
datalen = KNO_STRLEN(value);}
else if (KNO_PACKETP(value)) {
datalen = KNO_PACKET_LENGTH(value);
data = u8_malloc(datalen);
memcpy(data,KNO_PACKET_DATA(value),datalen);}
else return kno_type_error("zip source","zipadd_prim",value);
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
zsource = zip_source_buffer(zf->zip,data,datalen,1);
if (!(zsource)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipadd/source",zf,(lispval)zf);}
index = zipadd(zf,fname,zsource);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipadd",zf,(lispval)zf);}
#if (HAVE_ZIP_SET_FILE_COMMENT)
if (!(KNO_FALSEP(comment))) {
int retval = -1;
if (KNO_STRINGP(comment))
retval = zip_set_file_comment
(zf->zip,index,KNO_CSTRING(comment),KNO_STRLEN(comment));
else if (KNO_PACKETP(comment))
retval = zip_set_file_comment
(zf->zip,index,KNO_PACKET_DATA(comment),
KNO_PACKET_LENGTH(comment));
else {
struct U8_OUTPUT out; U8_INIT_OUTPUT(&out,128);
kno_unparse(&out,comment);
retval = zip_set_file_comment(zf->zip,index,out.u8_outbuf,
out.u8_write-out.u8_outbuf);}
if (retval<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipadd/comment",zf,(lispval)zf);}}
#else
if (!(KNO_FALSEP(comment))) {
u8_log(LOG_WARNING,"zipadd/comment",
"available libzip doesn't support comment fields");}
#endif
#if (HAVE_ZIP_SET_FILE_COMPRESSION)
if (KNO_FALSEP(compress)) {
int retval = zip_set_file_compression(zf->zip,index,ZIP_CM_STORE,0);
if (retval<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipadd/nocompresss",zf,(lispval)zf);}}
#else
if (KNO_FALSEP(compress)) {
u8_log(LOG_WARNING,"zipadd/compress",
"available libzip doesn't support uncompressed fields");}
#endif
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_INT(index);
}
DEFC_PRIM("zip/drop!",zipdrop_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(2),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID},
{"filename",kno_string_type,KNO_VOID})
static lispval zipdrop_prim(lispval zipfile,lispval filename)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(filename);
int index; int retval;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
index = zip_name_locate(zf->zip,fname,0);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else retval = zip_delete(zf->zip,index);
if (retval<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipdrop",zf,(lispval)zf);}
else {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_TRUE;}
}
static int istext(u8_byte *buf,int size)
{
u8_byte *scan = buf, *limit = buf+size;
while (scan<limit)
if ((*scan)>=0x80)
return u8_validp(scan);
else scan++;
return 1;
}
DEFC_PRIM("zip/get",zipget_prim,
KNO_MAX_ARGS(3)|KNO_MIN_ARGS(2),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID},
{"filename",kno_string_type,KNO_VOID},
{"isbinary",kno_any_type,KNO_VOID})
static lispval zipget_prim(lispval zipfile,lispval filename,lispval isbinary)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(filename);
struct zip_stat zstat; int zret;
struct zip_file *zfile;
int index;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
index = zip_name_locate(zf->zip,fname,0);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else if ((zret = zip_stat(zf->zip,fname,0,&zstat))) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipget_prim/stat",zf,filename);}
else if ((zfile = zip_fopen(zf->zip,fname,0))) {
unsigned char *buf = u8_malloc(zstat.size+1);
int size = zstat.size, block = 0, read = 0, togo = size;
while ((togo>0)&&((block = zip_fread(zfile,buf+read,togo))>0)) {
if (block<0) break;
read = read+block;
togo = togo-block;}
if (togo>0) {
u8_free(buf);
u8_unlock_mutex(&(zf->zipfile_lock));
return zfilerr("zipget_prim/fread",zf,zfile,filename);}
zip_fclose(zfile);
u8_unlock_mutex(&(zf->zipfile_lock));
buf[zstat.size]='\0';
if (KNO_VOIDP(isbinary)) {
if (istext(buf,size))
return kno_init_string(NULL,size,buf);
else return kno_init_packet(NULL,size,buf);}
else if (KNO_TRUEP(isbinary))
return kno_init_packet(NULL,size,buf);
else return kno_init_string(NULL,size,buf);}
else {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipget_prim/fopen",zf,filename);}
}
DEFC_PRIM("zip/exists?",zipexists_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(2),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID},
{"filename",kno_string_type,KNO_VOID})
static lispval zipexists_prim(lispval zipfile,lispval filename)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(filename); int index;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
index = zip_name_locate(zf->zip,fname,0);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_TRUE;}
}
DEFC_PRIM("zip/modtime",zipmodtime_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(2),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID},
{"filename",kno_string_type,KNO_VOID})
static lispval zipmodtime_prim(lispval zipfile,lispval filename)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(filename);
struct zip_stat zstat; int zret;
int index;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
index = zip_name_locate(zf->zip,fname,0);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else if ((zret = zip_stat(zf->zip,fname,0,&zstat))) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipmodtime_prim/stat",zf,filename);}
else {
time_t modified = zstat.mtime;
lispval timestamp = kno_time2timestamp(modified);
u8_unlock_mutex(&(zf->zipfile_lock));
return timestamp;}
}
DEFC_PRIM("zip/getsize",zipgetsize_prim,
KNO_MAX_ARGS(2)|KNO_MIN_ARGS(2),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID},
{"filename",kno_string_type,KNO_VOID})
static lispval zipgetsize_prim(lispval zipfile,lispval filename)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(filename);
struct zip_stat zstat; int zret;
int index;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
index = zip_name_locate(zf->zip,fname,0);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else if ((zret = zip_stat(zf->zip,fname,0,&zstat))) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zipgetsize_prim/stat",zf,filename);}
else {
size_t uncompressed_size = zstat.size;
lispval size = KNO_INT2LISP(uncompressed_size);
u8_unlock_mutex(&(zf->zipfile_lock));
return size;}
}
DEFC_PRIM("zip/getfiles",zipgetfiles_prim,
KNO_MAX_ARGS(1)|KNO_MIN_ARGS(1),
"**undocumented**",
{"zipfile",KNO_ZIPFILE_TYPE,KNO_VOID})
static lispval zipgetfiles_prim(lispval zipfile)
{
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
{
lispval files = KNO_EMPTY_CHOICE;
int numfiles = zip_get_num_files(zf->zip);
int i = 0; while (i<numfiles) {
u8_string name = (u8_string)zip_get_name(zf->zip,i,0);
if (!(name)) i++;
else {
lispval lname = kno_mkstring(name);
KNO_ADD_TO_CHOICE(files,lname);
i++;}}
u8_unlock_mutex(&(zf->zipfile_lock));
return files;}
}
DEFC_PRIM("zip/features",zipfeatures_prim,
KNO_MAX_ARGS(0)|KNO_MIN_ARGS(0),
"**undocumented**")
static lispval zipfeatures_prim()
{
lispval result = KNO_EMPTY_CHOICE;
#if (HAVE_ZIP_SET_FILE_EXTRA)
KNO_ADD_TO_CHOICE(result,kno_intern("extra"));
#endif
#if (HAVE_ZIP_SET_FILE_COMMENT)
KNO_ADD_TO_CHOICE(result,kno_intern("comment"));
#endif
#if (HAVE_ZIP_SET_FILE_COMPRESSION)
KNO_ADD_TO_CHOICE(result,kno_intern("compression"));
#endif
return result;
}
#if 0
static void set_prop(lispval result,lispval symbol,lispval consed_val)
{
kno_store(result,symbol,consed_val);
kno_decref(consed_val);
}
static lispval zip_gpath_handler(lispval zipfile,lispval path,lispval aspect,lispval opts)
{
if (aspect == existsp_symbol)
return zipexists_prim(zipfile,path);
else if (aspect == KNOSYM_CONTENT)
return zipget_prim(zipfile,path,KNO_VOID);
else {
int get_content = (kno_overlapp(aspect,KNOSYM_CONTENT));
struct KNO_ZIPFILE *zf = kno_consptr(kno_zipfile,zipfile,kno_zipfile_type);
u8_string fname = KNO_CSTRING(pathh);
struct zip_stat zstat; int zret;
struct zip_file *zfile = NULL;
int index;
if ((fname[0]=='.')&&(fname[1]=='/')) fname = fname+2;
u8_lock_mutex(&(zf->zipfile_lock));
if (zf->closed) {
lispval errval = zipreopen(zf,1);
if (KNO_ABORTP(errval)) {
u8_unlock_mutex(&(zf->zipfile_lock));
return errval;}}
index = zip_name_locate(zf->zip,fname,0);
if (index<0) {
u8_unlock_mutex(&(zf->zipfile_lock));
return KNO_FALSE;}
else if ((zret = zip_stat(zf->zip,fname,0,&zstat))) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zip_gpath_handler/stat",zf,filename);}
else {
if (get_content) {
zfile = zip_fopen(zf->zip,fname,0);
if (zfile == NULL) {
u8_unlock_mutex(&(zf->zipfile_lock));
return ziperr("zip_gpath_handler/open",zf,filename);}}
lispval result = kno_make_slotmap(5,0,NULL);
time_t modified = zstat.mtime;
set_prop(result,KNOSYM(modtime),kno_time2timestamp(zstat.mtime));
set_prop(result,KNOSYM(modtime),KNO_INT(zstat.size));
}}
}
#endif
/* Initialization */
KNO_EXPORT int kno_init_ziptools(void) KNO_LIBINIT_FN;
static long long int ziptools_init = 0;
static lispval ziptools_module;
KNO_EXPORT int kno_init_ziptools()
{
if (ziptools_init) return 0;
ziptools_init = u8_millitime();
ziptools_module =
kno_new_cmodule("ziptools",0,kno_init_ziptools);
kno_zipfile_type = kno_register_cons_type("ZIPFILE",KNO_ZIPFILE_TYPE);
kno_store(ziptools_module,kno_intern("zipfile-type"),
KNO_CTYPE(kno_zipfile_type));
kno_unparsers[kno_zipfile_type]=unparse_zipfile;
kno_recyclers[kno_zipfile_type]=recycle_zipfile;
link_local_cprims();
kno_finish_module(ziptools_module);
kno_load_module("gpath/ziptools");
u8_register_source_file(_FILEINFO);
return 1;
}
static void link_local_cprims()
{
KNO_LINK_CPRIM("zip/features",zipfeatures_prim,0,ziptools_module);
KNO_LINK_CPRIM("zip/make",zipmake_prim,1,ziptools_module);
KNO_LINK_CPRIM("zip/open",zipopen_prim,2,ziptools_module);
KNO_LINK_CPRIM("zipfile?",iszipfile_prim,1,ziptools_module);
KNO_LINK_CPRIM("zip/open?",zipfile_openp,1,ziptools_module);
KNO_LINK_CPRIM("zip/add!",zipadd_prim,5,ziptools_module);
KNO_LINK_CPRIM("zip/drop!",zipdrop_prim,2,ziptools_module);
KNO_LINK_CPRIM("zip/get",zipget_prim,3,ziptools_module);
KNO_LINK_CPRIM("zip/filename",zipfilename_prim,1,ziptools_module);
KNO_LINK_CPRIM("zip/getfiles",zipgetfiles_prim,1,ziptools_module);
KNO_LINK_CPRIM("zip/getsize",zipgetsize_prim,2,ziptools_module);
KNO_LINK_CPRIM("zip/modtime",zipmodtime_prim,2,ziptools_module);
KNO_LINK_CPRIM("zip/exists?",zipexists_prim,2,ziptools_module);
KNO_LINK_CPRIM("zip/close!",close_zipfile,1,ziptools_module);
KNO_LINK_ALIAS("zip/close",close_zipfile,ziptools_module);
KNO_LINK_ALIAS("zip/commit!",close_zipfile,ziptools_module);
}