# HG changeset patch # User Paul Boddie # Date 1615674151 -3600 # Node ID a03e69c0ff65264ef6f36e00c206846aaa9290aa # Parent 54dcc50bf1930ff7717ad60d619feb9ac34b09a9 Renamed file_open to file_context_open, providing file_open as a convenience function that creates a context, sets the filename, opens the file, and closes the context. Changed the string-setting function to be more immediately useful, if more complicated in operation, for this application. diff -r 54dcc50bf193 -r a03e69c0ff65 dstest_host_client.cc --- a/dstest_host_client.cc Sat Mar 13 22:38:33 2021 +0100 +++ b/dstest_host_client.cc Sat Mar 13 23:22:31 2021 +0100 @@ -54,25 +54,11 @@ /* Obtain access to the filesystem. */ l4_cap_idx_t server = l4re_env_get_cap("server"); - file_t context; - - long err = file_context(&context, server); - - if (err) - { - printf("Could not obtain context: %s\n", l4sys_errtostr(err)); - return 1; - } - - /* Write the filename. */ - - file_string_set(&context, filename, 0); /* Invoke the open method to receive the file reference. */ file_t file; - - err = file_open(&file, &context); + long err = file_open(&file, filename, server); if (err) { diff -r 54dcc50bf193 -r a03e69c0ff65 dstest_test_client.cc --- a/dstest_test_client.cc Sat Mar 13 22:38:33 2021 +0100 +++ b/dstest_test_client.cc Sat Mar 13 23:22:31 2021 +0100 @@ -65,7 +65,7 @@ file_t file; - long err = file_open(&file, context); + long err = file_context_open(&file, context); if (err) { diff -r 54dcc50bf193 -r a03e69c0ff65 file.cc --- a/file.cc Sat Mar 13 22:38:33 2021 +0100 +++ b/file.cc Sat Mar 13 23:22:31 2021 +0100 @@ -55,36 +55,62 @@ long err; file_init(file); + err = opener.context(&file->ref); - if (err) return err; client_Dataspace context_ds(file->ref); err = context_ds.info(&size, &flags); - if (err) return err; + file->start_pos = 0; + file->end_pos = size; + return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); } +/* Open a file using the given structure and context. */ + +long file_context_open(file_t *file, file_t *context) +{ + client_OpenerContext openercontext(context->ref); + file_init(file); + return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); +} + /* Initialise the given file structure. */ void file_init(file_t *file) { file->memory = NULL; file->ref = L4_INVALID_CAP; + file->start_pos = 0; + file->end_pos = 0; + file->data_end = 0; } -/* Open a file using the given structure and context. */ +/* Open a file using the given structure, indicating the filename and + filesystem server. This is a convenience function invoking file_context and + file_context_open. */ -long file_open(file_t *file, file_t *context) +long file_open(file_t *file, const char *filename, l4_cap_idx_t server) { - client_OpenerContext openercontext(context->ref); - file_init(file); - return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); + file_t context; + long err; + + err = file_context(&context, server); + if (err) + return err; + + if (!file_string_set(&context, filename, 0, NULL)) + return -L4_ENOMEM; + + err = file_context_open(file, &context); + file_close(&context); + return err; } @@ -132,20 +158,26 @@ return NULL; } -/* Copy a string to the mapped region at the given offset, returning the number - of copied characters excluding the zero terminator. */ +/* Copy a string to the mapped region at the given offset, returning 1 (true) + where all characters were copied, 0 (false) otherwise. The precise number of + characters copied, excluding the zero terminator is provided via the written + parameter if it is not specified as NULL. */ -offset_t file_string_set(file_t *file, const char *data, offset_t offset) +int file_string_set(file_t *file, const char *data, offset_t offset, + offset_t *written) { - offset_t i, pos, limit = file_span(file) - 1; + offset_t i, pos, limit = file_span(file); /* Do not attempt to copy data with an invalid offset. */ - if (offset > limit) + if (offset >= limit) + { + if (written != NULL) + *written = 0; return 0; + } - /* Copy the data to the given offset, stopping before the final byte of the - region. */ + /* Copy the data to the given offset, stopping at the end of the region. */ for (i = 0, pos = offset; pos < limit; i++, pos++) { @@ -154,13 +186,19 @@ /* Terminator written, can return immediately. */ if (!data[i]) - return pos - offset - 1; + { + if (written != NULL) + *written = pos - offset; + return 1; + } } /* Terminate the incomplete string at the end of the region. */ - file->memory[limit] = '\0'; - return limit - offset; + file->memory[limit - 1] = '\0'; + if (written != NULL) + *written = limit - 1 - offset; + return 0; } // vim: tabstop=2 expandtab shiftwidth=2 diff -r 54dcc50bf193 -r a03e69c0ff65 file.h --- a/file.h Sat Mar 13 22:38:33 2021 +0100 +++ b/file.h Sat Mar 13 23:22:31 2021 +0100 @@ -52,12 +52,16 @@ -/* File lifecycle operations. */ +/* File operations. */ void file_close(file_t *file); +long file_open(file_t *file, const char *filename, l4_cap_idx_t server); + +/* File lifecycle operations. */ + long file_context(file_t *file, l4_cap_idx_t server); +long file_context_open(file_t *file, file_t *context); void file_init(file_t *file); -long file_open(file_t *file, file_t *context); /* File access region operations. */ @@ -68,7 +72,7 @@ /* Convenience functions. */ char *file_string_get(file_t *file, offset_t offset); -offset_t file_string_set(file_t *file, const char *data, offset_t offset); +int file_string_set(file_t *file, const char *data, offset_t offset, offset_t *written); EXTERN_C_END