Commit | Line | Data |
---|---|---|
60a17f4e JH |
1 | /* util.c - Several utility routines for cpio. |
2 | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <sys/types.h> | |
20 | #ifdef HPUX_CDF | |
21 | #include <sys/stat.h> | |
22 | #endif | |
23 | #include "system.h" | |
24 | #include "cpiohdr.h" | |
25 | #include "dstring.h" | |
26 | #include "extern.h" | |
27 | #include "rmt.h" | |
28 | ||
29 | #ifndef __MSDOS__ | |
30 | #include <sys/ioctl.h> | |
31 | #else | |
32 | #include <io.h> | |
33 | #endif | |
34 | ||
35 | #ifdef HAVE_SYS_MTIO_H | |
36 | #ifdef HAVE_SYS_IO_TRIOCTL_H | |
37 | #include <sys/io/trioctl.h> | |
38 | #endif | |
39 | #include <sys/mtio.h> | |
40 | #endif | |
41 | ||
42 | static void empty_output_buffer_swap (); | |
43 | static void hash_insert (); | |
44 | ||
45 | /* Write `output_size' bytes of `output_buffer' to file | |
46 | descriptor OUT_DES and reset `output_size' and `out_buff'. */ | |
47 | ||
48 | void | |
49 | empty_output_buffer (out_des) | |
50 | int out_des; | |
51 | { | |
52 | int bytes_written; | |
53 | ||
54 | #ifdef BROKEN_LONG_TAPE_DRIVER | |
55 | static long output_bytes_before_lseek = 0; | |
56 | #endif | |
57 | ||
58 | if (swapping_halfwords || swapping_bytes) | |
59 | { | |
60 | empty_output_buffer_swap (out_des); | |
61 | return; | |
62 | } | |
63 | ||
64 | #ifdef BROKEN_LONG_TAPE_DRIVER | |
65 | /* Some tape drivers seem to have a signed internal seek pointer and | |
66 | they lose if it overflows and becomes negative (e.g. when writing | |
67 | tapes > 2Gb). Doing an lseek (des, 0, SEEK_SET) seems to reset the | |
68 | seek pointer and prevent it from overflowing. */ | |
69 | if (output_is_special | |
70 | && (output_bytes_before_lseek += output_size) < 0L) | |
71 | { | |
72 | lseek(out_des, 0L, SEEK_SET); | |
73 | output_bytes_before_lseek = 0; | |
74 | } | |
75 | #endif | |
76 | ||
77 | bytes_written = rmtwrite (out_des, output_buffer, output_size); | |
78 | if (bytes_written != output_size) | |
79 | { | |
80 | int rest_bytes_written; | |
81 | int rest_output_size; | |
82 | ||
83 | if (output_is_special | |
84 | && (bytes_written >= 0 | |
85 | || (bytes_written < 0 | |
86 | && (errno == ENOSPC || errno == EIO || errno == ENXIO)))) | |
87 | { | |
88 | get_next_reel (out_des); | |
89 | if (bytes_written > 0) | |
90 | rest_output_size = output_size - bytes_written; | |
91 | else | |
92 | rest_output_size = output_size; | |
93 | rest_bytes_written = rmtwrite (out_des, output_buffer, | |
94 | rest_output_size); | |
95 | if (rest_bytes_written != rest_output_size) | |
96 | error (1, errno, "write error"); | |
97 | } | |
98 | else | |
99 | error (1, errno, "write error"); | |
100 | } | |
101 | output_bytes += output_size; | |
102 | out_buff = output_buffer; | |
103 | output_size = 0; | |
104 | } | |
105 | ||
106 | /* Write `output_size' bytes of `output_buffer' to file | |
107 | descriptor OUT_DES with byte and/or halfword swapping and reset | |
108 | `output_size' and `out_buff'. This routine should not be called | |
109 | with `swapping_bytes' set unless the caller knows that the | |
110 | file being written has an even number of bytes, and it should not be | |
111 | called with `swapping_halfwords' set unless the caller knows | |
112 | that the file being written has a length divisible by 4. If either | |
113 | of those restrictions are not met, bytes may be lost in the output | |
114 | file. OUT_DES must refer to a file that we are creating during | |
115 | a process_copy_in, so we don't have to check for end of media | |
116 | errors or be careful about only writing in blocks of `output_size' | |
117 | bytes. */ | |
118 | ||
119 | static void | |
120 | empty_output_buffer_swap (out_des) | |
121 | int out_des; | |
122 | { | |
123 | /* Since `output_size' might not be divisible by 4 or 2, we might | |
124 | not be able to be able to swap all the bytes and halfwords in | |
125 | `output_buffer' (e.g., if `output_size' is odd), so we might not be | |
126 | able to write them all. We will swap and write as many bytes as | |
127 | we can, and save the rest in `left_overs' for the next time we are | |
128 | called. */ | |
129 | static char left_overs[4]; | |
130 | static int left_over_bytes = 0; | |
131 | ||
132 | int bytes_written; | |
133 | int complete_halfwords; | |
134 | int complete_words; | |
135 | int extra_bytes; | |
136 | ||
137 | output_bytes += output_size; | |
138 | ||
139 | out_buff = output_buffer; | |
140 | ||
141 | if (swapping_halfwords) | |
142 | { | |
143 | if (left_over_bytes != 0) | |
144 | { | |
145 | while (output_size > 0 && left_over_bytes < 4) | |
146 | { | |
147 | left_overs[left_over_bytes++] = *out_buff++; | |
148 | --output_size; | |
149 | } | |
150 | if (left_over_bytes < 4) | |
151 | { | |
152 | out_buff = output_buffer; | |
153 | output_size = 0; | |
154 | return; | |
155 | } | |
156 | swahw_array (left_overs, 1); | |
157 | if (swapping_bytes) | |
158 | swab_array (left_overs, 2); | |
159 | bytes_written = rmtwrite (out_des, left_overs, 4); | |
160 | if (bytes_written != 4) | |
161 | error (1, errno, "write error"); | |
162 | left_over_bytes = 0; | |
163 | } | |
164 | complete_words = output_size / 4; | |
165 | if (complete_words > 0) | |
166 | { | |
167 | swahw_array (out_buff, complete_words); | |
168 | if (swapping_bytes) | |
169 | swab_array (out_buff, 2 * complete_words); | |
170 | bytes_written = rmtwrite (out_des, out_buff, 4 * complete_words); | |
171 | if (bytes_written != (4 * complete_words)) | |
172 | error (1, errno, "write error"); | |
173 | } | |
174 | out_buff += (4 * complete_words); | |
175 | extra_bytes = output_size % 4; | |
176 | while (extra_bytes > 0) | |
177 | { | |
178 | left_overs[left_over_bytes++] = *out_buff++; | |
179 | --extra_bytes; | |
180 | } | |
181 | ||
182 | } | |
183 | else | |
184 | { | |
185 | if (left_over_bytes != 0) | |
186 | { | |
187 | while (output_size > 0 && left_over_bytes < 2) | |
188 | { | |
189 | left_overs[left_over_bytes++] = *out_buff++; | |
190 | --output_size; | |
191 | } | |
192 | if (left_over_bytes < 2) | |
193 | { | |
194 | out_buff = output_buffer; | |
195 | output_size = 0; | |
196 | return; | |
197 | } | |
198 | swab_array (left_overs, 1); | |
199 | bytes_written = rmtwrite (out_des, left_overs, 2); | |
200 | if (bytes_written != 2) | |
201 | error (1, errno, "write error"); | |
202 | left_over_bytes = 0; | |
203 | } | |
204 | complete_halfwords = output_size / 2; | |
205 | if (complete_halfwords > 0) | |
206 | { | |
207 | swab_array (out_buff, complete_halfwords); | |
208 | bytes_written = rmtwrite (out_des, out_buff, 2 * complete_halfwords); | |
209 | if (bytes_written != (2 * complete_halfwords)) | |
210 | error (1, errno, "write error"); | |
211 | } | |
212 | out_buff += (2 * complete_halfwords); | |
213 | extra_bytes = output_size % 2; | |
214 | while (extra_bytes > 0) | |
215 | { | |
216 | left_overs[left_over_bytes++] = *out_buff++; | |
217 | --extra_bytes; | |
218 | } | |
219 | } | |
220 | ||
221 | out_buff = output_buffer; | |
222 | output_size = 0; | |
223 | } | |
224 | ||
225 | /* Exchange the halfwords of each element of the array of COUNT longs | |
226 | starting at PTR. PTR does not have to be aligned at a word | |
227 | boundary. */ | |
228 | ||
229 | void | |
230 | swahw_array (ptr, count) | |
231 | char *ptr; | |
232 | int count; | |
233 | { | |
234 | char tmp; | |
235 | ||
236 | for (; count > 0; --count) | |
237 | { | |
238 | tmp = *ptr; | |
239 | *ptr = *(ptr + 2); | |
240 | *(ptr + 2) = tmp; | |
241 | ++ptr; | |
242 | tmp = *ptr; | |
243 | *ptr = *(ptr + 2); | |
244 | *(ptr + 2) = tmp; | |
245 | ptr += 3; | |
246 | } | |
247 | } | |
248 | ||
249 | /* Read at most NUM_BYTES or `io_block_size' bytes, whichever is smaller, | |
250 | into the start of `input_buffer' from file descriptor IN_DES. | |
251 | Set `input_size' to the number of bytes read and reset `in_buff'. | |
252 | Exit with an error if end of file is reached. */ | |
253 | ||
254 | #ifdef BROKEN_LONG_TAPE_DRIVER | |
255 | static long input_bytes_before_lseek = 0; | |
256 | #endif | |
257 | ||
258 | void | |
259 | fill_input_buffer (in_des, num_bytes) | |
260 | int in_des; | |
261 | int num_bytes; | |
262 | { | |
263 | #ifdef BROKEN_LONG_TAPE_DRIVER | |
264 | /* Some tape drivers seem to have a signed internal seek pointer and | |
265 | they lose if it overflows and becomes negative (e.g. when writing | |
266 | tapes > 4Gb). Doing an lseek (des, 0, SEEK_SET) seems to reset the | |
267 | seek pointer and prevent it from overflowing. */ | |
268 | if (input_is_special | |
269 | && (input_bytes_before_lseek += num_bytes) < 0L) | |
270 | { | |
271 | lseek(in_des, 0L, SEEK_SET); | |
272 | input_bytes_before_lseek = 0; | |
273 | } | |
274 | #endif | |
275 | in_buff = input_buffer; | |
276 | num_bytes = (num_bytes < io_block_size) ? num_bytes : io_block_size; | |
277 | input_size = rmtread (in_des, input_buffer, num_bytes); | |
278 | if (input_size == 0 && input_is_special) | |
279 | { | |
280 | get_next_reel (in_des); | |
281 | input_size = rmtread (in_des, input_buffer, num_bytes); | |
282 | } | |
283 | if (input_size < 0) | |
284 | error (1, errno, "read error"); | |
285 | if (input_size == 0) | |
286 | { | |
287 | error (0, 0, "premature end of file"); | |
288 | exit (1); | |
289 | } | |
290 | input_bytes += input_size; | |
291 | } | |
292 | \f | |
293 | /* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full. | |
294 | When `out_buff' fills up, flush it to file descriptor OUT_DES. */ | |
295 | ||
296 | void | |
297 | copy_buf_out (in_buf, out_des, num_bytes) | |
298 | char *in_buf; | |
299 | int out_des; | |
300 | long num_bytes; | |
301 | { | |
302 | register long bytes_left = num_bytes; /* Bytes needing to be copied. */ | |
303 | register long space_left; /* Room left in output buffer. */ | |
304 | ||
305 | while (bytes_left > 0) | |
306 | { | |
307 | space_left = io_block_size - output_size; | |
308 | if (space_left == 0) | |
309 | empty_output_buffer (out_des); | |
310 | else | |
311 | { | |
312 | if (bytes_left < space_left) | |
313 | space_left = bytes_left; | |
314 | bcopy (in_buf, out_buff, (unsigned) space_left); | |
315 | out_buff += space_left; | |
316 | output_size += space_left; | |
317 | in_buf += space_left; | |
318 | bytes_left -= space_left; | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | /* Copy NUM_BYTES of buffer `in_buff' into IN_BUF. | |
324 | `in_buff' may be partly full. | |
325 | When `in_buff' is exhausted, refill it from file descriptor IN_DES. */ | |
326 | ||
327 | void | |
328 | copy_in_buf (in_buf, in_des, num_bytes) | |
329 | char *in_buf; | |
330 | int in_des; | |
331 | long num_bytes; | |
332 | { | |
333 | register long bytes_left = num_bytes; /* Bytes needing to be copied. */ | |
334 | register long space_left; /* Bytes to copy from input buffer. */ | |
335 | ||
336 | while (bytes_left > 0) | |
337 | { | |
338 | if (input_size == 0) | |
339 | fill_input_buffer (in_des, io_block_size); | |
340 | if (bytes_left < input_size) | |
341 | space_left = bytes_left; | |
342 | else | |
343 | space_left = input_size; | |
344 | bcopy (in_buff, in_buf, (unsigned) space_left); | |
345 | in_buff += space_left; | |
346 | in_buf += space_left; | |
347 | input_size -= space_left; | |
348 | bytes_left -= space_left; | |
349 | } | |
350 | } | |
351 | ||
352 | /* Copy the the next NUM_BYTES bytes of `input_buffer' into PEEK_BUF. | |
353 | If NUM_BYTES bytes are not available, read the next `io_block_size' bytes | |
354 | into the end of `input_buffer' and update `input_size'. | |
355 | ||
356 | Return the number of bytes copied into PEEK_BUF. | |
357 | If the number of bytes returned is less than NUM_BYTES, | |
358 | then EOF has been reached. */ | |
359 | ||
360 | int | |
361 | peek_in_buf (peek_buf, in_des, num_bytes) | |
362 | char *peek_buf; | |
363 | int in_des; | |
364 | int num_bytes; | |
365 | { | |
366 | long tmp_input_size; | |
367 | long got_bytes; | |
368 | char *append_buf; | |
369 | ||
370 | #ifdef BROKEN_LONG_TAPE_DRIVER | |
371 | /* Some tape drivers seem to have a signed internal seek pointer and | |
372 | they lose if it overflows and becomes negative (e.g. when writing | |
373 | tapes > 4Gb). Doing an lseek (des, 0, SEEK_SET) seems to reset the | |
374 | seek pointer and prevent it from overflowing. */ | |
375 | if (input_is_special | |
376 | && (input_bytes_before_lseek += num_bytes) < 0L) | |
377 | { | |
378 | lseek(in_des, 0L, SEEK_SET); | |
379 | input_bytes_before_lseek = 0; | |
380 | } | |
381 | #endif | |
382 | ||
383 | while (input_size < num_bytes) | |
384 | { | |
385 | append_buf = in_buff + input_size; | |
386 | tmp_input_size = rmtread (in_des, append_buf, io_block_size); | |
387 | if (tmp_input_size == 0) | |
388 | { | |
389 | if (input_is_special) | |
390 | { | |
391 | get_next_reel (in_des); | |
392 | tmp_input_size = rmtread (in_des, append_buf, io_block_size); | |
393 | } | |
394 | else | |
395 | break; | |
396 | } | |
397 | if (tmp_input_size < 0) | |
398 | error (1, errno, "read error"); | |
399 | input_bytes += tmp_input_size; | |
400 | input_size += tmp_input_size; | |
401 | } | |
402 | if (num_bytes <= input_size) | |
403 | got_bytes = num_bytes; | |
404 | else | |
405 | got_bytes = input_size; | |
406 | bcopy (in_buff, peek_buf, (unsigned) got_bytes); | |
407 | return got_bytes; | |
408 | } | |
409 | \f | |
410 | /* Skip the next NUM_BYTES bytes of file descriptor IN_DES. */ | |
411 | ||
412 | void | |
413 | toss_input (in_des, num_bytes) | |
414 | int in_des; | |
415 | long num_bytes; | |
416 | { | |
417 | register long bytes_left = num_bytes; /* Bytes needing to be copied. */ | |
418 | register long space_left; /* Bytes to copy from input buffer. */ | |
419 | ||
420 | while (bytes_left > 0) | |
421 | { | |
422 | if (input_size == 0) | |
423 | fill_input_buffer (in_des, io_block_size); | |
424 | if (bytes_left < input_size) | |
425 | space_left = bytes_left; | |
426 | else | |
427 | space_left = input_size; | |
428 | in_buff += space_left; | |
429 | input_size -= space_left; | |
430 | bytes_left -= space_left; | |
431 | } | |
432 | } | |
433 | \f | |
434 | /* Copy a file using the input and output buffers, which may start out | |
435 | partly full. After the copy, the files are not closed nor the last | |
436 | block flushed to output, and the input buffer may still be partly | |
437 | full. If `crc_i_flag' is set, add each byte to `crc'. | |
438 | IN_DES is the file descriptor for input; | |
439 | OUT_DES is the file descriptor for output; | |
440 | NUM_BYTES is the number of bytes to copy. */ | |
441 | ||
442 | void | |
443 | copy_files (in_des, out_des, num_bytes) | |
444 | int in_des; | |
445 | int out_des; | |
446 | long num_bytes; | |
447 | { | |
448 | long size; | |
449 | long k; | |
450 | ||
451 | while (num_bytes > 0) | |
452 | { | |
453 | if (input_size == 0) | |
454 | fill_input_buffer (in_des, io_block_size); | |
455 | size = (input_size < num_bytes) ? input_size : num_bytes; | |
456 | if (crc_i_flag) | |
457 | { | |
458 | for (k = 0; k < size; ++k) | |
459 | crc += in_buff[k] & 0xff; | |
460 | } | |
461 | copy_buf_out (in_buff, out_des, size); | |
462 | num_bytes -= size; | |
463 | input_size -= size; | |
464 | in_buff += size; | |
465 | } | |
466 | } | |
467 | \f | |
468 | /* Create all directories up to but not including the last part of NAME. | |
469 | Do not destroy any nondirectories while creating directories. */ | |
470 | ||
471 | void | |
472 | create_all_directories (name) | |
473 | char *name; | |
474 | { | |
475 | char *dir; | |
476 | int mode; | |
477 | #ifdef HPUX_CDF | |
478 | int cdf; | |
479 | #endif | |
480 | ||
481 | dir = dirname (name); | |
482 | mode = 0700; | |
483 | #ifdef HPUX_CDF | |
484 | cdf = islastparentcdf (name); | |
485 | if (cdf) | |
486 | { | |
487 | dir [strlen (dir) - 1] = '\0'; /* remove final + */ | |
488 | mode = 04700; | |
489 | } | |
490 | ||
491 | #endif | |
492 | ||
493 | if (dir == NULL) | |
494 | error (2, 0, "virtual memory exhausted"); | |
495 | ||
496 | if (dir[0] != '.' || dir[1] != '\0') | |
497 | make_path (dir, mode, 0700, -1, -1, (char *) NULL); | |
498 | ||
499 | free (dir); | |
500 | } | |
501 | ||
502 | /* Prepare to append to an archive. We have been in | |
503 | process_copy_in, keeping track of the position where | |
504 | the last header started in `last_header_start'. Now we | |
505 | have the starting position of the last header (the TRAILER!!! | |
506 | header, or blank record for tar archives) and we want to start | |
507 | writing (appending) over the last header. The last header may | |
508 | be in the middle of a block, so to keep the buffering in sync | |
509 | we lseek back to the start of the block, read everything up | |
510 | to but not including the last header, lseek back to the start | |
511 | of the block, and then do a copy_buf_out of what we read. | |
512 | Actually, we probably don't have to worry so much about keeping the | |
513 | buffering perfect since you can only append to archives that | |
514 | are disk files. */ | |
515 | ||
516 | void | |
517 | prepare_append (out_file_des) | |
518 | int out_file_des; | |
519 | { | |
520 | int start_of_header; | |
521 | int start_of_block; | |
522 | int useful_bytes_in_block; | |
523 | char *tmp_buf; | |
524 | ||
525 | start_of_header = last_header_start; | |
526 | /* Figure out how many bytes we will rewrite, and where they start. */ | |
527 | useful_bytes_in_block = start_of_header % io_block_size; | |
528 | start_of_block = start_of_header - useful_bytes_in_block; | |
529 | ||
530 | if (lseek (out_file_des, start_of_block, SEEK_SET) < 0) | |
531 | error (1, errno, "cannot seek on output"); | |
532 | if (useful_bytes_in_block > 0) | |
533 | { | |
534 | tmp_buf = (char *) xmalloc (useful_bytes_in_block); | |
535 | read (out_file_des, tmp_buf, useful_bytes_in_block); | |
536 | if (lseek (out_file_des, start_of_block, SEEK_SET) < 0) | |
537 | error (1, errno, "cannot seek on output"); | |
538 | copy_buf_out (tmp_buf, out_file_des, useful_bytes_in_block); | |
539 | free (tmp_buf); | |
540 | } | |
541 | ||
542 | /* We are done reading the archive, so clear these since they | |
543 | will now be used for reading in files that we are appending | |
544 | to the archive. */ | |
545 | input_size = 0; | |
546 | input_bytes = 0; | |
547 | in_buff = input_buffer; | |
548 | } | |
549 | ||
550 | /* Support for remembering inodes with multiple links. Used in the | |
551 | "copy in" and "copy pass" modes for making links instead of copying | |
552 | the file. */ | |
553 | ||
554 | struct inode_val | |
555 | { | |
556 | unsigned long inode; | |
557 | unsigned long major_num; | |
558 | unsigned long minor_num; | |
559 | char *file_name; | |
560 | }; | |
561 | ||
562 | /* Inode hash table. Allocated by first call to add_inode. */ | |
563 | static struct inode_val **hash_table = NULL; | |
564 | ||
565 | /* Size of current hash table. Initial size is 47. (47 = 2*22 + 3) */ | |
566 | static int hash_size = 22; | |
567 | ||
568 | /* Number of elements in current hash table. */ | |
569 | static int hash_num; | |
570 | ||
571 | /* Find the file name associated with NODE_NUM. If there is no file | |
572 | associated with NODE_NUM, return NULL. */ | |
573 | ||
574 | char * | |
575 | find_inode_file (node_num, major_num, minor_num) | |
576 | unsigned long node_num; | |
577 | unsigned long major_num; | |
578 | unsigned long minor_num; | |
579 | { | |
580 | #ifndef __MSDOS__ | |
581 | int start; /* Initial hash location. */ | |
582 | int temp; /* Rehash search variable. */ | |
583 | ||
584 | if (hash_table != NULL) | |
585 | { | |
586 | /* Hash function is node number modulo the table size. */ | |
587 | start = node_num % hash_size; | |
588 | ||
589 | /* Initial look into the table. */ | |
590 | if (hash_table[start] == NULL) | |
591 | return NULL; | |
592 | if (hash_table[start]->inode == node_num | |
593 | && hash_table[start]->major_num == major_num | |
594 | && hash_table[start]->minor_num == minor_num) | |
595 | return hash_table[start]->file_name; | |
596 | ||
597 | /* The home position is full with a different inode record. | |
598 | Do a linear search terminated by a NULL pointer. */ | |
599 | for (temp = (start + 1) % hash_size; | |
600 | hash_table[temp] != NULL && temp != start; | |
601 | temp = (temp + 1) % hash_size) | |
602 | { | |
603 | if (hash_table[temp]->inode == node_num | |
604 | && hash_table[start]->major_num == major_num | |
605 | && hash_table[start]->minor_num == minor_num) | |
606 | return hash_table[temp]->file_name; | |
607 | } | |
608 | } | |
609 | #endif | |
610 | return NULL; | |
611 | } | |
612 | ||
613 | /* Associate FILE_NAME with the inode NODE_NUM. (Insert into hash table.) */ | |
614 | ||
615 | void | |
616 | add_inode (node_num, file_name, major_num, minor_num) | |
617 | unsigned long node_num; | |
618 | char *file_name; | |
619 | unsigned long major_num; | |
620 | unsigned long minor_num; | |
621 | { | |
622 | #ifndef __MSDOS__ | |
623 | struct inode_val *temp; | |
624 | ||
625 | /* Create new inode record. */ | |
626 | temp = (struct inode_val *) xmalloc (sizeof (struct inode_val)); | |
627 | temp->inode = node_num; | |
628 | temp->major_num = major_num; | |
629 | temp->minor_num = minor_num; | |
630 | temp->file_name = xstrdup (file_name); | |
631 | ||
632 | /* Do we have to increase the size of (or initially allocate) | |
633 | the hash table? */ | |
634 | if (hash_num == hash_size || hash_table == NULL) | |
635 | { | |
636 | struct inode_val **old_table; /* Pointer to old table. */ | |
637 | int i; /* Index for re-insert loop. */ | |
638 | ||
639 | /* Save old table. */ | |
640 | old_table = hash_table; | |
641 | if (old_table == NULL) | |
642 | hash_num = 0; | |
643 | ||
644 | /* Calculate new size of table and allocate it. | |
645 | Sequence of table sizes is 47, 97, 197, 397, 797, 1597, 3197, 6397 ... | |
646 | where 3197 and most of the sizes after 6397 are not prime. The other | |
647 | numbers listed are prime. */ | |
648 | hash_size = 2 * hash_size + 3; | |
649 | hash_table = (struct inode_val **) | |
650 | xmalloc (hash_size * sizeof (struct inode_val *)); | |
651 | bzero (hash_table, hash_size * sizeof (struct inode_val *)); | |
652 | ||
653 | /* Insert the values from the old table into the new table. */ | |
654 | for (i = 0; i < hash_num; i++) | |
655 | hash_insert (old_table[i]); | |
656 | ||
657 | if (old_table != NULL) | |
658 | free (old_table); | |
659 | } | |
660 | ||
661 | /* Insert the new record and increment the count of elements in the | |
662 | hash table. */ | |
663 | hash_insert (temp); | |
664 | hash_num++; | |
665 | #endif /* __MSDOS__ */ | |
666 | } | |
667 | ||
668 | /* Do the hash insert. Used in normal inserts and resizing the hash | |
669 | table. It is guaranteed that there is room to insert the item. | |
670 | NEW_VALUE is the pointer to the previously allocated inode, file | |
671 | name association record. */ | |
672 | ||
673 | static void | |
674 | hash_insert (new_value) | |
675 | struct inode_val *new_value; | |
676 | { | |
677 | int start; /* Home position for the value. */ | |
678 | int temp; /* Used for rehashing. */ | |
679 | ||
680 | /* Hash function is node number modulo the table size. */ | |
681 | start = new_value->inode % hash_size; | |
682 | ||
683 | /* Do the initial look into the table. */ | |
684 | if (hash_table[start] == NULL) | |
685 | { | |
686 | hash_table[start] = new_value; | |
687 | return; | |
688 | } | |
689 | ||
690 | /* If we get to here, the home position is full with a different inode | |
691 | record. Do a linear search for the first NULL pointer and insert | |
692 | the new item there. */ | |
693 | temp = (start + 1) % hash_size; | |
694 | while (hash_table[temp] != NULL) | |
695 | temp = (temp + 1) % hash_size; | |
696 | ||
697 | /* Insert at the NULL. */ | |
698 | hash_table[temp] = new_value; | |
699 | } | |
700 | \f | |
701 | /* Open FILE in the mode specified by the command line options | |
702 | and return an open file descriptor for it, | |
703 | or -1 if it can't be opened. */ | |
704 | ||
705 | int | |
706 | open_archive (file) | |
707 | char *file; | |
708 | { | |
709 | int fd; | |
710 | void (*copy_in) (); /* Workaround for pcc bug. */ | |
711 | ||
712 | copy_in = process_copy_in; | |
713 | ||
714 | if (copy_function == copy_in) | |
715 | fd = rmtopen (file, O_RDONLY | O_BINARY, 0666); | |
716 | else | |
717 | { | |
718 | if (!append_flag) | |
719 | fd = rmtopen (file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); | |
720 | else | |
721 | fd = rmtopen (file, O_RDWR | O_BINARY, 0666); | |
722 | } | |
723 | ||
724 | return fd; | |
725 | } | |
726 | ||
727 | /* Attempt to rewind the tape drive on file descriptor TAPE_DES | |
728 | and take it offline. */ | |
729 | ||
730 | void | |
731 | tape_offline (tape_des) | |
732 | int tape_des; | |
733 | { | |
734 | #if defined(MTIOCTOP) && defined(MTOFFL) | |
735 | struct mtop control; | |
736 | ||
737 | control.mt_op = MTOFFL; | |
738 | control.mt_count = 1; | |
739 | rmtioctl (tape_des, MTIOCTOP, &control); /* Don't care if it fails. */ | |
740 | #endif | |
741 | } | |
742 | ||
743 | /* The file on file descriptor TAPE_DES is assumed to be magnetic tape | |
744 | (or floppy disk or other device) and the end of the medium | |
745 | has been reached. Ask the user for to mount a new "tape" to continue | |
746 | the processing. If the user specified the device name on the | |
747 | command line (with the -I, -O, -F or --file options), then we can | |
748 | automatically re-open the same device to use the next medium. If the | |
749 | user did not specify the device name, then we have to ask them which | |
750 | device to use. */ | |
751 | ||
752 | void | |
753 | get_next_reel (tape_des) | |
754 | int tape_des; | |
755 | { | |
756 | static int reel_number = 1; | |
757 | FILE *tty_in; /* File for interacting with user. */ | |
758 | FILE *tty_out; /* File for interacting with user. */ | |
759 | int old_tape_des; | |
760 | char *next_archive_name; | |
761 | dynamic_string new_name; | |
762 | char *str_res; | |
763 | ||
764 | ds_init (&new_name, 128); | |
765 | ||
766 | /* Open files for interactive communication. */ | |
767 | tty_in = fopen (CONSOLE, "r"); | |
768 | if (tty_in == NULL) | |
769 | error (2, errno, CONSOLE); | |
770 | tty_out = fopen (CONSOLE, "w"); | |
771 | if (tty_out == NULL) | |
772 | error (2, errno, CONSOLE); | |
773 | ||
774 | old_tape_des = tape_des; | |
775 | tape_offline (tape_des); | |
776 | rmtclose (tape_des); | |
777 | ||
778 | /* Give message and wait for carrage return. User should hit carrage return | |
779 | only after loading the next tape. */ | |
780 | ++reel_number; | |
781 | if (new_media_message) | |
782 | fprintf (tty_out, "%s", new_media_message); | |
783 | else if (new_media_message_with_number) | |
784 | fprintf (tty_out, "%s%d%s", new_media_message_with_number, reel_number, | |
785 | new_media_message_after_number); | |
786 | else if (archive_name) | |
787 | fprintf (tty_out, "Found end of tape. Load next tape and press RETURN. "); | |
788 | else | |
789 | fprintf (tty_out, "Found end of tape. To continue, type device/file name when ready.\n"); | |
790 | ||
791 | fflush (tty_out); | |
792 | ||
793 | if (archive_name) | |
794 | { | |
795 | int c; | |
796 | ||
797 | do | |
798 | c = getc (tty_in); | |
799 | while (c != EOF && c != '\n'); | |
800 | ||
801 | tape_des = open_archive (archive_name); | |
802 | if (tape_des == -1) | |
803 | error (1, errno, "%s", archive_name); | |
804 | } | |
805 | else | |
806 | { | |
807 | do | |
808 | { | |
809 | if (tape_des < 0) | |
810 | { | |
811 | fprintf (tty_out, | |
812 | "To continue, type device/file name when ready.\n"); | |
813 | fflush (tty_out); | |
814 | } | |
815 | ||
816 | str_res = ds_fgets (tty_in, &new_name); | |
817 | if (str_res == NULL || str_res[0] == '\0') | |
818 | exit (1); | |
819 | next_archive_name = str_res; | |
820 | ||
821 | tape_des = open_archive (next_archive_name); | |
822 | if (tape_des == -1) | |
823 | error (0, errno, "%s", next_archive_name); | |
824 | } | |
825 | while (tape_des < 0); | |
826 | } | |
827 | ||
828 | /* We have to make sure that `tape_des' has not changed its value even | |
829 | though we closed it and reopened it, since there are local | |
830 | copies of it in other routines. This works fine on Unix (even with | |
831 | rmtread and rmtwrite) since open will always return the lowest | |
832 | available file descriptor and we haven't closed any files (e.g., | |
833 | stdin, stdout or stderr) that were opened before we originally opened | |
834 | the archive. */ | |
835 | ||
836 | if (tape_des != old_tape_des) | |
837 | error (1, 0, "internal error: tape descriptor changed from %d to %d", | |
838 | old_tape_des, tape_des); | |
839 | ||
840 | free (new_name.ds_string); | |
841 | fclose (tty_in); | |
842 | fclose (tty_out); | |
843 | } | |
844 | ||
845 | /* If MESSAGE does not contain the string "%d", make `new_media_message' | |
846 | a copy of MESSAGE. If MESSAGES does contain the string "%d", make | |
847 | `new_media_message_with_number' a copy of MESSAGE up to, but | |
848 | not including, the string "%d", and make `new_media_message_after_number' | |
849 | a copy of MESSAGE after the string "%d". */ | |
850 | ||
851 | void | |
852 | set_new_media_message (message) | |
853 | char *message; | |
854 | { | |
855 | char *p; | |
856 | int prev_was_percent; | |
857 | ||
858 | p = message; | |
859 | prev_was_percent = 0; | |
860 | while (*p != '\0') | |
861 | { | |
862 | if (*p == 'd' && prev_was_percent) | |
863 | break; | |
864 | prev_was_percent = (*p == '%'); | |
865 | ++p; | |
866 | } | |
867 | if (*p == '\0') | |
868 | { | |
869 | new_media_message = xstrdup (message); | |
870 | } | |
871 | else | |
872 | { | |
873 | int length = p - message - 1; | |
874 | ||
875 | new_media_message_with_number = xmalloc (length + 1); | |
876 | strncpy (new_media_message_with_number, message, length); | |
877 | new_media_message_with_number[length] = '\0'; | |
878 | length = strlen (p + 1); | |
879 | new_media_message_after_number = xmalloc (length + 1); | |
880 | strcpy (new_media_message_after_number, message); | |
881 | } | |
882 | } | |
883 | ||
884 | #ifdef SYMLINK_USES_UMASK | |
885 | /* Most machines always create symlinks with rwxrwxrwx protection, | |
886 | but some (HP/UX 8.07; maybe DEC's OSF on MIPS, too?) use the | |
887 | umask when creating symlinks, so if your umask is 022 you end | |
888 | up with rwxr-xr-x symlinks (although HP/UX seems to completely | |
889 | ignore the protection). There doesn't seem to be any way to | |
890 | manipulate the modes once the symlinks are created (e.g. | |
891 | a hypothetical "lchmod"), so to create them with the right | |
892 | modes we have to set the umask first. */ | |
893 | ||
894 | int | |
895 | umasked_symlink (name1, name2, mode) | |
896 | char *name1; | |
897 | char *name2; | |
898 | int mode; | |
899 | { | |
900 | int old_umask; | |
901 | int rc; | |
902 | mode = ~(mode & 0777) & 0777; | |
903 | old_umask = umask (mode); | |
904 | rc = symlink (name1, name2); | |
905 | umask (old_umask); | |
906 | return rc; | |
907 | } | |
908 | #endif /* SYMLINK_USES_UMASK */ | |
909 | ||
910 | #ifdef __MSDOS__ | |
911 | int | |
912 | chown (path, owner, group) | |
913 | char *path; | |
914 | int owner, group; | |
915 | { | |
916 | return 0; | |
917 | } | |
918 | #endif | |
919 | ||
920 | #ifdef __TURBOC__ | |
921 | #include <time.h> | |
922 | #include <fcntl.h> | |
923 | #include <io.h> | |
924 | ||
925 | int | |
926 | utime (char *filename, struct utimbuf *utb) | |
927 | { | |
928 | extern int errno; | |
929 | struct tm *tm; | |
930 | struct ftime filetime; | |
931 | time_t when; | |
932 | int fd; | |
933 | int status; | |
934 | ||
935 | if (utb == 0) | |
936 | when = time (0); | |
937 | else | |
938 | when = utb->modtime; | |
939 | ||
940 | fd = _open (filename, O_RDWR); | |
941 | if (fd == -1) | |
942 | return -1; | |
943 | ||
944 | tm = localtime (&when); | |
945 | if (tm->tm_year < 80) | |
946 | filetime.ft_year = 0; | |
947 | else | |
948 | filetime.ft_year = tm->tm_year - 80; | |
949 | filetime.ft_month = tm->tm_mon + 1; | |
950 | filetime.ft_day = tm->tm_mday; | |
951 | if (tm->tm_hour < 0) | |
952 | filetime.ft_hour = 0; | |
953 | else | |
954 | filetime.ft_hour = tm->tm_hour; | |
955 | filetime.ft_min = tm->tm_min; | |
956 | filetime.ft_tsec = tm->tm_sec / 2; | |
957 | ||
958 | status = setftime (fd, &filetime); | |
959 | _close (fd); | |
960 | return status; | |
961 | } | |
962 | #endif | |
963 | #ifdef HPUX_CDF | |
964 | /* When we create a cpio archive we mark CDF's by putting an extra `/' | |
965 | after their component name so we can distinguish the CDF's when we | |
966 | extract the archive (in case the "hidden" directory's files appear | |
967 | in the archive before the directory itself). E.g., in the path | |
968 | "a/b+/c", if b+ is a CDF, we will write this path as "a/b+//c" in | |
969 | the archive so when we extract the archive we will know that b+ | |
970 | is actually a CDF, and not an ordinary directory whose name happens | |
971 | to end in `+'. We also do the same thing internally in copypass.c. */ | |
972 | ||
973 | ||
974 | /* Take an input pathname and check it for CDF's. Insert an extra | |
975 | `/' in the pathname after each "hidden" directory. If we add | |
976 | any `/'s, return a malloced string (which it will reuse for | |
977 | later calls so our caller doesn't have to worry about freeing | |
978 | the string) instead of the original input string. */ | |
979 | ||
980 | char * | |
981 | add_cdf_double_slashes (input_name) | |
982 | char *input_name; | |
983 | { | |
984 | static char *ret_name = NULL; /* re-usuable return buffer (malloc'ed) */ | |
985 | static int ret_size = -1; /* size of return buffer. */ | |
986 | char *p; | |
987 | char *q; | |
988 | int n; | |
989 | struct stat dir_stat; | |
990 | ||
991 | /* Search for a `/' preceeded by a `+'. */ | |
992 | ||
993 | for (p = input_name; *p != '\0'; ++p) | |
994 | { | |
995 | if ( (*p == '+') && (*(p + 1) == '/') ) | |
996 | break; | |
997 | } | |
998 | ||
999 | /* If we didn't find a `/' preceeded by a `+' then there are | |
1000 | no CDF's in this pathname. Return the original pathname. */ | |
1001 | ||
1002 | if (*p == '\0') | |
1003 | return input_name; | |
1004 | ||
1005 | /* There was a `/' preceeded by a `+' in the pathname. If it is a CDF | |
1006 | then we will need to copy the input pathname to our return | |
1007 | buffer so we can insert the extra `/'s. Since we can't tell | |
1008 | yet whether or not it is a CDF we will just always copy the | |
1009 | string to the return buffer. First we have to make sure the | |
1010 | buffer is large enough to hold the string and any number of | |
1011 | extra `/'s we might add. */ | |
1012 | ||
1013 | n = 2 * (strlen (input_name) + 1); | |
1014 | if (n >= ret_size) | |
1015 | { | |
1016 | if (ret_size < 0) | |
1017 | ret_name = (char *) malloc (n); | |
1018 | else | |
1019 | ret_name = (char *)realloc (ret_name, n); | |
1020 | ret_size = n; | |
1021 | } | |
1022 | ||
1023 | /* Clear the `/' after this component, so we can stat the pathname | |
1024 | up to and including this component. */ | |
1025 | ++p; | |
1026 | *p = '\0'; | |
1027 | if ((*xstat) (input_name, &dir_stat) < 0) | |
1028 | { | |
1029 | error (0, errno, "%s", input_name); | |
1030 | return input_name; | |
1031 | } | |
1032 | ||
1033 | /* Now put back the `/' after this component and copy the pathname up to | |
1034 | and including this component and its trailing `/' to the return | |
1035 | buffer. */ | |
1036 | *p++ = '/'; | |
1037 | strncpy (ret_name, input_name, p - input_name); | |
1038 | q = ret_name + (p - input_name); | |
1039 | ||
1040 | /* If it was a CDF, add another `/'. */ | |
1041 | if (S_ISDIR (dir_stat.st_mode) && (dir_stat.st_mode & 04000) ) | |
1042 | *q++ = '/'; | |
1043 | ||
1044 | /* Go through the rest of the input pathname, copying it to the | |
1045 | return buffer, and adding an extra `/' after each CDF. */ | |
1046 | while (*p != '\0') | |
1047 | { | |
1048 | if ( (*p == '+') && (*(p + 1) == '/') ) | |
1049 | { | |
1050 | *q++ = *p++; | |
1051 | ||
1052 | *p = '\0'; | |
1053 | if ((*xstat) (input_name, &dir_stat) < 0) | |
1054 | { | |
1055 | error (0, errno, "%s", input_name); | |
1056 | return input_name; | |
1057 | } | |
1058 | *p = '/'; | |
1059 | ||
1060 | if (S_ISDIR (dir_stat.st_mode) && (dir_stat.st_mode & 04000) ) | |
1061 | *q++ = '/'; | |
1062 | } | |
1063 | *q++ = *p++; | |
1064 | } | |
1065 | *q = '\0'; | |
1066 | ||
1067 | return ret_name; | |
1068 | } | |
1069 | ||
1070 | /* Is the last parent directory (e.g., c in a/b/c/d) a CDF? If the | |
1071 | directory name ends in `+' and is followed by 2 `/'s instead of 1 | |
1072 | then it is. This is only the case for cpio archives, but we don't | |
1073 | have to worry about tar because tar always has the directory before | |
1074 | its files (or else we lose). */ | |
1075 | ||
1076 | islastparentcdf(path) | |
1077 | char *path; | |
1078 | { | |
1079 | char *newpath; | |
1080 | char *slash; | |
1081 | int slash_count; | |
1082 | int length; /* Length of result, not including NUL. */ | |
1083 | ||
1084 | slash = rindex (path, '/'); | |
1085 | if (slash == 0) | |
1086 | return 0; | |
1087 | else | |
1088 | { | |
1089 | slash_count = 0; | |
1090 | while (slash > path && *slash == '/') | |
1091 | { | |
1092 | ++slash_count; | |
1093 | --slash; | |
1094 | } | |
1095 | ||
1096 | ||
1097 | if ( (*slash == '+') && (slash_count >= 2) ) | |
1098 | return 1; | |
1099 | } | |
1100 | return 0; | |
1101 | } | |
1102 | #endif |