aboutsummaryrefslogtreecommitdiff
path: root/lib/io/src/unix/ostream.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/io/src/unix/ostream.c')
-rw-r--r--lib/io/src/unix/ostream.c110
1 files changed, 57 insertions, 53 deletions
diff --git a/lib/io/src/unix/ostream.c b/lib/io/src/unix/ostream.c
index 52a566a..a97d8f3 100644
--- a/lib/io/src/unix/ostream.c
+++ b/lib/io/src/unix/ostream.c
@@ -6,6 +6,7 @@
*/
#include "../internal.h"
#include "sqfs/io.h"
+#include "sqfs/error.h"
typedef struct {
sqfs_ostream_t base;
@@ -23,15 +24,14 @@ static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size)
ssize_t ret = write(file->fd, data, size);
if (ret == 0) {
- fprintf(stderr, "%s: truncated write.\n", file->path);
- return -1;
+ errno = EPIPE;
+ return SQFS_ERROR_IO;
}
if (ret < 0) {
if (errno == EINTR)
continue;
- perror(file->path);
- return -1;
+ return SQFS_ERROR_IO;
}
file->size += ret;
@@ -46,6 +46,7 @@ static int realize_sparse(file_ostream_t *file)
{
unsigned char *buffer;
size_t diff, bufsz;
+ int ret;
if (file->sparse_count == 0)
return 0;
@@ -54,15 +55,18 @@ static int realize_sparse(file_ostream_t *file)
bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count;
buffer = calloc(1, bufsz);
if (buffer == NULL)
- goto fail;
+ return SQFS_ERROR_ALLOC;
while (file->sparse_count > 0) {
diff = file->sparse_count > (off_t)bufsz ?
bufsz : (size_t)file->sparse_count;
- if (write_all(file, buffer, diff)) {
+ ret = write_all(file, buffer, diff);
+ if (ret) {
+ int temp = errno;
free(buffer);
- return -1;
+ errno = temp;
+ return ret;
}
file->sparse_count -= diff;
@@ -71,35 +75,31 @@ static int realize_sparse(file_ostream_t *file)
free(buffer);
} else {
if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1)
- goto fail;
+ return SQFS_ERROR_IO;
if (ftruncate(file->fd, file->size) != 0)
- goto fail;
+ return SQFS_ERROR_IO;
file->sparse_count = 0;
}
return 0;
-fail:
- perror(file->path);
- return -1;
}
static int file_append(sqfs_ostream_t *strm, const void *data, size_t size)
{
file_ostream_t *file = (file_ostream_t *)strm;
+ int ret;
- if (size == 0)
- return 0;
-
- if (data == NULL) {
+ if (size == 0 || data == NULL) {
file->sparse_count += size;
file->size += size;
return 0;
}
- if (realize_sparse(file))
- return -1;
+ ret = realize_sparse(file);
+ if (ret)
+ return ret;
return write_all(file, data, size);
}
@@ -107,15 +107,15 @@ static int file_append(sqfs_ostream_t *strm, const void *data, size_t size)
static int file_flush(sqfs_ostream_t *strm)
{
file_ostream_t *file = (file_ostream_t *)strm;
+ int ret;
- if (realize_sparse(file))
- return -1;
+ ret = realize_sparse(file);
+ if (ret)
+ return ret;
if (fsync(file->fd) != 0) {
- if (errno == EINVAL)
- return 0;
- perror(file->path);
- return -1;
+ if (errno != EINVAL)
+ return SQFS_ERROR_IO;
}
return 0;
@@ -132,33 +132,36 @@ static void file_destroy(sqfs_object_t *obj)
static const char *file_get_filename(sqfs_ostream_t *strm)
{
- file_ostream_t *file = (file_ostream_t *)strm;
-
- return file->path;
+ return ((file_ostream_t *)strm)->path;
}
-sqfs_ostream_t *ostream_open_handle(const char *path, int fd, int flags)
+int ostream_open_handle(sqfs_ostream_t **out, const char *path,
+ sqfs_file_handle_t fd, int flags)
{
file_ostream_t *file = calloc(1, sizeof(*file));
sqfs_ostream_t *strm = (sqfs_ostream_t *)file;
- if (file == NULL) {
- perror(path);
- return NULL;
- }
+ *out = NULL;
+ if (file == NULL)
+ return SQFS_ERROR_ALLOC;
sqfs_object_init(file, file_destroy, NULL);
file->path = strdup(path);
if (file->path == NULL) {
- perror(path);
- goto fail_free;
+ int temp = errno;
+ free(file);
+ errno = temp;
+ return SQFS_ERROR_ALLOC;
}
file->fd = dup(fd);
if (file->fd < 0) {
- perror(path);
- goto fail_path;
+ int temp = errno;
+ free(file->path);
+ free(file);
+ errno = temp;
+ return SQFS_ERROR_IO;
}
close(fd);
@@ -167,33 +170,34 @@ sqfs_ostream_t *ostream_open_handle(const char *path, int fd, int flags)
strm->append = file_append;
strm->flush = file_flush;
strm->get_filename = file_get_filename;
- return strm;
-fail_path:
- free(file->path);
-fail_free:
- free(file);
- return NULL;
+
+ *out = strm;
+ return 0;
}
-sqfs_ostream_t *ostream_open_file(const char *path, int flags)
+int ostream_open_file(sqfs_ostream_t **out, const char *path, int flags)
{
sqfs_file_handle_t fd;
- sqfs_ostream_t *out;
+ int ret;
- if (sqfs_open_native_file(&fd, path, flags)) {
- perror(path);
- return NULL;
- }
+ *out = NULL;
+ ret = sqfs_open_native_file(&fd, path, flags);
+ if (ret)
+ return ret;
- out = ostream_open_handle(path, fd, flags);
- if (out == NULL)
+ ret = ostream_open_handle(out, path, fd, flags);
+ if (ret) {
+ int temp = errno;
close(fd);
+ errno = temp;
+ return ret;
+ }
- return out;
+ return 0;
}
-sqfs_ostream_t *ostream_open_stdout(void)
+int ostream_open_stdout(sqfs_ostream_t **out)
{
- return ostream_open_handle("stdout", STDOUT_FILENO,
+ return ostream_open_handle(out, "stdout", STDOUT_FILENO,
SQFS_FILE_OPEN_NO_SPARSE);
}