summaryrefslogtreecommitdiff
path: root/sys/src/cmd/hgfs/patch.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@gmx.de>2012-11-21 19:22:46 +0100
committercinap_lenrek <cinap_lenrek@gmx.de>2012-11-21 19:22:46 +0100
commit40d11cea3fac49a660b5714b18393c2e74487d54 (patch)
tree09ae5180eaeefffa293cec6d2e71765e0e951f9b /sys/src/cmd/hgfs/patch.c
parent73744b9f4896c513d4d7c40ef4ce1d24703810e9 (diff)
hgfs: various improvements
lazily close revlog files and keep up to 8 revlogs arround. also cache the latest extracted file descriptor of a revision in the revlog. this avoids the quite expensive reextracting/patching when we reopen the same file revision. dont use the racy mktemp()/create, instead create a uniqueue name and create with OEXCL. this also avoids a bunch of access() calls. fix eof case and use pread() in fcopy() to avoid the seeks. dont modify changelog temp file but simulate trailing newline instead.
Diffstat (limited to 'sys/src/cmd/hgfs/patch.c')
-rw-r--r--sys/src/cmd/hgfs/patch.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/sys/src/cmd/hgfs/patch.c b/sys/src/cmd/hgfs/patch.c
index a3d6ef37a..a9d93aa1a 100644
--- a/sys/src/cmd/hgfs/patch.c
+++ b/sys/src/cmd/hgfs/patch.c
@@ -5,19 +5,25 @@
#include "fns.h"
int
-fcopy(int dfd, int sfd, vlong len)
+fcopy(int dfd, int sfd, vlong off, vlong len)
{
uchar buf[BUFSZ];
int n;
- while(len > 0){
- if((n = BUFSZ) > len)
+ while(len != 0){
+ n = BUFSZ;
+ if(len > 0 && n > len)
n = len;
- if((n = read(sfd, buf, n)) < 0)
+ if((n = pread(sfd, buf, n, off)) < 0)
return -1;
+ if(n == 0)
+ return len > 0 ? -1 : 0;
if(write(dfd, buf, n) != n)
return -1;
- len -= n;
+ if(off >= 0)
+ off += n;
+ if(len > 0)
+ len -= n;
}
return 0;
}
@@ -58,6 +64,8 @@ fpatch(int ofd, int bfd, int pfd)
if(bfd >= 0){
h = malloc(sizeof(Frag));
+ if(h == nil)
+ goto errout;
h->next = nil;
h->off = 0;
h->fd = bfd;
@@ -101,6 +109,8 @@ fpatch(int ofd, int bfd, int pfd)
back = end < fend;
if(front && back){
p = malloc(sizeof(Frag));
+ if(p == nil)
+ goto errout;
*p = *f;
f->next = p;
f->len = start - fstart;
@@ -123,6 +133,8 @@ fpatch(int ofd, int bfd, int pfd)
fstart += f->len;
f = malloc(sizeof(Frag));
+ if(f == nil)
+ goto errout;
f->fd = pfd;
f->len = len;
f->off = seek(f->fd, 0, 1);
@@ -141,12 +153,9 @@ fpatch(int ofd, int bfd, int pfd)
goto errout;
}
- for(f = h; f; f = f->next){
- if(seek(f->fd, f->off, 0) < 0)
+ for(f = h; f; f = f->next)
+ if(fcopy(ofd, f->fd, f->off, f->len) < 0)
goto errout;
- if(fcopy(ofd, f->fd, f->len) < 0)
- goto errout;
- }
err = 0;
errout: