1 /*
2 * InspIRCd -- Internet Relay Chat Daemon
3 *
4 * Copyright (C) 2013, 2019-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
6 *
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.
10 *
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
14 * details.
15 *
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/>.
18 */
19
20
21 #include "inspircd.h"
22
23 #include <fstream>
24
25 #ifndef _WIN32
26 # include <dirent.h>
27 #endif
28
FileReader(const std::string & filename)29 FileReader::FileReader(const std::string& filename)
30 {
31 Load(filename);
32 }
33
Load(const std::string & filename)34 void FileReader::Load(const std::string& filename)
35 {
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())
39 {
40 this->lines = it->second;
41 }
42 else
43 {
44 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
45 lines.clear();
46
47 std::ifstream stream(realName.c_str());
48 if (!stream.is_open())
49 throw CoreException(filename + " does not exist or is not readable!");
50
51 std::string line;
52 while (std::getline(stream, line))
53 {
54 lines.push_back(line);
55 totalSize += line.size() + 2;
56 }
57
58 stream.close();
59 }
60 }
61
GetString() const62 std::string FileReader::GetString() const
63 {
64 std::string buffer;
65 for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
66 {
67 buffer.append(*it);
68 buffer.append("\r\n");
69 }
70 return buffer;
71 }
72
ExpandPath(const std::string & base,const std::string & fragment)73 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
74 {
75 // The fragment is an absolute path, don't modify it.
76 if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
77 return fragment;
78
79 // The fragment is relative to a home directory, expand that.
80 if (!fragment.compare(0, 2, "~/", 2))
81 {
82 const char* homedir = getenv("HOME");
83 if (homedir && *homedir)
84 return std::string(homedir) + '/' + fragment.substr(2);
85 }
86
87 return base + '/' + fragment;
88 }
89
FileExists(const std::string & file)90 bool FileSystem::FileExists(const std::string& file)
91 {
92 struct stat sb;
93 if (stat(file.c_str(), &sb) == -1)
94 return false;
95
96 if ((sb.st_mode & S_IFDIR) > 0)
97 return false;
98
99 return !access(file.c_str(), F_OK);
100 }
101
GetFileList(const std::string & directory,std::vector<std::string> & entries,const std::string & match)102 bool FileSystem::GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match)
103 {
104 #ifdef _WIN32
105 const std::string search_path = directory + "\\" + match;
106
107 WIN32_FIND_DATAA wfd;
108 HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd);
109 if (fh == INVALID_HANDLE_VALUE)
110 return false;
111
112 do
113 {
114 entries.push_back(wfd.cFileName);
115 } while (FindNextFile(fh, &wfd) != 0);
116
117 FindClose(fh);
118 return true;
119 #else
120 DIR* library = opendir(directory.c_str());
121 if (!library)
122 return false;
123
124 dirent* entry = NULL;
125 while ((entry = readdir(library)))
126 {
127 if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map))
128 entries.push_back(entry->d_name);
129 }
130 closedir(library);
131 return true;
132 #endif
133 }
134
135
GetFileName(const std::string & name)136 std::string FileSystem::GetFileName(const std::string& name)
137 {
138 #ifdef _WIN32
139 size_t pos = name.find_last_of("\\/");
140 #else
141 size_t pos = name.rfind('/');
142 #endif
143 return pos == std::string::npos ? name : name.substr(++pos);
144 }
145
StartsWithWindowsDriveLetter(const std::string & path)146 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
147 {
148 return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');
149 }
150