1.1 --- a/dstest_host_client.cc Sat Mar 13 22:38:33 2021 +0100
1.2 +++ b/dstest_host_client.cc Sat Mar 13 23:22:31 2021 +0100
1.3 @@ -54,25 +54,11 @@
1.4 /* Obtain access to the filesystem. */
1.5
1.6 l4_cap_idx_t server = l4re_env_get_cap("server");
1.7 - file_t context;
1.8 -
1.9 - long err = file_context(&context, server);
1.10 -
1.11 - if (err)
1.12 - {
1.13 - printf("Could not obtain context: %s\n", l4sys_errtostr(err));
1.14 - return 1;
1.15 - }
1.16 -
1.17 - /* Write the filename. */
1.18 -
1.19 - file_string_set(&context, filename, 0);
1.20
1.21 /* Invoke the open method to receive the file reference. */
1.22
1.23 file_t file;
1.24 -
1.25 - err = file_open(&file, &context);
1.26 + long err = file_open(&file, filename, server);
1.27
1.28 if (err)
1.29 {
2.1 --- a/dstest_test_client.cc Sat Mar 13 22:38:33 2021 +0100
2.2 +++ b/dstest_test_client.cc Sat Mar 13 23:22:31 2021 +0100
2.3 @@ -65,7 +65,7 @@
2.4
2.5 file_t file;
2.6
2.7 - long err = file_open(&file, context);
2.8 + long err = file_context_open(&file, context);
2.9
2.10 if (err)
2.11 {
3.1 --- a/file.cc Sat Mar 13 22:38:33 2021 +0100
3.2 +++ b/file.cc Sat Mar 13 23:22:31 2021 +0100
3.3 @@ -55,36 +55,62 @@
3.4 long err;
3.5
3.6 file_init(file);
3.7 +
3.8 err = opener.context(&file->ref);
3.9 -
3.10 if (err)
3.11 return err;
3.12
3.13 client_Dataspace context_ds(file->ref);
3.14
3.15 err = context_ds.info(&size, &flags);
3.16 -
3.17 if (err)
3.18 return err;
3.19
3.20 + file->start_pos = 0;
3.21 + file->end_pos = size;
3.22 +
3.23 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory);
3.24 }
3.25
3.26 +/* Open a file using the given structure and context. */
3.27 +
3.28 +long file_context_open(file_t *file, file_t *context)
3.29 +{
3.30 + client_OpenerContext openercontext(context->ref);
3.31 + file_init(file);
3.32 + return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref);
3.33 +}
3.34 +
3.35 /* Initialise the given file structure. */
3.36
3.37 void file_init(file_t *file)
3.38 {
3.39 file->memory = NULL;
3.40 file->ref = L4_INVALID_CAP;
3.41 + file->start_pos = 0;
3.42 + file->end_pos = 0;
3.43 + file->data_end = 0;
3.44 }
3.45
3.46 -/* Open a file using the given structure and context. */
3.47 +/* Open a file using the given structure, indicating the filename and
3.48 + filesystem server. This is a convenience function invoking file_context and
3.49 + file_context_open. */
3.50
3.51 -long file_open(file_t *file, file_t *context)
3.52 +long file_open(file_t *file, const char *filename, l4_cap_idx_t server)
3.53 {
3.54 - client_OpenerContext openercontext(context->ref);
3.55 - file_init(file);
3.56 - return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref);
3.57 + file_t context;
3.58 + long err;
3.59 +
3.60 + err = file_context(&context, server);
3.61 + if (err)
3.62 + return err;
3.63 +
3.64 + if (!file_string_set(&context, filename, 0, NULL))
3.65 + return -L4_ENOMEM;
3.66 +
3.67 + err = file_context_open(file, &context);
3.68 + file_close(&context);
3.69 + return err;
3.70 }
3.71
3.72
3.73 @@ -132,20 +158,26 @@
3.74 return NULL;
3.75 }
3.76
3.77 -/* Copy a string to the mapped region at the given offset, returning the number
3.78 - of copied characters excluding the zero terminator. */
3.79 +/* Copy a string to the mapped region at the given offset, returning 1 (true)
3.80 + where all characters were copied, 0 (false) otherwise. The precise number of
3.81 + characters copied, excluding the zero terminator is provided via the written
3.82 + parameter if it is not specified as NULL. */
3.83
3.84 -offset_t file_string_set(file_t *file, const char *data, offset_t offset)
3.85 +int file_string_set(file_t *file, const char *data, offset_t offset,
3.86 + offset_t *written)
3.87 {
3.88 - offset_t i, pos, limit = file_span(file) - 1;
3.89 + offset_t i, pos, limit = file_span(file);
3.90
3.91 /* Do not attempt to copy data with an invalid offset. */
3.92
3.93 - if (offset > limit)
3.94 + if (offset >= limit)
3.95 + {
3.96 + if (written != NULL)
3.97 + *written = 0;
3.98 return 0;
3.99 + }
3.100
3.101 - /* Copy the data to the given offset, stopping before the final byte of the
3.102 - region. */
3.103 + /* Copy the data to the given offset, stopping at the end of the region. */
3.104
3.105 for (i = 0, pos = offset; pos < limit; i++, pos++)
3.106 {
3.107 @@ -154,13 +186,19 @@
3.108 /* Terminator written, can return immediately. */
3.109
3.110 if (!data[i])
3.111 - return pos - offset - 1;
3.112 + {
3.113 + if (written != NULL)
3.114 + *written = pos - offset;
3.115 + return 1;
3.116 + }
3.117 }
3.118
3.119 /* Terminate the incomplete string at the end of the region. */
3.120
3.121 - file->memory[limit] = '\0';
3.122 - return limit - offset;
3.123 + file->memory[limit - 1] = '\0';
3.124 + if (written != NULL)
3.125 + *written = limit - 1 - offset;
3.126 + return 0;
3.127 }
3.128
3.129 // vim: tabstop=2 expandtab shiftwidth=2
4.1 --- a/file.h Sat Mar 13 22:38:33 2021 +0100
4.2 +++ b/file.h Sat Mar 13 23:22:31 2021 +0100
4.3 @@ -52,12 +52,16 @@
4.4
4.5
4.6
4.7 -/* File lifecycle operations. */
4.8 +/* File operations. */
4.9
4.10 void file_close(file_t *file);
4.11 +long file_open(file_t *file, const char *filename, l4_cap_idx_t server);
4.12 +
4.13 +/* File lifecycle operations. */
4.14 +
4.15 long file_context(file_t *file, l4_cap_idx_t server);
4.16 +long file_context_open(file_t *file, file_t *context);
4.17 void file_init(file_t *file);
4.18 -long file_open(file_t *file, file_t *context);
4.19
4.20 /* File access region operations. */
4.21
4.22 @@ -68,7 +72,7 @@
4.23 /* Convenience functions. */
4.24
4.25 char *file_string_get(file_t *file, offset_t offset);
4.26 -offset_t file_string_set(file_t *file, const char *data, offset_t offset);
4.27 +int file_string_set(file_t *file, const char *data, offset_t offset, offset_t *written);
4.28
4.29 EXTERN_C_END
4.30