write_or_die.con commit Disallow working directory commands in a bare repository. (7eff28a)
   1#include "cache.h"
   2
   3void read_or_die(int fd, void *buf, size_t count)
   4{
   5        char *p = buf;
   6        ssize_t loaded;
   7
   8        while (count > 0) {
   9                loaded = xread(fd, p, count);
  10                if (loaded == 0)
  11                        die("unexpected end of file");
  12                else if (loaded < 0)
  13                        die("read error (%s)", strerror(errno));
  14                count -= loaded;
  15                p += loaded;
  16        }
  17}
  18
  19void write_or_die(int fd, const void *buf, size_t count)
  20{
  21        const char *p = buf;
  22        ssize_t written;
  23
  24        while (count > 0) {
  25                written = xwrite(fd, p, count);
  26                if (written == 0)
  27                        die("disk full?");
  28                else if (written < 0) {
  29                        if (errno == EPIPE)
  30                                exit(0);
  31                        die("write error (%s)", strerror(errno));
  32                }
  33                count -= written;
  34                p += written;
  35        }
  36}
  37
  38int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
  39{
  40        const char *p = buf;
  41        ssize_t written;
  42
  43        while (count > 0) {
  44                written = xwrite(fd, p, count);
  45                if (written == 0) {
  46                        fprintf(stderr, "%s: disk full?\n", msg);
  47                        return 0;
  48                }
  49                else if (written < 0) {
  50                        if (errno == EPIPE)
  51                                exit(0);
  52                        fprintf(stderr, "%s: write error (%s)\n",
  53                                msg, strerror(errno));
  54                        return 0;
  55                }
  56                count -= written;
  57                p += written;
  58        }
  59
  60        return 1;
  61}
  62
  63int write_in_full(int fd, const void *buf, size_t count, const char *msg)
  64{
  65        const char *p = buf;
  66        ssize_t written;
  67
  68        while (count > 0) {
  69                written = xwrite(fd, p, count);
  70                if (written == 0) {
  71                        fprintf(stderr, "%s: disk full?\n", msg);
  72                        return 0;
  73                }
  74                else if (written < 0) {
  75                        fprintf(stderr, "%s: write error (%s)\n",
  76                                msg, strerror(errno));
  77                        return 0;
  78                }
  79                count -= written;
  80                p += written;
  81        }
  82
  83        return 1;
  84}