]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/GuidChk/FileSearch.c
3 Copyright (c) 2004, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Module used to support file searches on the system.
24 #include "CommonUtils.h"
25 #include "FileSearch.h"
26 #include "UtilsMsgs.h"
29 // Internal file search flag for sanity checks
31 #define FILE_SEARCH_STARTED 0x8000
32 #define FILE_SEARCH_INITED 0x4000
36 FileSearchMeetsCriteria (
37 FILE_SEARCH_DATA
*FSData
40 /*****************************************************************************/
43 FILE_SEARCH_DATA
*FSData
46 memset ((char *) FSData
, 0, sizeof (FILE_SEARCH_DATA
));
47 FSData
->Handle
= INVALID_HANDLE_VALUE
;
48 FSData
->FileSearchFlags
= FILE_SEARCH_INITED
;
49 FSData
->FileName
[0] = 0;
50 return STATUS_SUCCESS
;
55 FILE_SEARCH_DATA
*FSData
,
63 // Save their flags, and set a flag to indicate that they called this
64 // start function so we can perform extended checking in the other
65 // routines we have in this module.
67 FSData
->FileSearchFlags
|= (SearchFlags
| FILE_SEARCH_STARTED
);
68 FSData
->FileName
[0] = 0;
73 FSData
->Handle
= FindFirstFile (FileMask
, &(FSData
->FindData
));
74 if (FSData
->Handle
== INVALID_HANDLE_VALUE
) {
78 // Keep looping through until we find a file meeting the caller's
79 // criteria per the search flags
84 // If we're done (we found a match) copy the file name found and return
86 Done
= FileSearchMeetsCriteria (FSData
);
88 return STATUS_SUCCESS
;
93 if (!FindNextFile (FSData
->Handle
, &(FSData
->FindData
))) {
94 return STATUS_NOT_FOUND
;
100 return STATUS_NOT_FOUND
;
104 // Find the next file meeting their criteria and return it.
108 FILE_SEARCH_DATA
*FSData
115 if (!FindNextFile (FSData
->Handle
, &(FSData
->FindData
))) {
116 return STATUS_NOT_FOUND
;
119 // See if it matches their criteria
121 Done
= FileSearchMeetsCriteria (FSData
);
123 return STATUS_SUCCESS
;
129 return STATUS_NOT_FOUND
;
132 // Perform any cleanup necessary to close down a search
136 FILE_SEARCH_DATA
*FSData
139 if (FSData
->Handle
!= INVALID_HANDLE_VALUE
) {
140 FindClose (FSData
->Handle
);
141 FSData
->Handle
= INVALID_HANDLE_VALUE
;
144 FSData
->FileName
[0] = 0;
145 FSData
->FileSearchFlags
= 0;
146 return STATUS_SUCCESS
;
151 FileSearchMeetsCriteria (
152 FILE_SEARCH_DATA
*FSData
156 STRING_LIST
*StrList
;
163 // First clear the flag indicating this is neither a file or a
166 FSData
->FileFlags
&= ~(FILE_SEARCH_DIR
| FILE_SEARCH_FILE
);
169 // We found a file. See if it matches the user's search criteria. First
170 // check for this being a directory, and they want directories, and
171 // it's not "." and it's not ".."
173 if ((FSData
->FindData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
174 (FSData
->FileSearchFlags
& FILE_SEARCH_DIR
) &&
175 (strcmp (FSData
->FindData
.cFileName
, ".")) &&
176 (strcmp (FSData
->FindData
.cFileName
, ".."))
179 // Assume we'll make it past this check
183 // If they have a list of exclude directories, then check for those
185 StrList
= FSData
->ExcludeDirs
;
186 while (StrList
!= NULL
) {
187 if (stricmp (FSData
->FindData
.cFileName
, StrList
->Str
) == 0) {
192 StrList
= StrList
->Next
;
195 // If we didn't fail due to excluded directories, then set the dir flag
198 FSData
->FileFlags
|= FILE_SEARCH_DIR
;
201 // Else check for a file, and they want files....
203 } else if (((FSData
->FindData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == 0) &&
204 (FSData
->FileSearchFlags
& FILE_SEARCH_FILE
)
207 // See if it's in our list of excluded files
210 StrList
= FSData
->ExcludeFiles
;
211 while (StrList
!= NULL
) {
212 if (stricmp (FSData
->FindData
.cFileName
, StrList
->Str
) == 0) {
217 StrList
= StrList
->Next
;
222 // See if it's in our list of excluded file extensions
224 FileNameLen
= strlen (FSData
->FindData
.cFileName
);
225 StrList
= FSData
->ExcludeExtensions
;
226 while (StrList
!= NULL
) {
227 ExtLen
= strlen (StrList
->Str
);
229 FSData
->FindData
.cFileName
+ FileNameLen
- ExtLen
,
236 StrList
= StrList
->Next
;
241 FSData
->FileFlags
|= FILE_SEARCH_FILE
;
245 // If it's a match, copy the filename into another field of the structure
249 strcpy (FSData
->FileName
, FSData
->FindData
.cFileName
);
255 // Exclude a list of subdirectories.
258 FileSearchExcludeDirs (
259 FILE_SEARCH_DATA
*FSData
,
263 FSData
->ExcludeDirs
= StrList
;
264 return STATUS_SUCCESS
;
268 FileSearchExcludeFiles (
269 FILE_SEARCH_DATA
*FSData
,
273 FSData
->ExcludeFiles
= StrList
;
274 return STATUS_SUCCESS
;
278 FileSearchExcludeExtensions (
279 FILE_SEARCH_DATA
*FSData
,
283 FSData
->ExcludeExtensions
= StrList
;
284 return STATUS_SUCCESS
;