154
cdef class Win32Finder:
155
"""A class which encapsulates the search of files in a given directory"""
154
cdef class Win32ReadDir:
155
"""Read directories on win32."""
160
157
cdef object _directory_kind
161
158
cdef object _file_kind
164
cdef object _last_dirblock
166
def __init__(self, top, prefix=""):
168
self._prefix = prefix
170
self._directory_kind = osutils._directory_kind
171
self._file_kind = osutils._formats[stat.S_IFREG]
173
self._pending = [(osutils.safe_utf8(prefix), osutils.safe_unicode(top))]
174
self._last_dirblock = None
161
self._directory_kind = _readdir_py._directory
162
self._file_kind = _readdir_py._file
164
def top_prefix_to_starting_dir(self, top, prefix=""):
165
"""See DirReader.top_prefix_to_starting_dir."""
166
return (osutils.safe_utf8(prefix), None, None, None,
167
osutils.safe_unicode(top))
179
169
cdef object _get_kind(self, WIN32_FIND_DATAW *data):
180
170
if data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY:
195
185
statvalue.st_dev = 0
198
def _get_files_in(self, directory, relprefix):
199
"""Return the dirblock for all files in the given directory.
188
def read_dir(self, prefix, top):
189
"""Win32 implementation of DirReader.read_dir.
201
:param directory: A path that can directly access the files on disk.
202
Should be a Unicode object.
203
:param relprefix: A psuedo path for these files (as inherited from the
204
original 'prefix=XXX' when instantiating this class.)
205
It should be a UTF-8 string.
206
:return: A dirblock for all the files of the form
207
[(utf8_relpath, utf8_fname, kind, _Win32Stat, unicode_abspath)]
191
:seealso: DirReader.read_dir
209
193
cdef WIN32_FIND_DATAW search_data
210
194
cdef HANDLE hFindFile
232
222
name_unicode = _get_name(&search_data)
233
223
name_utf8 = PyUnicode_AsUTF8String(name_unicode)
234
PyList_Append(dirblock,
235
(relprefix + name_utf8, name_utf8,
224
PyList_Append(dirblock,
225
(relprefix + name_utf8, name_utf8,
236
226
self._get_kind(&search_data),
237
227
self._get_stat_value(&search_data),
238
directory + name_unicode))
228
top_slash + name_unicode))
240
230
result = FindNextFileW(hFindFile, &search_data)
241
231
# FindNextFileW sets GetLastError() == ERROR_NO_MORE_FILES when it
251
241
# TODO: We should probably raise an exception if FindClose
252
242
# returns an error, however, I don't want to supress an
253
243
# earlier Exception, so for now, I'm ignoring this
244
dirblock.sort(key=operator.itemgetter(1))
256
cdef _update_pending(self):
257
"""If we had a result before, add the subdirs to pending."""
258
if self._last_dirblock is not None:
259
# push the entries left in the dirblock onto the pending queue
260
# we do this here, because we allow the user to modified the
261
# queue before the next iteration
262
for d in reversed(self._last_dirblock):
263
if d[2] == self._directory_kind:
264
self._pending.append((d[0], d[-1]))
265
self._last_dirblock = None
268
self._update_pending()
269
if not self._pending:
270
raise StopIteration()
271
relroot, top = self._pending.pop()
272
# NB: At the moment Pyrex doesn't support Unicode literals, which means
273
# that all of these string literals are going to be upcasted to Unicode
275
# Maybe we could use unicode(x) during __init__?
277
relprefix = relroot + '/'
280
top_slash = top + '/'
282
dirblock = self._get_files_in(top_slash, relprefix)
283
dirblock.sort(key=operator.itemgetter(1))
284
self._last_dirblock = dirblock
285
return (relroot, top), dirblock
288
def _walkdirs_utf8_win32_find_file(top, prefix=""):
289
"""Implement a version of walkdirs_utf8 for win32.
291
This uses the find files api to both list the files and to stat them.
293
return Win32Finder(top, prefix=prefix)