]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2004, Intel Corporation \r | |
4 | All rights reserved. This program and the accompanying materials \r | |
5 | are licensed and made available under the terms and conditions of the BSD License \r | |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | Module Name: \r | |
13 | \r | |
14 | FileSearch.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | Module used to support file searches on the system.\r | |
19 | \r | |
20 | --*/\r | |
21 | \r | |
22 | #include <stdio.h>\r | |
23 | \r | |
24 | #include "CommonUtils.h"\r | |
25 | #include "FileSearch.h"\r | |
26 | #include "UtilsMsgs.h"\r | |
27 | \r | |
28 | //\r | |
29 | // Internal file search flag for sanity checks\r | |
30 | //\r | |
31 | #define FILE_SEARCH_STARTED 0x8000\r | |
32 | #define FILE_SEARCH_INITED 0x4000\r | |
33 | \r | |
34 | static\r | |
35 | BOOLEAN\r | |
36 | FileSearchMeetsCriteria (\r | |
37 | FILE_SEARCH_DATA *FSData\r | |
38 | );\r | |
39 | \r | |
40 | /*****************************************************************************/\r | |
41 | STATUS\r | |
42 | FileSearchInit (\r | |
43 | FILE_SEARCH_DATA *FSData\r | |
44 | )\r | |
45 | {\r | |
46 | memset ((char *) FSData, 0, sizeof (FILE_SEARCH_DATA));\r | |
47 | FSData->Handle = INVALID_HANDLE_VALUE;\r | |
48 | FSData->FileSearchFlags = FILE_SEARCH_INITED;\r | |
49 | FSData->FileName[0] = 0;\r | |
50 | return STATUS_SUCCESS;\r | |
51 | }\r | |
52 | \r | |
53 | STATUS\r | |
54 | FileSearchStart (\r | |
55 | FILE_SEARCH_DATA *FSData,\r | |
56 | char *FileMask,\r | |
57 | UINT32 SearchFlags\r | |
58 | )\r | |
59 | {\r | |
60 | BOOLEAN Done;\r | |
61 | \r | |
62 | //\r | |
63 | // Save their flags, and set a flag to indicate that they called this\r | |
64 | // start function so we can perform extended checking in the other\r | |
65 | // routines we have in this module.\r | |
66 | //\r | |
67 | FSData->FileSearchFlags |= (SearchFlags | FILE_SEARCH_STARTED);\r | |
68 | FSData->FileName[0] = 0;\r | |
69 | \r | |
70 | //\r | |
71 | // Begin the search\r | |
72 | //\r | |
73 | FSData->Handle = FindFirstFile (FileMask, &(FSData->FindData));\r | |
74 | if (FSData->Handle == INVALID_HANDLE_VALUE) {\r | |
75 | return STATUS_ERROR;\r | |
76 | }\r | |
77 | //\r | |
78 | // Keep looping through until we find a file meeting the caller's\r | |
79 | // criteria per the search flags\r | |
80 | //\r | |
81 | Done = FALSE;\r | |
82 | while (!Done) {\r | |
83 | //\r | |
84 | // If we're done (we found a match) copy the file name found and return\r | |
85 | //\r | |
86 | Done = FileSearchMeetsCriteria (FSData);\r | |
87 | if (Done) {\r | |
88 | return STATUS_SUCCESS;\r | |
89 | }\r | |
90 | //\r | |
91 | // Go on to next file\r | |
92 | //\r | |
93 | if (!FindNextFile (FSData->Handle, &(FSData->FindData))) {\r | |
94 | return STATUS_NOT_FOUND;\r | |
95 | }\r | |
96 | }\r | |
97 | //\r | |
98 | // Not reached\r | |
99 | //\r | |
100 | return STATUS_NOT_FOUND;\r | |
101 | }\r | |
102 | \r | |
103 | //\r | |
104 | // Find the next file meeting their criteria and return it.\r | |
105 | //\r | |
106 | STATUS\r | |
107 | FileSearchFindNext (\r | |
108 | FILE_SEARCH_DATA *FSData\r | |
109 | )\r | |
110 | {\r | |
111 | BOOLEAN Done;\r | |
112 | \r | |
113 | Done = FALSE;\r | |
114 | while (!Done) {\r | |
115 | if (!FindNextFile (FSData->Handle, &(FSData->FindData))) {\r | |
116 | return STATUS_NOT_FOUND;\r | |
117 | }\r | |
118 | //\r | |
119 | // See if it matches their criteria\r | |
120 | //\r | |
121 | Done = FileSearchMeetsCriteria (FSData);\r | |
122 | if (Done) {\r | |
123 | return STATUS_SUCCESS;\r | |
124 | }\r | |
125 | }\r | |
126 | //\r | |
127 | // Not reached\r | |
128 | //\r | |
129 | return STATUS_NOT_FOUND;\r | |
130 | }\r | |
131 | //\r | |
132 | // Perform any cleanup necessary to close down a search\r | |
133 | //\r | |
134 | STATUS\r | |
135 | FileSearchDestroy (\r | |
136 | FILE_SEARCH_DATA *FSData\r | |
137 | )\r | |
138 | {\r | |
139 | if (FSData->Handle != INVALID_HANDLE_VALUE) {\r | |
140 | FindClose (FSData->Handle);\r | |
141 | FSData->Handle = INVALID_HANDLE_VALUE;\r | |
142 | }\r | |
143 | \r | |
144 | FSData->FileName[0] = 0;\r | |
145 | FSData->FileSearchFlags = 0;\r | |
146 | return STATUS_SUCCESS;\r | |
147 | }\r | |
148 | \r | |
149 | static\r | |
150 | BOOLEAN\r | |
151 | FileSearchMeetsCriteria (\r | |
152 | FILE_SEARCH_DATA *FSData\r | |
153 | )\r | |
154 | {\r | |
155 | BOOLEAN Status;\r | |
156 | STRING_LIST *StrList;\r | |
157 | UINT32 ExtLen;\r | |
158 | UINT32 FileNameLen;\r | |
159 | \r | |
160 | Status = FALSE;\r | |
161 | \r | |
162 | //\r | |
163 | // First clear the flag indicating this is neither a file or a\r | |
164 | // directory.\r | |
165 | //\r | |
166 | FSData->FileFlags &= ~(FILE_SEARCH_DIR | FILE_SEARCH_FILE);\r | |
167 | \r | |
168 | //\r | |
169 | // We found a file. See if it matches the user's search criteria. First\r | |
170 | // check for this being a directory, and they want directories, and\r | |
171 | // it's not "." and it's not ".."\r | |
172 | //\r | |
173 | if ((FSData->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&\r | |
174 | (FSData->FileSearchFlags & FILE_SEARCH_DIR) &&\r | |
175 | (strcmp (FSData->FindData.cFileName, ".")) &&\r | |
176 | (strcmp (FSData->FindData.cFileName, ".."))\r | |
177 | ) {\r | |
178 | //\r | |
179 | // Assume we'll make it past this check\r | |
180 | //\r | |
181 | Status = TRUE;\r | |
182 | //\r | |
183 | // If they have a list of exclude directories, then check for those\r | |
184 | //\r | |
185 | StrList = FSData->ExcludeDirs;\r | |
186 | while (StrList != NULL) {\r | |
187 | if (stricmp (FSData->FindData.cFileName, StrList->Str) == 0) {\r | |
188 | Status = FALSE;\r | |
189 | break;\r | |
190 | }\r | |
191 | \r | |
192 | StrList = StrList->Next;\r | |
193 | }\r | |
194 | //\r | |
195 | // If we didn't fail due to excluded directories, then set the dir flag\r | |
196 | //\r | |
197 | if (Status) {\r | |
198 | FSData->FileFlags |= FILE_SEARCH_DIR;\r | |
199 | }\r | |
200 | //\r | |
201 | // Else check for a file, and they want files....\r | |
202 | //\r | |
203 | } else if (((FSData->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) &&\r | |
204 | (FSData->FileSearchFlags & FILE_SEARCH_FILE)\r | |
205 | ) {\r | |
206 | //\r | |
207 | // See if it's in our list of excluded files\r | |
208 | //\r | |
209 | Status = TRUE;\r | |
210 | StrList = FSData->ExcludeFiles;\r | |
211 | while (StrList != NULL) {\r | |
212 | if (stricmp (FSData->FindData.cFileName, StrList->Str) == 0) {\r | |
213 | Status = FALSE;\r | |
214 | break;\r | |
215 | }\r | |
216 | \r | |
217 | StrList = StrList->Next;\r | |
218 | }\r | |
219 | \r | |
220 | if (Status) {\r | |
221 | //\r | |
222 | // See if it's in our list of excluded file extensions\r | |
223 | //\r | |
224 | FileNameLen = strlen (FSData->FindData.cFileName);\r | |
225 | StrList = FSData->ExcludeExtensions;\r | |
226 | while (StrList != NULL) {\r | |
227 | ExtLen = strlen (StrList->Str);\r | |
228 | if (stricmp (\r | |
229 | FSData->FindData.cFileName + FileNameLen - ExtLen,\r | |
230 | StrList->Str\r | |
231 | ) == 0) {\r | |
232 | Status = FALSE;\r | |
233 | break;\r | |
234 | }\r | |
235 | \r | |
236 | StrList = StrList->Next;\r | |
237 | }\r | |
238 | }\r | |
239 | \r | |
240 | if (Status) {\r | |
241 | FSData->FileFlags |= FILE_SEARCH_FILE;\r | |
242 | }\r | |
243 | }\r | |
244 | //\r | |
245 | // If it's a match, copy the filename into another field of the structure\r | |
246 | // for portability.\r | |
247 | //\r | |
248 | if (Status) {\r | |
249 | strcpy (FSData->FileName, FSData->FindData.cFileName);\r | |
250 | }\r | |
251 | \r | |
252 | return Status;\r | |
253 | }\r | |
254 | //\r | |
255 | // Exclude a list of subdirectories.\r | |
256 | //\r | |
257 | STATUS\r | |
258 | FileSearchExcludeDirs (\r | |
259 | FILE_SEARCH_DATA *FSData,\r | |
260 | STRING_LIST *StrList\r | |
261 | )\r | |
262 | {\r | |
263 | FSData->ExcludeDirs = StrList;\r | |
264 | return STATUS_SUCCESS;\r | |
265 | }\r | |
266 | \r | |
267 | STATUS\r | |
268 | FileSearchExcludeFiles (\r | |
269 | FILE_SEARCH_DATA *FSData,\r | |
270 | STRING_LIST *StrList\r | |
271 | )\r | |
272 | {\r | |
273 | FSData->ExcludeFiles = StrList;\r | |
274 | return STATUS_SUCCESS;\r | |
275 | }\r | |
276 | \r | |
277 | STATUS\r | |
278 | FileSearchExcludeExtensions (\r | |
279 | FILE_SEARCH_DATA *FSData,\r | |
280 | STRING_LIST *StrList\r | |
281 | )\r | |
282 | {\r | |
283 | FSData->ExcludeExtensions = StrList;\r | |
284 | return STATUS_SUCCESS;\r | |
285 | }\r |