~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_walkdirs_win32.pyx

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Helper functions for Walkdirs on win32."""
18
18
 
19
19
 
20
 
cdef extern from "_walkdirs_win32.h":
 
20
cdef extern from "python-compat.h":
21
21
    struct _HANDLE:
22
22
        pass
23
23
    ctypedef _HANDLE *HANDLE
71
71
import operator
72
72
import stat
73
73
 
74
 
from bzrlib import osutils
 
74
from bzrlib import osutils, _readdir_py
75
75
 
76
76
 
77
77
cdef class _Win32Stat:
81
81
    cdef readonly double st_ctime
82
82
    cdef readonly double st_mtime
83
83
    cdef readonly double st_atime
84
 
    cdef readonly __int64 st_size
 
84
    # We can't just declare this as 'readonly' because python2.4 doesn't define
 
85
    # T_LONGLONG as a structure member. So instead we just use a property that
 
86
    # will convert it correctly anyway.
 
87
    cdef __int64 _st_size
 
88
 
 
89
    property st_size:
 
90
        def __get__(self):
 
91
            return self._st_size
85
92
 
86
93
    # os.stat always returns 0, so we hard code it here
87
94
    cdef readonly int st_dev
151
158
    return 0
152
159
 
153
160
 
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
 
161
cdef class Win32ReadDir:
 
162
    """Read directories on win32."""
159
163
 
160
164
    cdef object _directory_kind
161
165
    cdef object _file_kind
162
166
 
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
 
167
    def __init__(self):
 
168
        self._directory_kind = _readdir_py._directory
 
169
        self._file_kind = _readdir_py._file
 
170
 
 
171
    def top_prefix_to_starting_dir(self, top, prefix=""):
 
172
        """See DirReader.top_prefix_to_starting_dir."""
 
173
        return (osutils.safe_utf8(prefix), None, None, None,
 
174
                osutils.safe_unicode(top))
178
175
 
179
176
    cdef object _get_kind(self, WIN32_FIND_DATAW *data):
180
177
        if data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY:
190
187
        statvalue.st_ctime = _ftime_to_timestamp(&data.ftCreationTime)
191
188
        statvalue.st_mtime = _ftime_to_timestamp(&data.ftLastWriteTime)
192
189
        statvalue.st_atime = _ftime_to_timestamp(&data.ftLastAccessTime)
193
 
        statvalue.st_size = _get_size(data)
 
190
        statvalue._st_size = _get_size(data)
194
191
        statvalue.st_ino = 0
195
192
        statvalue.st_dev = 0
196
193
        return statvalue
197
194
 
198
 
    def _get_files_in(self, directory, relprefix):
199
 
        """Return the dirblock for all files in the given directory.
 
195
    def read_dir(self, prefix, top):
 
196
        """Win32 implementation of DirReader.read_dir.
200
197
 
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)]
 
198
        :seealso: DirReader.read_dir
208
199
        """
209
200
        cdef WIN32_FIND_DATAW search_data
210
201
        cdef HANDLE hFindFile
212
203
        cdef WCHAR *query
213
204
        cdef int result
214
205
 
215
 
        top_star = directory + '*'
 
206
        if prefix:
 
207
            relprefix = prefix + '/'
 
208
        else:
 
209
            relprefix = ''
 
210
        top_slash = top + '/'
 
211
 
 
212
        top_star = top_slash + '*'
216
213
 
217
214
        dirblock = []
218
215
 
231
228
                    continue
232
229
                name_unicode = _get_name(&search_data)
233
230
                name_utf8 = PyUnicode_AsUTF8String(name_unicode)
234
 
                PyList_Append(dirblock, 
235
 
                    (relprefix + name_utf8, name_utf8, 
 
231
                PyList_Append(dirblock,
 
232
                    (relprefix + name_utf8, name_utf8,
236
233
                     self._get_kind(&search_data),
237
234
                     self._get_stat_value(&search_data),
238
 
                     directory + name_unicode))
 
235
                     top_slash + name_unicode))
239
236
 
240
237
                result = FindNextFileW(hFindFile, &search_data)
241
238
            # FindNextFileW sets GetLastError() == ERROR_NO_MORE_FILES when it
251
248
                # TODO: We should probably raise an exception if FindClose
252
249
                #       returns an error, however, I don't want to supress an
253
250
                #       earlier Exception, so for now, I'm ignoring this
 
251
        dirblock.sort(key=operator.itemgetter(1))
254
252
        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)
283
 
        dirblock.sort(key=operator.itemgetter(1))
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)