Skip to content

Commit

Permalink
Merge pull request #1303 from jluebbe/refactor-output-stream
Browse files Browse the repository at this point in the history
update_utils: refactor output stream
  • Loading branch information
jluebbe committed Jan 15, 2024
2 parents af19bbf + d62b81f commit 689d76b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
18 changes: 17 additions & 1 deletion include/update_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,29 @@
* Opens a device for writing and returns a GUnixOutputStream for it.
* Optionally, the FD is returned as well.
*
* @param filename the device to be opened
* @param fd the associated file descriptor, for use with ioctls
* @param error return location for a GError, or NULL
*
* @return the new GUnixOutputStream if successful, NULL otherwise
*/
GUnixOutputStream* r_unix_output_stream_open_device(const gchar *filename, int *fd, GError **error)
G_GNUC_WARN_UNUSED_RESULT;

/**
* Creates a file for writing and returns a GUnixOutputStream for it.
* Optionally, the FD is returned as well.
*
* This method ensures that the file is newly created by us.
*
* @param filename the file to be opened
* @param mode the access mode for the new file
* @param fd the associated file descriptor, for use with ioctls
* @param error return location for a GError, or NULL
*
* @return the new GUnixOutputStream if successful, NULL otherwise
*/
GUnixOutputStream* r_open_unix_output_stream(const gchar *filename, int *fd, GError **error)
GUnixOutputStream* r_unix_output_stream_create_file(const gchar *filename, int *fd, GError **error)
G_GNUC_WARN_UNUSED_RESULT;

/**
Expand Down
10 changes: 5 additions & 5 deletions src/update_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static gboolean clear_slot(RaucSlot *slot, GError **error)
g_autoptr(GOutputStream) outstream = NULL;
gint write_count = 0;

outstream = G_OUTPUT_STREAM(r_open_unix_output_stream(slot->device, NULL, &ierror));
outstream = G_OUTPUT_STREAM(r_unix_output_stream_open_device(slot->device, NULL, &ierror));
if (outstream == NULL) {
g_propagate_error(error, ierror);
return FALSE;
Expand Down Expand Up @@ -638,7 +638,7 @@ static gboolean copy_raw_image_to_dev(RaucImage *image, RaucSlot *slot, GError *

/* open */
g_message("opening slot device %s", slot->device);
outstream = r_open_unix_output_stream(slot->device, NULL, &ierror);
outstream = r_unix_output_stream_open_device(slot->device, NULL, &ierror);
if (outstream == NULL) {
res = FALSE;
g_propagate_error(error, ierror);
Expand Down Expand Up @@ -1466,7 +1466,7 @@ static gboolean img_to_ubivol_handler(RaucImage *image, RaucSlot *dest_slot, con

/* open */
g_message("opening slot device %s", dest_slot->device);
outstream = r_open_unix_output_stream(dest_slot->device, &out_fd, &ierror);
outstream = r_unix_output_stream_open_device(dest_slot->device, &out_fd, &ierror);
if (outstream == NULL) {
res = FALSE;
g_propagate_error(error, ierror);
Expand Down Expand Up @@ -1535,7 +1535,7 @@ static gboolean img_to_ubifs_handler(RaucImage *image, RaucSlot *dest_slot, cons

/* open */
g_message("opening slot device %s", dest_slot->device);
outstream = r_open_unix_output_stream(dest_slot->device, &out_fd, &ierror);
outstream = r_unix_output_stream_open_device(dest_slot->device, &out_fd, &ierror);
if (outstream == NULL) {
res = FALSE;
g_propagate_error(error, ierror);
Expand Down Expand Up @@ -2258,7 +2258,7 @@ static gboolean img_to_boot_emmc_handler(RaucImage *image, RaucSlot *dest_slot,

/* open */
g_message("Opening slot device partition %s", part_slot->device);
outstream = r_open_unix_output_stream(part_slot->device, &out_fd, &ierror);
outstream = r_unix_output_stream_open_device(part_slot->device, &out_fd, &ierror);
if (outstream == NULL) {
g_propagate_error(error, ierror);
res = FALSE;
Expand Down
26 changes: 20 additions & 6 deletions src/update_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
#include "update_utils.h"
#include "context.h"

/* the fd will only live as long as the returned output stream */
GUnixOutputStream* r_open_unix_output_stream(const gchar *filename, int *fd, GError **error)
static GUnixOutputStream* open_unix_output_stream(const gchar *filename, int flags, int mode, int *fd, GError **error)
{
GUnixOutputStream *outstream = NULL;
int fd_out;

g_return_val_if_fail(filename, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);

fd_out = g_open(filename, O_WRONLY | O_EXCL);
fd_out = g_open(filename, O_WRONLY | flags, mode);

if (fd_out == -1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
Expand All @@ -41,6 +37,24 @@ GUnixOutputStream* r_open_unix_output_stream(const gchar *filename, int *fd, GEr
return outstream;
}

/* the fd will only live as long as the returned output stream */
GUnixOutputStream* r_unix_output_stream_open_device(const gchar *filename, int *fd, GError **error)
{
g_return_val_if_fail(filename, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);

return open_unix_output_stream(filename, O_EXCL, 0, fd, error);
}

/* the fd will only live as long as the returned output stream */
GUnixOutputStream* r_unix_output_stream_create_file(const gchar *filename, int *fd, GError **error)
{
g_return_val_if_fail(filename, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);

return open_unix_output_stream(filename, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, fd, error);
}

/* the fd will only live as long as the returned input stream */
GUnixInputStream* r_open_unix_input_stream(const gchar *filename, int *fd, GError **error)
{
Expand Down

0 comments on commit 689d76b

Please sign in to comment.