1.1 --- a/Makefile Sun Feb 28 22:14:39 2021 +0100
1.2 +++ b/Makefile Sun Feb 28 22:48:57 2021 +0100
1.3 @@ -42,7 +42,7 @@
1.4
1.5 PLAIN_SRC_CC_dstest_host_client = dstest_host_client.cc
1.6
1.7 -PLAIN_SRC_CC_dstest_test_client = dstest_test_client.cc
1.8 +PLAIN_SRC_CC_dstest_test_client = dstest_test_client.cc file.cc
1.9
1.10 PLAIN_SRC_CC_dstest_host_server = \
1.11 access_map.cc accessing.cc accessor.cc \
2.1 --- a/dstest_test_client.cc Sun Feb 28 22:14:39 2021 +0100
2.2 +++ b/dstest_test_client.cc Sun Feb 28 22:48:57 2021 +0100
2.3 @@ -28,14 +28,9 @@
2.4
2.5 #include <thread>
2.6
2.7 -#include <ipc/cap_alloc.h>
2.8 -#include <ipc/mem_ipc.h>
2.9 #include <ipc/thread.h>
2.10
2.11 -#include "dataspace_client.h"
2.12 -#include "opener_client.h"
2.13 -#include "opener_context_client.h"
2.14 -#include "mapped_file_client.h"
2.15 +#include "file.h"
2.16 #include "memory_utils.h"
2.17
2.18
2.19 @@ -53,81 +48,6 @@
2.20
2.21
2.22
2.23 -typedef struct
2.24 -{
2.25 - l4_cap_idx_t ref;
2.26 - char *memory;
2.27 - offset_t start_pos, end_pos, data_end, size;
2.28 -} file_t;
2.29 -
2.30 -
2.31 -
2.32 -static offset_t file_span(file_t *file)
2.33 -{
2.34 - offset_t size = file->end_pos - file->start_pos;
2.35 -
2.36 - return (file->data_end < size) ? file->data_end : size;
2.37 -}
2.38 -
2.39 -static void file_init(file_t *file)
2.40 -{
2.41 - file->memory = NULL;
2.42 - file->ref = L4_INVALID_CAP;
2.43 -}
2.44 -
2.45 -static long file_open(file_t *file, l4_cap_idx_t context_ref)
2.46 -{
2.47 - client_OpenerContext context(context_ref);
2.48 - file_init(file);
2.49 - return context.open(L4_FPAGE_RW, &file->size, &file->ref);
2.50 -}
2.51 -
2.52 -static void file_close(file_t *file)
2.53 -{
2.54 - if (l4_is_valid_cap(file->ref))
2.55 - ipc_cap_free_um(file->ref);
2.56 -
2.57 - if (file->memory != NULL)
2.58 - ipc_detach_dataspace(file->memory);
2.59 -
2.60 - file_init(file);
2.61 -}
2.62 -
2.63 -static long file_mmap(file_t *file, offset_t position, offset_t length)
2.64 -{
2.65 - client_MappedFile mapped_file(file->ref);
2.66 - long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end);
2.67 -
2.68 - if (err)
2.69 - return err;
2.70 -
2.71 - return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory);
2.72 -}
2.73 -
2.74 -static long context_open(file_t *file, l4_cap_idx_t server)
2.75 -{
2.76 - client_Opener opener(server);
2.77 - unsigned long size, flags;
2.78 - long err;
2.79 -
2.80 - file_init(file);
2.81 - err = opener.context(&file->ref);
2.82 -
2.83 - if (err)
2.84 - return err;
2.85 -
2.86 - client_Dataspace context_ds(file->ref);
2.87 -
2.88 - err = context_ds.info(&size, &flags);
2.89 -
2.90 - if (err)
2.91 - return err;
2.92 -
2.93 - return ipc_attach_dataspace(file->ref, size, (void **) &file->memory);
2.94 -}
2.95 -
2.96 -
2.97 -
2.98 /* An activity opening and reading from a file. */
2.99
2.100 static long activity(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page)
2.101 @@ -293,3 +213,5 @@
2.102
2.103 return 0;
2.104 }
2.105 +
2.106 +// vim: tabstop=2 expandtab shiftwidth=2
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/file.cc Sun Feb 28 22:48:57 2021 +0100
3.3 @@ -0,0 +1,98 @@
3.4 +/*
3.5 + * File access convenience functions.
3.6 + *
3.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#include <ipc/cap_alloc.h>
3.26 +#include <ipc/mem_ipc.h>
3.27 +
3.28 +#include "dataspace_client.h"
3.29 +#include "opener_client.h"
3.30 +#include "opener_context_client.h"
3.31 +#include "mapped_file_client.h"
3.32 +
3.33 +#include "file.h"
3.34 +
3.35 +
3.36 +
3.37 +offset_t file_span(file_t *file)
3.38 +{
3.39 + offset_t size = file->end_pos - file->start_pos;
3.40 +
3.41 + return (file->data_end < size) ? file->data_end : size;
3.42 +}
3.43 +
3.44 +void file_init(file_t *file)
3.45 +{
3.46 + file->memory = NULL;
3.47 + file->ref = L4_INVALID_CAP;
3.48 +}
3.49 +
3.50 +long file_open(file_t *file, l4_cap_idx_t context_ref)
3.51 +{
3.52 + client_OpenerContext context(context_ref);
3.53 + file_init(file);
3.54 + return context.open(L4_FPAGE_RW, &file->size, &file->ref);
3.55 +}
3.56 +
3.57 +void file_close(file_t *file)
3.58 +{
3.59 + if (l4_is_valid_cap(file->ref))
3.60 + ipc_cap_free_um(file->ref);
3.61 +
3.62 + if (file->memory != NULL)
3.63 + ipc_detach_dataspace(file->memory);
3.64 +
3.65 + file_init(file);
3.66 +}
3.67 +
3.68 +long file_mmap(file_t *file, offset_t position, offset_t length)
3.69 +{
3.70 + client_MappedFile mapped_file(file->ref);
3.71 + long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end);
3.72 +
3.73 + if (err)
3.74 + return err;
3.75 +
3.76 + return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory);
3.77 +}
3.78 +
3.79 +long context_open(file_t *file, l4_cap_idx_t server)
3.80 +{
3.81 + client_Opener opener(server);
3.82 + unsigned long size, flags;
3.83 + long err;
3.84 +
3.85 + file_init(file);
3.86 + err = opener.context(&file->ref);
3.87 +
3.88 + if (err)
3.89 + return err;
3.90 +
3.91 + client_Dataspace context_ds(file->ref);
3.92 +
3.93 + err = context_ds.info(&size, &flags);
3.94 +
3.95 + if (err)
3.96 + return err;
3.97 +
3.98 + return ipc_attach_dataspace(file->ref, size, (void **) &file->memory);
3.99 +}
3.100 +
3.101 +// vim: tabstop=2 expandtab shiftwidth=2
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/file.h Sun Feb 28 22:48:57 2021 +0100
4.3 @@ -0,0 +1,42 @@
4.4 +/*
4.5 + * File access convenience functions and types.
4.6 + *
4.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
4.8 + *
4.9 + * This program is free software; you can redistribute it and/or
4.10 + * modify it under the terms of the GNU General Public License as
4.11 + * published by the Free Software Foundation; either version 2 of
4.12 + * the License, or (at your option) any later version.
4.13 + *
4.14 + * This program is distributed in the hope that it will be useful,
4.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + * GNU General Public License for more details.
4.18 + *
4.19 + * You should have received a copy of the GNU General Public License
4.20 + * along with this program; if not, write to the Free Software
4.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
4.22 + * Boston, MA 02110-1301, USA
4.23 + */
4.24 +
4.25 +#include <l4/sys/types.h>
4.26 +
4.27 +#include <systypes/base.h>
4.28 +
4.29 +
4.30 +
4.31 +typedef struct
4.32 +{
4.33 + l4_cap_idx_t ref;
4.34 + char *memory;
4.35 + offset_t start_pos, end_pos, data_end, size;
4.36 +} file_t;
4.37 +
4.38 +offset_t file_span(file_t *file);
4.39 +void file_init(file_t *file);
4.40 +long file_open(file_t *file, l4_cap_idx_t context_ref);
4.41 +void file_close(file_t *file);
4.42 +long file_mmap(file_t *file, offset_t position, offset_t length);
4.43 +long context_open(file_t *file, l4_cap_idx_t server);
4.44 +
4.45 +// vim: tabstop=2 expandtab shiftwidth=2