diff options
author | Ray Strode <rstrode@redhat.com> | 2007-05-06 13:13:39 -0400 |
---|---|---|
committer | Ray Strode <rstrode@redhat.com> | 2007-05-06 13:13:39 -0400 |
commit | badc13604b44393676737acced67cc2835a9eeeb (patch) | |
tree | 29044a562328a33a1e1870ad5683b0ad88e18d43 | |
parent | e463e7515f4cef2714dc2ef3e172bd2689354133 (diff) |
change test case from file copy to cat
-rw-r--r-- | src/pop-transaction.c | 220 |
1 files changed, 61 insertions, 159 deletions
diff --git a/src/pop-transaction.c b/src/pop-transaction.c index 17258be..387102f 100644 --- a/src/pop-transaction.c +++ b/src/pop-transaction.c @@ -1127,6 +1127,7 @@ pop_transaction_get_error (PopTransaction *transaction) #ifdef POP_TRANSACTION_ENABLE_TEST +#include <fcntl.h> #include <stdio.h> #include <unistd.h> @@ -1159,8 +1160,9 @@ proces_io_action (PopTransaction *transaction, is_ready = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction), "is-ready")); - if (is_ready) + if (!is_ready) { + g_print ("waiting for fd %d to become ready.\n", arguments->fd); /* pause transaction until fd is ready for reading (or writing * depending on the io_func) */ @@ -1176,11 +1178,12 @@ proces_io_action (PopTransaction *transaction, return POP_ACTION_PROCESS_STATUS_NOT_FINISHED; } + g_print ("fd %d is now ready!\n", arguments->fd); + bytes_done = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction), "bytes-already-processed")); bytes_left = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction), "bytes-left-to-process")); - bytes_done_this_time = io_func (arguments->fd, arguments->buffer + bytes_done, bytes_left); @@ -1204,8 +1207,17 @@ proces_io_action (PopTransaction *transaction, g_assert (bytes_done_this_time <= bytes_left); - bytes_done += bytes_done_this_time; - bytes_left -= bytes_done_this_time; + /* end of file case + */ + if (bytes_done_this_time == 0) + { + bytes_left = 0; + } + else + { + bytes_done += bytes_done_this_time; + bytes_left -= bytes_done_this_time; + } g_object_set_data (G_OBJECT (transaction), "bytes-already-processed", GINT_TO_POINTER (bytes_done)); @@ -1214,7 +1226,12 @@ proces_io_action (PopTransaction *transaction, if (bytes_left == 0) { - pop_transaction_set_result (transaction, arguments->buffer, NULL); + pop_transaction_set_result (transaction, GINT_TO_POINTER (bytes_done), NULL); + + g_object_set_data (G_OBJECT (transaction), "bytes-already-processed", + GINT_TO_POINTER (0)); + g_object_set_data (G_OBJECT (transaction), "is-ready", + GINT_TO_POINTER (FALSE)); return POP_ACTION_PROCESS_STATUS_SUCCEEDED; } @@ -1222,171 +1239,68 @@ proces_io_action (PopTransaction *transaction, } static PopActionProcessStatus -process_read_action (PopTransaction *transaction, - InputOutputArguments *arguments) +process_read_action (PopTransaction *transaction, + gpointer arguments) { + g_print ("processing read action\n"); return proces_io_action (transaction, (InputOutputFunc) read, arguments, G_IO_IN); } static PopActionProcessStatus -process_write_action (PopTransaction *transaction, - InputOutputArguments *arguments) +process_write_action (PopTransaction *transaction, + gpointer arguments) { + g_print ("processing write action\n"); return proces_io_action (transaction, (InputOutputFunc) write, arguments, G_IO_OUT); } -typedef struct -{ - const char *filename; - int flags; - mode_t mode; -} OpenArguments; - static PopActionProcessStatus -open_action (PopTransaction *transaction, - OpenArguments *arguments) +process_cat_action (PopTransaction *transaction, + gpointer data) { - int fd; + char *buffer; + static const size_t buffer_size = 4096 * 10; + InputOutputArguments *input_arguments, *output_arguments; - fd = open (arguments->filename, arguments->flags, arguments->mode); + fcntl (STDIN_FILENO, F_SETFL, O_NONBLOCK); + fcntl (STDOUT_FILENO, F_SETFL, O_NONBLOCK); - if (fd < 0) - { - GError *error; + input_arguments = g_slice_new (InputOutputArguments); + input_arguments->fd = STDIN_FILENO; + input_arguments->buffer = g_malloc (buffer_size); + input_arguments->number_of_bytes_to_process = buffer_size; - if (errno == EWOULDBLOCK) - { - pop_transaction_wait_a_while (transaction, 500); - return POP_ACTION_PROCESS_STATUS_NOT_FINISHED; - } + pop_transaction_add_action (transaction, process_read_action, + NULL, input_arguments); + g_object_set_data (G_OBJECT (transaction), "cat-input-args", input_arguments); - error = g_error_new_literal (G_FILE_ERROR, - g_file_error_from_errno (errno), - g_strerror (errno)); - - pop_transaction_set_error (transaction, error); - g_error_free (error); - return POP_ACTION_PROCESS_STATUS_FAILED; - } - - g_object_set_data (G_OBJECT (transaction), "fd", GINT_TO_POINTER (fd)); - g_object_set_data (G_OBJECT (transaction), "fd-is-set", GINT_TO_POINTER (TRUE)); + output_arguments = g_slice_new (InputOutputArguments); + output_arguments->fd = STDOUT_FILENO; + output_arguments->buffer = g_malloc (buffer_size); + output_arguments->number_of_bytes_to_process = buffer_size; + pop_transaction_add_action (transaction, process_write_action, NULL, + output_arguments); + g_object_set_data (G_OBJECT (transaction), "cat-output-args", output_arguments); + return POP_ACTION_PROCESS_STATUS_SUCCEEDED; } static PopActionRollbackStatus -open_action_cleanup (PopTransaction *transaction, - OpenArguments *arguments) -{ - gboolean fd_is_set; - int fd, status; - - fd_is_set = g_object_get_data (G_OBJECT (transaction), "fd-is-set"); - - if (!fd_is_set) - return POP_ACTION_ROLLBACK_STATUS_FINISHED; - - fd = g_object_get_data (G_OBJECT (transaction), "fd"); - - if (fd < 0) - { - g_object_set_data (G_OBJECT (transaction), "fd-is-set", - GPOINTER_TO_INT (FALSE)); - return POP_ACTION_ROLLBACK_STATUS_FINISHED; - } - - if ((close (fd) < 0) && (errno == EINTR)) - { - return POP_ACTION_ROLLBACK_STATUS_NOT_FINISHED; - } - - g_object_set_data (G_OBJECT (transaction), "fd-is-set", - GPOINTER_TO_INT (FALSE)); - - return POP_ACTION_ROLLBACK_STATUS_FINISHED; -} - - -static PopActionProcessStatus -save_fd_action (PopTransaction *transaction, - const char *name) -{ - int fd; - - g_return_val_if_fail (g_object_get_data (G_OBJECT (transaction), - "fd-is-set"), - POP_ACTION_PROCESS_STATUS_FAILED); - - fd = g_object_get_data (G_OBJECT (transaction), "fd"); - g_object_set_data (G_OBJECT (transaction), name, GINT_TO_POINTER (fd)); -} - -static PopActionRollbackStatus -save_fd_action_cleanup (PopTransaction *transaction, - const char *name) +rollback_cat_action (PopTransaction *transaction, + gpointer data) { - g_return_val_if_fail (g_object_get_data (G_OBJECT (transaction), - "fd-is-set"), - POP_ACTION_PROCESS_STATUS_FAILED); + InputOutputArguments *input_arguments, *output_arguments; - g_object_set_data (G_OBJECT (transaction), name, NULL); -} + input_arguments = g_object_get_data (G_OBJECT (transaction), "cat-input-args"); + g_slice_free (InputOutputArguments, input_arguments); + g_object_set_data (G_OBJECT (transaction), "cat-input-args", NULL); -static PopActionProcessStatus -open_files_action (PopTransaction *transaction, - const char **files) -{ - OpenArguments *input_file_args, *output_file_args; - int index; - - g_return_val_if_fail (files != NULL, POP_ACTION_PROCESS_STATUS_FAILED); - g_return_val_if_fail (g_strv_length (files) == 2, - POP_ACTION_PROCESS_STATUS_FAILED); - - input_file_args = g_slice_new0 (OpenArguments); - input_file_args->filename = g_strdup (files[0]); - input_file_args->flags = O_RDONLY; - - pop_transaction_add_action (transaction, open_action, open_action_cleanup, - input_file_args); - g_object_set_data (G_OBJECT (transaction), "input-file-args", input_file_args); - - pop_transaction_add_action (transaction, save_fd_action, save_fd_action_cleanup, - "input-fd"); - - output_file_args = g_slice_new0 (OpenArguments); - output_file_args->filename = g_strdup (files[1]); - output_file_args->flags = O_WRONLY | O_CREAT; - output_file_args->mode = 0644; - - pop_transaction_add_action (transaction, open_action, open_action_cleanup, - output_file_args); - g_object_set_data (G_OBJECT (transaction), "output-file-args", output_file_args); - - pop_transaction_add_action (transaction, save_fd_action, save_fd_action_cleanup, - "output-fd"); - - return POP_ACTION_PROCESS_STATUS_FINISHED; -} - -static PopActionRollbackStatus -open_files_action_cleanup (PopTransaction *transaction, - const char **files) -{ - OpenArguments *input_file_args, *output_file_args; - - input_file_args = g_object_get_data (G_OBJECT (transaction), - "input-file-args"); - g_slice_free (OpenArguments, input_file_args); - g_object_set_data (G_OBJECT (transaction), "input-file-args", NULL); - - output_file_args = g_object_get_data (G_OBJECT (transaction), - "output-file-args"); - g_slice_free (OpenArguments, output_file_args); - g_object_set_data (G_OBJECT (transaction), "output-file-args", NULL); + output_arguments = g_object_get_data (G_OBJECT (transaction), "cat-output-args"); + g_slice_free (InputOutputArguments, output_arguments); + g_object_set_data (G_OBJECT (transaction), "cat-input-args", NULL); return POP_ACTION_ROLLBACK_STATUS_FINISHED; } @@ -1417,12 +1331,6 @@ main (int argc, g_type_init (); - if (argc != 3) - { - g_printerr ("Usage: %s src dst\n", argv[0]); - return 1; - } - loop = g_main_loop_new (NULL, FALSE); transaction = pop_transaction_new (); @@ -1430,14 +1338,8 @@ main (int argc, g_signal_connect (G_OBJECT (transaction), "finish", G_CALLBACK (on_transaction_finish), loop); - pop_transaction_add_action (transaction, open_files_action, - open_files_action_cleanup, - argv + 1); - -#if 0 - pop_transaction_add_action (transaction, copy_files_action, - copy_files_action_cleanup, NULL); -#endif + pop_transaction_add_action (transaction, process_cat_action, + rollback_cat_action, NULL); pop_transaction_commit (transaction); g_main_loop_run (loop); |