Commit 25f98a41 authored by Max Kellermann's avatar Max Kellermann

input_file: don't use buffered I/O

Yet another superfluous buffering layer. input_file was using FILE*, but we're better off with unbuffered I/O using open(), read(), ...
parent 0a618777
...@@ -17,35 +17,40 @@ ...@@ -17,35 +17,40 @@
*/ */
#include "input_file.h" #include "input_file.h"
#include "log.h" #include "log.h"
#include "os_compat.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
static bool static bool
input_file_open(struct input_stream *is, const char *filename) input_file_open(struct input_stream *is, const char *filename)
{ {
FILE *fp; int fd, ret;
struct stat st;
if (filename[0] != '/') if (filename[0] != '/')
return false; return false;
fp = fopen(filename, "r"); fd = open(filename, O_RDONLY);
if (!fp) { if (fd < 0) {
is->error = errno; is->error = errno;
return false; return false;
} }
is->seekable = true; is->seekable = true;
fseek(fp, 0, SEEK_END); ret = fstat(fd, &st);
is->size = ftell(fp); is->size = st.st_size;
fseek(fp, 0, SEEK_SET);
#ifdef POSIX_FADV_SEQUENTIAL #ifdef POSIX_FADV_SEQUENTIAL
posix_fadvise(fileno(fp), (off_t)0, is->size, POSIX_FADV_SEQUENTIAL); posix_fadvise(fd, (off_t)0, is->size, POSIX_FADV_SEQUENTIAL);
#endif #endif
is->data = fp; is->data = GINT_TO_POINTER(fd);
is->ready = true; is->ready = true;
return true; return true;
...@@ -54,50 +59,48 @@ input_file_open(struct input_stream *is, const char *filename) ...@@ -54,50 +59,48 @@ input_file_open(struct input_stream *is, const char *filename)
static bool static bool
input_file_seek(struct input_stream *is, off_t offset, int whence) input_file_seek(struct input_stream *is, off_t offset, int whence)
{ {
if (fseek((FILE *) is->data, offset, whence) == 0) { int fd = GPOINTER_TO_INT(is->data);
is->offset = ftell((FILE *) is->data);
} else { offset = lseek(fd, offset, whence);
if (offset < 0) {
is->error = errno; is->error = errno;
return false; return false;
} }
is->offset = offset;
return true; return true;
} }
static size_t static size_t
input_file_read(struct input_stream *is, void *ptr, size_t size) input_file_read(struct input_stream *is, void *ptr, size_t size)
{ {
size_t readSize; int fd = GPOINTER_TO_INT(is->data);
ssize_t nbytes;
readSize = fread(ptr, 1, size, (FILE *) is->data); nbytes = read(fd, ptr, size);
if (readSize <= 0 && ferror((FILE *) is->data)) { if (nbytes < 0) {
is->error = errno; is->error = errno;
DEBUG("input_file_read: error reading: %s\n", DEBUG("input_file_read: error reading: %s\n",
strerror(is->error)); strerror(is->error));
return 0;
} }
is->offset = ftell((FILE *) is->data); is->offset += nbytes;
return (size_t)nbytes;
return readSize;
} }
static void static void
input_file_close(struct input_stream *is) input_file_close(struct input_stream *is)
{ {
fclose((FILE *) is->data); int fd = GPOINTER_TO_INT(is->data);
close(fd);
} }
static bool static bool
input_file_eof(struct input_stream *is) input_file_eof(struct input_stream *is)
{ {
if (feof((FILE *) is->data)) return is->offset >= is->size;
return 1;
if (ferror((FILE *) is->data) && is->error != EINTR) {
return 1;
}
return 0;
} }
static int static int
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment