    #include 
    #include 
    #include 
    #include 
    #include 
    #ifndef EINTR
    #define EINTR (-5004)
    #endif
    #ifndef EAGAIN
    #define EAGAIN (-5011)
    #endif
    #ifndef EWOULDBLOCK
    #define EWOULDBLOCK (-7011)
    #endif
    int writeall(int fd,const void *xv,long long xlen)
    {
      const unsigned char *x = xv;
      long long w;
      while (xlen > 0) {
        w = xlen;
        if (w > 1048576) w = 1048576;
        w = write(fd,x,w);
        if (w < 0) {
          if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
            struct pollfd p;
            p.fd = fd;
            p.events = POLLOUT | POLLERR;
            poll(&p,1,-1);
            continue;
          }
          return -1;
        }
        x += w;
        xlen -= w;
      }
      return 0;
    }
    long long readblock(int fd, void *x, long long xlen) {
        long long r;
        char *buf = x;
        while (xlen > 0) {
            r = xlen;
            if (r > 1048576) r = 1048576;
            r = read(fd, buf, r);
            if (r == 0) break;
            if (r == -1) {
                if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
                    struct pollfd p;
                    p.fd = fd;
                    p.events = POLLIN;
                    poll(&p, 1, -1);
                    continue;
                }
                return -1;
            }
            buf += r; xlen -= r;
        }
        return (buf - (char *)x);
    }
    int fsyncfd(int fd) {
        struct stat st;
        if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
            if (fsync(fd) == -1) return -1;
        }
        return 0;
    }