1 /* ------------------------------------------------------------------------ */
3 /* lhadd.c -- LHarc Add Command */
5 /* Copyright (C) MCMLXXXIX Yooichi.Tagawa */
6 /* Modified Nobutaka Watazaki */
8 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
9 /* ------------------------------------------------------------------------ */
11 /* ------------------------------------------------------------------------ */
12 static void remove_files();
14 static char new_archive_name_buffer[FILENAME_LENGTH];
15 static char *new_archive_name;
16 /* ------------------------------------------------------------------------ */
18 add_one(fp, nafp, hdr)
22 off_t header_pos, next_pos, org_pos, data_pos;
23 size_t v_original_size, v_packed_size;
25 reading_filename = hdr->name;
26 writing_filename = temporary_name;
28 if (!fp && generic_format) /* [generic] doesn't need directory info. */
30 header_pos = ftello(nafp);
31 write_header(nafp, hdr);/* DUMMY */
33 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
35 printf("%s -> %s\t- Symbolic Link\n", hdr->realname, hdr->name);
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. (^_^) */
43 data_pos = ftello(nafp);
45 hdr->crc = encode_lzhuf(fp, nafp, hdr->original_size,
46 &v_original_size, &v_packed_size, hdr->name, hdr->method);
48 if (v_packed_size < v_original_size) {
49 next_pos = ftello(nafp);
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);
57 next_pos = ftello(nafp);
59 if (ftruncate(fileno(nafp), next_pos) == -1)
60 error("cannot truncate archive");
62 if (chsize(fileno(nafp), next_pos) == -1)
63 error("cannot truncate archive");
67 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
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);
77 /* ------------------------------------------------------------------------ */
79 append_it(name, oafp, nafp)
92 boolean directory, symlink;
94 if (GETSTAT(name, &stbuf) < 0) {
95 error("Cannot access file \"%s\"", name); /* See cleaning_files, Why? */
99 directory = is_directory(&stbuf);
101 symlink = is_symlink(&stbuf);
105 init_header(name, &stbuf, &hdr);
108 if (!directory && !symlink && !noexec) {
109 fp = fopen(name, READ_BINARY);
111 error("Cannot open file \"%s\": %s", name, strerror(errno));
116 cmp = 0; /* avoid compiler warnings `uninitialized' */
118 old_header = ftello(oafp);
119 if (!get_header(oafp, &ahdr)) {
120 /* end of archive or error occurred */
126 cmp = strcmp(ahdr.name, hdr.name);
127 if (cmp < 0) { /* SKIP */
128 /* copy old to new */
130 fseeko(oafp, old_header, SEEK_SET);
131 copy_old_one(oafp, nafp, &ahdr);
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);
139 } else { /* cmp > 0, INSERT */
140 fseeko(oafp, old_header, SEEK_SET);
145 if (!oafp || cmp > 0) { /* not in archive */
147 printf("ADD %s\n", name);
149 add_one(fp, nafp, &hdr);
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 */
156 printf("REPLACE %s\n", name);
158 add_one(fp, nafp, &hdr);
160 else { /* copy old to new */
162 fseeko(oafp, old_header, SEEK_SET);
163 copy_old_one(oafp, nafp, &ahdr);
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);
180 /* ------------------------------------------------------------------------ */
182 find_update_files(oafp)
183 FILE *oafp; /* old archive */
185 char name[FILENAME_LENGTH];
186 struct string_pool 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);
200 else if ((hdr.unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY) {
201 strcpy(name, hdr.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);
208 fseeko(oafp, hdr.packed_size, SEEK_CUR);
211 fseeko(oafp, pos, SEEK_SET);
213 finish_sp(&sp, &cmd_filec, &cmd_filev);
216 /* ------------------------------------------------------------------------ */
222 off_t old_header_pos;
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);
232 message("delete %s", ahdr.name);
237 fseeko(oafp, ahdr.packed_size, SEEK_CUR);
240 fseeko(oafp, old_header_pos, SEEK_SET);
241 copy_old_one(oafp, nafp, &ahdr);
244 old_header_pos = ftello(oafp);
249 /* ------------------------------------------------------------------------ */
251 /* ------------------------------------------------------------------------ */
253 build_temporary_file()
257 signal(SIGINT, interrupt);
259 signal(SIGHUP, interrupt);
262 temporary_fd = build_temporary_name();
263 if (temporary_fd == -1)
264 fatal_error("Cannot open temporary file \"%s\"", temporary_name);
266 afp = fdopen(temporary_fd, WRITE_BINARY);
268 fatal_error("Cannot open temporary file \"%s\"", temporary_name);
273 /* ------------------------------------------------------------------------ */
278 build_backup_name(backup_archive_name, archive_name);
280 signal(SIGINT, SIG_IGN);
282 signal(SIGHUP, SIG_IGN);
284 if (rename(archive_name, backup_archive_name) < 0) {
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)
291 fatal_error("Cannot make backup file \"%s\"", archive_name);
293 recover_archive_when_interrupt = TRUE;
294 signal(SIGINT, interrupt);
296 signal(SIGHUP, interrupt);
301 /* ------------------------------------------------------------------------ */
303 report_archive_name_if_different()
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);
311 /* ------------------------------------------------------------------------ */
313 temporary_to_new_archive_file(new_archive_size)
314 size_t new_archive_size;
318 if (!strcmp(new_archive_name, "-")) {
320 writing_filename = "starndard output";
322 setmode(fileno(stdout), O_BINARY);
326 unlink(new_archive_name);
327 if (rename(temporary_name, new_archive_name) == 0)
329 nafp = xfopen(new_archive_name, WRITE_BINARY);
330 writing_filename = archive_name;
333 oafp = xfopen(temporary_name, READ_BINARY);
334 reading_filename = temporary_name;
335 copyfile(oafp, nafp, new_archive_size, 0, 0);
340 recover_archive_when_interrupt = FALSE;
341 unlink(temporary_name);
344 /* ------------------------------------------------------------------------ */
346 set_archive_file_mode()
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;
357 if (archive_file_gid >= 0)
358 chown(new_archive_name, getuid(), archive_file_gid);
360 chmod(new_archive_name, archive_file_mode);
363 /* ------------------------------------------------------------------------ */
364 /* REMOVE FILE/DIRECTORY */
365 /* ------------------------------------------------------------------------ */
374 if (GETSTAT(name, &stbuf) < 0) {
375 warning("Cannot access \"%s\": %s", name, strerror(errno));
377 else if (is_directory(&stbuf)) {
378 if (find_files(name, &filec, &filev)) {
379 remove_files(filec, filev);
380 free_files(filec, filev);
383 warning("Cannot open directory \"%s\"", name);
386 message("REMOVE DIRECTORY %s", name);
387 else if (rmdir(name) < 0)
388 warning("Cannot remove directory \"%s\"", name);
390 message("Removed %s.", name);
392 else if (is_regularfile(&stbuf)) {
394 message("REMOVE FILE %s.", name);
395 else if (unlink(name) < 0)
396 warning("Cannot remove \"%s\"", name);
398 message("Removed %s.", name);
401 else if (is_symlink(&stbuf)) {
403 printf("REMOVE SYMBOLIC LINK %s.\n", name);
404 else if (unlink(name) < 0)
405 warning("Cannot remove", name);
407 message("Removed %s.", name);
411 error("Cannot remove file \"%s\" (not a file or directory)", name);
416 remove_files(filec, filev)
422 for (i = 0; i < filec; i++)
423 remove_one(filev[i]);
426 /* ------------------------------------------------------------------------ */
428 /* ------------------------------------------------------------------------ */
436 boolean old_archive_exist;
437 size_t new_archive_size;
439 /* exit if no operation */
440 if (!update_if_newer && cmd_filec == 0) {
441 error("No files given in argument, do nothing.");
445 /* open old archive if exist */
446 if ((oafp = open_old_archive()) == NULL)
447 old_archive_exist = FALSE;
449 old_archive_exist = TRUE;
451 if (update_if_newer && cmd_filec == 0) {
452 warning("No files given in argument");
454 error("archive file \"%s\" does not exists.",
460 if (new_archive && old_archive_exist) {
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;
471 new_archive_name = archive_name;
474 /* build temporary file */
475 nafp = NULL; /* avoid compiler warnings `uninitialized' */
477 nafp = build_temporary_file();
479 /* find needed files when automatic update */
480 if (update_if_newer && cmd_filec == 0)
481 find_update_files(oafp);
483 /* build new archive file */
484 /* cleaning arguments */
485 cleaning_files(&cmd_filec, &cmd_filev);
486 if (cmd_filec == 0) {
494 for (i = 0; i < cmd_filec; i++) {
497 if (strcmp(cmd_filev[i], archive_name) == 0) {
498 /* exclude target archive */
499 warning("specified file \"%s\" is the generating archive. skip",
501 for (j = i; j < cmd_filec-1; j++)
502 cmd_filev[j] = cmd_filev[j+1];
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)
515 oafp = append_it(cmd_filev[i], oafp, nafp);
521 old_header = ftello(oafp);
522 while (get_header(oafp, &ahdr)) {
524 fseeko(oafp, ahdr.packed_size, SEEK_CUR);
526 fseeko(oafp, old_header, SEEK_SET);
527 copy_old_one(oafp, nafp, &ahdr);
529 old_header = ftello(oafp);
534 new_archive_size = 0; /* avoid compiler warnings `uninitialized' */
538 write_archive_tail(nafp);
541 warning("ftello(): %s", strerror(errno));
542 new_archive_size = 0;
545 new_archive_size = tmp;
550 /* build backup archive file */
551 if (old_archive_exist && backup_old_archive)
554 report_archive_name_if_different();
556 /* copy temporary file to new archive file */
558 if (strcmp(new_archive_name, "-") == 0 ||
559 rename(temporary_name, new_archive_name) < 0) {
561 temporary_to_new_archive_file(new_archive_size);
565 /* set new archive file mode/group */
566 set_archive_file_mode();
568 /* remove archived files */
569 if (delete_after_append)
570 remove_files(cmd_filec, cmd_filev);
575 /* ------------------------------------------------------------------------ */
580 size_t new_archive_size;
582 /* open old archive if exist */
583 if ((oafp = open_old_archive()) == NULL)
584 fatal_error("Cannot open archive file \"%s\"", archive_name);
586 /* exit if no operation */
587 if (cmd_filec == 0) {
589 warning("No files given in argument, do nothing.");
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;
599 new_archive_name = archive_name;
602 /* build temporary file */
603 nafp = NULL; /* avoid compiler warnings `uninitialized' */
605 nafp = build_temporary_file();
607 /* build new archive file */
611 new_archive_size = 0; /* avoid compiler warnings `uninitialized' */
615 write_archive_tail(nafp);
618 warning("ftello(): %s", strerror(errno));
619 new_archive_size = 0;
622 new_archive_size = tmp;
627 /* build backup archive file */
628 if (backup_old_archive)
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);
640 report_archive_name_if_different();
642 /* copy temporary file to new archive file */
644 if (rename(temporary_name, new_archive_name) < 0)
645 temporary_to_new_archive_file(new_archive_size);
648 /* set new archive file mode/group */
649 set_archive_file_mode();