paul@202 | 1 | /* |
paul@224 | 2 | * Filesystem provider registry. |
paul@202 | 3 | * |
paul@202 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@202 | 5 | * |
paul@202 | 6 | * This program is free software; you can redistribute it and/or |
paul@202 | 7 | * modify it under the terms of the GNU General Public License as |
paul@202 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@202 | 9 | * the License, or (at your option) any later version. |
paul@202 | 10 | * |
paul@202 | 11 | * This program is distributed in the hope that it will be useful, |
paul@202 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@202 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@202 | 14 | * GNU General Public License for more details. |
paul@202 | 15 | * |
paul@202 | 16 | * You should have received a copy of the GNU General Public License |
paul@202 | 17 | * along with this program; if not, write to the Free Software |
paul@202 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@202 | 19 | * Boston, MA 02110-1301, USA |
paul@202 | 20 | */ |
paul@202 | 21 | |
paul@224 | 22 | #include "provider_registry.h" |
paul@202 | 23 | |
paul@230 | 24 | /* To complete forward declaration. */ |
paul@230 | 25 | |
paul@230 | 26 | #include "provider.h" |
paul@230 | 27 | |
paul@202 | 28 | |
paul@202 | 29 | |
paul@202 | 30 | /* Methods for use with the lock already acquired. */ |
paul@202 | 31 | |
paul@202 | 32 | /* Return any registered provider for the given 'fileid' or NULL if no such |
paul@202 | 33 | provider is registered. */ |
paul@202 | 34 | |
paul@230 | 35 | Provider *ProviderRegistry::get(fileid_t fileid) |
paul@202 | 36 | { |
paul@230 | 37 | ProviderMapping::iterator entry = _providers.find(fileid); |
paul@230 | 38 | Provider *provider; |
paul@202 | 39 | |
paul@202 | 40 | if (entry == _providers.end()) |
paul@202 | 41 | provider = NULL; |
paul@202 | 42 | else |
paul@202 | 43 | provider = entry->second; |
paul@202 | 44 | |
paul@202 | 45 | return provider; |
paul@202 | 46 | } |
paul@202 | 47 | |
paul@202 | 48 | /* Remove a provider and its resources for the given 'fileid'. */ |
paul@202 | 49 | |
paul@230 | 50 | void ProviderRegistry::remove(fileid_t fileid, Provider *provider) |
paul@202 | 51 | { |
paul@202 | 52 | _providers.erase(fileid); |
paul@202 | 53 | delete provider; |
paul@202 | 54 | } |
paul@202 | 55 | |
paul@202 | 56 | /* Register a 'provider' for the given 'fileid'. */ |
paul@202 | 57 | |
paul@230 | 58 | void ProviderRegistry::set(fileid_t fileid, Provider *provider) |
paul@202 | 59 | { |
paul@230 | 60 | ProviderMapping::iterator entry = _providers.find(fileid); |
paul@202 | 61 | |
paul@202 | 62 | if (entry != _providers.end()) |
paul@202 | 63 | return; |
paul@202 | 64 | |
paul@202 | 65 | _providers[fileid] = provider; |
paul@202 | 66 | } |
paul@202 | 67 | |
paul@202 | 68 | |
paul@202 | 69 | |
paul@202 | 70 | /* Detach from a provider, potentially removing it from the registry. */ |
paul@202 | 71 | |
paul@230 | 72 | void ProviderRegistry::detach(fileid_t fileid, Provider *provider) |
paul@202 | 73 | { |
paul@202 | 74 | std::lock_guard<std::mutex> guard(_lock); |
paul@202 | 75 | |
paul@202 | 76 | if (!provider->detach()) |
paul@232 | 77 | { |
paul@232 | 78 | /* Remove any underlying object if pending. */ |
paul@232 | 79 | |
paul@232 | 80 | if (provider->removal_pending()) |
paul@232 | 81 | provider->remove(); |
paul@232 | 82 | |
paul@232 | 83 | /* Remove from the registry. */ |
paul@232 | 84 | |
paul@202 | 85 | remove(fileid, provider); |
paul@232 | 86 | } |
paul@202 | 87 | } |
paul@202 | 88 | |
paul@202 | 89 | // vim: tabstop=4 expandtab shiftwidth=4 |