Debian Codegraph @

Source/dirreader.cpp

raw
1 /*
2 * dirreader.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17 #include "Platform.h"
18 #include "dirreader.h"
19 #include "tstring.h"
20 #include "util.h"
21 #include <set>
22
23 #include <string.h> // for stricmp()
24 #include <ctype.h> // for tolower()
25 #ifdef _UNICODE
26 # include <wctype.h> // towlower()
27 #endif
28
29 using namespace std;
30
31 dir_reader::dir_reader() {
32 exclude(_T("."));
33 exclude(_T(".."));
34 }
35
36 const set<tstring>& dir_reader::files() {
37 return m_files;
38 }
39
40 const set<tstring>& dir_reader::dirs() {
41 return m_dirs;
42 }
43
44 void dir_reader::exclude(const tstring& spec) {
45 if (spec.find_first_of(_T("?*")) != tstring::npos) {
46 m_wildcard_excluded.insert(spec);
47 } else {
48 m_excluded.insert(spec);
49 }
50 }
51
52 void dir_reader::exclude(const set<tstring>& specs) {
53 iterator i = specs.begin();
54 iterator e = specs.end();
55
56 for (; i != e; i++) {
57 exclude(*i);
58 }
59 }
60
61 bool dir_reader::matches(const tstring& name, const tstring& spec) {
62 tstring::const_iterator name_itr = name.begin();
63 tstring::const_iterator name_end = name.end();
64
65 tstring::const_iterator spec_itr = spec.begin();
66 tstring::const_iterator spec_end = spec.end();
67
68 tstring::const_iterator last_good_spec = spec_end;
69 tstring::const_iterator last_good_name = name_end;
70
71 while (name_itr != name_end && spec_itr != spec_end) {
72 switch (*spec_itr) {
73 case _T('?'):
74 // question mark matches one char
75 name_itr++;
76 spec_itr++;
77 break;
78
79 case _T('*'):
80 // double asterisk is the same as a single asterisk
81 while (*spec_itr == _T('*')) {
82 spec_itr++;
83 // asterisk at the end of the spec matches the end of the name
84 if (spec_itr == spec_end)
85 return true;
86 }
87
88 // remember last good name and spec for prematurely stopped asterisk
89 last_good_spec = spec_itr;
90 last_good_name = name_itr;
91
92 break;
93
94 default:
95 // Jim Park: This should work since tolower is templated with Chartype.
96 if (::tolower(*name_itr) != ::tolower(*spec_itr)) {
97 if (last_good_spec != spec_end) {
98 // matched wrong part of the name, try again
99 spec_itr = last_good_spec;
100 name_itr = ++last_good_name;
101 } else {
102 // no match and no asterisk to use
103 return false;
104 }
105 } else {
106 // remember last good name for prematurely stopped asterisk
107 last_good_name = name_itr;
108
109 spec_itr++;
110 name_itr++;
111
112 if (spec_itr == spec_end && name_itr != name_end && last_good_spec != spec_end) {
113 // asterisk hasn't matched enough, keep matching
114 spec_itr = last_good_spec;
115 }
116 }
117 break;
118 }
119 }
120
121 // skip any redundant asterisks and periods at the end of the name
122 while (spec_itr != spec_end) {
123 if (*spec_itr != _T('.') && *spec_itr != _T('*')) {
124 break;
125 }
126 spec_itr++;
127 }
128
129 // return true only if managed to match everything
130 return name_itr == name_end && spec_itr == spec_end;
131 }
132
133 void dir_reader::add_file(const tstring& file) {
134 if (!is_excluded(file)) {
135 m_files.insert(file);
136 }
137 }
138
139 void dir_reader::add_dir(const tstring& dir) {
140 if (!is_excluded(dir)) {
141 m_dirs.insert(dir);
142 }
143 }
144
145 bool dir_reader::is_excluded(const tstring& name) const {
146 iterator i = m_excluded.begin();
147 iterator e = m_excluded.end();
148
149 for (; i != e; i++) {
150 if (!::_tcsicmp(name.c_str(), i->c_str())) {
151 return true;
152 }
153 }
154
155 i = m_wildcard_excluded.begin();
156 e = m_wildcard_excluded.end();
157
158 for (; i != e; i++) {
159 if (matches(name, *i)) {
160 return true;
161 }
162 }
163
164 return false;
165 }
166
167 #ifdef _WIN32
168
169 class win32_dir_reader : public dir_reader {
170 public:
171
172 virtual void read(const tstring& dir) {
173 WIN32_FIND_DATA fd;
174
175 tstring spec = dir + PLATFORM_PATH_SEPARATOR_STR + _T("*.*");
176
177 HANDLE h = ::FindFirstFile(spec.c_str(), &fd);
178 if (h != INVALID_HANDLE_VALUE) {
179 do {
180 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
181 dir_reader::add_dir(fd.cFileName);
182 else
183 dir_reader::add_file(fd.cFileName);
184 } while (::FindNextFile(h, &fd));
185 ::FindClose(h);
186 }
187 }
188
189 };
190
191 #else
192
193 #include <sys/stat.h>
194 #include <sys/types.h>
195 #include <dirent.h>
196
197 class posix_dir_reader : public dir_reader {
198 public:
199
200 virtual void read(const tstring& dir) {
201
202 static const char platformpathsep[2] = {(char)PLATFORM_PATH_SEPARATOR_C, '\0'};
203
204 char *nativedir = NSISRT_ttombpath(dir.c_str());
205 if (!nativedir) return ;
206
207 DIR *dip = ::opendir(nativedir);
208 if (dip) {
209 dirent *dit;
210 while ((dit = ::readdir(dip))) {
211 struct stat st;
212 string file = nativedir;
213 file += platformpathsep, file += dit->d_name;
214
215 if (!stat(file.c_str(), &st)) {
216 tstring name;
217 name = PosixBug_CtoTString(dit->d_name);
218 if (S_ISDIR(st.st_mode))
219 dir_reader::add_dir(name);
220 else
221 dir_reader::add_file(name);
222 }
223 }
224 ::closedir(dip);
225 }
226 NSISRT_free(nativedir);
227 }
228 };
229
230 #endif
231
232 dir_reader* new_dir_reader() {
233 #ifdef _WIN32
234 return new win32_dir_reader();
235 #else
236 return new posix_dir_reader();
237 #endif
238 }