1.1 --- a/libfsserver/lib/files/block_file_opener.cc Fri Jul 16 00:40:22 2021 +0200
1.2 +++ b/libfsserver/lib/files/block_file_opener.cc Sun Jul 18 00:47:14 2021 +0200
1.3 @@ -19,19 +19,55 @@
1.4 * Boston, MA 02110-1301, USA
1.5 */
1.6
1.7 +#include <systypes/fcntl.h>
1.8 +
1.9 #include "block_file_accessor.h"
1.10 #include "block_file_opener.h"
1.11
1.12 +/* Return a file identifier for the given 'path'. */
1.13 +
1.14 +fileid_t BlockFileOpener::get_fileid(const char *path, flags_t flags)
1.15 +{
1.16 + /* Obtain any registered identifier for the path. */
1.17 +
1.18 + fileid_t fileid = _get_fileid(path, false);
1.19 +
1.20 + if (fileid != FILEID_INVALID)
1.21 + return fileid;
1.22 +
1.23 + /* Test for a valid file or an attempt to create a file. */
1.24 +
1.25 + FILE *fp = fopen(path, "r");
1.26 +
1.27 + if (fp == NULL)
1.28 + {
1.29 + if (!(flags & O_CREAT))
1.30 + return FILEID_INVALID;
1.31 + }
1.32 + else
1.33 + fclose(fp);
1.34 +
1.35 + return _get_fileid(path, true);
1.36 +}
1.37 +
1.38 /* Return a new accessor for 'fileid'. */
1.39
1.40 -Accessor *BlockFileOpener::make_accessor(const char *path, fileid_t fileid)
1.41 +long BlockFileOpener::make_accessor(const char *path, flags_t flags,
1.42 + fileid_t fileid, Accessor **accessor)
1.43 {
1.44 FILE *fp = fopen(path, "r");
1.45
1.46 if (fp == NULL)
1.47 - return NULL;
1.48 + {
1.49 + if (flags & O_CREAT)
1.50 + *accessor = new BlockFileAccessor(fileid);
1.51 + else
1.52 + return -L4_ENOENT;
1.53 + }
1.54 + else
1.55 + *accessor = new BlockFileAccessor(fp, fileid);
1.56
1.57 - return new BlockFileAccessor(fp, fileid);
1.58 + return L4_EOK;
1.59 }
1.60
1.61 // vim: tabstop=4 expandtab shiftwidth=4