aboutsummaryrefslogtreecommitdiff
path: root/lib/io/src/unix
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-11 23:03:21 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-12 13:34:55 +0200
commit2b10bb09beb03380c8b815a6f6be268f188ac78d (patch)
tree0d21f998d90aaa709e7ebb23c413a58232154439 /lib/io/src/unix
parente57196f2f80432900523258af1038fb95a100b6b (diff)
libio: add open handle functions to istream/ostream
For the backends, this simplifies the code as both paths (open file and open stdio) use the same basic code. Even when merging them only in the backend, it would be done in a similar way. Making the functions public allows other uses as well. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io/src/unix')
-rw-r--r--lib/io/src/unix/istream.c39
-rw-r--r--lib/io/src/unix/ostream.c49
2 files changed, 40 insertions, 48 deletions
diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c
index 60abff1..e96bc6c 100644
--- a/lib/io/src/unix/istream.c
+++ b/lib/io/src/unix/istream.c
@@ -105,7 +105,7 @@ static void file_destroy(sqfs_object_t *obj)
free(file);
}
-istream_t *istream_open_file(const char *path)
+istream_t *istream_open_handle(const char *path, int fd)
{
file_istream_t *file = calloc(1, sizeof(*file));
istream_t *strm = (istream_t *)file;
@@ -123,11 +123,12 @@ istream_t *istream_open_file(const char *path)
goto fail_free;
}
- file->fd = open(path, O_RDONLY);
+ file->fd = dup(fd);
if (file->fd < 0) {
perror(path);
goto fail_path;
}
+ close(fd);
strm->get_buffered_data = file_get_buffered_data;
strm->advance_buffer = file_advance_buffer;
@@ -140,27 +141,25 @@ fail_free:
return NULL;
}
-istream_t *istream_open_stdin(void)
+istream_t *istream_open_file(const char *path)
{
- file_istream_t *file = calloc(1, sizeof(*file));
- istream_t *strm = (istream_t *)file;
+ istream_t *out;
+ int fd;
- if (file == NULL)
- goto fail;
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ perror(path);
+ return NULL;
+ }
- sqfs_object_init(file, file_destroy, NULL);
+ out = istream_open_handle(path, fd);
+ if (out == NULL)
+ close(fd);
- file->path = strdup("stdin");
- if (file->path == NULL)
- goto fail;
+ return out;
+}
- file->fd = STDIN_FILENO;
- strm->get_buffered_data = file_get_buffered_data;
- strm->advance_buffer = file_advance_buffer;
- strm->get_filename = file_get_filename;
- return strm;
-fail:
- perror("creating file wrapper for stdin");
- free(file);
- return NULL;
+istream_t *istream_open_stdin(void)
+{
+ return istream_open_handle("stdin", STDIN_FILENO);
}
diff --git a/lib/io/src/unix/ostream.c b/lib/io/src/unix/ostream.c
index 5ef2af2..294a15e 100644
--- a/lib/io/src/unix/ostream.c
+++ b/lib/io/src/unix/ostream.c
@@ -90,9 +90,7 @@ static void file_destroy(sqfs_object_t *obj)
{
file_ostream_t *file = (file_ostream_t *)obj;
- if (file->fd != STDOUT_FILENO)
- close(file->fd);
-
+ close(file->fd);
free(file->path);
free(file);
}
@@ -104,7 +102,7 @@ static const char *file_get_filename(ostream_t *strm)
return file->path;
}
-ostream_t *ostream_open_file(const char *path, int flags)
+ostream_t *ostream_open_handle(const char *path, int fd, int flags)
{
file_ostream_t *file = calloc(1, sizeof(*file));
ostream_t *strm = (ostream_t *)file;
@@ -122,17 +120,14 @@ ostream_t *ostream_open_file(const char *path, int flags)
goto fail_free;
}
- if (flags & OSTREAM_OPEN_OVERWRITE) {
- file->fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- } else {
- file->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
- }
-
+ file->fd = dup(fd);
if (file->fd < 0) {
perror(path);
goto fail_path;
}
+ close(fd);
+
if (flags & OSTREAM_OPEN_SPARSE)
strm->append_sparse = file_append_sparse;
@@ -147,27 +142,25 @@ fail_free:
return NULL;
}
-ostream_t *ostream_open_stdout(void)
+ostream_t *ostream_open_file(const char *path, int flags)
{
- file_ostream_t *file = calloc(1, sizeof(*file));
- ostream_t *strm = (ostream_t *)file;
+ ostream_t *out;
+ int fd;
- if (file == NULL)
- goto fail;
+ if (flags & OSTREAM_OPEN_OVERWRITE) {
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ } else {
+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
+ }
- file->path = strdup("stdout");
- if (file->path == NULL)
- goto fail;
+ out = ostream_open_handle(path, fd, flags);
+ if (out == NULL)
+ close(fd);
- file->fd = STDOUT_FILENO;
- strm->append = file_append;
- strm->flush = file_flush;
- strm->get_filename = file_get_filename;
+ return out;
+}
- sqfs_object_init(file, file_destroy, NULL);
- return strm;
-fail:
- perror("creating file wrapper for stdout");
- free(file);
- return NULL;
+ostream_t *ostream_open_stdout(void)
+{
+ return ostream_open_handle("stdout", STDOUT_FILENO, 0);
}