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 long header_pos, next_pos, org_pos, data_pos;
23 long v_original_size, v_packed_size;
26 reading_filename = hdr->name;
27 writting_filename = temporary_name;
29 if (!fp && generic_format) /* [generic] doesn't need directory
32 header_pos = ftell(nafp);
33 write_header(nafp, hdr);/* DUMMY */
35 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
36 char buf[256], *b1, *b2;
38 strcpy(buf, hdr->name);
39 b1 = strtok(buf, "|");
40 b2 = strtok(NULL, "|");
41 printf("%s -> %s\t- Symbolic Link\n", b1, b2);
45 if (hdr->original_size == 0) /* empty file or directory */
46 return; /* previous write_header is not DUMMY. (^_^) */
49 data_pos = ftell(nafp);
51 hdr->crc = encode_lzhuf(fp, nafp, hdr->original_size,
52 &v_original_size, &v_packed_size, hdr->name, hdr->method);
54 if (v_packed_size < v_original_size) {
55 next_pos = ftell(nafp);
57 else { /* retry by stored method */
58 fseek(fp, org_pos, SEEK_SET);
59 fseek(nafp, data_pos, SEEK_SET);
60 hdr->crc = encode_stored_crc(fp, nafp, hdr->original_size,
61 &v_original_size, &v_packed_size);
63 next_pos = ftell(nafp);
65 ftruncate(fileno(nafp), next_pos);
67 bcopy(LZHUFF0_METHOD, hdr->method, METHOD_TYPE_STRAGE);
69 hdr->original_size = v_original_size;
70 hdr->packed_size = v_packed_size;
71 fseek(nafp, header_pos, SEEK_SET);
72 write_header(nafp, hdr);
73 fseek(nafp, next_pos, SEEK_SET);
77 /* ------------------------------------------------------------------------ */
79 append_it(name, oafp, nafp)
90 struct stat stbuf /*, lstbuf*/;
92 boolean directory, symlink;
94 if (GETSTAT(name, &stbuf) < 0) {
95 error("Cannot access", name); /* See cleaning_files, Why? */
99 directory = is_directory(&stbuf);
101 symlink = is_symlink(&stbuf);
105 init_header(name, &stbuf, &hdr);
107 if (!directory && !noexec)
111 fp = xfopen(name, READ_BINARY);
117 old_header = ftell(oafp);
118 if (!get_header(oafp, &ahdr)) {
124 cmp = STRING_COMPARE(ahdr.name, hdr.name);
126 /* for symbolic link. t.okamoto */
127 cmp = strcmp_filename(ahdr.name, hdr.name);
128 if (cmp < 0) { /* SKIP */
129 /* copy old to new */
131 fseek(oafp, old_header, SEEK_SET);
132 copy_old_one(oafp, nafp, &ahdr);
135 fseek(oafp, ahdr.packed_size, SEEK_CUR);
136 } else if (cmp == 0) { /* REPLACE */
137 /* drop old archive's */
138 fseek(oafp, ahdr.packed_size, SEEK_CUR);
140 } else { /* cmp > 0, INSERT */
141 fseek(oafp, old_header, SEEK_SET);
147 if (update_if_newer) {
148 if (!oafp || /* not in archive */
150 ahdr.unix_last_modified_stamp < /* newer than archive's */
151 hdr.unix_last_modified_stamp) {
153 printf("ADD %s\n", name);
155 add_one(fp, nafp, &hdr);
156 } else { /* cmp == 0 *//* copy old to new */
158 fseek(oafp, old_header, SEEK_SET);
159 copy_old_one(oafp, nafp, &ahdr);
163 if (!oafp || cmp > 0) { /* not in archive or dropped */
165 printf("ADD %s\n", name);
167 add_one(fp, nafp, &hdr);
169 else { /* cmp == 0 */
174 add_one(fp, nafp, &hdr);
183 else { /* recurcive call */
184 if (find_files(name, &filec, &filev)) {
185 for (i = 0; i < filec; i++)
186 oafp = append_it(filev[i], oafp, nafp);
187 free_files(filec, filev);
193 /* ------------------------------------------------------------------------ */
195 find_update_files(oafp)
196 FILE *oafp; /* old archive */
198 char name[FILENAME_LENGTH];
199 struct string_pool sp;
208 while (get_header(oafp, &hdr)) {
209 if ((hdr.unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR) {
210 if (stat(hdr.name, &stbuf) >= 0) /* exist ? */
211 add_sp(&sp, hdr.name, strlen(hdr.name) + 1);
213 else if ((hdr.unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY) {
214 strcpy(name, hdr.name);
216 if (len > 0 && name[len - 1] == '/')
217 name[--len] = '\0'; /* strip tail '/' */
218 if (stat(name, &stbuf) >= 0) /* exist ? */
219 add_sp(&sp, name, len + 1);
221 fseek(oafp, hdr.packed_size, SEEK_CUR);
224 fseek(oafp, pos, SEEK_SET);
226 finish_sp(&sp, &cmd_filec, &cmd_filev);
229 /* ------------------------------------------------------------------------ */
236 char lpath[256], *b1, *b2;
238 old_header_pos = ftell(oafp);
239 while (get_header(oafp, &ahdr)) {
240 strcpy(lpath, ahdr.name);
241 b1 = strtok(lpath, "|");
242 b2 = strtok(NULL, "|");
243 if (need_file(b1)) { /* skip */
244 fseek(oafp, ahdr.packed_size, SEEK_CUR);
245 if (noexec || !quiet)
247 printf("delete %s -> %s\n", b1, b2);
249 printf("delete %s\n", b1);
253 fseek(oafp, ahdr.packed_size, SEEK_CUR);
256 fseek(oafp, old_header_pos, SEEK_SET);
257 copy_old_one(oafp, nafp, &ahdr);
260 old_header_pos = ftell(oafp);
265 /* ------------------------------------------------------------------------ */
267 /* ------------------------------------------------------------------------ */
269 build_temporary_file()
273 signal(SIGINT, interrupt);
275 signal(SIGHUP, interrupt);
278 remove_temporary_at_error = TRUE;
279 temporary_fd = build_temporary_name();
280 if (temporary_fd == -1)
281 fatal_error(temporary_name);
283 afp = fdopen(temporary_fd, WRITE_BINARY);
285 fatal_error(temporary_name);
290 /* ------------------------------------------------------------------------ */
295 build_backup_name(backup_archive_name, archive_name);
297 signal(SIGINT, SIG_IGN);
299 signal(SIGHUP, SIG_IGN);
301 if (rename(archive_name, backup_archive_name) < 0)
302 fatal_error(archive_name);
303 recover_archive_when_interrupt = TRUE;
304 signal(SIGINT, interrupt);
306 signal(SIGHUP, interrupt);
311 /* ------------------------------------------------------------------------ */
313 report_archive_name_if_different()
315 if (!quiet && new_archive_name == new_archive_name_buffer) {
316 /* warning at old archive is SFX */
317 printf("New archive file is \"%s\"\n", new_archive_name);
321 /* ------------------------------------------------------------------------ */
323 temporary_to_new_archive_file(new_archive_size)
324 long new_archive_size;
328 if (!strcmp(new_archive_name, "-")) {
330 writting_filename = "starndard output";
332 setmode(fileno(stdout), O_BINARY);
336 unlink(new_archive_name);
337 if (rename(temporary_name, new_archive_name) == 0)
339 nafp = xfopen(new_archive_name, WRITE_BINARY);
340 writting_filename = archive_name;
343 oafp = xfopen(temporary_name, READ_BINARY);
344 reading_filename = temporary_name;
345 copyfile(oafp, nafp, new_archive_size, 0);
350 recover_archive_when_interrupt = FALSE;
351 unlink(temporary_name);
353 remove_temporary_at_error = FALSE;
356 /* ------------------------------------------------------------------------ */
358 set_archive_file_mode()
363 if (archive_file_gid < 0) {
364 umask(umask_value = umask(0));
365 archive_file_mode = (~umask_value) & 0666; /* rw-rw-rw- */
366 if (stat(".", &stbuf) >= 0)
367 archive_file_gid = stbuf.st_gid;
369 if (archive_file_gid >= 0)
370 chown(new_archive_name, getuid(), archive_file_gid);
372 chmod(new_archive_name, archive_file_mode);
375 /* ------------------------------------------------------------------------ */
376 /* REMOVE FILE/DIRECTORY */
377 /* ------------------------------------------------------------------------ */
386 if (GETSTAT(name, &stbuf) < 0) {
387 warning("Cannot access", name);
389 else if (is_directory(&stbuf)) {
390 if (find_files(name, &filec, &filev)) {
391 remove_files(filec, filev);
392 free_files(filec, filev);
395 warning("Cannot open directory", name);
398 printf("REMOVE DIRECTORY %s\n", name);
399 else if (rmdir(name) < 0)
400 warning("Cannot remove directory", name);
402 printf("Removed %s.\n", name);
404 else if (is_regularfile(&stbuf)) {
406 printf("REMOVE FILE %s.\n", name);
407 else if (unlink(name) < 0)
408 warning("Cannot remove", name);
410 printf("Removed %s.\n", name);
413 else if (is_symlink(&stbuf)) {
415 printf("REMOVE SYMBOLIC LINK %s.\n", name);
416 else if (unlink(name) < 0)
417 warning("Cannot remove", name);
419 printf("Removed %s.\n", name);
423 error("Cannot remove (not a file or directory)", name);
428 remove_files(filec, filev)
434 for (i = 0; i < filec; i++)
435 remove_one(filev[i]);
438 /* ------------------------------------------------------------------------ */
440 /* ------------------------------------------------------------------------ */
448 boolean old_archive_exist;
449 long new_archive_size;
451 /* exit if no operation */
452 if (!update_if_newer && cmd_filec == 0) {
453 error("No files given in argument, do nothing.", "");
457 /* open old archive if exist */
458 if ((oafp = open_old_archive()) == NULL)
459 old_archive_exist = FALSE;
461 old_archive_exist = TRUE;
463 if (update_if_newer && cmd_filec == 0 && !oafp)
464 fatal_error(archive_name); /* exit if cannot execute
465 * automatic update */
468 if (new_archive && old_archive_exist) {
473 if (oafp && archive_is_msdos_sfx1(archive_name)) {
474 skip_msdos_sfx1_code(oafp);
475 build_standard_archive_name(new_archive_name_buffer, archive_name);
476 new_archive_name = new_archive_name_buffer;
479 new_archive_name = archive_name;
482 /* build temporary file */
484 nafp = build_temporary_file();
486 /* find needed files when automatic update */
487 if (update_if_newer && cmd_filec == 0)
488 find_update_files(oafp);
490 /* build new archive file */
491 /* cleaning arguments */
492 cleaning_files(&cmd_filec, &cmd_filev);
493 if (cmd_filec == 0) {
501 for (i = 0; i < cmd_filec; i++)
502 oafp = append_it(cmd_filev[i], oafp, nafp);
504 old_header = ftell(oafp);
505 while (get_header(oafp, &ahdr)) {
507 fseek(oafp, ahdr.packed_size, SEEK_CUR);
509 fseek(oafp, old_header, SEEK_SET);
510 copy_old_one(oafp, nafp, &ahdr);
512 old_header = ftell(oafp);
517 write_archive_tail(nafp);
518 new_archive_size = ftell(nafp);
522 /* build backup archive file */
523 if (old_archive_exist)
526 report_archive_name_if_different();
528 /* copy temporary file to new archive file */
529 if (!noexec && (!strcmp(new_archive_name, "-") ||
530 rename(temporary_name, new_archive_name) < 0))
531 temporary_to_new_archive_file(new_archive_size);
533 /* set new archive file mode/group */
534 set_archive_file_mode();
536 /* remove archived files */
537 if (delete_after_append)
538 remove_files(cmd_filec, cmd_filev);
543 /* ------------------------------------------------------------------------ */
548 long new_archive_size;
550 /* open old archive if exist */
551 if ((oafp = open_old_archive()) == NULL)
552 fatal_error(archive_name);
555 /* exit if no operation */
556 if (cmd_filec == 0) {
558 warning("No files given in argument, do nothing.", "");
562 if (archive_is_msdos_sfx1(archive_name)) {
563 skip_msdos_sfx1_code(oafp);
564 build_standard_archive_name(new_archive_name_buffer, archive_name);
565 new_archive_name = new_archive_name_buffer;
568 new_archive_name = archive_name;
571 /* build temporary file */
573 nafp = build_temporary_file();
575 /* build new archive file */
580 write_archive_tail(nafp);
581 new_archive_size = ftell(nafp);
585 /* build backup archive file */
588 /* 1999.5.24 t.oka */
589 if(!noexec && new_archive_size <= 1){
590 unlink(temporary_name);
591 printf("New archive file \"%s\" is not created because it would be empty.\n", new_archive_name);
595 report_archive_name_if_different();
597 /* copy temporary file to new archive file */
598 if (!noexec && rename(temporary_name, new_archive_name) < 0)
599 temporary_to_new_archive_file(new_archive_size);
601 /* set new archive file mode/group */
602 set_archive_file_mode();
607 /* for symbolic link name. t.okamoto 96/2/20 */
608 int strcmp_filename( str1, str2 )
615 while (*p != 0 && *q != 0) {
617 if (*q == 0) return 0;
618 else if (*q != '|') return -1;
619 } else if (*q == '|') {
620 if (*p == 0) return 0;
621 else if (*q != '|') return 1;
622 } else if (*p != *q) break;
625 return (int)*p-(int)*q;
629 /* Local Variables: */
632 /* compile-command:"gcc -c lhadd.c" */
634 /* vi: set tabstop=4: */