summaryrefslogtreecommitdiff
path: root/Tools/Source/TianoTools/GuidChk/FileSearch.c
blob: 8b5b58fddfa1923dce5e5a8aee60b7f9a1185e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*++

Copyright (c) 2004, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:  

  FileSearch.c
  
Abstract:

  Module used to support file searches on the system.
  
--*/

#include <stdio.h>

#include "CommonUtils.h"
#include "FileSearch.h"
#include "UtilsMsgs.h"

//
// Internal file search flag for sanity checks
//
#define FILE_SEARCH_STARTED 0x8000
#define FILE_SEARCH_INITED  0x4000

static
BOOLEAN
FileSearchMeetsCriteria (
  FILE_SEARCH_DATA    *FSData
  );

/*****************************************************************************/
STATUS
FileSearchInit (
  FILE_SEARCH_DATA    *FSData
  )
{
  memset ((char *) FSData, 0, sizeof (FILE_SEARCH_DATA));
  FSData->Handle          = INVALID_HANDLE_VALUE;
  FSData->FileSearchFlags = FILE_SEARCH_INITED;
  FSData->FileName[0]     = 0;
  return STATUS_SUCCESS;
}

STATUS
FileSearchStart (
  FILE_SEARCH_DATA    *FSData,
  char                *FileMask,
  UINT32              SearchFlags
  )
{
  BOOLEAN Done;

  //
  // Save their flags, and set a flag to indicate that they called this
  // start function so we can perform extended checking in the other
  // routines we have in this module.
  //
  FSData->FileSearchFlags |= (SearchFlags | FILE_SEARCH_STARTED);
  FSData->FileName[0] = 0;

  //
  // Begin the search
  //
  FSData->Handle = FindFirstFile (FileMask, &(FSData->FindData));
  if (FSData->Handle == INVALID_HANDLE_VALUE) {
    return STATUS_ERROR;
  }
  //
  // Keep looping through until we find a file meeting the caller's
  // criteria per the search flags
  //
  Done = FALSE;
  while (!Done) {
    //
    // If we're done (we found a match) copy the file name found and return
    //
    Done = FileSearchMeetsCriteria (FSData);
    if (Done) {
      return STATUS_SUCCESS;
    }
    //
    // Go on to next file
    //
    if (!FindNextFile (FSData->Handle, &(FSData->FindData))) {
      return STATUS_NOT_FOUND;
    }
  }
  //
  // Not reached
  //
  return STATUS_NOT_FOUND;
}

//
// Find the next file meeting their criteria and return it.
//
STATUS
FileSearchFindNext (
  FILE_SEARCH_DATA    *FSData
  )
{
  BOOLEAN Done;

  Done = FALSE;
  while (!Done) {
    if (!FindNextFile (FSData->Handle, &(FSData->FindData))) {
      return STATUS_NOT_FOUND;
    }
    //
    // See if it matches their criteria
    //
    Done = FileSearchMeetsCriteria (FSData);
    if (Done) {
      return STATUS_SUCCESS;
    }
  }
  //
  // Not reached
  //
  return STATUS_NOT_FOUND;
}
//
// Perform any cleanup necessary to close down a search
//
STATUS
FileSearchDestroy (
  FILE_SEARCH_DATA    *FSData
  )
{
  if (FSData->Handle != INVALID_HANDLE_VALUE) {
    FindClose (FSData->Handle);
    FSData->Handle = INVALID_HANDLE_VALUE;
  }

  FSData->FileName[0]     = 0;
  FSData->FileSearchFlags = 0;
  return STATUS_SUCCESS;
}

static
BOOLEAN
FileSearchMeetsCriteria (
  FILE_SEARCH_DATA    *FSData
  )
{
  BOOLEAN     Status;
  STRING_LIST *StrList;
  UINT32      ExtLen;
  UINT32      FileNameLen;

  Status = FALSE;

  //
  // First clear the flag indicating this is neither a file or a
  // directory.
  //
  FSData->FileFlags &= ~(FILE_SEARCH_DIR | FILE_SEARCH_FILE);

  //
  // We found a file. See if it matches the user's search criteria. First
  // check for this being a directory, and they want directories, and
  // it's not "." and it's not ".."
  //
  if ((FSData->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
      (FSData->FileSearchFlags & FILE_SEARCH_DIR) &&
      (strcmp (FSData->FindData.cFileName, ".")) &&
      (strcmp (FSData->FindData.cFileName, ".."))
      ) {
    //
    // Assume we'll make it past this check
    //
    Status = TRUE;
    //
    // If they have a list of exclude directories, then check for those
    //
    StrList = FSData->ExcludeDirs;
    while (StrList != NULL) {
      if (stricmp (FSData->FindData.cFileName, StrList->Str) == 0) {
        Status = FALSE;
        break;
      }

      StrList = StrList->Next;
    }
    //
    // If we didn't fail due to excluded directories, then set the dir flag
    //
    if (Status) {
      FSData->FileFlags |= FILE_SEARCH_DIR;
    }
    //
    // Else check for a file, and they want files....
    //
  } else if (((FSData->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) &&
           (FSData->FileSearchFlags & FILE_SEARCH_FILE)
          ) {
    //
    // See if it's in our list of excluded files
    //
    Status  = TRUE;
    StrList = FSData->ExcludeFiles;
    while (StrList != NULL) {
      if (stricmp (FSData->FindData.cFileName, StrList->Str) == 0) {
        Status = FALSE;
        break;
      }

      StrList = StrList->Next;
    }

    if (Status) {
      //
      // See if it's in our list of excluded file extensions
      //
      FileNameLen = strlen (FSData->FindData.cFileName);
      StrList     = FSData->ExcludeExtensions;
      while (StrList != NULL) {
        ExtLen = strlen (StrList->Str);
        if (stricmp (
              FSData->FindData.cFileName + FileNameLen - ExtLen,
              StrList->Str
              ) == 0) {
          Status = FALSE;
          break;
        }

        StrList = StrList->Next;
      }
    }

    if (Status) {
      FSData->FileFlags |= FILE_SEARCH_FILE;
    }
  }
  //
  // If it's a match, copy the filename into another field of the structure
  // for portability.
  //
  if (Status) {
    strcpy (FSData->FileName, FSData->FindData.cFileName);
  }

  return Status;
}
//
// Exclude a list of subdirectories.
//
STATUS
FileSearchExcludeDirs (
  FILE_SEARCH_DATA    *FSData,
  STRING_LIST         *StrList
  )
{
  FSData->ExcludeDirs = StrList;
  return STATUS_SUCCESS;
}

STATUS
FileSearchExcludeFiles (
  FILE_SEARCH_DATA    *FSData,
  STRING_LIST         *StrList
  )
{
  FSData->ExcludeFiles = StrList;
  return STATUS_SUCCESS;
}

STATUS
FileSearchExcludeExtensions (
  FILE_SEARCH_DATA    *FSData,
  STRING_LIST         *StrList
  )
{
  FSData->ExcludeExtensions = StrList;
  return STATUS_SUCCESS;
}