1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/file.cc Sun Feb 28 22:48:57 2021 +0100
1.3 @@ -0,0 +1,98 @@
1.4 +/*
1.5 + * File access convenience functions.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <ipc/cap_alloc.h>
1.26 +#include <ipc/mem_ipc.h>
1.27 +
1.28 +#include "dataspace_client.h"
1.29 +#include "opener_client.h"
1.30 +#include "opener_context_client.h"
1.31 +#include "mapped_file_client.h"
1.32 +
1.33 +#include "file.h"
1.34 +
1.35 +
1.36 +
1.37 +offset_t file_span(file_t *file)
1.38 +{
1.39 + offset_t size = file->end_pos - file->start_pos;
1.40 +
1.41 + return (file->data_end < size) ? file->data_end : size;
1.42 +}
1.43 +
1.44 +void file_init(file_t *file)
1.45 +{
1.46 + file->memory = NULL;
1.47 + file->ref = L4_INVALID_CAP;
1.48 +}
1.49 +
1.50 +long file_open(file_t *file, l4_cap_idx_t context_ref)
1.51 +{
1.52 + client_OpenerContext context(context_ref);
1.53 + file_init(file);
1.54 + return context.open(L4_FPAGE_RW, &file->size, &file->ref);
1.55 +}
1.56 +
1.57 +void file_close(file_t *file)
1.58 +{
1.59 + if (l4_is_valid_cap(file->ref))
1.60 + ipc_cap_free_um(file->ref);
1.61 +
1.62 + if (file->memory != NULL)
1.63 + ipc_detach_dataspace(file->memory);
1.64 +
1.65 + file_init(file);
1.66 +}
1.67 +
1.68 +long file_mmap(file_t *file, offset_t position, offset_t length)
1.69 +{
1.70 + client_MappedFile mapped_file(file->ref);
1.71 + long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end);
1.72 +
1.73 + if (err)
1.74 + return err;
1.75 +
1.76 + return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory);
1.77 +}
1.78 +
1.79 +long context_open(file_t *file, l4_cap_idx_t server)
1.80 +{
1.81 + client_Opener opener(server);
1.82 + unsigned long size, flags;
1.83 + long err;
1.84 +
1.85 + file_init(file);
1.86 + err = opener.context(&file->ref);
1.87 +
1.88 + if (err)
1.89 + return err;
1.90 +
1.91 + client_Dataspace context_ds(file->ref);
1.92 +
1.93 + err = context_ds.info(&size, &flags);
1.94 +
1.95 + if (err)
1.96 + return err;
1.97 +
1.98 + return ipc_attach_dataspace(file->ref, size, (void **) &file->memory);
1.99 +}
1.100 +
1.101 +// vim: tabstop=2 expandtab shiftwidth=2