Optimize x_fread to speed up the smbclient put command
authorVolker Lendecke <vl@samba.org>
Thu, 23 Oct 2008 13:43:36 +0000 (15:43 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 23 Oct 2008 15:45:52 +0000 (17:45 +0200)
lib/util/xfile.c

index 94b0ee9b1897d28f9480d72d369e8d8077b8b458..cf195706db0b7514f41a1b5fc0c20eefbb99bb71 100644 (file)
@@ -329,12 +329,27 @@ int x_fgetc(XFILE *f)
 /** simulate fread */
 size_t x_fread(void *p, size_t size, size_t nmemb, XFILE *f)
 {
+       size_t remaining = size * nmemb;
        size_t total = 0;
-       while (total < size*nmemb) {
-               int c = x_fgetc(f);
-               if (c == EOF) break;
-               (total+(char *)p)[0] = (char)c;
-               total++;
+
+       while (remaining > 0) {
+               size_t thistime;
+
+               x_fillbuf(f);
+
+               if (f->bufused == 0) {
+                       f->flags |= X_FLAG_EOF;
+                       break;
+               }
+
+               thistime = MIN(f->bufused, remaining);
+
+               memcpy((char *)p+total, f->next, thistime);
+
+               f->next += thistime;
+               f->bufused -= thistime;
+               remaining -= thistime;
+               total += thistime;
        }
        return total/size;
 }