L4Re/departure

Annotated libfsserver/lib/files/file_provider.cc

645:3047b11cc814
7 months ago Paul Boddie Fixed the file_data_available result to return zero if the populated span has somehow become less than the current position in the memory region.
paul@200 1
/*
paul@200 2
 * An object providing access to file functionality.
paul@200 3
 *
paul@330 4
 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@200 5
 *
paul@200 6
 * This program is free software; you can redistribute it and/or
paul@200 7
 * modify it under the terms of the GNU General Public License as
paul@200 8
 * published by the Free Software Foundation; either version 2 of
paul@200 9
 * the License, or (at your option) any later version.
paul@200 10
 *
paul@200 11
 * This program is distributed in the hope that it will be useful,
paul@200 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@200 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@200 14
 * GNU General Public License for more details.
paul@200 15
 *
paul@200 16
 * You should have received a copy of the GNU General Public License
paul@200 17
 * along with this program; if not, write to the Free Software
paul@200 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@200 19
 * Boston, MA  02110-1301, USA
paul@200 20
 */
paul@200 21
paul@221 22
#include "file_pager.h"
paul@200 23
#include "file_provider.h"
paul@200 24
paul@200 25
paul@200 26
paul@200 27
/* Initialise the provider with a page 'mapper' for the file's contents. */
paul@200 28
paul@330 29
FileProvider::FileProvider(fileid_t fileid, ProviderRegistry *registry,
paul@330 30
                           PageMapper *mapper)
paul@330 31
: Provider(fileid, registry), _mapper(mapper)
paul@200 32
{
paul@200 33
}
paul@200 34
paul@200 35
/* Deallocate the provider's resources. */
paul@200 36
paul@200 37
FileProvider::~FileProvider()
paul@200 38
{
paul@200 39
    /* Accessors are allocated exclusively for page mappers for files, and
paul@387 40
       so can be deleted here. */
paul@200 41
paul@200 42
    Accessor *accessor = _mapper->accessor();
paul@200 43
    delete _mapper;
paul@387 44
paul@387 45
    accessor->close();
paul@440 46
paul@441 47
    if (_remove)
paul@440 48
        accessor->remove();
paul@440 49
paul@200 50
    delete accessor;
paul@200 51
}
paul@200 52
paul@200 53
/* Return the page mapper provided. */
paul@200 54
paul@200 55
PageMapper *FileProvider::mapper()
paul@200 56
{
paul@200 57
    return _mapper;
paul@200 58
}
paul@200 59
paul@221 60
/* Return a file pager initialised with a provider, page mapper and accessor. */
paul@221 61
paul@346 62
long FileProvider::make_resource(flags_t flags, offset_t *size,
paul@330 63
                                 object_flags_t *object_flags,
paul@221 64
                                 Resource **resource)
paul@221 65
{
paul@221 66
    /* Initialise the pager with the provider and a reference to this object for
paul@221 67
       detaching from the provider. */
paul@221 68
paul@330 69
    FilePager *pager = new FilePager(_fileid, this, flags);
paul@221 70
paul@221 71
    /* Obtain the size details from the pager, also providing appropriate
paul@221 72
       flags. */
paul@221 73
paul@221 74
    *size = pager->get_data_size();
paul@221 75
    *object_flags = OBJECT_SUPPORTS_MMAP | OBJECT_HAS_SIZE;
paul@221 76
paul@221 77
    this->attach();
paul@221 78
    *resource = pager;
paul@221 79
    return L4_EOK;
paul@221 80
}
paul@221 81
paul@200 82
// vim: tabstop=4 expandtab shiftwidth=4