Skip to content

Latest commit

 

History

History
2182 lines (1762 loc) · 57.7 KB

mojopatch.c

File metadata and controls

2182 lines (1762 loc) · 57.7 KB
 
Jan 5, 2004
Jan 5, 2004
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
*----------------------------------------------------------------------------
*
* mojopatch
* Copyright (C) 2003 Ryan C. Gordon.
*
*----------------------------------------------------------------------------
*
* (Insert documentation here.)
*
*----------------------------------------------------------------------------
*
* This software was written quickly, is not well-engineered, and may have
* catastrophic bugs. Its method is brute-force, at best. Use at your
* own risk. Don't eat yellow snow.
*
May 27, 2004
May 27, 2004
17
18
* Please see the file LICENSE in the root of the source tree.
*
Jan 5, 2004
Jan 5, 2004
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
* Send patches, improvements, suggestions, etc to Ryan:
* icculus@clutteredmind.org.
*
*----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include "platform.h"
#include "ui.h"
#include "md5.h"
May 25, 2004
May 25, 2004
38
#define VERSION "0.0.5"
Jan 5, 2004
Jan 5, 2004
39
40
41
42
43
#define DEFAULT_PATCHFILENAME "default.mojopatch"
#define MOJOPATCHSIG "mojopatch " VERSION " (icculus@clutteredmind.org)\r\n"
May 25, 2004
May 25, 2004
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#define STATIC_STRING_SIZE 1024
static void make_static_string(char *statstr, const char *str)
{
size_t len = strlen(str);
if (len >= STATIC_STRING_SIZE)
_fatal("Unexpected data in make_static_string()");
else
strcpy(statstr, str);
} /* make_static_string */
typedef struct
{
char signature[sizeof (MOJOPATCHSIG)];
char product[STATIC_STRING_SIZE];
char identifier[STATIC_STRING_SIZE];
char version[STATIC_STRING_SIZE];
char newversion[STATIC_STRING_SIZE];
char readmefname[STATIC_STRING_SIZE];
char *readmedata;
char renamedir[STATIC_STRING_SIZE];
May 27, 2004
May 27, 2004
66
char titlebar[STATIC_STRING_SIZE];
May 25, 2004
May 25, 2004
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
} PatchHeader;
typedef enum
{
OPERATION_DELETE = 0,
OPERATION_DELETEDIRECTORY,
OPERATION_ADD,
OPERATION_ADDDIRECTORY,
OPERATION_PATCH,
OPERATION_REPLACE,
OPERATION_DONE,
OPERATION_TOTAL /* must be last! */
} OperationType;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
} DeleteOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
} DeleteDirOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
size_t fsize;
md5_byte_t md5[16];
mode_t mode;
} AddOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
mode_t mode;
} AddDirOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
md5_byte_t md5_1[16];
md5_byte_t md5_2[16];
size_t fsize;
size_t deltasize;
mode_t mode;
} PatchOperation;
typedef struct
{
OperationType operation;
} DoneOperation;
typedef union
{
OperationType operation;
DeleteOperation del;
DeleteDirOperation deldir;
AddOperation add;
AddDirOperation adddir;
PatchOperation patch;
AddOperation replace;
DoneOperation done;
} Operations;
typedef struct
{
FILE *io;
int reading;
} SerialArchive;
Jan 5, 2004
Jan 5, 2004
142
143
144
145
146
147
148
149
150
151
152
typedef enum
{
COMMAND_NONE = 0,
COMMAND_CREATE,
COMMAND_INFO,
COMMAND_DOPATCHING,
COMMAND_TOTAL
} PatchCommands;
May 25, 2004
May 25, 2004
153
Jan 5, 2004
Jan 5, 2004
154
155
156
static int debug = 0;
static int interactive = 0;
static int replace = 0;
May 27, 2004
May 27, 2004
157
static int appending = 0;
May 28, 2004
May 28, 2004
158
static int alwaysadd = 0;
May 28, 2004
May 28, 2004
159
static int quietonsuccess = 0;
Jan 5, 2004
Jan 5, 2004
160
161
162
163
164
165
166
167
168
static PatchCommands command = COMMAND_NONE;
static const char *patchfile = NULL;
static const char *dir1 = NULL;
static const char *dir2 = NULL;
static char *patchtmpfile = NULL;
static char *patchtmpfile2 = NULL;
May 25, 2004
May 25, 2004
169
static PatchHeader header;
Jan 5, 2004
Jan 5, 2004
170
171
172
173
174
175
176
177
static char **ignorelist = NULL;
static int ignorecount = 0;
static unsigned int maxxdeltamem = 128; /* in megabytes. */
static unsigned char iobuf[512 * 1024];
May 25, 2004
May 25, 2004
178
179
180
181
static int serialize(SerialArchive *ar, void *val, size_t size)
{
int rc;
May 27, 2004
May 27, 2004
182
183
184
185
if (size == 0)
return(1);
May 25, 2004
May 25, 2004
186
187
188
189
190
if (ar->reading)
rc = fread(val, size, 1, ar->io);
else
rc = fwrite(val, size, 1, ar->io);
May 27, 2004
May 27, 2004
191
if ( (rc != 1) && (ferror(ar->io)) )
May 25, 2004
May 25, 2004
192
193
_fatal("%s error: %s.", ar->reading ? "Read":"Write", strerror(errno));
May 27, 2004
May 27, 2004
194
return(rc == 1); /* may fail without calling _fatal() on EOF! */
May 25, 2004
May 25, 2004
195
196
197
198
199
200
201
202
203
} /* serialize */
#define SERIALIZE(ar, x) serialize(ar, &x, sizeof (x))
static int serialize_static_string(SerialArchive *ar, char *val)
{
size_t len;
May 27, 2004
May 27, 2004
204
205
206
if (!ar->reading)
len = strlen(val);
May 25, 2004
May 25, 2004
207
208
209
210
211
212
213
214
215
216
if (!SERIALIZE(ar, len))
return(0);
if (len >= STATIC_STRING_SIZE)
{
_fatal("Bogus string data in patchfile.");
return(0);
} /* if */
val[len] = 0;
May 27, 2004
May 27, 2004
217
return(serialize(ar, val, len));
May 25, 2004
May 25, 2004
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
} /* serialize_static_string */
/*
* This will only overwrite (val)'s contents if (val) is empty ("!val[0]").
* Mostly, this lets us optionally override patchfiles from the command line.
* Writing is done, regardless of string's state.
*/
static int serialize_static_string_if_empty(SerialArchive *ar, char *val)
{
if ((!ar->reading) || (val[0] == '\0'))
return(serialize_static_string(ar, val));
else
{
char buffer[STATIC_STRING_SIZE];
return(serialize_static_string(ar, buffer)); /* throw away data */
} /* else */
} /* serialize_static_string_if_empty */
static int serialize_asciz_string(SerialArchive *ar, char **_buffer)
{
char *buffer = *_buffer;
size_t i = 0;
size_t allocated = 0;
int ch;
if (!ar->reading)
return(serialize(ar, buffer, strlen(buffer) + 1));
buffer = NULL;
/* read an arbitrary amount, allocate storage... */
do
{
if (i <= allocated)
{
allocated += 128;
buffer = realloc(buffer, allocated);
if (buffer == NULL)
{
_fatal("Out of memory.");
return(0);
} /* if */
} /* if */
ch = fgetc(ar->io);
if (ch == EOF)
{
if (feof(ar->io))
_fatal("Unexpected EOF during read.");
else
_fatal("Error during read: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
buffer[i] = (char) ch;
} while (buffer[i++] != '\0');
*_buffer = buffer;
return(1);
} /* serialize_asciz_string */
May 27, 2004
May 27, 2004
282
static int serialize_header(SerialArchive *ar, PatchHeader *h, int *legitEOF)
May 25, 2004
May 25, 2004
283
{
May 27, 2004
May 27, 2004
284
285
286
287
288
int rc;
int dummy = 0;
if (legitEOF == NULL)
legitEOF = &dummy;
May 27, 2004
May 27, 2004
289
290
291
assert(sizeof (h->signature) == sizeof (MOJOPATCHSIG));
memcpy(h->signature, MOJOPATCHSIG, sizeof (h->signature));
May 27, 2004
May 27, 2004
292
293
294
295
rc = SERIALIZE(ar, h->signature);
*legitEOF = ( (feof(ar->io)) && (!ferror(ar->io)) );
if (!rc)
return(*legitEOF);
May 25, 2004
May 25, 2004
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
if (memcmp(h->signature, MOJOPATCHSIG, sizeof (h->signature)) != 0)
{
h->signature[sizeof (h->signature) - 1] = '\0'; /* just in case. */
_fatal("[%s] is not a compatible mojopatch file.", patchfile);
_log("signature is: %s.", h->signature);
_log(" expected: %s.", MOJOPATCHSIG);
return(PATCHERROR);
} /* if */
if (serialize_static_string_if_empty(ar, h->product))
if (serialize_static_string_if_empty(ar, h->identifier))
if (serialize_static_string_if_empty(ar, h->version))
if (serialize_static_string_if_empty(ar, h->newversion))
if (serialize_static_string_if_empty(ar, h->readmefname))
if (serialize_asciz_string(ar, &h->readmedata))
if (serialize_static_string_if_empty(ar, h->renamedir))
May 27, 2004
May 27, 2004
313
if (serialize_static_string_if_empty(ar, h->titlebar))
May 25, 2004
May 25, 2004
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
return(1);
return(0);
} /* serialize_header */
static int serialize_delete_op(SerialArchive *ar, void *d)
{
DeleteOperation *del = (DeleteOperation *) d;
assert(del->operation == OPERATION_DELETE);
if (serialize_static_string(ar, del->fname))
return(1);
return(0);
} /* serialize_delete_op */
static int serialize_deldir_op(SerialArchive *ar, void *d)
{
DeleteDirOperation *deldir = (DeleteDirOperation *) d;
assert(deldir->operation == OPERATION_DELETEDIRECTORY);
if (serialize_static_string(ar, deldir->fname))
return(1);
return(0);
} /* serialize_deldir_op */
static int serialize_add_op(SerialArchive *ar, void *d)
{
AddOperation *add = (AddOperation *) d;
assert((add->operation == OPERATION_ADD) ||
(add->operation == OPERATION_REPLACE));
if (serialize_static_string(ar, add->fname))
if (SERIALIZE(ar, add->fsize))
if (SERIALIZE(ar, add->md5))
if (SERIALIZE(ar, add->mode))
return(1);
return(0);
} /* serialize_add_op */
static int serialize_adddir_op(SerialArchive *ar, void *d)
{
AddDirOperation *adddir = (AddDirOperation *) d;
assert(adddir->operation == OPERATION_ADDDIRECTORY);
if (serialize_static_string(ar, adddir->fname))
if (SERIALIZE(ar, adddir->mode))
return(1);
return(0);
} /* serialize_adddir_op */
static int serialize_patch_op(SerialArchive *ar, void *d)
{
PatchOperation *patch = (PatchOperation *) d;
assert(patch->operation == OPERATION_PATCH);
if (serialize_static_string(ar, patch->fname))
if (SERIALIZE(ar, patch->md5_1))
May 27, 2004
May 27, 2004
370
if (SERIALIZE(ar, patch->md5_2))
May 25, 2004
May 25, 2004
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
if (SERIALIZE(ar, patch->fsize))
if (SERIALIZE(ar, patch->deltasize))
if (SERIALIZE(ar, patch->mode))
return(1);
return(0);
} /* serialize_patch_op */
static int serialize_replace_op(SerialArchive *ar, void *d)
{
AddOperation *add = (AddOperation *) d;
assert(add->operation == OPERATION_REPLACE);
return(serialize_add_op(ar, d));
} /* serialize_replace_op */
static int serialize_done_op(SerialArchive *ar, void *d)
{
DoneOperation *done = (DoneOperation *) d;
assert(done->operation == OPERATION_DONE);
return(1);
} /* serialize_done_op */
typedef int (*OpSerializers)(SerialArchive *ar, void *data);
static OpSerializers serializers[OPERATION_TOTAL] =
{
/* Must match OperationType order! */
serialize_delete_op,
serialize_deldir_op,
serialize_add_op,
serialize_adddir_op,
serialize_patch_op,
serialize_replace_op,
serialize_done_op,
};
static int handle_delete_op(SerialArchive *ar, OperationType op, void *data);
static int handle_deldir_op(SerialArchive *ar, OperationType op, void *data);
static int handle_add_op(SerialArchive *ar, OperationType op, void *data);
static int handle_adddir_op(SerialArchive *ar, OperationType op, void *data);
static int handle_patch_op(SerialArchive *ar, OperationType op, void *data);
static int handle_replace_op(SerialArchive *ar, OperationType op, void *data);
May 27, 2004
May 27, 2004
415
static int handle_done_op(SerialArchive *ar, OperationType op, void *data);
May 25, 2004
May 25, 2004
416
417
418
419
420
421
422
423
424
425
426
typedef int (*OpHandlers)(SerialArchive *ar, OperationType op, void *data);
static OpHandlers operation_handlers[OPERATION_TOTAL] =
{
/* Must match OperationType order! */
handle_delete_op,
handle_deldir_op,
handle_add_op,
handle_adddir_op,
handle_patch_op,
handle_replace_op,
May 27, 2004
May 27, 2004
427
handle_done_op,
May 25, 2004
May 25, 2004
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
};
static int serialize_operation(SerialArchive *ar, Operations *ops)
{
if (!SERIALIZE(ar, ops->operation))
return(0);
if ((ops->operation < 0) || (ops->operation >= OPERATION_TOTAL))
{
_fatal("Invalid operation in patch file.");
return(0);
} /* if */
May 27, 2004
May 27, 2004
443
return(serializers[ops->operation](ar, ops));
May 25, 2004
May 25, 2004
444
445
446
447
448
449
450
451
452
453
454
455
456
457
} /* serialize_operation */
static int open_serialized_archive(SerialArchive *ar,
const char *fname,
int is_reading,
int *sizeok,
size_t *file_size)
{
memset(ar, '\0', sizeof (*ar));
if (strcmp(fname, "-") == 0) /* read from stdin? */
ar->io = (is_reading) ? stdin : stdout;
else
{
May 27, 2004
May 27, 2004
458
459
460
461
const char *fopenstr = "rb";
if (!is_reading)
fopenstr = ((appending) ? "ab" : "wb");
May 25, 2004
May 25, 2004
462
463
464
465
466
467
468
if (file_size != NULL)
{
int tmp = get_file_size(patchfile, file_size);
if (sizeok != NULL)
*sizeok = tmp;
} /* if */
May 27, 2004
May 27, 2004
469
ar->io = fopen(patchfile, fopenstr);
May 25, 2004
May 25, 2004
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
if (ar->io == NULL)
{
_fatal("Couldn't open [%s]: %s.", patchfile, strerror(errno));
return(PATCHERROR);
} /* if */
} /* else */
ar->reading = is_reading;
return(PATCHSUCCESS);
} /* open_serialized_archive */
static inline int close_serialized_archive(SerialArchive *ar)
{
if (ar->io != NULL)
{
if (fclose(ar->io) == EOF)
return(PATCHERROR);
ar->io = NULL;
} /* if */
return(PATCHSUCCESS);
} /* close_serialized_archive */
Jan 5, 2004
Jan 5, 2004
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
/* printf-style: makes string for UI to put in the log. */
void _fatal(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_fatal(buf);
ui_pump();
} /* _fatal */
/* printf-style: makes string for UI to put in the log. */
void _log(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_add_to_log(buf, 0);
ui_pump();
} /* _log */
/* printf-style: makes string for UI to put in the log if debugging enabled. */
void _dlog(const char *fmt, ...)
{
if (debug)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_add_to_log(buf, 1);
ui_pump();
} /* if */
} /* _dlog */
static void _current_operation(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_status(buf);
ui_pump();
} /* _current_operation */
May 25, 2004
May 25, 2004
552
553
554
555
/* don't taunt this function. */
int version_ok(const char *ver, const char *allowed_ver)
{
char *ptr;
May 27, 2004
May 27, 2004
556
557
558
559
560
561
char *buf;
if (*allowed_ver == '\0')
return 1; /* No specified version? Anything is okay, then. */
buf = (char *) alloca(strlen(allowed_ver) + 1);
May 25, 2004
May 25, 2004
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
strcpy(buf, allowed_ver);
while ((ptr = strstr(buf, " or ")) != NULL)
{
*ptr = '\0';
if (strcmp(ver, buf) == 0)
return(1);
buf = ptr + 4;
} /* while */
return(strcmp(ver, buf) == 0);
} /* version_ok */
Jan 5, 2004
Jan 5, 2004
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
static int _do_xdelta(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
_dlog("(xdelta call: [%s].)", buf);
return(spawn_xdelta(buf));
} /* _do_xdelta */
static int in_ignore_list(const char *fname)
{
int i;
for (i = 0; i < ignorecount; i++)
{
if (strcmp(fname, ignorelist[i]) == 0)
{
_log("Ignoring %s on user's instructions.", fname);
return(1);
} /* if */
} /* for */
return(0);
} /* in_ignore_list */
static inline int info_only(void)
{
return(command == COMMAND_INFO);
} /* info_only */
static void free_filelist(file_list *list)
{
file_list *next;
while (list != NULL)
{
next = list->next;
free(list->fname);
free(list);
list = next;
} /* while */
} /* free_filelist */
static int write_between_files(FILE *in, FILE *out, long fsize)
{
while (fsize > 0)
{
int max = sizeof (iobuf);
if (max > fsize)
max = fsize;
int br = fread(iobuf, 1, max, in);
if (br <= 0)
{
_fatal("read error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
if (fwrite(iobuf, br, 1, out) != 1)
{
_fatal("write error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
fsize -= br;
} /* while */
return(PATCHSUCCESS);
} /* write_between_files */
static int do_rename(const char *from, const char *to)
{
FILE *in;
FILE *out;
long fsize;
int rc;
unlink(to); /* just in case. */
if (rename(from, to) != -1)
return(PATCHSUCCESS);
/* rename() might fail if from and to are on seperate filesystems. */
rc = get_file_size(from, &fsize);
in = fopen(from, "rb");
out = fopen(to, "wb");
if ((!rc) || (!in) || (!out))
{
if (in)
fclose(in);
if (out)
fclose(out);
unlink(to);
_fatal("File copy failed.");
return(PATCHERROR);
} /* if */
rc = write_between_files(in, out, fsize);
fclose(in);
if ((fclose(out) == -1) && (rc != PATCHERROR))
{
_fatal("File copy failed.");
return(PATCHERROR);
} /* if */
unlink(from);
return(rc);
} /* do_rename */
static int md5sum(FILE *in, md5_byte_t *digest, int output)
{
md5_state_t md5state;
long br;
_dlog("md5summing...");
memset(digest, '\0', 16);
md5_init(&md5state);
if (fseek(in, 0, SEEK_SET) == -1)
{
_fatal("Couldn't seek in file: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
while (1)
{
ui_pump();
br = fread(iobuf, 1, sizeof (iobuf), in);
if (br == 0)
{
int err = errno;
if (feof(in))
break;
else
{
_fatal("Read error: %s.", strerror(err));
return(PATCHERROR);
} /* else */
} /* if */
md5_append(&md5state, (const md5_byte_t *) iobuf, br);
} /* while */
md5_finish(&md5state, digest);
if ((output) || (debug))
{
/* ugly, but want to print it all on one line... */
_log(" (md5sum: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x)",
digest[0], digest[1], digest[2], digest[3],
digest[4], digest[5], digest[6], digest[7],
digest[8], digest[9], digest[10], digest[11],
digest[12], digest[13], digest[14], digest[15]);
} /* if */
if (fseek(in, 0, SEEK_SET) == -1)
{
_fatal("Couldn't seek in file: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
return(PATCHSUCCESS);
} /* md5sum */
static int verify_md5sum(md5_byte_t *md5, md5_byte_t *result, FILE *in, int isfatal)
{
md5_byte_t thismd5[16];
if (md5sum(in, thismd5, 0) == PATCHERROR)
return(PATCHERROR);
if (result != NULL)
memcpy(result, thismd5, sizeof (thismd5));
if (memcmp(thismd5, md5, sizeof (thismd5)) != 0)
{
if (isfatal)
_fatal("md5sum doesn't match original!");
return(PATCHERROR);
} /* if */
return(PATCHSUCCESS);
} /* verify_md5sum */
May 25, 2004
May 25, 2004
775
/* !!! FIXME: This should be in the UI abstraction. */
Jan 5, 2004
Jan 5, 2004
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
static int confirm(void)
{
char buf[256];
char *ptr;
if (!interactive)
return(1);
while (1)
{
printf("Confirm [Y/n] : ");
fgets(buf, sizeof (buf) - 1, stdin);
if ( (ptr = strchr(buf, '\r')) != NULL )
*ptr = '\0';
if ( (ptr = strchr(buf, '\n')) != NULL )
*ptr = '\0';
if (strlen(buf) <= 1)
{
int ch = tolower((int) buf[0]);
if ((ch == '\0') || (ch == 'y'))
{
printf("Answered YES\n");
return(1);
} /* if */
else if (ch == 'n')
{
printf("Answered NO\n");
return(0);
} /* else if */
} /* if */
} /* while */
} /* confirm */
static const char *final_path_element(const char *fname)
{
const char *ptr = (const char *) strrchr(fname, PATH_SEP[0]);
assert( (sizeof (PATH_SEP)) == (sizeof (char) * 2) );
return(ptr ? ptr + 1 : fname);
} /* final_path_element */
/* put a DELETE operation in the mojopatch file... */
May 25, 2004
May 25, 2004
820
static int put_delete(SerialArchive *ar, const char *fname)
Jan 5, 2004
Jan 5, 2004
821
{
May 27, 2004
May 27, 2004
822
Operations ops;
Jan 5, 2004
Jan 5, 2004
823
824
825
826
827
828
829
830
831
832
_current_operation("DELETE %s", final_path_element(fname));
_log("DELETE %s", fname);
if (in_ignore_list(fname))
return(PATCHSUCCESS);
if (!confirm())
return(PATCHSUCCESS);
May 27, 2004
May 27, 2004
833
834
835
ops.operation = OPERATION_DELETE;
make_static_string(ops.del.fname, fname);
return(serialize_operation(ar, &ops));
Jan 5, 2004
Jan 5, 2004
836
837
838
839
} /* put_delete */
/* get a DELETE operation from the mojopatch file... */
May 25, 2004
May 25, 2004
840
static int handle_delete_op(SerialArchive *ar, OperationType op, void *d)
Jan 5, 2004
Jan 5, 2004
841
{
May 25, 2004
May 25, 2004
842
DeleteOperation *del = (DeleteOperation *) d;
May 27, 2004
May 27, 2004
843
assert(op == OPERATION_DELETE);
Jan 5, 2004
Jan 5, 2004
844
May 25, 2004
May 25, 2004
845
846
_current_operation("DELETE %s", final_path_element(del->fname));
_log("DELETE %s", del->fname);
Jan 5, 2004
Jan 5, 2004
847
848
849
850
if ( (info_only()) || (!confirm()) )
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
851
if (in_ignore_list(del->fname))
Jan 5, 2004
Jan 5, 2004
852
853
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
854
if (!file_exists(del->fname))
Jan 5, 2004
Jan 5, 2004
855
856
857
858
859
{
_log("file seems to be gone already.");
return(PATCHSUCCESS);
} /* if */
May 25, 2004
May 25, 2004
860
if (file_is_directory(del->fname))
Jan 5, 2004
Jan 5, 2004
861
862
863
864
865
{
_fatal("Expected file, found directory!");
return(PATCHERROR);
} /* if */
May 25, 2004
May 25, 2004
866
if (remove(del->fname) == -1)
Jan 5, 2004
Jan 5, 2004
867
{
May 25, 2004
May 25, 2004
868
_fatal("Error removing [%s]: %s.", del->fname, strerror(errno));
Jan 5, 2004
Jan 5, 2004
869
870
871
872
873
return(PATCHERROR);
} /* if */
_log("done DELETE.");
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
874
} /* handle_delete_op */
Jan 5, 2004
Jan 5, 2004
875
876
877
/* put a DELETEDIRECTORY operation in the mojopatch file... */
May 25, 2004
May 25, 2004
878
static int put_delete_dir(SerialArchive *ar, const char *fname)
Jan 5, 2004
Jan 5, 2004
879
{
May 27, 2004
May 27, 2004
880
Operations ops;
Jan 5, 2004
Jan 5, 2004
881
May 25, 2004
May 25, 2004
882
883
_current_operation("DELETEDIRECTORY %s", final_path_element(fname));
_log("DELETEDIRECTORY %s", fname);
Jan 5, 2004
Jan 5, 2004
884
885
886
887
if (!confirm())
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
888
if (in_ignore_list(fname))
Jan 5, 2004
Jan 5, 2004
889
890
return(PATCHSUCCESS);
May 27, 2004
May 27, 2004
891
892
893
ops.operation = OPERATION_DELETEDIRECTORY;
make_static_string(ops.deldir.fname, fname);
return(serialize_operation(ar, &ops));
Jan 5, 2004
Jan 5, 2004
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
} /* put_delete_dir */
static int delete_dir_tree(const char *fname)
{
char filebuf[MAX_PATH];
file_list *files = make_filelist(fname);
file_list *i;
int rc = 0;
_log("Deleting directory tree %s", fname);
for (i = files; i != NULL; i = i->next)
{
snprintf(filebuf, sizeof (filebuf), "%s%s%s", fname, PATH_SEP, i->fname);
if (file_is_directory(filebuf))
rc = delete_dir_tree(filebuf);
else
{
_log("Deleting file %s from dir tree", filebuf);
rc = (remove(filebuf) == -1) ? PATCHERROR : PATCHSUCCESS;
if (rc == PATCHERROR)
_fatal("failed to delete %s: %s.", filebuf, strerror(errno));
} /* else */
if (rc == PATCHERROR)
{
free_filelist(files);
return(PATCHERROR);
} /* if */
} /* for */
free_filelist(files);
if (rmdir(fname) == -1)
{
_fatal("Error removing directory [%s]: %s.", fname, strerror(errno));
return(PATCHERROR);
} /* if */
return(PATCHSUCCESS);
} /* delete_dir_tree */
/* get a DELETEDIRECTORY operation from the mojopatch file... */
May 25, 2004
May 25, 2004
939
static int handle_deldir_op(SerialArchive *ar, OperationType op, void *d)
Jan 5, 2004
Jan 5, 2004
940
{
May 25, 2004
May 25, 2004
941
942
DeleteDirOperation *deldir = (DeleteDirOperation *) d;
assert(op == OPERATION_DELETEDIRECTORY);
Jan 5, 2004
Jan 5, 2004
943
May 25, 2004
May 25, 2004
944
945
_current_operation("DELETEDIRECTORY %s", final_path_element(deldir->fname));
_log("DELETEDIRECTORY %s", deldir->fname);
Jan 5, 2004
Jan 5, 2004
946
947
948
949
if ( (info_only()) || (!confirm()) )
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
950
if (in_ignore_list(deldir->fname))
Jan 5, 2004
Jan 5, 2004
951
952
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
953
if (!file_exists(deldir->fname))
Jan 5, 2004
Jan 5, 2004
954
955
956
957
958
{
_log("directory seems to be gone already.");
return(PATCHSUCCESS);
} /* if */
May 25, 2004
May 25, 2004
959
if (!file_is_directory(deldir->fname))
Jan 5, 2004
Jan 5, 2004
960
961
962
963
964
{
_fatal("Expected directory, found file!");
return(PATCHERROR);
} /* if */
May 25, 2004
May 25, 2004
965
if (!delete_dir_tree(deldir->fname))
Jan 5, 2004
Jan 5, 2004
966
967
968
969
return(PATCHERROR);
_log("done DELETEDIRECTORY.");
return(PATCHSUCCESS);
May 25, 2004
May 25, 2004
970
} /* handle_deldir_op */
Jan 5, 2004
Jan 5, 2004
971
972
973
/* put an ADD operation in the mojopatch file... */
May 25, 2004
May 25, 2004
974
static int put_add(SerialArchive *ar, const char *fname)
Jan 5, 2004
Jan 5, 2004
975
{
May 27, 2004
May 27, 2004
976
Operations ops;
May 25, 2004
May 25, 2004
977
FILE *in = NULL;
Jan 5, 2004
Jan 5, 2004
978
struct stat statbuf;
May 25, 2004
May 25, 2004
979
int retval = PATCHERROR;
Jan 5, 2004
Jan 5, 2004
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
_current_operation("%s %s", (replace) ? "ADDORREPLACE" : "ADD",
final_path_element(fname));
_log("%s %s", (replace) ? "ADDORREPLACE" : "ADD", fname);
if (!confirm())
return(PATCHSUCCESS);
if (in_ignore_list(fname))
return(PATCHSUCCESS);
if (stat(fname, &statbuf) == -1)
{
_fatal("Couldn't stat %s: %s.", fname, strerror(errno));
return(PATCHERROR);
} /* if */
in = fopen(fname, "rb");
if (in == NULL)
{
_fatal("failed to open [%s]: %s.", fname, strerror(errno));