diff options
| author | Johann Latocha | 2011-11-29 15:06:45 +0100 |
|---|---|---|
| committer | Johann Latocha | 2011-11-29 15:06:45 +0100 |
| commit | 9970fb00c79834703bc990d052439290338467be (patch) | |
| tree | 9311a9c766193464b269894b18b7dd4f967652a0 /src/server/file.c | |
| download | dnbd3-9970fb00c79834703bc990d052439290338467be.tar.gz dnbd3-9970fb00c79834703bc990d052439290338467be.tar.xz dnbd3-9970fb00c79834703bc990d052439290338467be.zip | |
initial commit
Diffstat (limited to 'src/server/file.c')
| -rw-r--r-- | src/server/file.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/server/file.c b/src/server/file.c new file mode 100644 index 0000000..c53ca15 --- /dev/null +++ b/src/server/file.c @@ -0,0 +1,58 @@ +#include <fcntl.h> +#include <errno.h> +#include "file.h" + +int file_open(char *filename) +{ + int fd = open(filename, O_RDONLY); + if (fd == -1) + return -1; + + struct stat st; + if (fstat(fd, &st) == -1) + return -1; + + return fd; +} + +int file_getsize(int fd, off_t *size) +{ + *size = lseek64(fd, 0, SEEK_END); + + if (*size == -1) + return -1; + + return 0; +} + +int file_read(int fd, void *buf, size_t size, off_t pos) +{ + off_t newpos = lseek(fd, pos, SEEK_SET); + + if (newpos == -1) + return -1; + + size_t nleft = size; + ssize_t nread; + char *ptr = buf; + + while (nleft > 0) + { + if ((nread = read(fd, ptr, nleft)) < 0) + { + if (errno == EINTR) + continue; + + return -1; + } + if (nread == 0) + { + break; + } + + nleft -= nread; + ptr += nread; + } + + return 0; +} |
