Lines Matching refs:ap

130 blokread(register Archive_t* ap, char* buf, int n)
136 if (!ap->io->blokflag)
138 ap->io->blokeof = 0;
139 ap->io->blokflag = 1;
140 if ((i = read(ap->io->fd, buf, ap->io->blok ? 4 : n)) < 4 || !strneq(buf, "\002\014\017\013", 4))
142 if (ap->io->blok)
143 error(3, "%s: input archive is not a BLOK file", ap->name);
146 if (i > 4 && lseek(ap->io->fd, (off_t)4, SEEK_SET) != 4)
147 error(3, "%s: cannot seek on input archive BLOK file -- use --blok=i", ap->name);
148 ap->io->blok = 1;
150 if (ap->io->blok)
155 if ((i = read(ap->io->fd, &c, 1)) < 1)
159 if (!ap->io->blokeof)
161 ap->io->blokeof = 1;
172 error(2, "%s: blokread buffer overflow (%d>%d)", ap->name, j, n);
173 if ((i = read(ap->io->fd, buf, j)) != j)
174 error(2, "%s: blokread blocking error", ap->name);
180 i = read(ap->io->fd, buf, n);
185 blokwrite(register Archive_t* ap, char* buf, int n)
192 if (ap->io->blok)
195 if (!ap->io->blokflag)
197 ap->io->blokflag = 1;
221 if ((i = write(ap->io->fd, blk, j)) != j)
222 error(ERROR_SYSTEM|3, "%s: blokwrite count write error (%d!=%d)", ap->name, i, j);
225 else if ((i = write(ap->io->fd, buf, n)) != n)
226 error(ERROR_SYSTEM|3, "%s: blokwrite data write error (%d!=%d", ap->name, i, n);
229 i = write(ap->io->fd, buf, n);
274 binit(register Archive_t* ap)
279 if (ap->io->buffer)
281 if (ap->delta)
282 ap->delta->hdr = ap->delta->hdrbuf;
283 ap->io->buffersize = state.buffersize;
285 if ((ap->io->mode & O_ACCMODE) != O_WRONLY)
287 else if (!(ap->format->flags & OUT))
288 error(3, "%s: archive format not supported on output" , ap->format->name);
291 if (!(ap->io->buffer = newof(0, char, n, u)))
292 error(3, "%s: cannot allocate buffer", ap->name);
293 ap->io->unread = u;
294 ap->io->next = ap->io->last = ap->io->buffer + u;
295 message((-7, "binit(%s) %s", ap->name, bstatus(ap->io)));
303 bskip(register Archive_t* ap)
306 int skip = ap->io->skip;
317 if (ap->io->mode != O_RDONLY)
319 ap->io->next = ap->io->last = ap->io->buffer + ap->io->unread;
320 ap->io->eof = 0;
330 if (ioctl(ap->io->fd, MTIOCTOP, &mt) >= 0)
332 if (ap->io->mode != O_RDONLY)
333 ap->io->eof = 1;
344 if (ioctl(ap->io->fd, MTIOCTOP, &mt) >= 0)
351 if (ap->io->mode != O_RDONLY)
352 ap->io->eof = 1;
359 while ((c = read(ap->io->fd, state.tmp.buffer, state.tmp.buffersize)) > 0);
362 if (ap->io->mode != O_RDONLY)
363 ap->io->eof = 1;
372 * fill input buffer at ap->io->last
377 bfill(register Archive_t* ap, int must)
381 if (ap->io->eof)
383 if (ap->io->skip)
384 ap->io->skip = bskip(ap);
385 while ((c = read(ap->io->fd, ap->io->last, ap->io->buffersize)) <= 0)
388 newio(ap, c, 0);
391 ap->io->eof = 1;
395 message((-8, "read(%s,%d): %s", ap->name, c, show(ap->io->last, c)));
396 ap->io->eof = 0;
397 ap->io->last += c;
398 ap->io->fill++;
399 message((-7, "bfill(%s) %s", ap->name, bstatus(ap->io)));
408 chunk(register Archive_t* ap, char* t, char* f, register size_t n, char* o)
410 if (ap->sum > 0)
412 if (ap->flags & SUM)
413 FNVSUM(ap->memsum, f, n);
416 ap->memsum = memsum(f, n, ap->memsum);
417 ap->old.memsum = omemsum(f, n, ap->old.memsum);
424 CONVERT(ap, t, n);
436 bread(register Archive_t* ap, void* ob, off_t n, off_t m, int must)
444 if (ap->io->eof)
447 message((-7, "bread(%s,%I*d,%I*d) %s", ap->name, sizeof(n), n, sizeof(m), m, bstatus(ap->io)));
452 if (ap->io->blocked)
456 while ((c = read(ap->io->fd, s, r > ap->io->buffersize ? ap->io->buffersize : r)) <= 0)
459 newio(ap, c, 0);
460 else if (ap->io->empty)
462 ap->io->eof = 1;
468 ap->io->eof = 1;
470 ap->io->empty = 1;
474 ap->io->empty = 0;
475 chunk(ap, s, s, c, ob);
482 if ((c = ap->io->last - ap->io->next) < r)
487 memcpy(s, ap->io->next, c);
488 chunk(ap, s, ap->io->next, c, ob);
492 ap->io->next = ap->io->last = ap->io->buffer + ap->io->unread;
493 if (!ob && ap->sum <= 0 && ap->io->seekable && (z = r / BUFFERSIZE) && lseek(ap->io->fd, z *= BUFFERSIZE, SEEK_CUR) >= 0)
498 if (bfill(ap, must) < 0)
503 chunk(ap, s, ap->io->next, r, ob);
504 ap->io->next += r;
510 ap->io->count += (r = m - r);
515 bunread(ap, b, r);
522 message((-7, "bread(%s,%I*d@%I*d) %s: %s", ap->name, sizeof(r), r, sizeof(ap->io->count), ap->io->count, bstatus(ap->io), show(b, r)));
524 message((-7, "bread(%s) skip(%I*d@%I*d) %s", ap->name, sizeof(r), r, sizeof(ap->io->count), ap->io->count, bstatus(ap->io)));
534 bunread(register Archive_t* ap, void* b, register int n)
536 ap->io->eof = 0;
537 ap->io->count -= n;
538 if (ap->io->next == (ap->io->buffer + ap->io->unread))
539 ap->io->last = ap->io->next + n;
540 else if ((ap->io->next -= n) < ap->io->buffer + ap->io->unread)
542 if (ap->io->next < ap->io->buffer)
543 error(PANIC, "bunread(%s,%d): too much pushback", ap->name, n);
545 memcpy(ap->io->next, b, n);
546 REVERT(ap, ap->io->next, n);
548 message((-7, "bunread(%s,%d@%I*d) %s: %s", ap->name, n, sizeof(ap->io->count), ap->io->count, bstatus(ap->io), show(ap->io->next, n)));
557 bget(register Archive_t* ap, register off_t n, off_t* p)
566 if ((ap->io->mode & O_ACCMODE) == O_WRONLY)
569 *p = ap->io->last - ap->io->next;
570 return ap->io->next;
581 if (!ap->io->eof && ap->io->seekable)
583 if (ap->io->last > ap->io->next)
584 n = ap->io->last - ap->io->next;
585 else if ((n = ap->io->size - (ap->io->offset + ap->io->count)) < 0)
587 else if (n > ap->io->buffersize)
588 n = ap->io->buffersize;
592 return n ? ap->io->next : (char*)0;
594 if (n > ap->io->buffersize)
596 i = ap->io->next - ap->io->buffer;
597 j = ap->io->last - ap->io->buffer;
599 message((-8, "bget(%s,%I*d,%d): reallocate %u=>%d", ap->name, sizeof(n), n, must, ap->io->buffersize, m));
600 if (!(b = newof(ap->io->buffer, char, 2 * m, ap->io->unread)))
601 error(3, "%s: cannot reallocate buffer", ap->name);
602 ap->io->buffersize = m;
603 if (b != ap->io->buffer)
605 ap->io->buffer = b;
606 ap->io->next = b + i;
607 ap->io->last = b + j;
612 b = ap->io->next;
613 ap->io->next += n;
614 while (ap->io->next > ap->io->last)
616 if (ap->io->last > ap->io->buffer + ap->io->unread + ap->io->buffersize)
618 i = ap->io->last - b;
620 t = ap->io->next = ap->io->buffer + ap->io->unread + j;
621 ap->io->last = t + i;
626 message((-8, "bget(%s,%I*d,%d) overlapping memcpy n=%I*d i=%d m=%d next=%p last=%p", ap->name, sizeof(n), n, must, sizeof(n), n, i, m, ap->io->next + n, ap->io->last));
632 message((-8, "bget(%s,%I*d,%d): slide %u align %u", ap->name, sizeof(n), n, must, i, j));
635 b = ap->io->next;
636 ap->io->next += n;
638 if (bfill(ap, must) < 0)
641 chunk(ap, b, b, n, b);
642 ap->io->count += n;
643 message((-7, "bget(%s,%I*d@%I*d,%d): %s", ap->name, sizeof(n), n, sizeof(ap->io->count), ap->io->count, must, show(b, n)));
654 bsave(register Archive_t* ap)
656 state.backup = *ap->io;
657 message((-7, "bsave(%s,@%I*d)", ap->name, sizeof(ap->io->count), ap->io->count));
667 backup(register Archive_t* ap)
675 if (ap->format->backup)
676 (*ap->format->backup)(&state, ap);
679 message((-7, "backup(%s) old %s new %s", ap->name, bstatus(&state.backup), bstatus(ap->io)));
680 if (ap->io->fill == state.backup.fill)
686 m = ap->io->last - (ap->io->buffer + ap->io->unread);
687 if ((n = lseek(ap->io->fd, -m, SEEK_CUR)) == -1)
692 if (ioctl(ap->io->fd, MTIOCTOP, &mt))
698 message((-7, "backup(%s) %I*d => %I*d", ap->name, sizeof(m), m, sizeof(n), n));
699 ap->io->count = state.backup.count;
700 ap->io->next = state.backup.next;
703 error(PANIC, "%s: backup over intervening hard read not implemented yet", ap->name);
704 ap->io->last = ap->io->buffer + ap->io->unread + state.blocksize;
705 message((-7, "backup(%s) %s", ap->name, bstatus(ap->io)));
709 error(3, "%s: cannot position %s archive for append", ap->name, ap->format->name);
717 bflushin(register Archive_t* ap, int hard)
719 ap->io->count += ap->io->last - ap->io->next;
720 ap->io->next = ap->io->last = ap->io->buffer + ap->io->unread;
721 if (hard && !ap->io->eof)
723 while (read(ap->io->fd, ap->io->next, ap->io->buffersize) > 0);
724 ap->io->eof = 1;
733 bseek(register Archive_t* ap, off_t pos, int op, int hard)
738 message((-8, "bseek(%s,%I*d,%d,%d)", ap->name, sizeof(pos), pos, op, hard));
746 l = ap->io->next - (ap->io->buffer + ap->io->unread);
747 u = ap->io->last - ap->io->next;
750 if ((pos + ap->io->count) < 0)
754 ap->io->next += pos;
755 return ap->io->count += pos;
757 pos += ap->io->count;
762 else if (-pos <= (l + ap->io->count) && pos <= (u + ap->io->count))
764 ap->io->next += (pos - ap->io->count);
765 return ap->io->count = pos;
768 ap->io->next = ap->io->last = ap->io->buffer + ap->io->unread;
769 message((-8, "lseek(%s,%I*d,%d)", ap->name, sizeof(pos), pos + ap->io->offset, op));
770 if ((u = lseek(ap->io->fd, ap->io->offset + pos, op)) < 0 && (op != SEEK_SET || (u = pos - ap->io->count) < 0 || u > 0 && bread(ap, NiL, u, u, 1) != u || !(u += ap->io->count + ap->io->offset)))
772 ap->io->empty = 0;
773 ap->io->eof = 0;
774 return ap->io->count = u - ap->io->offset;
782 bflushout(register Archive_t* ap)
787 if (n = ap->io->next - (ap->io->buffer + ap->io->unread))
789 ap->io->next = ap->io->buffer + ap->io->unread;
790 while ((c = write(ap->io->fd, ap->io->next, n)) != n)
792 message((-8, "write(%s,%d): %s", ap->name, c, show(ap->io->next, c)));
794 newio(ap, c, n);
797 ap->io->next += c;
801 message((-8, "write(%s,%d): %s", ap->name, c, show(ap->io->next, c)));
802 ap->io->next = ap->io->buffer + ap->io->unread;
804 message((-7, "bflushout(%s) %s", ap->name, bstatus(ap->io)));
812 bwrite(register Archive_t* ap, void* ab, register off_t n)
818 if (!ap->raw)
820 CONVERT(ap, b, n);
821 an = ap->convert[SECTION(ap)].on ? n : 0;
822 if (ap->sum > 0)
823 ap->memsum = memsum(b, n, ap->memsum);
824 if (state.checksum.sum && SECTION(ap) == SECTION_DATA)
827 if (ap->io->skip)
828 ap->io->skip = bskip(ap);
829 if (state.maxout && ap->io->count >= state.maxout)
831 bflushout(ap);
832 newio(ap, 0, 0);
834 ap->io->count += n;
835 if (ap->io->blocked)
839 message((-7, "bwrite(%s,%I*d@%I*d): %s", ap->name, sizeof(n), n, sizeof(ap->io->count), ap->io->count + n, show(b, n)));
841 message((-7, "bwrite(%s,%I*d@%I*d):", ap->name, sizeof(n), n, sizeof(ap->io->count), ap->io->count + n));
843 while ((c = write(ap->io->fd, b, n)) != n)
853 if (ioctl(ap->io->fd, MTIOCTOP, &mt) >= 0)
857 error(3, "%s: cannot write tape EOF marks", ap->name);
860 newio(ap, c, n);
871 message((-7, "bwrite(%s,%I*d@%I*d): %s", ap->name, sizeof(n), n, sizeof(ap->io->count), ap->io->count + n, show(b, n)));
873 message((-7, "bwrite(%s,%I*d@%I*d):", ap->name, sizeof(n), n, sizeof(ap->io->count), ap->io->count + n));
877 if ((c = ap->io->buffer + ap->io->unread + state.blocksize - ap->io->next) <= n)
881 memcpy(ap->io->next, b, c);
885 ap->io->next = ap->io->buffer + ap->io->unread;
886 while ((c = write(ap->io->fd, ap->io->next, state.blocksize)) != state.blocksize)
889 newio(ap, c, n);
892 memcpy(state.tmp.buffer, ap->io->buffer + ap->io->unread + c, state.blocksize - c);
893 memcpy(ap->io->buffer + ap->io->unread, state.tmp.buffer, state.blocksize - c);
894 ap->io->next = ap->io->buffer + ap->io->unread + state.blocksize - c;
898 message((-8, "write(%s,%ld): %s", ap->name, c, show(ap->io->buffer + ap->io->unread, c)));
902 memcpy(ap->io->next, b, n);
903 ap->io->next += n;
908 if (!ap->raw)
909 REVERT(ap, ab, an);
918 bput(register Archive_t* ap, register off_t n)
920 ap->io->count += n;
921 message((-7, "bput(%s,%I*d@%I*d): %s", ap->name, sizeof(n), n, sizeof(ap->io->count), ap->io->count, show(ap->io->next, n)));
922 CONVERT(ap, ap->io->next, n);
923 if (ap->sum > 0)
924 ap->memsum = memsum(ap->io->next, n, ap->memsum);
925 if (state.checksum.sum && SECTION(ap) == SECTION_DATA)
926 sumblock(state.checksum.sum, ap->io->next, n);
927 if ((ap->io->next += n) > ap->io->buffer + ap->io->unread + state.blocksize)
929 n = (ap->io->next - (ap->io->buffer + ap->io->unread)) - state.blocksize;
930 ap->io->count -= n;
936 ap->raw++;
937 bwrite(ap, ap->io->next = ap->io->buffer + ap->io->unread + state.blocksize, n);
938 ap->raw--;
1010 newio(register Archive_t* ap, int c, int n)
1025 if (!ap->part)
1026 ap->part++;
1027 if (ap->io->mode != O_RDONLY)
1031 ap->io->offset += ap->io->count - n;
1032 ap->io->count = n;
1033 z = ap->io->offset + ap->io->count;
1034 if (ap->format->putepilogue && (*ap->format->putepilogue)(&state, ap) < 0)
1041 z = ap->io->offset + ap->io->count;
1043 if (fstat(ap->io->fd, &st) < 0)
1044 error(ERROR_SYSTEM|3, "%s: cannot stat %s", ap->name, io);
1053 if (ap->io->mode != O_RDONLY)
1066 error(ERROR_SYSTEM|3, "%s: %s %s error -- cannot recover", ap->name, io, rw);
1079 error(i, "%s: end of %s medium", ap->name, io);
1082 error(ERROR_SYSTEM|i, "%s: %s %s error", ap->name, io, rw);
1085 if (ap->total == z)
1086 error(1, "%s: no %s on part %d", ap->name, io, ap->part--);
1088 ap->total = z;
1089 if (!file && (ap->name == definput || ap->name == defoutput))
1094 ap->name = dev.path;
1096 close(ap->io->fd);
1099 if (file && ap->name != definput && ap->name != defoutput && strmatch(ap->name, "*.+([0-9])") && (s = strrchr(ap->name, '.')) && (int)strtol(++s, NiL, 10) == ap->part)
1102 * the parts will be ap->name in sequence
1107 if (ap->part == 1)
1109 c = s - ap->name;
1110 if (!(t = newof(0, char, s - ap->name, 16)))
1112 strcpy(t, ap->name);
1113 s = (ap->name = t) + c;
1115 sfsprintf(s, 16, "%d", ap->part + 1);
1116 if ((ap->io->fd = open(ap->name, ap->io->mode|O_BINARY, st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) >= 0)
1118 error(ERROR_SYSTEM|1, "%s: cannot %s", ap->name, rw);
1122 else if (file || ap->name == definput || ap->name == defoutput)
1128 sfprintf(state.wtty, "Enter part %d %s %s name: ", ap->part + 1, io, file ? file : "device");
1138 if ((ap->io->fd = open(s, ap->io->mode|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) >= 0)
1143 ap->name = strdup(s);
1154 sfprintf(cp, "%s %s %d", eomprompt + 1, rw, ap->part + 1);
1155 if (ap->name)
1156 sfprintf(cp, " %s", ap->name);
1166 sfprintf(state.wtty, eomprompt, ap->part + 1);
1189 else if (file = *s ? s : ap->name)
1191 if ((ap->io->fd = open(file, ap->io->mode|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) >= 0)
1197 if (!*t && (ap->io->fd = open(file, ap->io->mode|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) >= 0)
1201 error(ERROR_SYSTEM|1, "cannot %s %s", rw, *s ? s : ap->name);
1206 if (ap->name != file)
1207 ap->name = strdup(file);
1213 ap->part++;
1214 error(1, "continuing %s %d %s on %s", ap->part == ap->volume + 1 ? "volume" : "part", ap->part, io, ap->name);
1217 ap->part++;
1218 if (ap->format->putprologue)
1219 (*ap->format->putprologue)(&state, ap, 0);