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