truncate splitted file to correct size on pull

Thu, 28 Mar 2019 20:05:10 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 28 Mar 2019 20:05:10 +0100
changeset 546
33e312dd851d
parent 545
268157722a0e
child 547
4a249c645ae4

truncate splitted file to correct size on pull

dav/sync.c file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Thu Mar 28 18:23:22 2019 +0100
+++ b/dav/sync.c	Thu Mar 28 20:05:10 2019 +0100
@@ -958,6 +958,8 @@
     UcxList *updates = NULL;
     
     size_t local_numparts = local ? local->numparts : 0;
+    fseeko(out, 0, SEEK_END);
+    off_t end = ftello(out);
     
     int error = 0;
     
@@ -996,6 +998,7 @@
                 break;
             }
             
+            int64_t block_end = 0;
             if(download_part) {
                 if(fseeko(out, offset, SEEK_SET)) {
                     error = 1;
@@ -1009,7 +1012,6 @@
                     error = 1;
                     break;
                 }
-                printf("offset: %lu length: %lu\n", offset, (uint64_t)buf->size);
                 if(fwrite(buf->space, 1, buf->size, out) == 0) {
                     perror("write");
                     error = 1;
@@ -1021,9 +1023,21 @@
                 update->etag = etag ? strdup(etag) : NULL;
                 update->hash = dav_create_hash(buf->space, buf->size);
                 updates = ucx_list_append(updates, update);
+                
+                block_end = offset+buf->size;
+            } else {
+                if(offset+blocksize > end) {
+                    // if we don't download the block, we don't know the size
+                    // but it can't be bigger than the file
+                    block_end = end;
+                } else {
+                    block_end = offset+blocksize;
+                }
             }
             
-            
+            if(block_end > maxsize) {
+                maxsize = block_end;
+            }
             
             i++;
         } // else: res is not a regular file part
@@ -1038,9 +1052,14 @@
         return NULL;
     }
     
+    if(maxsize < end) {
+        *truncate_file = maxsize;
+    } else {
+        *truncate_file = -1;
+    }
+    
     *err = 0;
     *blockcount = i;
-    *truncate_file = maxsize;
     return updates;
 }
 

mercurial