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