1 /* 2 * A context resource offering support for opening files. 3 * 4 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <ipc/cap_alloc.h> 23 24 #include "opener_context_resource.h" 25 #include "opener_context_object_server.h" 26 #include "opener_resource.h" 27 28 #include <string.h> 29 30 31 32 /* Support for providing access to files. */ 33 34 OpenerContextResource::OpenerContextResource(OpenerResource *opener, Memory *memory) 35 : SimplePager(memory), _opener(opener) 36 { 37 } 38 39 ipc_server_default_config_type OpenerContextResource::config() 40 { 41 return config_OpenerContextObject; 42 } 43 44 45 46 /* Data access methods. */ 47 48 char *OpenerContextResource::get_path(offset_t offset) 49 { 50 char *buffer = _region->read(offset); 51 offset_t size = _region->size(); 52 53 /* Confine the path to the limit of the buffer. */ 54 55 if ((buffer != NULL) && (strnlen(buffer, size) < size)) 56 return buffer; 57 else 58 return NULL; 59 } 60 61 62 63 /* Opener context interface methods. */ 64 65 long OpenerContextResource::mkdir(sys_mode_t mode) 66 { 67 char *path = get_path(); 68 69 if (path == NULL) 70 return -L4_EINVAL; 71 72 return _opener->mkdir(path, mode); 73 } 74 75 long OpenerContextResource::open(flags_t flags, offset_t *size, 76 l4_cap_idx_t *file, object_flags_t *object_flags) 77 { 78 char *path = get_path(); 79 80 if (path == NULL) 81 return -L4_EINVAL; 82 83 return _opener->open(path, flags, size, file, object_flags); 84 } 85 86 long OpenerContextResource::remove() 87 { 88 char *path = get_path(); 89 90 if (path == NULL) 91 return -L4_EINVAL; 92 93 return _opener->remove(path); 94 } 95 96 long OpenerContextResource::rename() 97 { 98 char *source, *target; 99 100 source = get_path(); 101 102 if (source == NULL) 103 return -L4_EINVAL; 104 105 target = get_path(strlen(source) + 1); 106 107 if (target == NULL) 108 return -L4_EINVAL; 109 110 return _opener->rename(source, target); 111 } 112 113 long OpenerContextResource::stat() 114 { 115 char *path = get_path(); 116 117 if (path == NULL) 118 return -L4_EINVAL; 119 120 return _opener->stat(path, (void *) _region->start, _region->size()); 121 } 122 123 // vim: tabstop=4 expandtab shiftwidth=4