OSDN Git Service

* src/header.c: fixed typo.
[lha/lha.git] / src / lhadd.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                             */
3 /*              lhadd.c -- LHarc Add Command                                */
4 /*                                                                          */
5 /*      Copyright (C) MCMLXXXIX Yooichi.Tagawa                              */
6 /*      Modified                Nobutaka Watazaki                           */
7 /*                                                                          */
8 /*  Ver. 1.14   Source All chagned              1995.01.14  N.Watazaki      */
9 /* ------------------------------------------------------------------------ */
10 #include "lha.h"
11 /* ------------------------------------------------------------------------ */
12 static void     remove_files();
13
14 static char     new_archive_name_buffer[FILENAME_LENGTH];
15 static char    *new_archive_name;
16 /* ------------------------------------------------------------------------ */
17 static void
18 add_one(fp, nafp, hdr)
19     FILE           *fp, *nafp;
20     LzHeader       *hdr;
21 {
22     off_t header_pos, next_pos, org_pos, data_pos;
23     size_t v_original_size, v_packed_size;
24
25     reading_filename = hdr->name;
26     writing_filename = temporary_name;
27
28     if (!fp && generic_format)  /* [generic] doesn't need directory info. */
29         return;
30     header_pos = ftello(nafp);
31     write_header(nafp, hdr);/* DUMMY */
32
33     if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
34         if (!quiet)
35             printf("%s -> %s\t- Symbolic Link\n", hdr->realname, hdr->name);
36     }
37
38     if (hdr->original_size == 0) {  /* empty file, symlink or directory */
39         finish_indicator2(hdr->name, "Frozen", 0);
40         return;     /* previous write_header is not DUMMY. (^_^) */
41     }
42     org_pos = ftello(fp);
43     data_pos = ftello(nafp);
44
45     hdr->crc = encode_lzhuf(fp, nafp, hdr->original_size,
46           &v_original_size, &v_packed_size, hdr->name, hdr->method);
47
48     if (v_packed_size < v_original_size) {
49         next_pos = ftello(nafp);
50     }
51     else {          /* retry by stored method */
52         fseeko(fp, org_pos, SEEK_SET);
53         fseeko(nafp, data_pos, SEEK_SET);
54         hdr->crc = encode_stored_crc(fp, nafp, hdr->original_size,
55                       &v_original_size, &v_packed_size);
56         fflush(nafp);
57         next_pos = ftello(nafp);
58 #if HAVE_FTRUNCATE
59         if (ftruncate(fileno(nafp), next_pos) == -1)
60             error("cannot truncate archive");
61 #elif HAVE_CHSIZE
62         if (chsize(fileno(nafp), next_pos) == -1)
63             error("cannot truncate archive");
64 #else
65         CAUSE COMPILE ERROR
66 #endif
67         memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
68     }
69     hdr->original_size = v_original_size;
70     hdr->packed_size = v_packed_size;
71     fseeko(nafp, header_pos, SEEK_SET);
72     write_header(nafp, hdr);
73     fseeko(nafp, next_pos, SEEK_SET);
74 }
75
76
77 /* ------------------------------------------------------------------------ */
78 FILE           *
79 append_it(name, oafp, nafp)
80     char           *name;
81     FILE           *oafp, *nafp;
82 {
83     LzHeader        ahdr, hdr;
84     FILE           *fp;
85     long            old_header;
86     int             cmp;
87     int             filec;
88     char          **filev;
89     int             i;
90     struct stat     stbuf;
91
92     boolean         directory, symlink;
93
94     if (GETSTAT(name, &stbuf) < 0) {
95         error("Cannot access file \"%s\"", name);   /* See cleaning_files, Why? */
96         return oafp;
97     }
98
99     directory = is_directory(&stbuf);
100 #ifdef S_IFLNK
101     symlink = is_symlink(&stbuf);
102 #else
103     symlink = 0;
104 #endif
105     init_header(name, &stbuf, &hdr);
106
107     fp = NULL;
108     if (!directory && !symlink && !noexec) {
109         fp = fopen(name, READ_BINARY);
110         if (!fp) {
111             error("Cannot open file \"%s\": %s", name, strerror(errno));
112             return oafp;
113         }
114     }
115
116     cmp = 0;                    /* avoid compiler warnings `uninitialized' */
117     while (oafp) {
118         old_header = ftello(oafp);
119         if (!get_header(oafp, &ahdr)) {
120             /* end of archive or error occurred */
121             fclose(oafp);
122             oafp = NULL;
123             break;
124         }
125
126         cmp = strcmp(ahdr.name, hdr.name);
127         if (cmp < 0) {          /* SKIP */
128             /* copy old to new */
129             if (!noexec) {
130                 fseeko(oafp, old_header, SEEK_SET);
131                 copy_old_one(oafp, nafp, &ahdr);
132             }
133             else
134                 fseeko(oafp, ahdr.packed_size, SEEK_CUR);
135         } else if (cmp == 0) {  /* REPLACE */
136             /* drop old archive's */
137             fseeko(oafp, ahdr.packed_size, SEEK_CUR);
138             break;
139         } else {                /* cmp > 0, INSERT */
140             fseeko(oafp, old_header, SEEK_SET);
141             break;
142         }
143     }
144
145     if (!oafp || cmp > 0) { /* not in archive */
146         if (noexec)
147             printf("ADD %s\n", name);
148         else
149             add_one(fp, nafp, &hdr);
150     }
151     else {      /* cmp == 0 */
152         if (!update_if_newer ||
153             ahdr.unix_last_modified_stamp < hdr.unix_last_modified_stamp) {
154                                 /* newer than archive's */
155             if (noexec)
156                 printf("REPLACE %s\n", name);
157             else
158                 add_one(fp, nafp, &hdr);
159         }
160         else {                  /* copy old to new */
161             if (!noexec) {
162                 fseeko(oafp, old_header, SEEK_SET);
163                 copy_old_one(oafp, nafp, &ahdr);
164             }
165         }
166     }
167
168     if (fp) fclose(fp);
169
170     if (directory) {            /* recursive call */
171         if (find_files(name, &filec, &filev)) {
172             for (i = 0; i < filec; i++)
173                 oafp = append_it(filev[i], oafp, nafp);
174             free_files(filec, filev);
175         }
176     }
177     return oafp;
178 }
179
180 /* ------------------------------------------------------------------------ */
181 static void
182 find_update_files(oafp)
183     FILE           *oafp;   /* old archive */
184 {
185     char            name[FILENAME_LENGTH];
186     struct string_pool sp;
187     LzHeader        hdr;
188     off_t           pos;
189     struct stat     stbuf;
190     int             len;
191
192     pos = ftello(oafp);
193
194     init_sp(&sp);
195     while (get_header(oafp, &hdr)) {
196         if ((hdr.unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR) {
197             if (stat(hdr.name, &stbuf) >= 0)    /* exist ? */
198                 add_sp(&sp, hdr.name, strlen(hdr.name) + 1);
199         }
200         else if ((hdr.unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY) {
201             strcpy(name, hdr.name);
202             len = strlen(name);
203             if (len > 0 && name[len - 1] == '/')
204                 name[--len] = '\0'; /* strip tail '/' */
205             if (stat(name, &stbuf) >= 0)    /* exist ? */
206                 add_sp(&sp, name, len + 1);
207         }
208         fseeko(oafp, hdr.packed_size, SEEK_CUR);
209     }
210
211     fseeko(oafp, pos, SEEK_SET);
212
213     finish_sp(&sp, &cmd_filec, &cmd_filev);
214 }
215
216 /* ------------------------------------------------------------------------ */
217 static void
218 delete(oafp, nafp)
219     FILE           *oafp, *nafp;
220 {
221     LzHeader ahdr;
222     off_t old_header_pos;
223
224     old_header_pos = ftello(oafp);
225     while (get_header(oafp, &ahdr)) {
226         if (need_file(ahdr.name)) { /* skip */
227             fseeko(oafp, ahdr.packed_size, SEEK_CUR);
228             if (noexec || !quiet) {
229                 if ((ahdr.unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
230                     message("delete %s -> %s", ahdr.realname, ahdr.name);
231                 else
232                     message("delete %s", ahdr.name);
233             }
234         }
235         else {      /* copy */
236             if (noexec) {
237                 fseeko(oafp, ahdr.packed_size, SEEK_CUR);
238             }
239             else {
240                 fseeko(oafp, old_header_pos, SEEK_SET);
241                 copy_old_one(oafp, nafp, &ahdr);
242             }
243         }
244         old_header_pos = ftello(oafp);
245     }
246     return;
247 }
248
249 /* ------------------------------------------------------------------------ */
250 /*                                                                          */
251 /* ------------------------------------------------------------------------ */
252 static FILE    *
253 build_temporary_file()
254 {
255     FILE *afp;
256
257     signal(SIGINT, interrupt);
258 #ifdef SIGHUP
259     signal(SIGHUP, interrupt);
260 #endif
261
262     temporary_fd = build_temporary_name();
263     if (temporary_fd == -1)
264         fatal_error("Cannot open temporary file \"%s\"", temporary_name);
265
266     afp = fdopen(temporary_fd, WRITE_BINARY);
267     if (afp == NULL)
268         fatal_error("Cannot open temporary file \"%s\"", temporary_name);
269
270     return afp;
271 }
272
273 /* ------------------------------------------------------------------------ */
274 static void
275 build_backup_file()
276 {
277
278     build_backup_name(backup_archive_name, archive_name);
279     if (!noexec) {
280         signal(SIGINT, SIG_IGN);
281 #ifdef SIGHUP
282         signal(SIGHUP, SIG_IGN);
283 #endif
284         if (rename(archive_name, backup_archive_name) < 0) {
285 #if __MINGW32__
286             /* On MinGW, cannot rename when
287                newfile (backup_archive_name) already exists */
288             if (unlink(backup_archive_name) < 0 ||
289                 rename(archive_name, backup_archive_name) < 0)
290 #endif
291             fatal_error("Cannot make backup file \"%s\"", archive_name);
292         }
293         recover_archive_when_interrupt = TRUE;
294         signal(SIGINT, interrupt);
295 #ifdef SIGHUP
296         signal(SIGHUP, interrupt);
297 #endif
298     }
299 }
300
301 /* ------------------------------------------------------------------------ */
302 static void
303 report_archive_name_if_different()
304 {
305     if (!quiet && new_archive_name == new_archive_name_buffer) {
306         /* warning at old archive is SFX */
307         message("New archive file is \"%s\"", new_archive_name);
308     }
309 }
310
311 /* ------------------------------------------------------------------------ */
312 void
313 temporary_to_new_archive_file(new_archive_size)
314     size_t new_archive_size;
315 {
316     FILE *oafp, *nafp;
317
318     if (!strcmp(new_archive_name, "-")) {
319         nafp = stdout;
320         writing_filename = "starndard output";
321 #if __MINGW32__
322         setmode(fileno(stdout), O_BINARY);
323 #endif
324     }
325     else {
326         unlink(new_archive_name);
327         if (rename(temporary_name, new_archive_name) == 0)
328             return;
329         nafp = xfopen(new_archive_name, WRITE_BINARY);
330         writing_filename = archive_name;
331     }
332
333     oafp = xfopen(temporary_name, READ_BINARY);
334     reading_filename = temporary_name;
335     copyfile(oafp, nafp, new_archive_size, 0, 0);
336     if (nafp != stdout)
337         fclose(nafp);
338     fclose(oafp);
339
340     recover_archive_when_interrupt = FALSE;
341     unlink(temporary_name);
342 }
343
344 /* ------------------------------------------------------------------------ */
345 static void
346 set_archive_file_mode()
347 {
348     int             umask_value;
349     struct stat     stbuf;
350
351     if (archive_file_gid < 0) {
352         umask(umask_value = umask(0));
353         archive_file_mode = (~umask_value) & 0666;  /* rw-rw-rw- */
354         if (stat(".", &stbuf) >= 0)
355             archive_file_gid = stbuf.st_gid;
356     }
357     if (archive_file_gid >= 0)
358         chown(new_archive_name, getuid(), archive_file_gid);
359
360     chmod(new_archive_name, archive_file_mode);
361 }
362
363 /* ------------------------------------------------------------------------ */
364 /*                          REMOVE FILE/DIRECTORY                           */
365 /* ------------------------------------------------------------------------ */
366 static void
367 remove_one(name)
368     char           *name;
369 {
370     struct stat     stbuf;
371     int             filec;
372     char          **filev;
373
374     if (GETSTAT(name, &stbuf) < 0) {
375         warning("Cannot access \"%s\": %s", name, strerror(errno));
376     }
377     else if (is_directory(&stbuf)) {
378         if (find_files(name, &filec, &filev)) {
379             remove_files(filec, filev);
380             free_files(filec, filev);
381         }
382         else
383             warning("Cannot open directory \"%s\"", name);
384
385         if (noexec)
386             message("REMOVE DIRECTORY %s", name);
387         else if (rmdir(name) < 0)
388             warning("Cannot remove directory \"%s\"", name);
389         else if (verbose)
390             message("Removed %s.", name);
391     }
392     else if (is_regularfile(&stbuf)) {
393         if (noexec)
394             message("REMOVE FILE %s.", name);
395         else if (unlink(name) < 0)
396             warning("Cannot remove \"%s\"", name);
397         else if (verbose)
398             message("Removed %s.", name);
399     }
400 #ifdef S_IFLNK
401     else if (is_symlink(&stbuf)) {
402         if (noexec)
403             printf("REMOVE SYMBOLIC LINK %s.\n", name);
404         else if (unlink(name) < 0)
405             warning("Cannot remove", name);
406         else if (verbose)
407             message("Removed %s.", name);
408     }
409 #endif
410     else {
411         error("Cannot remove file \"%s\" (not a file or directory)", name);
412     }
413 }
414
415 static void
416 remove_files(filec, filev)
417     int             filec;
418     char          **filev;
419 {
420     int             i;
421
422     for (i = 0; i < filec; i++)
423         remove_one(filev[i]);
424 }
425
426 /* ------------------------------------------------------------------------ */
427 /*                                                                          */
428 /* ------------------------------------------------------------------------ */
429 void
430 cmd_add()
431 {
432     LzHeader        ahdr;
433     FILE           *oafp, *nafp;
434     int             i;
435     long            old_header;
436     boolean         old_archive_exist;
437     size_t          new_archive_size;
438
439     /* exit if no operation */
440     if (!update_if_newer && cmd_filec == 0) {
441         error("No files given in argument, do nothing.");
442         exit(1);
443     }
444
445     /* open old archive if exist */
446     if ((oafp = open_old_archive()) == NULL)
447         old_archive_exist = FALSE;
448     else
449         old_archive_exist = TRUE;
450
451     if (update_if_newer && cmd_filec == 0) {
452         warning("No files given in argument");
453         if (!oafp) {
454             error("archive file \"%s\" does not exists.",
455                   archive_name);
456             exit(1);
457         }
458     }
459
460     if (new_archive && old_archive_exist) {
461         fclose(oafp);
462         oafp = NULL;
463     }
464
465     if (oafp && archive_is_msdos_sfx1(archive_name)) {
466         seek_lha_header(oafp);
467         build_standard_archive_name(new_archive_name_buffer, archive_name);
468         new_archive_name = new_archive_name_buffer;
469     }
470     else {
471         new_archive_name = archive_name;
472     }
473
474     /* build temporary file */
475     nafp = NULL;                /* avoid compiler warnings `uninitialized' */
476     if (!noexec)
477         nafp = build_temporary_file();
478
479     /* find needed files when automatic update */
480     if (update_if_newer && cmd_filec == 0)
481         find_update_files(oafp);
482
483     /* build new archive file */
484     /* cleaning arguments */
485     cleaning_files(&cmd_filec, &cmd_filev);
486     if (cmd_filec == 0) {
487         if (oafp)
488             fclose(oafp);
489         if (!noexec)
490             fclose(nafp);
491         return;
492     }
493
494     for (i = 0; i < cmd_filec; i++) {
495         int j;
496
497         if (strcmp(cmd_filev[i], archive_name) == 0) {
498             /* exclude target archive */
499             warning("specified file \"%s\" is the generating archive. skip",
500                     cmd_filev[i]);
501             for (j = i; j < cmd_filec-1; j++)
502                 cmd_filev[j] = cmd_filev[j+1];
503             cmd_filec--;
504             i--;
505             continue;
506         }
507
508         /* exclude files specified by -x option */
509         for (j = 0; exclude_files && exclude_files[j]; j++) {
510             if (fnmatch(exclude_files[j], basename(cmd_filev[i]),
511                         FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
512                 goto next;
513         }
514
515         oafp = append_it(cmd_filev[i], oafp, nafp);
516     next:
517         ;
518     }
519
520     if (oafp) {
521         old_header = ftello(oafp);
522         while (get_header(oafp, &ahdr)) {
523             if (noexec)
524                 fseeko(oafp, ahdr.packed_size, SEEK_CUR);
525             else {
526                 fseeko(oafp, old_header, SEEK_SET);
527                 copy_old_one(oafp, nafp, &ahdr);
528             }
529             old_header = ftello(oafp);
530         }
531         fclose(oafp);
532     }
533
534     new_archive_size = 0;       /* avoid compiler warnings `uninitialized' */
535     if (!noexec) {
536         off_t tmp;
537
538         write_archive_tail(nafp);
539         tmp = ftello(nafp);
540         if (tmp == -1) {
541             warning("ftello(): %s", strerror(errno));
542             new_archive_size = 0;
543         }
544         else
545             new_archive_size = tmp;
546
547         fclose(nafp);
548     }
549
550     /* build backup archive file */
551     if (old_archive_exist && backup_old_archive)
552         build_backup_file();
553
554     report_archive_name_if_different();
555
556     /* copy temporary file to new archive file */
557     if (!noexec) {
558         if (strcmp(new_archive_name, "-") == 0 ||
559             rename(temporary_name, new_archive_name) < 0) {
560
561             temporary_to_new_archive_file(new_archive_size);
562         }
563     }
564
565     /* set new archive file mode/group */
566     set_archive_file_mode();
567
568     /* remove archived files */
569     if (delete_after_append)
570         remove_files(cmd_filec, cmd_filev);
571
572     return;
573 }
574
575 /* ------------------------------------------------------------------------ */
576 void
577 cmd_delete()
578 {
579     FILE *oafp, *nafp;
580     size_t new_archive_size;
581
582     /* open old archive if exist */
583     if ((oafp = open_old_archive()) == NULL)
584         fatal_error("Cannot open archive file \"%s\"", archive_name);
585
586     /* exit if no operation */
587     if (cmd_filec == 0) {
588         fclose(oafp);
589         warning("No files given in argument, do nothing.");
590         return;
591     }
592
593     if (archive_is_msdos_sfx1(archive_name)) {
594         seek_lha_header(oafp);
595         build_standard_archive_name(new_archive_name_buffer, archive_name);
596         new_archive_name = new_archive_name_buffer;
597     }
598     else {
599         new_archive_name = archive_name;
600     }
601
602     /* build temporary file */
603     nafp = NULL;                /* avoid compiler warnings `uninitialized' */
604     if (!noexec)
605         nafp = build_temporary_file();
606
607     /* build new archive file */
608     delete(oafp, nafp);
609     fclose(oafp);
610
611     new_archive_size = 0;       /* avoid compiler warnings `uninitialized' */
612     if (!noexec) {
613         off_t tmp;
614
615         write_archive_tail(nafp);
616         tmp = ftello(nafp);
617         if (tmp == -1) {
618             warning("ftello(): %s", strerror(errno));
619             new_archive_size = 0;
620         }
621         else
622             new_archive_size = tmp;
623
624         fclose(nafp);
625     }
626
627     /* build backup archive file */
628     if (backup_old_archive)
629         build_backup_file();
630
631     /* 1999.5.24 t.oka */
632     if(!noexec && new_archive_size <= 1){
633         unlink(temporary_name);
634         if (!backup_old_archive)
635             unlink(archive_name);
636         warning("The archive file \"%s\" was removed because it would be empty.", new_archive_name);
637         return;
638     }
639
640     report_archive_name_if_different();
641
642     /* copy temporary file to new archive file */
643     if (!noexec) {
644         if (rename(temporary_name, new_archive_name) < 0)
645             temporary_to_new_archive_file(new_archive_size);
646     }
647
648     /* set new archive file mode/group */
649     set_archive_file_mode();
650
651     return;
652 }