Make fread/fwrite-like functions in http.c more like fread/fwrite.

The fread/fwrite-like functions in http.c, namely fread_buffer, fwrite_buffer, fwrite_null, fwrite_sha1_file all return the multiplication of the size and number of items they are being given. Practically speaking, it doesn't matter, because in all contexts where those functions are used, size is 1. But those functions being similar to fread and fwrite (the curl API is designed around being able to use fread and fwrite directly), it might be preferable to make them behave like fread and fwrite, which, from the fread/fwrite manual page, is: On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). Signed-off-by: Mike Hommey <mh@glandium.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mike Hommey committed May 8, 2019 at 08:03 UTC 5c3d5a38231c2c8c5414232cfbb662b6610662b1
1 file changed +6 -6
http.c
+6 -6
@@ -176,7 +176,7 @@ size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
176 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
177 buffer->posn += size;
178
179 - return size;
179 + return size / eltsize;
180 }
181
182 #ifndef NO_CURL_IOCTL
@@ -204,12 +204,12 @@ size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
204 struct strbuf *buffer = buffer_;
205
206 strbuf_add(buffer, ptr, size);
207 - return size;
207 + return nmemb;
208 }
209
210 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
211 {
212 - return eltsize * nmemb;
212 + return nmemb;
213 }
214
215 static void closedown_active_slot(struct active_request_slot *slot)
@@ -2319,14 +2319,14 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
2319 BUG("curl_easy_getinfo for HTTP code failed: %s",
2320 curl_easy_strerror(c));
2321 if (slot->http_code >= 300)
2322 - return size;
2322 + return nmemb;
2323 }
2324
2325 do {
2326 ssize_t retval = xwrite(freq->localfile,
2327 (char *) ptr + posn, size - posn);
2328 if (retval < 0)
2329 - return posn;
2329 + return posn / eltsize;
2330 posn += retval;
2331 } while (posn < size);
2332
@@ -2339,7 +2339,7 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
2339 the_hash_algo->update_fn(&freq->c, expn,
2340 sizeof(expn) - freq->stream.avail_out);
2341 } while (freq->stream.avail_in && freq->zret == Z_OK);
2342 - return size;
2342 + return nmemb;
2343 }
2344
2345 struct http_object_request *new_http_object_request(const char *base_url,