1 /* 2 * An opener for a file employing a rewritable memory area. 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 <systypes/fcntl.h> 23 24 #include "block_file_accessor.h" 25 #include "block_file_opener.h" 26 27 BlockFileOpener::~BlockFileOpener() 28 { 29 } 30 31 /* Return a file identifier for the given 'path'. */ 32 33 long BlockFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) 34 { 35 /* Obtain any registered identifier for the path. */ 36 37 *fileid = _get_fileid(path, false); 38 39 if (*fileid != FILEID_INVALID) 40 return L4_EOK; 41 42 /* Test for a valid file or an attempt to create a file. */ 43 44 FILE *fp = fopen(path, "r"); 45 46 if (fp == NULL) 47 { 48 if (!(flags & O_CREAT)) 49 return -L4_ENOENT; 50 } 51 else 52 fclose(fp); 53 54 *fileid = _get_fileid(path, true); 55 return L4_EOK; 56 } 57 58 /* Return a new accessor for 'fileid'. */ 59 60 long BlockFileOpener::make_accessor(flags_t flags, fileid_t fileid, 61 Accessor **accessor) 62 { 63 const char *path = _get_path(fileid); 64 65 if (path == NULL) 66 return -L4_ENOENT; 67 68 FILE *fp = fopen(path, "r"); 69 70 if (fp == NULL) 71 { 72 if (flags & O_CREAT) 73 *accessor = new BlockFileAccessor(fileid); 74 else 75 return -L4_ENOENT; 76 } 77 else 78 *accessor = new BlockFileAccessor(fp, fileid); 79 80 return L4_EOK; 81 } 82 83 // vim: tabstop=4 expandtab shiftwidth=4