paul@10 | 1 | #include "opener_server.h" |
paul@10 | 2 | #include "opener_resource.h" |
paul@10 | 3 | #include "resource_server.h" |
paul@9 | 4 | |
paul@9 | 5 | /* Support for providing access to files. */ |
paul@9 | 6 | |
paul@10 | 7 | OpenerResource::OpenerResource(Accessing *accessing, Paging *paging, Pages *pages) |
paul@9 | 8 | : _accessing(accessing), _paging(paging), _pages(pages) |
paul@9 | 9 | { |
paul@9 | 10 | } |
paul@9 | 11 | |
paul@10 | 12 | int OpenerResource::expected_items() |
paul@9 | 13 | { |
paul@10 | 14 | return Opener_expected_items; |
paul@9 | 15 | } |
paul@9 | 16 | |
paul@10 | 17 | ipc_server_handler_type OpenerResource::handler() |
paul@10 | 18 | { |
paul@10 | 19 | return (ipc_server_handler_type) handle_Opener; |
paul@10 | 20 | } |
paul@10 | 21 | |
paul@10 | 22 | |
paul@10 | 23 | |
paul@48 | 24 | /* Return a pager object for the given path and flags. */ |
paul@10 | 25 | |
paul@51 | 26 | FilePager *OpenerResource::open(const char *path, flags_t flags) |
paul@9 | 27 | { |
paul@9 | 28 | fileid_t fileid = get_fileid(path); |
paul@9 | 29 | |
paul@9 | 30 | if (fileid == FILEID_INVALID) |
paul@9 | 31 | return NULL; |
paul@9 | 32 | |
paul@9 | 33 | Accessor *accessor = get_accessor(fileid); |
paul@9 | 34 | PageMapper *mapper = get_mapper(accessor); |
paul@48 | 35 | return new FilePager(fileid, mapper, flags); |
paul@9 | 36 | } |
paul@9 | 37 | |
paul@9 | 38 | /* Obtain an accessor for the 'fileid' or register a new one in the accessing |
paul@9 | 39 | object. */ |
paul@9 | 40 | |
paul@10 | 41 | Accessor *OpenerResource::get_accessor(fileid_t fileid) |
paul@9 | 42 | { |
paul@9 | 43 | Accessor *accessor; |
paul@9 | 44 | |
paul@9 | 45 | if (_accessing->reserve(fileid)) |
paul@9 | 46 | accessor = _accessing->get(fileid); |
paul@9 | 47 | else |
paul@9 | 48 | { |
paul@9 | 49 | accessor = make_accessor(fileid); |
paul@9 | 50 | _accessing->set(fileid, accessor); |
paul@9 | 51 | } |
paul@9 | 52 | |
paul@9 | 53 | return accessor; |
paul@9 | 54 | } |
paul@9 | 55 | |
paul@9 | 56 | /* Obtain a page mapper for the 'accessor' or register a new one in the |
paul@9 | 57 | paging object. */ |
paul@9 | 58 | |
paul@10 | 59 | PageMapper *OpenerResource::get_mapper(Accessor *accessor) |
paul@9 | 60 | { |
paul@9 | 61 | PageMapper *mapper; |
paul@9 | 62 | |
paul@9 | 63 | if (_paging->reserve(accessor)) |
paul@9 | 64 | mapper = _paging->get(accessor); |
paul@9 | 65 | else |
paul@9 | 66 | { |
paul@9 | 67 | mapper = new PageMapper(accessor, _pages); |
paul@9 | 68 | _paging->set(accessor, mapper); |
paul@9 | 69 | } |
paul@9 | 70 | |
paul@9 | 71 | return mapper; |
paul@9 | 72 | } |
paul@9 | 73 | |
paul@10 | 74 | |
paul@10 | 75 | |
paul@10 | 76 | /* Opener interface methods. */ |
paul@10 | 77 | |
paul@10 | 78 | long OpenerResource::context(l4_cap_idx_t *context) |
paul@10 | 79 | { |
paul@10 | 80 | OpenerContextResource *resource = new OpenerContextResource(this); |
paul@10 | 81 | |
paul@62 | 82 | /* Complete the initialisation and start a server in a new thread. |
paul@62 | 83 | If the thread does not start, the resource should be finalised. */ |
paul@10 | 84 | |
paul@10 | 85 | ResourceServer server(resource); |
paul@10 | 86 | long err = server.start_thread(); |
paul@10 | 87 | |
paul@10 | 88 | /* Return the server capability to the caller. */ |
paul@10 | 89 | |
paul@62 | 90 | if (!err) |
paul@62 | 91 | *context = server.config()->server; |
paul@62 | 92 | |
paul@10 | 93 | return err; |
paul@10 | 94 | } |
paul@10 | 95 | |
paul@9 | 96 | // vim: tabstop=4 expandtab shiftwidth=4 |