8 fcopy(int dfd, int sfd, vlong len)
16 if((n = read(sfd, buf, n)) < 0)
18 if(write(dfd, buf, n) != n)
25 static uchar patchmark[12] = {
26 0xff, 0xff, 0xff, 0xff,
27 0xff, 0xff, 0xff, 0xff,
28 0xff, 0xff, 0xff, 0xff,
32 fpatchmark(int pfd, char *)
34 if(write(pfd, patchmark, 12) != 12)
39 typedef struct Frag Frag;
49 fpatch(int ofd, int bfd, int pfd)
51 vlong off, fstart, fend, start, end, len;
60 h = malloc(sizeof(Frag));
64 h->len = seek(h->fd, 0, 2);
71 if(readn(pfd, buf, 12) != 12)
74 if(memcmp(buf, patchmark, 12) == 0){
79 start = buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3];
80 end = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
81 len = buf[8]<<24 | buf[9]<<16 | buf[10]<<8 | buf[11];
84 werrstr("bad patch: start > end");
90 off += start + len - end;
93 for(f = h; f; f = f->next, fstart = fend){
94 fend = fstart + f->len;
100 front = start > fstart;
103 p = malloc(sizeof(Frag));
106 f->len = start - fstart;
107 p->off += end - fstart;
108 p->len -= end - fstart;
111 f->off += end - fstart;
112 f->len -= end - fstart;
115 f->len = start - fstart;
122 for(p = nil, f = h; f && fstart < start; p = f, f = f->next)
125 f = malloc(sizeof(Frag));
128 f->off = seek(f->fd, 0, 1);
140 if(seek(pfd, f->len, 1) < 0)
144 for(f = h; f; f = f->next){
145 if(seek(f->fd, f->off, 0) < 0)
147 if(fcopy(ofd, f->fd, f->len) < 0)