ref: 7d3cc1c55a435bd9e3d37d9bf8a9299ea9e1f87e
parent: d4bc9052beb3305d64a353a16641740380eb87af
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Jun 23 18:35:14 EDT 2019
ape: revert rename() change new implementation gets stuck in a infinite loop. backing this out for now.
--- a/sys/src/ape/lib/ap/plan9/rename.c
+++ b/sys/src/ape/lib/ap/plan9/rename.c
@@ -10,7 +10,7 @@
int
rename(const char *from, const char *to)
{
- int n, ffd, tfd;
+ int n, i;
char *f, *t;
Dir *d, nd;
@@ -31,45 +31,45 @@
}
f = strrchr(from, '/');
t = strrchr(to, '/');
- f = f? f+1 : (char*)from;
- t = t? t+1 : (char*)to;
+ f = f? f+1 : from;
+ t = t? t+1 : to;
+ n = 0;
if(f-from==t-to && strncmp(from, to, f-from)==0){
/* from and to are in same directory (we miss some cases) */
+ i = strlen(t);
_nulldir(&nd);
nd.name = t;
if(_dirwstat(from, &nd) < 0){
_syserrno();
- return -1;
+ n = -1;
}
}else{
/* different directories: have to copy */
+ int ffd, tfd;
char buf[8192];
-
- if((ffd = _OPEN(from, OREAD)) == -1)
- goto err1;
- if((tfd = _CREATE(to, OWRITE, d->mode)) == -1)
- goto err2;
- n = 0;
- while(n>=0){
- if((n = _READ(ffd, buf, sizeof(buf))) == -1)
- goto err2;
- if(_WRITE(tfd, buf, n) != n)
- goto err2;
+ if((ffd = _OPEN(from, OREAD)) < 0 ||
+ (tfd = _CREATE(to, OWRITE, d->mode)) < 0){
+ _CLOSE(ffd);
+ _syserrno();
+ n = -1;
}
+ while(n>=0 && (n = _READ(ffd, buf, sizeof(buf))) > 0)
+ if(_WRITE(tfd, buf, n) != n){
+ _syserrno();
+ n = -1;
+ }
_CLOSE(ffd);
_CLOSE(tfd);
- if(_REMOVE(from) < 0)
- goto err2;
+ if(n>0)
+ n = 0;
+ if(n == 0) {
+ if(_REMOVE(from) < 0){
+ _syserrno();
+ return -1;
+ }
+ }
}
free(d);
- return 0;
-
-err2:
- _CLOSE(tfd);
-err1:
- _CLOSE(ffd);
- _syserrno();
- free(d);
- return -1;
+ return n;
}