~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_walkdirs_win32.pyx

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2012 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Helper functions for Walkdirs on win32."""
18
18
 
19
19
 
20
 
cdef extern from "python-compat.h":
 
20
cdef extern from "_walkdirs_win32.h":
21
21
    struct _HANDLE:
22
22
        pass
23
23
    ctypedef _HANDLE *HANDLE
69
69
 
70
70
 
71
71
import operator
72
 
import os
73
72
import stat
74
73
 
75
 
from bzrlib import _readdir_py
76
 
 
77
 
cdef object osutils
78
 
osutils = None
 
74
from bzrlib import osutils
79
75
 
80
76
 
81
77
cdef class _Win32Stat:
85
81
    cdef readonly double st_ctime
86
82
    cdef readonly double st_mtime
87
83
    cdef readonly double st_atime
88
 
    # We can't just declare this as 'readonly' because python2.4 doesn't define
89
 
    # T_LONGLONG as a structure member. So instead we just use a property that
90
 
    # will convert it correctly anyway.
91
 
    cdef __int64 _st_size
92
 
 
93
 
    property st_size:
94
 
        def __get__(self):
95
 
            return self._st_size
 
84
    cdef readonly __int64 st_size
96
85
 
97
86
    # os.stat always returns 0, so we hard code it here
98
 
    property st_dev:
99
 
        def __get__(self):
100
 
            return 0
101
 
    property st_ino:
102
 
        def __get__(self):
103
 
            return 0
104
 
    # st_uid and st_gid required for some external tools like bzr-git & dulwich
105
 
    property st_uid:
106
 
        def __get__(self):
107
 
            return 0
108
 
    property st_gid:
109
 
        def __get__(self):
110
 
            return 0
 
87
    cdef readonly int st_dev
 
88
    cdef readonly int st_ino
111
89
 
112
90
    def __repr__(self):
113
91
        """Repr is the same as a Stat object.
124
102
                                 wcslen(data.cFileName))
125
103
 
126
104
 
127
 
cdef int _get_mode_bits(WIN32_FIND_DATAW *data): # cannot_raise
 
105
cdef int _get_mode_bits(WIN32_FIND_DATAW *data):
128
106
    cdef int mode_bits
129
107
 
130
108
    mode_bits = 0100666 # writeable file, the most common
136
114
    return mode_bits
137
115
 
138
116
 
139
 
cdef __int64 _get_size(WIN32_FIND_DATAW *data): # cannot_raise
 
117
cdef __int64 _get_size(WIN32_FIND_DATAW *data):
140
118
    # Pyrex casts a DWORD into a PyLong anyway, so it is safe to do << 32
141
119
    # on a DWORD
142
120
    return ((<__int64>data.nFileSizeHigh) << 32) + data.nFileSizeLow
143
121
 
144
122
 
145
 
cdef double _ftime_to_timestamp(FILETIME *ft): # cannot_raise
 
123
cdef double _ftime_to_timestamp(FILETIME *ft):
146
124
    """Convert from a FILETIME struct into a floating point timestamp.
147
125
 
148
126
    The fields of a FILETIME structure are the hi and lo part
162
140
    return (val * 1.0e-7) - 11644473600.0
163
141
 
164
142
 
165
 
cdef int _should_skip(WIN32_FIND_DATAW *data): # cannot_raise
 
143
cdef int _should_skip(WIN32_FIND_DATAW *data):
166
144
    """Is this '.' or '..' so we should skip it?"""
167
145
    if (data.cFileName[0] != c'.'):
168
146
        return 0
173
151
    return 0
174
152
 
175
153
 
176
 
cdef class Win32ReadDir:
177
 
    """Read directories on win32."""
 
154
cdef class Win32Finder:
 
155
    """A class which encapsulates the search of files in a given directory"""
 
156
 
 
157
    cdef object _top
 
158
    cdef object _prefix
178
159
 
179
160
    cdef object _directory_kind
180
161
    cdef object _file_kind
181
162
 
182
 
    def __init__(self):
183
 
        self._directory_kind = _readdir_py._directory
184
 
        self._file_kind = _readdir_py._file
185
 
 
186
 
    def top_prefix_to_starting_dir(self, top, prefix=""):
187
 
        """See DirReader.top_prefix_to_starting_dir."""
188
 
        global osutils
189
 
        if osutils is None:
190
 
            from bzrlib import osutils
191
 
        return (osutils.safe_utf8(prefix), None, None, None,
192
 
                osutils.safe_unicode(top))
 
163
    cdef object _pending
 
164
    cdef object _last_dirblock
 
165
 
 
166
    def __init__(self, top, prefix=""):
 
167
        self._top = top
 
168
        self._prefix = prefix
 
169
 
 
170
        self._directory_kind = osutils._directory_kind
 
171
        self._file_kind = osutils._formats[stat.S_IFREG]
 
172
 
 
173
        self._pending = [(osutils.safe_utf8(prefix), osutils.safe_unicode(top))]
 
174
        self._last_dirblock = None
 
175
 
 
176
    def __iter__(self):
 
177
        return self
193
178
 
194
179
    cdef object _get_kind(self, WIN32_FIND_DATAW *data):
195
180
        if data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY:
205
190
        statvalue.st_ctime = _ftime_to_timestamp(&data.ftCreationTime)
206
191
        statvalue.st_mtime = _ftime_to_timestamp(&data.ftLastWriteTime)
207
192
        statvalue.st_atime = _ftime_to_timestamp(&data.ftLastAccessTime)
208
 
        statvalue._st_size = _get_size(data)
 
193
        statvalue.st_size = _get_size(data)
 
194
        statvalue.st_ino = 0
 
195
        statvalue.st_dev = 0
209
196
        return statvalue
210
197
 
211
 
    def read_dir(self, prefix, top):
212
 
        """Win32 implementation of DirReader.read_dir.
 
