8 fcopy(int dfd, int sfd, vlong off, vlong len)
15 if(len > 0 && n > len)
17 if((n = pread(sfd, buf, n, off)) < 0)
20 return len > 0 ? -1 : 0;
21 if(write(dfd, buf, n) != n)
31 static uchar patchmark[12] = {
32 0xff, 0xff, 0xff, 0xff,
33 0xff, 0xff, 0xff, 0xff,
34 0xff, 0xff, 0xff, 0xff,
38 fpatchmark(int pfd, char *)
40 if(write(pfd, patchmark, 12) != 12)
45 typedef struct Frag Frag;
55 fpatch(int ofd, int bfd, int pfd)
57 vlong off, fstart, fend, start, end, len;
66 h = malloc(sizeof(Frag));
72 h->len = seek(h->fd, 0, 2);
79 if(readn(pfd, buf, 12) != 12)
82 if(memcmp(buf, patchmark, 12) == 0){
87 start = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
88 end = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
89 len = buf[8]<<24 | buf[9]<<16 | buf[10]<<8 | buf[11];
92 werrstr("bad patch: start > end");
98 off += start + len - end;
101 for(f = h; f; f = f->next, fstart = fend){
102 fend = fstart + f->len;
108 front = start > fstart;
111 p = malloc(sizeof(Frag));
116 f->len = start - fstart;
117 p->off += end - fstart;
118 p->len -= end - fstart;
121 f->off += end - fstart;
122 f->len -= end - fstart;
125 f->len = start - fstart;
132 for(p = nil, f = h; f && fstart < start; p = f, f = f->next)
135 f = malloc(sizeof(Frag));
140 f->off = seek(f->fd, 0, 1);
152 if(seek(pfd, f->len, 1) < 0)
156 for(f = h; f; f = f->next)
157 if(fcopy(ofd, f->fd, f->off, f->len) < 0)