git-rm: do not fail on already removed file.
[gitweb.git] / write_or_die.c
index a119e1d208445093dd1d2869729eca9435aa020e..488de721da8a316c06bf53e9091633e5ac415390 100644 (file)
@@ -26,6 +26,8 @@ void read_or_die(int fd, void *buf, size_t count)
 {
        ssize_t loaded;
 
+       if (!count)
+               return;
        loaded = read_in_full(fd, buf, count);
        if (loaded == 0)
                die("unexpected end of file");
@@ -37,15 +39,14 @@ int write_in_full(int fd, const void *buf, size_t count)
 {
        const char *p = buf;
        ssize_t total = 0;
-       ssize_t written = 0;
 
        while (count > 0) {
-               written = xwrite(fd, p, count);
-               if (written <= 0) {
-                       if (total)
-                               return total;
-                       else
-                               return written;
+               size_t written = xwrite(fd, p, count);
+               if (written < 0)
+                       return -1;
+               if (!written) {
+                       errno = ENOSPC;
+                       return -1;
                }
                count -= written;
                p += written;
@@ -59,6 +60,8 @@ void write_or_die(int fd, const void *buf, size_t count)
 {
        ssize_t written;
 
+       if (!count)
+               return;
        written = write_in_full(fd, buf, count);
        if (written == 0)
                die("disk full?");
@@ -73,6 +76,8 @@ int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
 {
        ssize_t written;
 
+       if (!count)
+               return 1;
        written = write_in_full(fd, buf, count);
        if (written == 0) {
                fprintf(stderr, "%s: disk full?\n", msg);
@@ -93,6 +98,8 @@ int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
 {
        ssize_t written;
 
+       if (!count)
+               return 1;
        written = write_in_full(fd, buf, count);
        if (written == 0) {
                fprintf(stderr, "%s: disk full?\n", msg);