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
| /*
** Copyright (c) 2007 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains code used to generate ZIP archives.
*/
#include "config.h"
#include <assert.h>
#if defined(FOSSIL_ENABLE_MINIZ)
# define MINIZ_HEADER_FILE_ONLY
# include "miniz.c"
#else
# include <zlib.h>
#endif
#include "zip.h"
/*
** Write a 16- or 32-bit integer as little-endian into the given buffer.
*/
static void put16(char *z, int v){
z[0] = v & 0xff;
z[1] = (v>>8) & 0xff;
}
static void put32(char *z, int v){
z[0] = v & 0xff;
z[1] = (v>>8) & 0xff;
z[2] = (v>>16) & 0xff;
z[3] = (v>>24) & 0xff;
}
/*
** Variables in which to accumulate a growing ZIP archive.
*/
static Blob body; /* The body of the ZIP archive */
static Blob toc; /* The table of contents */
static int nEntry; /* Number of files */
static int dosTime; /* DOS-format time */
static int dosDate; /* DOS-format date */
static int unixTime; /* Seconds since 1970 */
static int nDir; /* Number of entries in azDir[] */
static char **azDir; /* Directory names already added to the archive */
/*
** Initialize a new ZIP archive.
*/
void zip_open(void){
blob_zero(&body);
blob_zero(&toc);
nEntry = 0;
dosTime = 0;
dosDate = 0;
unixTime = 0;
}
/*
** Set the date and time values from an ISO8601 date string.
*/
void zip_set_timedate_from_str(const char *zDate){
int y, m, d;
int H, M, S;
y = atoi(zDate);
m = atoi(&zDate[5]);
d = atoi(&zDate[8]);
H = atoi(&zDate[11]);
M = atoi(&zDate[14]);
S = atoi(&zDate[17]);
dosTime = (H<<11) + (M<<5) + (S>>1);
dosDate = ((y-1980)<<9) + (m<<5) + d;
}
/*
** Set the date and time from a julian day number.
*/
void zip_set_timedate(double rDate){
char *zDate = db_text(0, "SELECT datetime(%.17g)", rDate);
zip_set_timedate_from_str(zDate);
fossil_free(zDate);
unixTime = (rDate - 2440587.5)*86400.0;
}
/*
** If the given filename includes one or more directory entries, make
** sure the directories are already in the archive. If they are not
** in the archive, add them.
*/
void zip_add_folders(char *zName){
int i, c;
int j;
for(i=0; zName[i]; i++){
if( zName[i]=='/' ){
c = zName[i+1];
zName[i+1] = 0;
for(j=0; j<nDir; j++){
if( fossil_strcmp(zName, azDir[j])==0 ) break;
}
if( j>=nDir ){
nDir++;
azDir = fossil_realloc(azDir, sizeof(azDir[0])*nDir);
azDir[j] = mprintf("%s", zName);
zip_add_file(zName, 0, 0);
}
zName[i+1] = c;
}
}
}
/*
** Append a single file to a growing ZIP archive.
**
** pFile is the file to be appended. zName is the name
** that the file should be saved as.
*/
void zip_add_file(const char *zName, const Blob *pFile, int mPerm){
z_stream stream;
int nameLen;
int toOut = 0;
int iStart;
int iCRC = 0;
int nByte = 0;
int nByteCompr = 0;
int nBlob; /* Size of the blob */
int iMethod; /* Compression method. */
int iMode = 0644; /* Access permissions */
char *z;
char zHdr[30];
char zExTime[13];
char zBuf[100];
char zOutBuf[100000];
/* Fill in as much of the header as we know.
*/
nBlob = pFile ? blob_size(pFile) : 0;
if( pFile ){ /* This is a file, possibly empty... */
iMethod = (nBlob>0) ? 8 : 0; /* Cannot compress zero bytes. */
switch( mPerm ){
case PERM_LNK: iMode = 0120755; break;
case PERM_EXE: iMode = 0100755; break;
default: iMode = 0100644; break;
}
}else{ /* This is a directory, no blob... */
iMethod = 0;
iMode = 040755;
}
nameLen = strlen(zName);
memset(zHdr, 0, sizeof(zHdr));
put32(&zHdr[0], 0x04034b50);
put16(&zHdr[4], 0x000a);
put16(&zHdr[6], 0x0800);
put16(&zHdr[8], iMethod);
put16(&zHdr[10], dosTime);
put16(&zHdr[12], dosDate);
put16(&zHdr[26], nameLen);
put16(&zHdr[28], 13);
put16(&zExTime[0], 0x5455);
put16(&zExTime[2], 9);
zExTime[4] = 3;
put32(&zExTime[5], unixTime);
put32(&zExTime[9], unixTime);
/* Write the header and filename.
*/
iStart = blob_size(&body);
blob_append(&body, zHdr, 30);
blob_append(&body, zName, nameLen);
blob_append(&body, zExTime, 13);
if( nBlob>0 ){
/* Write the compressed file. Compute the CRC as we progress.
*/
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = 0;
stream.avail_in = blob_size(pFile);
stream.next_in = (unsigned char*)blob_buffer(pFile);
stream.avail_out = sizeof(zOutBuf);
stream.next_out = (unsigned char*)zOutBuf;
deflateInit2(&stream, 9, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
iCRC = crc32(0, stream.next_in, stream.avail_in);
while( stream.avail_in>0 ){
deflate(&stream, 0);
toOut = sizeof(zOutBuf) - stream.avail_out;
blob_append(&body, zOutBuf, toOut);
stream.avail_out = sizeof(zOutBuf);
stream.next_out = (unsigned char*)zOutBuf;
}
do{
stream.avail_out = sizeof(zOutBuf);
stream.next_out = (unsigned char*)zOutBuf;
deflate(&stream, Z_FINISH);
toOut = sizeof(zOutBuf) - stream.avail_out;
blob_append(&body, zOutBuf, toOut);
}while( stream.avail_out==0 );
nByte = stream.total_in;
nByteCompr = stream.total_out;
deflateEnd(&stream);
/* Go back and write the header, now that we know the compressed file size.
*/
z = &blob_buffer(&body)[iStart];
put32(&z[14], iCRC);
put32(&z[18], nByteCompr);
put32(&z[22], nByte);
}
/* Make an entry in the tables of contents
*/
memset(zBuf, 0, sizeof(zBuf));
put32(&zBuf[0], 0x02014b50);
put16(&zBuf[4], 0x0317);
put16(&zBuf[6], 0x000a);
put16(&zBuf[8], 0x0800);
put16(&zBuf[10], iMethod);
put16(&zBuf[12], dosTime);
put16(&zBuf[14], dosDate);
put32(&zBuf[16], iCRC);
put32(&zBuf[20], nByteCompr);
put32(&zBuf[24], nByte);
put16(&zBuf[28], nameLen);
put16(&zBuf[30], 9);
put16(&zBuf[32], 0);
put16(&zBuf[34], 0);
put16(&zBuf[36], 0);
put32(&zBuf[38], ((unsigned)iMode)<<16);
put32(&zBuf[42], iStart);
blob_append(&toc, zBuf, 46);
blob_append(&toc, zName, nameLen);
put16(&zExTime[2], 5);
blob_append(&toc, zExTime, 9);
nEntry++;
}
/*
** Write the ZIP archive into the given BLOB.
*/
void zip_close(Blob *pZip){
int iTocStart;
int iTocEnd;
int i;
char zBuf[30];
iTocStart = blob_size(&body);
blob_append(&body, blob_buffer(&toc), blob_size(&toc));
iTocEnd = blob_size(&body);
memset(zBuf, 0, sizeof(zBuf));
put32(&zBuf[0], 0x06054b50);
put16(&zBuf[4], 0);
put16(&zBuf[6], 0);
put16(&zBuf[8], nEntry);
put16(&zBuf[10], nEntry);
put32(&zBuf[12], iTocEnd - iTocStart);
put32(&zBuf[16], iTocStart);
put16(&zBuf[20], 0);
blob_append(&body, zBuf, 22);
blob_reset(&toc);
*pZip = body;
blob_zero(&body);
nEntry = 0;
for(i=0; i<nDir; i++){
fossil_free(azDir[i]);
}
fossil_free(azDir);
nDir = 0;
azDir = 0;
}
/*
** COMMAND: test-filezip
**
** Generate a ZIP archive specified by the first argument that
** contains files given in the second and subsequent arguments.
*/
void filezip_cmd(void){
int i;
Blob zip;
Blob file;
if( g.argc<3 ){
usage("ARCHIVE FILE....");
}
zip_open();
for(i=3; i<g.argc; i++){
blob_zero(&file);
blob_read_from_file(&file, g.argv[i]);
zip_add_file(g.argv[i], &file, file_wd_perm(g.argv[i]));
blob_reset(&file);
}
zip_close(&zip);
blob_write_to_file(&zip, g.argv[2]);
}
/*
** Given the RID for a manifest, construct a ZIP archive containing
** all files in the corresponding baseline.
**
** If RID is for an object that is not a real manifest, then the
** resulting ZIP archive contains a single file which is the RID
** object. The pInclude and pExclude parameters are ignored in this case.
**
** If the RID object does not exist in the repository, then
** pZip is zeroed.
**
** zDir is a "synthetic" subdirectory which all zipped files get
** added to as part of the zip file. It may be 0 or an empty string,
** in which case it is ignored. The intention is to create a zip which
** politely expands into a subdir instead of filling your current dir
** with source files. For example, pass a UUID or "ProjectName".
**
*/
void zip_of_checkin(
int rid, /* The RID of the checkin to construct the ZIP archive from */
Blob *pZip, /* Write the ZIP archive content into this blob */
const char *zDir, /* Top-level directory of the ZIP archive */
Glob *pInclude, /* Only include files that match this pattern */
Glob *pExclude /* Exclude files that match this pattern */
){
Blob mfile, hash, file;
Manifest *pManifest;
ManifestFile *pFile;
Blob filename;
int nPrefix;
content_get(rid, &mfile);
if( blob_size(&mfile)==0 ){
blob_zero(pZip);
return;
}
blob_set_dynamic(&hash, rid_to_uuid(rid));
blob_zero(&filename);
zip_open();
if( zDir && zDir[0] ){
blob_appendf(&filename, "%s/", zDir);
}
nPrefix = blob_size(&filename);
pManifest = manifest_get(rid, CFTYPE_MANIFEST, 0);
if( pManifest ){
int flg, eflg = 0;
char *zName = 0;
zip_set_timedate(pManifest->rDate);
flg = db_get_manifest_setting();
if( flg ){
/* eflg is the effective flags, taking include/exclude into account */
if( (pInclude==0 || glob_match(pInclude, "manifest"))
&& !glob_match(pExclude, "manifest")
&& (flg & MFESTFLG_RAW) ){
eflg |= MFESTFLG_RAW;
}
if( (pInclude==0 || glob_match(pInclude, "manifest.uuid"))
&& !glob_match(pExclude, "manifest.uuid")
&& (flg & MFESTFLG_UUID) ){
eflg |= MFESTFLG_UUID;
}
if( (pInclude==0 || glob_match(pInclude, "manifest.tags"))
&& !glob_match(pExclude, "manifest.tags")
&& (flg & MFESTFLG_TAGS) ){
eflg |= MFESTFLG_TAGS;
}
if( eflg & (MFESTFLG_RAW|MFESTFLG_UUID) ){
if( eflg & MFESTFLG_RAW ){
blob_append(&filename, "manifest", -1);
zName = blob_str(&filename);
zip_add_folders(zName);
}
if( eflg & MFESTFLG_RAW ){
sterilize_manifest(&mfile);
zip_add_file(zName, &mfile, 0);
}
}
blob_reset(&mfile);
if( eflg & MFESTFLG_UUID ){
blob_append(&hash, "\n", 1);
blob_resize(&filename, nPrefix);
blob_append(&filename, "manifest.uuid", -1);
zName = blob_str(&filename);
zip_add_folders(zName);
zip_add_file(zName, &hash, 0);
}
if( eflg & MFESTFLG_TAGS ){
Blob tagslist;
blob_zero(&tagslist);
get_checkin_taglist(rid, &tagslist);
blob_resize(&filename, nPrefix);
blob_append(&filename, "manifest.tags", -1);
zName = blob_str(&filename);
zip_add_folders(zName);
zip_add_file(zName, &tagslist, 0);
blob_reset(&tagslist);
}
}
manifest_file_rewind(pManifest);
while( (pFile = manifest_file_next(pManifest,0))!=0 ){
int fid;
if( pInclude!=0 && !glob_match(pInclude, pFile->zName) ) continue;
if( glob_match(pExclude, pFile->zName) ) continue;
fid = uuid_to_rid(pFile->zUuid, 0);
if( fid ){
content_get(fid, &file);
blob_resize(&filename, nPrefix);
blob_append(&filename, pFile->zName, -1);
zName = blob_str(&filename);
zip_add_folders(zName);
zip_add_file(zName, &file, manifest_file_mperm(pFile));
blob_reset(&file);
}
}
}else{
blob_reset(&mfile);
}
manifest_destroy(pManifest);
blob_reset(&filename);
blob_reset(&hash);
zip_close(pZip);
}
/*
** COMMAND: zip*
**
** Usage: %fossil zip VERSION OUTPUTFILE [OPTIONS]
**
** Generate a ZIP archive for a check-in. If the --name option is
** used, its argument becomes the name of the top-level directory in the
** resulting ZIP archive. If --name is omitted, the top-level directory
** name is derived from the project name, the check-in date and time, and
** the artifact ID of the check-in.
**
** The GLOBLIST argument to --exclude and --include can be a comma-separated
** list of glob patterns, where each glob pattern may optionally be enclosed
** in "..." or '...' so that it may contain commas. If a file matches both
** --include and --exclude then it is excluded.
**
** Options:
** -X|--exclude GLOBLIST Comma-separated list of GLOBs of files to exclude
** --include GLOBLIST Comma-separated list of GLOBs of files to include
** --name DIRECTORYNAME The name of the top-level directory in the archive
** -R REPOSITORY Specify a Fossil repository
*/
void zip_cmd(void){
int rid;
Blob zip;
const char *zName;
Glob *pInclude = 0;
Glob *pExclude = 0;
const char *zInclude;
const char *zExclude;
zName = find_option("name", 0, 1);
zExclude = find_option("exclude", "X", 1);
if( zExclude ) pExclude = glob_create(zExclude);
zInclude = find_option("include", 0, 1);
if( zInclude ) pInclude = glob_create(zInclude);
db_find_and_open_repository(0, 0);
/* We should be done with options.. */
verify_all_options();
if( g.argc!=4 ){
usage("VERSION OUTPUTFILE");
}
rid = name_to_typed_rid(g.argv[2], "ci");
if( rid==0 ){
fossil_fatal("Check-in not found: %s", g.argv[2]);
return;
}
if( zName==0 ){
zName = db_text("default-name",
"SELECT replace(%Q,' ','_') "
" || strftime('_%%Y-%%m-%%d_%%H%%M%%S_', event.mtime) "
" || substr(blob.uuid, 1, 10)"
" FROM event, blob"
" WHERE event.objid=%d"
" AND blob.rid=%d",
db_get("project-name", "unnamed"), rid, rid
);
}
zip_of_checkin(rid, &zip, zName, pInclude, pExclude);
glob_free(pInclude);
glob_free(pExclude);
blob_write_to_file(&zip, g.argv[3]);
blob_reset(&zip);
}
/*
** WEBPAGE: zip
** URL: /zip
**
** Generate a ZIP archive for the check-in specified by the "r"
** query parameter. Return that ZIP archive as the HTTP reply content.
**
** Query parameters:
**
** name=NAME[.zip] The base name of the output file. The default
** value is a configuration parameter in the project
** settings. A prefix of the name, omitting the
** extension, is used as the top-most directory name.
**
** r=TAG The check-in that is turned into a ZIP archive.
** Defaults to "trunk". This query parameter used to
** be called "uuid" and the older "uuid" name is still
** accepted for backwards compatibility. If this
** query paramater is omitted, the latest "trunk"
** check-in is used.
**
** in=PATTERN Only include files that match the comma-separate
** list of GLOB patterns in PATTERN, as with ex=
**
** ex=PATTERN Omit any file that match PATTERN. PATTERN is a
** comma-separated list of GLOB patterns, where each
** pattern can optionally be quoted using ".." or '..'.
** Any file matching both ex= and in= is excluded.
*/
void baseline_zip_page(void){
int rid;
const char *z;
char *zName, *zRid, *zKey;
int nName, nRid;
const char *zInclude; /* The in= query parameter */
const char *zExclude; /* The ex= query parameter */
Blob cacheKey; /* The key to cache */
Glob *pInclude = 0; /* The compiled in= glob pattern */
Glob *pExclude = 0; /* The compiled ex= glob pattern */
Blob zip; /* ZIP archive accumulated here */
login_check_credentials();
if( !g.perm.Zip ){ login_needed(g.anon.Zip); return; }
load_control();
zName = mprintf("%s", PD("name",""));
nName = strlen(zName);
z = P("r");
if( z==0 ) z = P("uuid");
if( z==0 ) z = "trunk";
zRid = fossil_strdup(z);
nRid = strlen(zRid);
zInclude = P("in");
if( zInclude ) pInclude = glob_create(zInclude);
zExclude = P("ex");
if( zExclude ) pExclude = glob_create(zExclude);
if( nName>4 && fossil_strcmp(&zName[nName-4], ".zip")==0 ){
/* Special case: Remove the ".zip" suffix. */
nName -= 4;
zName[nName] = 0;
}else{
/* If the file suffix is not ".zip" then just remove the
** suffix up to and including the last "." */
for(nName=strlen(zName)-1; nName>5; nName--){
if( zName[nName]=='.' ){
zName[nName] = 0;
break;
}
}
}
rid = name_to_typed_rid(nRid?zRid:zName, "ci");
if( rid==0 ){
@ Not found
return;
}
if( nRid==0 && nName>10 ) zName[10] = 0;
/* Compute a unique key for the cache entry based on query parameters */
blob_init(&cacheKey, 0, 0);
blob_appendf(&cacheKey, "/zip/%z", rid_to_uuid(rid));
blob_appendf(&cacheKey, "/%q", zName);
if( zInclude ) blob_appendf(&cacheKey, ",in=%Q", zInclude);
if( zExclude ) blob_appendf(&cacheKey, ",ex=%Q", zExclude);
zKey = blob_str(&cacheKey);
if( P("debug")!=0 ){
style_header("ZIP Archive Generator Debug Screen");
@ zName = "%h(zName)"<br />
@ rid = %d(rid)<br />
if( zInclude ){
@ zInclude = "%h(zInclude)"<br />
}
if( zExclude ){
@ zExclude = "%h(zExclude)"<br />
}
@ zKey = "%h(zKey)"
style_footer();
return;
}
if( referred_from_login() ){
style_header("ZIP Archive Download");
@ <form action='%R/zip/%h(zName).zip'>
cgi_query_parameters_to_hidden();
@ <p>ZIP Archive named <b>%h(zName).zip</b> holding the content
@ of check-in <b>%h(zRid)</b>:
@ <input type="submit" value="Download" />
@ </form>
style_footer();
return;
}
blob_zero(&zip);
if( cache_read(&zip, zKey)==0 ){
zip_of_checkin(rid, &zip, zName, pInclude, pExclude);
cache_write(&zip, zKey);
}
glob_free(pInclude);
glob_free(pExclude);
fossil_free(zName);
fossil_free(zRid);
blob_reset(&cacheKey);
cgi_set_content(&zip);
cgi_set_content_type("application/zip");
}
|