1 /* $Source: /u/mark/src/pax/RCS/buffer.c,v $
5 * buffer.c - Buffer management functions
9 * These functions handle buffer manipulations for the archiving
10 * formats. Functions are provided to get memory for buffers,
11 * flush buffers, read and write buffers and de-allocate buffers.
12 * Several housekeeping functions are provided as well to get some
13 * information about how full buffers are, etc.
17 * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
19 * Sponsored by The USENIX Association for public distribution.
21 * Copyright (c) 1989 Mark H. Colburn.
22 * All rights reserved.
24 * Redistribution and use in source and binary forms are permitted
25 * provided that the above copyright notice is duplicated in all such
26 * forms and that any documentation, advertising materials, and other
27 * materials related to such distribution and use acknowledge that the
28 * software was developed * by Mark H. Colburn and sponsored by The
31 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36 * Revision 1.2 89/02/12 10:04:02 mark
39 * Revision 1.1 88/12/23 18:02:01 mark
45 static char *ident = "$Id: buffer.c,v 1.2 89/02/12 10:04:02 mark Exp $";
46 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
55 /* Function Prototypes */
59 static int ar_write(int, char *, uint);
60 static void buf_pad(OFFSET);
61 static int indata(int, OFFSET, char *);
62 static void outflush(void);
63 static void buf_use(uint);
64 static int buf_in_avail(char **, uint *);
65 static uint buf_out_avail(char **);
69 static int ar_write();
70 static void buf_pad();
72 static void outflush();
73 static void buf_use();
74 static int buf_in_avail();
75 static uint buf_out_avail();
80 /* inentry - install a single archive entry
84 * Inentry reads an archive entry from the archive file and writes it
85 * out the the named file. If we are in PASS mode during archive
86 * processing, the pass() function is called, otherwise we will
87 * extract from the archive file.
89 * Inentry actaully calls indata to process the actual data to the
94 * char *name - name of the file to extract from the archive
95 * Stat *asb - stat block of the file to be extracted from the
100 * Returns zero if successful, -1 otherwise.
105 int inentry(char *name, Stat *asb)
109 int inentry(name, asb)
120 if ((ofd = openout(name, asb, linkp = linkfrom(name, asb), 0)) > 0) {
121 if (asb->sb_size || linkp == (Link *)NULL || linkp->l_size == 0) {
122 close(indata(ofd, asb->sb_size, name));
123 } else if ((ifd = open(linkp->l_path->p_name, O_RDONLY)) < 0) {
124 warn(linkp->l_path->p_name, strerror());
126 passdata(linkp->l_path->p_name, ifd, name, ofd);
131 return(buf_skip((OFFSET) asb->sb_size) >= 0);
133 tstamp[0] = (!f_pass && f_access_time) ? asb->sb_atime : time((time_t *) 0);
134 tstamp[1] = f_mtime ? asb->sb_mtime : time((time_t *) 0);
140 /* outdata - write archive data
144 * Outdata transfers data from the named file to the archive buffer.
145 * It knows about the file padding which is required by tar, but no
146 * by cpio. Outdata continues after file read errors, padding with
147 * null characters if neccessary. Closes the input file descriptor
152 * int fd - file descriptor of file to read data from
153 * char *name - name of file
154 * OFFSET size - size of the file
160 void outdata(int fd, char *name, OFFSET size)
164 void outdata(fd, name, size)
179 if (pad = (size % BLOCKSIZE)) {
180 pad = (BLOCKSIZE - pad);
183 avail = buf_out_avail(&buf);
184 size -= (chunk = size < avail ? (uint) size : avail);
185 if (oops == 0 && (got = read(fd, buf, (unsigned int) chunk)) < 0) {
187 warn(name, strerror());
194 warn(name, "Early EOF");
195 while (got < chunk) {
202 if (ar_format == TAR) {
203 buf_pad((OFFSET) pad);
208 /* write_eot - write the end of archive record(s)
212 * Write out an End-Of-Tape record. We actually zero at least one
213 * record, through the end of the block. Old tar writes garbage after
214 * two zeroed records -- and PDtar used to.
228 char header[M_STRLEN + H_STRLEN + 1];
230 if (ar_format == TAR) {
231 /* write out two zero blocks for trailer */
234 if (pad = (total + M_STRLEN + H_STRLEN + TRAILZ) % BLOCKSIZE) {
235 pad = BLOCKSIZE - pad;
237 strcpy(header, M_ASCII);
238 sprintf(header + M_STRLEN, H_PRINT, 0, 0,
239 0, 0, 0, 1, 0, (time_t) 0, TRAILZ, pad);
240 outwrite(header, M_STRLEN + H_STRLEN);
241 outwrite(TRAILER, TRAILZ);
243 buf_pad((OFFSET) pad);
248 /* outwrite - write archive data
252 * Writes out data in the archive buffer to the archive file. The
253 * buffer index and the total byte count are incremented by the number
254 * of data bytes written.
258 * char *idx - pointer to data to write
259 * uint len - length of the data to write
264 void outwrite(char *idx, uint len)
268 void outwrite(idx, len)
269 char *idx; /* pointer to data to write */
270 uint len; /* length of data to write */
279 while (want = endx - idx) {
280 if (bufend - bufidx < 0) {
281 fatal("Buffer overlow in out_write\n");
283 if ((have = bufend - bufidx) == 0) {
289 memcpy(bufidx, idx, (int) have);
297 /* passdata - copy data to one file
301 * Copies a file from one place to another. Doesn't believe in input
302 * file descriptor zero (see description of kludge in openin() comments).
303 * Closes the provided output file descriptor.
307 * char *from - input file name (old file)
308 * int ifd - input file descriptor
309 * char *to - output file name (new file)
310 * int ofd - output file descriptor
315 void passdata(char *from, int ifd, char *to, int ofd)
319 void passdata(from, ifd, to, ofd)
332 lseek(ifd, (OFFSET) 0, 0);
334 while ((got = read(ifd, block, sizeof(block))) > 0
335 && (sparse = ar_write(ofd, block, (uint) got)) >= 0) {
339 warn(got < 0 ? from : to, strerror());
340 } else if (sparse > 0
341 && (lseek(ofd, (OFFSET)(-sparse), 1) < 0
342 || write(ofd, block, (uint) sparse) != sparse)) {
343 warn(to, strerror());
350 /* buf_allocate - get space for the I/O buffer
354 * buf_allocate allocates an I/O buffer using malloc. The resulting
355 * buffer is used for all data buffering throughout the program.
356 * Buf_allocate must be called prior to any use of the buffer or any
357 * of the buffering calls.
361 * int size - size of the I/O buffer to request
365 * If an invalid size is given for a buffer or if a buffer of the
366 * required size cannot be allocated, then the function prints out an
367 * error message and returns a non-zero exit status to the calling
368 * process, terminating the program.
374 void buf_allocate(OFFSET size)
378 void buf_allocate(size)
384 fatal("invalid value for blocksize");
386 if ((bufstart = malloc((unsigned) size)) == (char *)NULL) {
387 fatal("Cannot allocate I/O buffer");
389 bufend = bufidx = bufstart;
394 /* buf_skip - skip input archive data
398 * Buf_skip skips past archive data. It is used when the length of
399 * the archive data is known, and we do not wish to process the data.
403 * OFFSET len - number of bytes to skip
407 * Returns zero under normal circumstances, -1 if unreadable data is
413 int buf_skip(OFFSET len)
426 if (bufend - bufidx < 0) {
427 fatal("Buffer overlow in buf_skip\n");
429 while ((chunk = bufend - bufidx) == 0) {
430 corrupt |= ar_read();
443 /* buf_read - read a given number of characters from the input archive
447 * Reads len number of characters from the input archive and
448 * stores them in the buffer pointed at by dst.
452 * char *dst - pointer to buffer to store data into
453 * uint len - length of data to read
457 * Returns zero with valid data, -1 if unreadable portions were
458 * replaced by null characters.
463 int buf_read(char *dst, uint len)
467 int buf_read(dst, len)
476 char *endx = dst + len;
478 while (want = endx - dst) {
479 if (bufend - bufidx < 0) {
480 fatal("Buffer overlow in buf_read\n");
482 while ((have = bufend - bufidx) == 0) {
484 corrupt |= ar_read();
489 memcpy(dst, bufidx, have);
498 /* indata - install data from an archive
502 * Indata writes size bytes of data from the archive buffer to the output
503 * file specified by fd. The filename which is being written, pointed
504 * to by name is provided only for diagnostics.
508 * int fd - output file descriptor
509 * OFFSET size - number of bytes to write to output file
510 * char *name - name of file which corresponds to fd
514 * Returns given file descriptor.
519 static int indata(int fd, OFFSET size, char *name)
523 static int indata(fd, size, name)
537 corrupt = sparse = 0;
540 corrupt |= buf_in_avail(&buf, &avail);
541 size -= (chunk = size < avail ? (uint) size : avail);
542 if (oops == (char *)NULL && (sparse = ar_write(fd, buf, chunk)) < 0) {
548 warn(name, "Corrupt archive data");
552 } else if (sparse > 0 && (lseek(fd, (OFFSET) - 1, 1) < 0
553 || write(fd, "", 1) != 1)) {
554 warn(name, strerror());
560 /* outflush - flush the output buffer
564 * The output buffer is written, if there is anything in it, to the
570 static void outflush(void)
574 static void outflush()
582 /* if (bufidx - buf > 0) */
583 for (buf = bufstart; len = bufidx - buf;) {
584 if ((got = write(archivefd, buf, MIN(len, blocksize))) > 0) {
586 } else if (got < 0) {
590 bufend = (bufidx = bufstart) + blocksize;
594 /* ar_read - fill the archive buffer
598 * Remembers mid-buffer read failures and reports them the next time
599 * through. Replaces unreadable data with null characters. Resets
600 * the buffer pointers as appropriate.
604 * Returns zero with valid data, -1 otherwise.
620 bufend = bufidx = bufstart;
629 while (!failed && !areof && bufstart + blocksize - bufend >= blocksize) {
630 if ((got = read(archivefd, bufend, (unsigned int) blocksize)) > 0) {
632 } else if (got < 0) {
634 warnarch(strerror(), (OFFSET) 0 - (bufend - bufidx));
640 if (failed && bufend == bufstart) {
642 for (got = 0; got < blocksize; ++got) {
651 /* ar_write - write a filesystem block
655 * Writes len bytes of data data from the specified buffer to the
656 * specified file. Seeks past sparse blocks.
660 * int fd - file to write to
661 * char *buf - buffer to get data from
662 * uint len - number of bytes to transfer
666 * Returns 0 if the block was written, the given length for a sparse
667 * block or -1 if unsuccessful.
672 static int ar_write(int fd, char *buf, uint len)
676 static int ar_write(fd, buf, len)
686 bend = (bidx = buf) + len;
687 while (bidx < bend) {
689 return (write(fd, buf, len) == len ? 0 : -1);
692 return (lseek(fd, (OFFSET) len, 1) < 0 ? -1 : len);
696 /* buf_pad - pad the archive buffer
700 * Buf_pad writes len zero bytes to the archive buffer in order to
705 * OFFSET pad - number of zero bytes to pad
711 static void buf_pad(OFFSET pad)
715 static void buf_pad(pad)
724 if ((have = bufend - bufidx) > pad) {
727 for (idx = 0; idx < have; ++idx) {
732 if (bufend - bufidx == 0) {
739 /* buf_use - allocate buffer space
743 * Buf_use marks space in the buffer as being used; advancing both the
744 * buffer index (bufidx) and the total byte count (total).
748 * uint len - Amount of space to allocate in the buffer
753 static void buf_use(uint len)
757 static void buf_use(len)
767 /* buf_in_avail - index available input data within the buffer
771 * Buf_in_avail fills the archive buffer, and points the bufp
772 * parameter at the start of the data. The lenp parameter is
773 * modified to contain the number of bytes which were read.
777 * char **bufp - pointer to the buffer to read data into
778 * uint *lenp - pointer to the number of bytes which were read
779 * (returned to the caller)
783 * Stores a pointer to the data and its length in given locations.
784 * Returns zero with valid data, -1 if unreadable portions were
785 * replaced with nulls.
789 * If an error occurs in ar_read, the error code is returned to the
796 static int buf_in_avail(char **bufp, uint *lenp)
800 static int buf_in_avail(bufp, lenp)
809 while ((have = bufend - bufidx) == 0) {
810 corrupt |= ar_read();
818 /* buf_out_avail - index buffer space for archive output
822 * Stores a buffer pointer at a given location. Returns the number
823 * of bytes available.
827 * char **bufp - pointer to the buffer which is to be stored
831 * The number of bytes which are available in the buffer.
837 static uint buf_out_avail(char **bufp)
841 static uint buf_out_avail(bufp)
848 if (bufend - bufidx < 0) {
849 fatal("Buffer overlow in buf_out_avail\n");
851 if ((have = bufend - bufidx) == 0) {