198
    def _get_files_in(self, directory, relprefix):
 
199
        """Return the dirblock for all files in the given directory.
213
200
 
214
 
        :seealso: 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)]
215
208
        """
216
209
        cdef WIN32_FIND_DATAW search_data
217
210
        cdef HANDLE hFindFile
219
212
        cdef WCHAR *query
220
213
        cdef int result
221
214
 
222
 
        if prefix:
223
 
            relprefix = prefix + '/'
224
 
        else:
225
 
            relprefix = ''
226
 
        top_slash = top + '/'
227
 
 
228
 
        top_star = top_slash + '*'
 
215
        top_star = directory + '*'
229
216
 
230
217
        dirblock = []
231
218
 
244
231
                    continue
245
232
                name_unicode = _get_name(&search_data)
246
233
                name_utf8 = PyUnicode_AsUTF8String(name_unicode)
247
 
                PyList_Append(dirblock,
248
 
                    (relprefix + name_utf8, name_utf8,
 
234
                PyList_Append(dirblock, 
 
235
                    (relprefix + name_utf8, name_utf8, 
249
236
                     self._get_kind(&search_data),
250
237
                     self._get_stat_value(&search_data),
251
 
                     top_slash + name_unicode))
 
238
                     directory + name_unicode))
252
239
 
253
240
                result = FindNextFileW(hFindFile, &search_data)
254
241
            # FindNextFileW sets GetLastError() == ERROR_NO_MORE_FILES when it
264
251
                # TODO: We should probably raise an exception if FindClose
265
252
                #       returns an error, however, I don't want to supress an
266
253
                #       earlier Exception, so for now, I'm ignoring this
 
254
        return dirblock
 
255
 
 
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
 
266
        
 
267
    def __next__(self):
 
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
 
274
        # at runtime... :(
 
275
        # Maybe we could use unicode(x) during __init__?
 
276
        if relroot:
 
277
            relprefix = relroot + '/'
 
278
        else:
 
279
            relprefix = ''
 
280
        top_slash = top + '/'
 
281
 
 
282
        dirblock = self._get_files_in(top_slash, relprefix)
267
283
        dirblock.sort(key=operator.itemgetter(1))
268
 
        return dirblock
269
 
 
270
 
 
271
 
def lstat(path):
272
 
    """Equivalent to os.lstat, except match Win32ReadDir._get_stat_value.
273
 
    """
274
 
    return wrap_stat(os.lstat(path))
275
 
 
276
 
 
277
 
def fstat(fd):
278
 
    """Like os.fstat, except match Win32ReadDir._get_stat_value
279
 
 
280
 
    :seealso: wrap_stat
281
 
    """
282
 
    return wrap_stat(os.fstat(fd))
283
 
 
284
 
 
285
 
def wrap_stat(st):
286
 
    """Return a _Win32Stat object, based on the given stat result.
287
 
 
288
 
    On Windows, os.fstat(open(fname).fileno()) != os.lstat(fname). This is
289
 
    generally because os.lstat and os.fstat differ in what they put into st_ino
290
 
    and st_dev. What gets set where seems to also be dependent on the python
291
 
    version. So we always set it to 0 to avoid worrying about it.
292
 
    """
293
 
    cdef _Win32Stat statvalue
294
 
    statvalue = _Win32Stat()
295
 
    statvalue.st_mode = st.st_mode
296
 
    statvalue.st_ctime = st.st_ctime
297
 
    statvalue.st_mtime = st.st_mtime
298
 
    statvalue.st_atime = st.st_atime
299
 
    statvalue._st_size = st.st_size
300
 
    return statvalue
 
284
        self._last_dirblock = dirblock
 
285
        return (relroot, top), dirblock
 
286
 
 
287
 
 
288
def _walkdirs_utf8_win32_find_file(top, prefix=""):
 
289
    """Implement a version of walkdirs_utf8 for win32.
 
290
 
 
291
    This uses the find files api to both list the files and to stat them.
 
292
    """
 
293
    return Win32Finder(top, prefix=prefix)