code feedback ...
Posted Sep 10, 2011 21:02 UTC (Sat) by
vapier (subscriber, #15768)
Parent article:
Ensuring data reaches disk
5 char *buf = malloc(MY_BUF_SIZE);
you forgot to free(buf) after the while loop and in the early returns inside of the loop. might as well just use the stack: char buf[MY_BUF_SIZE];
11 ret = read(sockfd, buf, MY_BUF_SIZE);
common mistake. the len should be min(MY_BUF_SIZE, nrbytes - written). otherwise, if (nrbytes % MY_BUF_SIZE) is non-zero, you read too many bytes from the sockfd and they get lost.
12 if (ret =< 0) {
typo ... should be "<=" as "=<" doesn't compile.
18 ret = fwrite((void *)buf, ret, 1, outfp);
19 if (ret != 1)
unless you build this with a C++ compiler, that cast is not needed. and another common mistake: the size/nmemb args are swapped ... the size is "1" (since sizeof(*buf) is 1 (a char)), and the number of elements is "ret". once you fix the arg order, the method of clobbering the value of ret won't work in the "if" check ...
27 ret = fsync(fileno(outfp));
28 if (ret < 0)
29 return -1;
30 return 0;
at this point, you could just as easily write:
return fsync(fileno(outfp));
(
Log in to post comments)