L4Re/departure

Annotated libfsserver/lib/files/block_file_opener.cc

240:ffd7b371fcfd
2022-02-10 Paul Boddie Removed file paths from various methods, instead relying on file identifiers. Some operations such as file renaming still need to be aware of file paths because they need to know the parent directories of renamed objects.
paul@93 1
/*
paul@93 2
 * An opener for a file employing a rewritable memory area.
paul@93 3
 *
paul@240 4
 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@93 5
 *
paul@93 6
 * This program is free software; you can redistribute it and/or
paul@93 7
 * modify it under the terms of the GNU General Public License as
paul@93 8
 * published by the Free Software Foundation; either version 2 of
paul@93 9
 * the License, or (at your option) any later version.
paul@93 10
 *
paul@93 11
 * This program is distributed in the hope that it will be useful,
paul@93 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@93 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@93 14
 * GNU General Public License for more details.
paul@93 15
 *
paul@93 16
 * You should have received a copy of the GNU General Public License
paul@93 17
 * along with this program; if not, write to the Free Software
paul@93 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@93 19
 * Boston, MA  02110-1301, USA
paul@93 20
 */
paul@93 21
paul@143 22
#include <systypes/fcntl.h>
paul@143 23
paul@50 24
#include "block_file_accessor.h"
paul@50 25
#include "block_file_opener.h"
paul@50 26
paul@156 27
BlockFileOpener::~BlockFileOpener()
paul@156 28
{
paul@156 29
}
paul@156 30
paul@143 31
/* Return a file identifier for the given 'path'. */
paul@143 32
paul@146 33
long BlockFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid)
paul@143 34
{
paul@143 35
    /* Obtain any registered identifier for the path. */
paul@143 36
paul@146 37
    *fileid = _get_fileid(path, false);
paul@143 38
paul@146 39
    if (*fileid != FILEID_INVALID)
paul@146 40
        return L4_EOK;
paul@143 41
paul@143 42
    /* Test for a valid file or an attempt to create a file. */
paul@143 43
paul@143 44
    FILE *fp = fopen(path, "r");
paul@143 45
paul@143 46
    if (fp == NULL)
paul@143 47
    {
paul@143 48
        if (!(flags & O_CREAT))
paul@146 49
            return -L4_ENOENT;
paul@143 50
    }
paul@143 51
    else
paul@143 52
        fclose(fp);
paul@143 53
paul@146 54
    *fileid = _get_fileid(path, true);
paul@146 55
    return L4_EOK;
paul@143 56
}
paul@143 57
paul@50 58
/* Return a new accessor for 'fileid'. */
paul@50 59
paul@240 60
long BlockFileOpener::make_accessor(flags_t flags, fileid_t fileid,
paul@240 61
                                    Accessor **accessor)
paul@50 62
{
paul@240 63
    const char *path = _get_path(fileid);
paul@240 64
paul@240 65
    if (path == NULL)
paul@240 66
        return -L4_ENOENT;
paul@240 67
paul@106 68
    FILE *fp = fopen(path, "r");
paul@50 69
paul@106 70
    if (fp == NULL)
paul@143 71
    {
paul@143 72
        if (flags & O_CREAT)
paul@143 73
            *accessor = new BlockFileAccessor(fileid);
paul@143 74
        else
paul@143 75
            return -L4_ENOENT;
paul@143 76
    }
paul@143 77
    else
paul@143 78
        *accessor = new BlockFileAccessor(fp, fileid);
paul@106 79
paul@143 80
    return L4_EOK;
paul@50 81
}
paul@50 82
paul@50 83
// vim: tabstop=4 expandtab shiftwidth=4