Fix entry writing, truncate file if rewriting.

This commit is contained in:
Howard Chu 2005-06-25 14:15:11 +00:00
parent e3c59d222c
commit 7b245a0a55

View File

@ -144,21 +144,18 @@ static char * slurp_file(int fd) {
return entry; return entry;
} }
static int spew_file(int fd, char * spew) { static int spew_file(int fd, char * spew, int len) {
int written = 0;
int writeres = 0; int writeres = 0;
int len = strlen(spew);
char * spewptr = spew;
while(written < len) { while(len > 0) {
writeres = write(fd, spewptr, len - written); writeres = write(fd, spew, len);
if(writeres == -1) { if(writeres == -1) {
perror("could not spew write"); perror("could not spew write");
return -1; return -1;
} }
else { else {
spewptr += writeres; spew += writeres;
written += writeres; len -= writeres;
} }
} }
return writeres; return writeres;
@ -171,7 +168,7 @@ static int spew_entry(Entry * e, struct berval * path) {
int entry_length; int entry_length;
char * entry_as_string; char * entry_as_string;
openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
if(openres == -1) { if(openres == -1) {
if(errno == ENOENT) if(errno == ENOENT)
rs = LDAP_NO_SUCH_OBJECT; rs = LDAP_NO_SUCH_OBJECT;
@ -204,7 +201,7 @@ static int spew_entry(Entry * e, struct berval * path) {
close(openres); close(openres);
} }
else { else {
spew_res = spew_file(openres, entry_as_string); spew_res = spew_file(openres, entry_as_string, entry_length);
close(openres); close(openres);
if(spew_res == -1) if(spew_res == -1)
rs = LDAP_UNWILLING_TO_PERFORM; rs = LDAP_UNWILLING_TO_PERFORM;