Lines Matching +full:write +full:- +full:0
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
60 static char *bufpt; /* read/write point in i/o buffer */
71 * set up the buffering system to operate in a write mode
73 * 0 if ok, -1 if the user specified write block size violates pax spec
81 * Check to make sure the write block size meets pax specs. If the user in wr_start()
88 wrblksz = frmt->bsz; in wr_start()
90 paxwarn(1, "Write block size of %d too large, maximum is: %d", in wr_start()
92 return(-1); in wr_start()
95 paxwarn(1, "Write block size of %d is not a %d byte multiple", in wr_start()
97 return(-1); in wr_start()
100 paxwarn(0, "Write block size of %d larger than POSIX max %d, archive may not be portable", in wr_start()
102 return(-1); in wr_start()
109 if ((ar_open(arcname) < 0) && (ar_next() < 0)) in wr_start()
110 return(-1); in wr_start()
111 wrcnt = 0; in wr_start()
114 return(0); in wr_start()
121 * 0 if ok, -1 otherwise
129 * going to append and user specified a write block size, check it in rd_start()
135 paxwarn(1,"Write block size %d too large, maximum is: %d", in rd_start()
137 return(-1); in rd_start()
140 paxwarn(1, "Write block size %d is not a %d byte multiple", in rd_start()
142 return(-1); in rd_start()
149 if ((ar_open(arcname) < 0) && (ar_next() < 0)) in rd_start()
150 return(-1); in rd_start()
153 rdcnt = 0; in rd_start()
154 return(0); in rd_start()
179 * position of the tape head) and the write point within the data we have
180 * stored in the read (soon to become write) buffer. We may have to move
187 * up. We set the buffer write pointer to be at this byte (the byte where
193 * about the conditions under which they will allow a write to occur.
198 * 0 for success, -1 for failure
207 if (exit_val != 0) { in appnd_start()
208 paxwarn(0, "Cannot append to an archive that may have flaws."); in appnd_start()
209 return(-1); in appnd_start()
212 * if the user did not specify a write blocksize, inherit the size used in appnd_start()
224 if (ar_app_ok() < 0) in appnd_start()
225 return(-1); in appnd_start()
233 skcnt += bufend - bufpt; in appnd_start()
236 if (ar_rev((off_t)cnt) < 0) in appnd_start()
244 if ((cnt -= skcnt) > 0) { in appnd_start()
254 if ((res = ar_read(bufpt, rdblksz)) <= 0) in appnd_start()
258 if (ar_rev((off_t)(bufpt - buf)) < 0) in appnd_start()
270 rdcnt -= skcnt; in appnd_start()
271 wrcnt = 0; in appnd_start()
274 * At this point we are ready to write. If the device requires special in appnd_start()
275 * handling to write at a point were previously recorded data resides, in appnd_start()
277 * ARCHIVE mode (write) conditions in appnd_start()
279 if (ar_set_wr() < 0) in appnd_start()
280 return(-1); in appnd_start()
282 return(0); in appnd_start()
286 return(-1); in appnd_start()
297 * 0 on success, and -1 on failure
303 int errcnt = 0; in rd_sync()
309 if (maxflt == 0) in rd_sync()
310 return(-1); in rd_sync()
313 return(-1); in rd_sync()
319 if (ar_rdsync() < 0) { in rd_sync()
320 if (ar_next() < 0) in rd_sync()
321 return(-1); in rd_sync()
323 rdcnt = 0; in rd_sync()
327 if ((res = ar_read(buf, blksz)) > 0) { in rd_sync()
334 return(0); in rd_sync()
345 if ((maxflt > 0) && (++errcnt > maxflt)) in rd_sync()
346 paxwarn(0,"Archive read error limit (%d) reached",maxflt); in rd_sync()
347 else if (ar_rdsync() == 0) in rd_sync()
349 if (ar_next() < 0) in rd_sync()
351 rdcnt = 0; in rd_sync()
352 errcnt = 0; in rd_sync()
354 return(-1); in rd_sync()
371 bufpt -= cnt; in pback()
381 * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.
389 off_t skipped = 0; in rd_skip()
397 if (skcnt == 0) in rd_skip()
398 return(0); in rd_skip()
399 res = MIN((bufend - bufpt), skcnt); in rd_skip()
401 skcnt -= res; in rd_skip()
404 * if skcnt is now 0, then no additional i/o is needed in rd_skip()
406 if (skcnt == 0) in rd_skip()
407 return(0); in rd_skip()
420 if (ar_fow(cnt, &skipped) < 0) in rd_skip()
421 return(-1); in rd_skip()
422 res += cnt - skipped; in rd_skip()
429 while (res > 0L) { in rd_skip()
430 cnt = bufend - bufpt; in rd_skip()
434 if ((cnt <= 0) && ((cnt = buf_fill()) < 0)) in rd_skip()
435 return(-1); in rd_skip()
436 if (cnt == 0) in rd_skip()
440 res -= cnt; in rd_skip()
442 return(0); in rd_skip()
448 * with zero (even though we do not have to). Padding with 0 makes it a
457 memset(bufpt, 0, bufend - bufpt); in wr_fin()
465 * fill the write buffer from data passed to it in a buffer (usually used
466 * by format specific write routines to pass a file header). On failure we
467 * punt. We do not allow the user to continue to write flawed archives.
471 * 0 if buffer was filled ok, -1 o.w. (buffer flush failure)
480 * while there is data to copy into the write buffer. when the in wr_rdbuf()
481 * write buffer fills, flush it to the archive and continue in wr_rdbuf()
483 while (outcnt > 0) { in wr_rdbuf()
484 cnt = bufend - bufpt; in wr_rdbuf()
485 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) in wr_rdbuf()
486 return(-1); in wr_rdbuf()
494 outcnt -= cnt; in wr_rdbuf()
496 return(0); in wr_rdbuf()
506 * number of bytes copied to the buffer, 0 indicates EOF on archive volume,
507 * -1 is a read error
520 while (incnt > 0) { in rd_wrbuf()
521 cnt = bufend - bufpt; in rd_wrbuf()
522 if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) { in rd_wrbuf()
529 if ((res = cpcnt - incnt) > 0) in rd_wrbuf()
541 incnt -= cnt; in rd_wrbuf()
549 * skip forward during a write. In other words add padding to the file.
555 * 0 if ok, -1 if there was a buf_flush failure
566 while (skcnt > 0L) { in wr_skip()
567 cnt = bufend - bufpt; in wr_skip()
568 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) in wr_skip()
569 return(-1); in wr_skip()
571 memset(bufpt, 0, cnt); in wr_skip()
573 skcnt -= cnt; in wr_skip()
575 return(0); in wr_skip()
580 * fill write buffer with the contents of a file. We are passed an open
583 * number of bytes of the file we were NOT able to write to the archive.
584 * it is important that we always write EXACTLY the number of bytes that
585 * the format specific write routine told us to. The file can also get
592 * 0 ok, -1 if archive write failure. a short read of the file returns a
593 * 0, but "left" is set to be greater than zero.
600 int res = 0; in wr_rdfile()
601 off_t size = arcn->sb.st_size; in wr_rdfile()
605 * while there are more bytes to write in wr_rdfile()
607 while (size > 0L) { in wr_rdfile()
608 cnt = bufend - bufpt; in wr_rdfile()
609 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) { in wr_rdfile()
611 return(-1); in wr_rdfile()
614 if ((res = read(ifd, bufpt, cnt)) <= 0) in wr_rdfile()
616 size -= res; in wr_rdfile()
624 if (res < 0) in wr_rdfile()
625 syswarn(1, errno, "Read fault on %s", arcn->org_name); in wr_rdfile()
626 else if (size != 0L) in wr_rdfile()
627 paxwarn(1, "File changed size during read %s", arcn->org_name); in wr_rdfile()
628 else if (fstat(ifd, &sb) < 0) in wr_rdfile()
629 syswarn(1, errno, "Failed stat on %s", arcn->org_name); in wr_rdfile()
630 else if (arcn->sb.st_mtime != sb.st_mtime) in wr_rdfile()
632 arcn->org_name); in wr_rdfile()
634 return(0); in wr_rdfile()
640 * extract the entire file (due to failure to write the file) we return
647 * We call a special function to write the file. This function attempts to
653 * 0 ok, -1 if archive read failure. if we cannot write the entire file,
654 * we return a 0 but "left" is set to be the amount unwritten
660 int cnt = 0; in rd_wrfile()
661 off_t size = arcn->sb.st_size; in rd_wrfile()
662 int res = 0; in rd_wrfile()
663 char *fnm = arcn->name; in rd_wrfile()
668 u_long crc = 0L; in rd_wrfile()
671 * pass the blocksize of the file being written to the write routine, in rd_wrfile()
674 if (fstat(ofd, &sb) == 0) { in rd_wrfile()
675 if (sb.st_blksize > 0) in rd_wrfile()
678 syswarn(0,errno,"Unable to obtain block size for file %s",fnm); in rd_wrfile()
680 *left = 0L; in rd_wrfile()
687 while (size > 0L) { in rd_wrfile()
688 cnt = bufend - bufpt; in rd_wrfile()
691 * miss a header, so we do not set left, but if we get a write in rd_wrfile()
694 if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) in rd_wrfile()
697 if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) { in rd_wrfile()
707 while (--cnt >= 0) in rd_wrfile()
708 crc += *bufpt++ & 0xff; in rd_wrfile()
711 size -= res; in rd_wrfile()
720 if (isem && (arcn->sb.st_size > 0L)) in rd_wrfile()
726 if ((size > 0L) && (*left == 0L)) in rd_wrfile()
727 return(-1); in rd_wrfile()
733 if (docrc && (size == 0L) && (arcn->crc != crc)) in rd_wrfile()
734 paxwarn(1,"Actual crc does not match expected crc %s",arcn->name); in rd_wrfile()
735 return(0); in rd_wrfile()
740 * copy the contents of one file to another. used during -rw phase of pax
741 * just as in rd_wrfile() we use a special write function to write the
749 off_t cpcnt = 0L; in cp_file()
750 int res = 0; in cp_file()
751 char *fnm = arcn->name; in cp_file()
752 int no_hole = 0; in cp_file()
760 * write instead of file write. in cp_file()
762 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size) in cp_file()
766 * pass the blocksize of the file being written to the write routine, in cp_file()
769 if (fstat(fd2, &sb) == 0) { in cp_file()
770 if (sb.st_blksize > 0) in cp_file()
773 syswarn(0,errno,"Unable to obtain block size for file %s",fnm); in cp_file()
780 if ((cnt = read(fd1, buf, blksz)) <= 0) in cp_file()
783 res = write(fd2, buf, cnt); in cp_file()
794 if (res < 0) in cp_file()
795 syswarn(1, errno, "Failed write during copy of %s to %s", in cp_file()
796 arcn->org_name, arcn->name); in cp_file()
797 else if (cpcnt != arcn->sb.st_size) in cp_file()
799 arcn->org_name, arcn->name); in cp_file()
800 else if (fstat(fd1, &sb) < 0) in cp_file()
801 syswarn(1, errno, "Failed stat of %s", arcn->org_name); in cp_file()
802 else if (arcn->sb.st_mtime != sb.st_mtime) in cp_file()
804 arcn->org_name, arcn->name); in cp_file()
812 if (!no_hole && isem && (arcn->sb.st_size > 0L)) in cp_file()
822 * Number of bytes of data in the read buffer, -1 for read error, and
823 * 0 when finished (user specified termination in ar_next()).
830 static int fini = 0; in buf_fill()
833 return(0); in buf_fill()
840 if ((cnt = ar_read(buf, blksz)) > 0) { in buf_fill()
853 if (cnt < 0) in buf_fill()
855 if (frmt == NULL || ar_next() < 0) { in buf_fill()
857 return(0); in buf_fill()
859 rdcnt = 0; in buf_fill()
862 return(-1); in buf_fill()
867 * force the write buffer to the archive. We are passed the number of
871 * 0 if all is ok, -1 when a write error occurs.
878 int push = 0; in buf_flush()
879 int totcnt = 0; in buf_flush()
883 * volume, prompt for the next volume. (The non-standard -R flag). in buf_flush()
884 * NOTE: If the wrlimit is smaller than wrcnt, we will always write in buf_flush()
887 if ((wrlimit > 0) && (wrcnt > wrlimit)) { in buf_flush()
888 paxwarn(0, "User specified archive volume byte limit reached."); in buf_flush()
889 if (ar_next() < 0) { in buf_flush()
890 wrcnt = 0; in buf_flush()
892 return(-1); in buf_flush()
894 wrcnt = 0; in buf_flush()
898 * write blocksize. if so we figure out if we need to write in buf_flush()
907 return(0); in buf_flush()
909 push = bufcnt - blksz; in buf_flush()
913 * We have enough data to write at least one archive block in buf_flush()
917 * write a block and check if it all went out ok in buf_flush()
922 * the write went ok in buf_flush()
926 if (push > 0) { in buf_flush()
929 * so we loop back to write again in buf_flush()
934 push -= blksz; in buf_flush()
940 } else if (cnt > 0) { in buf_flush()
942 * Oh drat we got a partial write! in buf_flush()
950 cnt = bufcnt - cnt; in buf_flush()
953 if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0)) in buf_flush()
961 wrcnt = 0; in buf_flush()
962 if (ar_next() < 0) in buf_flush()
972 return(0); in buf_flush()
974 push = bufcnt - blksz; in buf_flush()
978 * write failed, stop pax. we must not create a bad archive! in buf_flush()
981 return(-1); in buf_flush()