ape: revert rename() change

new implementation gets stuck in a infinite loop. backing
this out for now.
This commit is contained in:
cinap_lenrek 2019-06-23 22:35:14 +02:00
parent d4bc9052be
commit 7d3cc1c55a

View file

@ -10,7 +10,7 @@
int int
rename(const char *from, const char *to) rename(const char *from, const char *to)
{ {
int n, ffd, tfd; int n, i;
char *f, *t; char *f, *t;
Dir *d, nd; Dir *d, nd;
@ -31,45 +31,45 @@ rename(const char *from, const char *to)
} }
f = strrchr(from, '/'); f = strrchr(from, '/');
t = strrchr(to, '/'); t = strrchr(to, '/');
f = f? f+1 : (char*)from; f = f? f+1 : from;
t = t? t+1 : (char*)to; t = t? t+1 : to;
n = 0;
if(f-from==t-to && strncmp(from, to, f-from)==0){ if(f-from==t-to && strncmp(from, to, f-from)==0){
/* from and to are in same directory (we miss some cases) */ /* from and to are in same directory (we miss some cases) */
i = strlen(t);
_nulldir(&nd); _nulldir(&nd);
nd.name = t; nd.name = t;
if(_dirwstat(from, &nd) < 0){ if(_dirwstat(from, &nd) < 0){
_syserrno(); _syserrno();
return -1; n = -1;
} }
}else{ }else{
/* different directories: have to copy */ /* different directories: have to copy */
int ffd, tfd;
char buf[8192]; char buf[8192];
if((ffd = _OPEN(from, OREAD)) < 0 ||
if((ffd = _OPEN(from, OREAD)) == -1) (tfd = _CREATE(to, OWRITE, d->mode)) < 0){
goto err1; _CLOSE(ffd);
if((tfd = _CREATE(to, OWRITE, d->mode)) == -1) _syserrno();
goto err2; n = -1;
n = 0;
while(n>=0){
if((n = _READ(ffd, buf, sizeof(buf))) == -1)
goto err2;
if(_WRITE(tfd, buf, n) != n)
goto err2;
} }
while(n>=0 && (n = _READ(ffd, buf, sizeof(buf))) > 0)
if(_WRITE(tfd, buf, n) != n){
_syserrno();
n = -1;
}
_CLOSE(ffd); _CLOSE(ffd);
_CLOSE(tfd); _CLOSE(tfd);
if(_REMOVE(from) < 0) if(n>0)
goto err2; n = 0;
if(n == 0) {
if(_REMOVE(from) < 0){
_syserrno();
return -1;
}
}
} }
free(d); free(d);
return 0; return n;
err2:
_CLOSE(tfd);
err1:
_CLOSE(ffd);
_syserrno();
free(d);
return -1;
} }