2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2019-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 FileReader::FileReader(const std::string& filename)
34 void FileReader::Load(const std::string& filename)
36 // If the file is stored in the file cache then we used that version instead.
37 ConfigFileCache::const_iterator it = ServerInstance->Config->Files.find(filename);
38 if (it != ServerInstance->Config->Files.end())
40 this->lines = it->second;
44 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
47 std::ifstream stream(realName.c_str());
48 if (!stream.is_open())
49 throw CoreException(filename + " does not exist or is not readable!");
52 while (std::getline(stream, line))
54 lines.push_back(line);
55 totalSize += line.size() + 2;
62 std::string FileReader::GetString() const
65 for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
68 buffer.append("\r\n");
73 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
75 // The fragment is an absolute path, don't modify it.
76 if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
79 // The fragment is relative to a home directory, expand that.
80 if (!fragment.compare(0, 2, "~/", 2))
82 const char* homedir = getenv("HOME");
83 if (homedir && *homedir)
84 return std::string(homedir) + '/' + fragment.substr(2);
87 return base + '/' + fragment;
90 bool FileSystem::FileExists(const std::string& file)
93 if (stat(file.c_str(), &sb) == -1)
96 if ((sb.st_mode & S_IFDIR) > 0)
99 return !access(file.c_str(), F_OK);
102 bool FileSystem::GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match)
105 const std::string search_path = directory + "\\" + match;
107 WIN32_FIND_DATAA wfd;
108 HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd);
109 if (fh == INVALID_HANDLE_VALUE)
114 entries.push_back(wfd.cFileName);
115 } while (FindNextFile(fh, &wfd) != 0);
120 DIR* library = opendir(directory.c_str());
124 dirent* entry = NULL;
125 while ((entry = readdir(library)))
127 if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map))
128 entries.push_back(entry->d_name);
136 std::string FileSystem::GetFileName(const std::string& name)
139 size_t pos = name.find_last_of("\\/");
141 size_t pos = name.rfind('/');
143 return pos == std::string::npos ? name : name.substr(++pos);
146 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
148 return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');