]> git.sur5r.net Git - openldap/commitdiff
Fix entry writing, truncate file if rewriting.
authorHoward Chu <hyc@openldap.org>
Sat, 25 Jun 2005 14:15:11 +0000 (14:15 +0000)
committerHoward Chu <hyc@openldap.org>
Sat, 25 Jun 2005 14:15:11 +0000 (14:15 +0000)
servers/slapd/back-ldif/ldif.c

index a59fe576ebda46207f1847634f53095cbc394193..a9550a54156e177ed94c37b84b281a1fbe2cd983 100644 (file)
@@ -144,21 +144,18 @@ static char * slurp_file(int fd) {
        return entry;
 }
 
-static int spew_file(int fd, char * spew) {
-       int written = 0;
+static int spew_file(int fd, char * spew, int len) {
        int writeres = 0;
-       int len = strlen(spew);
-       char * spewptr = spew;
        
-       while(written < len) {
-               writeres = write(fd, spewptr, len - written);
+       while(len > 0) {
+               writeres = write(fd, spew, len);
                if(writeres == -1) {
                        perror("could not spew write");
                        return -1;
                }
                else {
-                       spewptr += writeres;
-                       written += writeres;
+                       spew += writeres;
+                       len -= writeres;
                }
        }
        return writeres;
@@ -171,7 +168,7 @@ static int spew_entry(Entry * e, struct berval * path) {
        int entry_length;
        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(errno == ENOENT)
                        rs = LDAP_NO_SUCH_OBJECT;
@@ -204,7 +201,7 @@ static int spew_entry(Entry * e, struct berval * path) {
                        close(openres);
                }
                else {
-                       spew_res = spew_file(openres, entry_as_string);
+                       spew_res = spew_file(openres, entry_as_string, entry_length);
                        close(openres);
                        if(spew_res == -1)
                                rs = LDAP_UNWILLING_TO_PERFORM;