1.1 --- a/dstest_host_client.cc Sat Mar 13 17:49:23 2021 +0100
1.2 +++ b/dstest_host_client.cc Sat Mar 13 22:38:33 2021 +0100
1.3 @@ -66,7 +66,7 @@
1.4
1.5 /* Write the filename. */
1.6
1.7 - strcpy(context.memory, filename);
1.8 + file_string_set(&context, filename, 0);
1.9
1.10 /* Invoke the open method to receive the file reference. */
1.11
2.1 --- a/file.cc Sat Mar 13 17:49:23 2021 +0100
2.2 +++ b/file.cc Sat Mar 13 22:38:33 2021 +0100
2.3 @@ -22,6 +22,8 @@
2.4 #include <ipc/cap_alloc.h>
2.5 #include <ipc/mem_ipc.h>
2.6
2.7 +#include <string.h>
2.8 +
2.9 #include "dataspace_client.h"
2.10 #include "opener_client.h"
2.11 #include "opener_context_client.h"
2.12 @@ -76,6 +78,17 @@
2.13 file->ref = L4_INVALID_CAP;
2.14 }
2.15
2.16 +/* Open a file using the given structure and context. */
2.17 +
2.18 +long file_open(file_t *file, file_t *context)
2.19 +{
2.20 + client_OpenerContext openercontext(context->ref);
2.21 + file_init(file);
2.22 + return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref);
2.23 +}
2.24 +
2.25 +
2.26 +
2.27 /* Map a region of the given file to a memory region. */
2.28
2.29 long file_mmap(file_t *file, offset_t position, offset_t length)
2.30 @@ -86,16 +99,7 @@
2.31 if (err)
2.32 return err;
2.33
2.34 - return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory);
2.35 -}
2.36 -
2.37 -/* Open a file using the given structure and context. */
2.38 -
2.39 -long file_open(file_t *file, file_t *context)
2.40 -{
2.41 - client_OpenerContext openercontext(context->ref);
2.42 - file_init(file);
2.43 - return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref);
2.44 + return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory);
2.45 }
2.46
2.47 /* Return the amount of data in the mapped region for the given file. */
2.48 @@ -113,4 +117,50 @@
2.49 return file->end_pos - file->start_pos;
2.50 }
2.51
2.52 +
2.53 +
2.54 +/* Get a pointer to any terminated string at the given offset or NULL if the
2.55 + data from offset is not terminated. */
2.56 +
2.57 +char *file_string_get(file_t *file, offset_t offset)
2.58 +{
2.59 + offset_t limit = file_span(file) - offset;
2.60 +
2.61 + if (strnlen(file->memory + offset, limit) < limit)
2.62 + return file->memory + offset;
2.63 + else
2.64 + return NULL;
2.65 +}
2.66 +
2.67 +/* Copy a string to the mapped region at the given offset, returning the number
2.68 + of copied characters excluding the zero terminator. */
2.69 +
2.70 +offset_t file_string_set(file_t *file, const char *data, offset_t offset)
2.71 +{
2.72 + offset_t i, pos, limit = file_span(file) - 1;
2.73 +
2.74 + /* Do not attempt to copy data with an invalid offset. */
2.75 +
2.76 + if (offset > limit)
2.77 + return 0;
2.78 +
2.79 + /* Copy the data to the given offset, stopping before the final byte of the
2.80 + region. */
2.81 +
2.82 + for (i = 0, pos = offset; pos < limit; i++, pos++)
2.83 + {
2.84 + file->memory[pos] = data[i];
2.85 +
2.86 + /* Terminator written, can return immediately. */
2.87 +
2.88 + if (!data[i])
2.89 + return pos - offset - 1;
2.90 + }
2.91 +
2.92 + /* Terminate the incomplete string at the end of the region. */
2.93 +
2.94 + file->memory[limit] = '\0';
2.95 + return limit - offset;
2.96 +}
2.97 +
2.98 // vim: tabstop=2 expandtab shiftwidth=2
3.1 --- a/file.h Sat Mar 13 17:49:23 2021 +0100
3.2 +++ b/file.h Sat Mar 13 22:38:33 2021 +0100
3.3 @@ -65,6 +65,11 @@
3.4 offset_t file_populated_span(file_t *file);
3.5 offset_t file_span(file_t *file);
3.6
3.7 +/* Convenience functions. */
3.8 +
3.9 +char *file_string_get(file_t *file, offset_t offset);
3.10 +offset_t file_string_set(file_t *file, const char *data, offset_t offset);
3.11 +
3.12 EXTERN_C_END
3.13
3.14 // vim: tabstop=2 expandtab shiftwidth=2