X-Git-Url: http://git.thejh.net/?p=libjh.git;a=blobdiff_plain;f=io.c;h=2173478513a88ae9e6e3ad8726963311d39222bf;hp=a7696f9f7fd74c277b7914483d81c8ad8b7213b8;hb=2f75df5a713a9b0373b496aeb4cd83768fd0a488;hpb=483ad7514b32f4557c8866bbde2091aed69e98b4;ds=sidebyside diff --git a/io.c b/io.c index a7696f9..2173478 100644 --- a/io.c +++ b/io.c @@ -24,6 +24,7 @@ PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) { size_t done = 0; while (done < count) { ssize_t part_res = read(fd, buf+done, count-done); + if (part_res == -1 && errno == EINTR) continue; if (part_res <= 0) { if (last_res) *last_res = part_res; if (done) return done; @@ -47,6 +48,7 @@ PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) { size_t done = 0; while (done < count) { ssize_t part_res = write(fd, buf+done, count-done); + if (part_res == -1 && errno == EINTR) continue; if (part_res <= 0) { if (last_res) *last_res = part_res; if (done) return done; @@ -66,7 +68,11 @@ PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) { // this and can specify len_out as NULL. PUBLIC_CONST JH_SLURP_NO_STAT 1 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */ -PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) { +PUBLIC_CONST JH_SLURP_8BYTE_PAD 4 /* pad buffer with eight nullbytes, not one */ +PUBLIC_CONST JH_SLURP_FIXSIZE 8 /* len_out is actually input and specifies +HEADER the expected length. if there is more data, +HEADER that might be ignored silently. */ +PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) { int errno_; // Let's just guess that the file is 1023 bytes. Will become 1024 with @@ -74,6 +80,15 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) { size_t size_guess = 1023; bool trusted_guess = false; /* can we rely on the guess? */ + if (flags&JH_SLURP_FIXSIZE) { + size_guess = *len_out; + trusted_guess = true; + // if we want a fixed size, of course we don't want to stat + flags |= JH_SLURP_NO_STAT; + } + + int padlen = (flags&JH_SLURP_8BYTE_PAD)?8:1; + // If we can determine the exact size, we don't have to guess. So try // to determine the exact size. if (!(flags&JH_SLURP_NO_STAT)) { @@ -90,7 +105,7 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) { int done = 0; while (1) { - buf = realloc(buf, size_guess+1); + buf = realloc(buf, size_guess+padlen); if (buf == NULL) return NULL; int last_res; ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res); @@ -98,14 +113,24 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) { done += read_res; if (last_res == 0 || trusted_guess) { // out - buf[done] = '\0'; + if (padlen == 1) { + buf[done] = '\0'; + } else { + *(uint64_t*)(buf+done) = 0; + } if (done != size_guess && (flags&JH_SLURP_REALLOC)) { // Well, it'd be weird if shrinking could fail... but meh, I can't find // an explicit statement about this being disallowed, so try staying on // the safe side. - char *buf_ = realloc(buf, done+1); + char *buf_ = realloc(buf, done+padlen); if (buf_) buf = buf_; } + if (done != size_guess && (flags&JH_SLURP_FIXSIZE)) { + free(buf); + // not the most correct error message ever, but whatever. + errno = EFBIG; + return NULL; + } if (len_out) *len_out = done; return buf; } @@ -113,7 +138,7 @@ PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) { } } -PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) { +PUBLIC_FN void *slurp_file(char *path, size_t *len_out, int flags) { int fd = open(path, O_RDONLY|O_CLOEXEC); if (fd == -1) return NULL; char *res = slurp_fd(fd, len_out, flags); @@ -125,10 +150,10 @@ PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) { // Write data into a file. len can be -1; that means that // the real length is strlen(buf). -PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) { +PUBLIC_FN int write_file(char *path, void *buf, ssize_t len, int open_flags) { if (len == -1) len = strlen(buf); - int fd = open(path, open_flags|O_CLOEXEC, 0777); + int fd = open(path, open_flags|O_CLOEXEC|O_WRONLY, 0777); if (fd == -1) return 1; ssize_t write_res = write_nointr(fd, buf, len, NULL); int write_errno = errno;