/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_readdir_pyx.pyx

  • Committer: Jelmer Vernooij
  • Date: 2020-02-18 01:57:45 UTC
  • mto: This revision was merged to the branch mainline in revision 7493.
  • Revision ID: jelmer@jelmer.uk-20200218015745-q2ss9tsk74h4nh61
drop use of future.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import os
21
21
import sys
22
22
 
23
 
#python2.4 support
24
23
cdef extern from "python-compat.h":
25
24
    pass
26
25
 
79
78
 
80
79
cdef extern from 'Python.h':
81
80
    int PyErr_CheckSignals() except -1
82
 
    char * PyString_AS_STRING(object)
83
 
    ctypedef int Py_ssize_t # Required for older pyrex versions
 
81
    char * PyBytes_AS_STRING(object)
84
82
    ctypedef struct PyObject:
85
83
        pass
86
 
    Py_ssize_t PyString_Size(object s)
 
84
    Py_ssize_t PyBytes_Size(object s)
87
85
    object PyList_GetItem(object lst, Py_ssize_t index)
88
86
    void *PyList_GetItem_object_void "PyList_GET_ITEM" (object lst, int index)
89
87
    int PyList_Append(object lst, object item) except -1
92
90
    int PyTuple_SetItem_obj "PyTuple_SetItem" (void *, Py_ssize_t pos, PyObject * item) except -1
93
91
    void Py_INCREF(object o)
94
92
    void Py_DECREF(object o)
95
 
    void PyString_Concat(PyObject **string, object newpart)
 
93
    void PyBytes_Concat(PyObject **string, object newpart)
96
94
 
97
95
 
98
96
cdef extern from 'dirent.h':
106
104
    int closedir(DIR * dir)
107
105
    dirent *readdir(DIR *dir)
108
106
 
 
107
cdef object _directory
109
108
_directory = 'directory'
 
109
cdef object _chardev
110
110
_chardev = 'chardev'
 
111
cdef object _block
111
112
_block = 'block'
 
113
cdef object _file
112
114
_file = 'file'
 
115
cdef object _fifo
113
116
_fifo = 'fifo'
 
117
cdef object _symlink
114
118
_symlink = 'symlink'
 
119
cdef object _socket
115
120
_socket = 'socket'
 
121
cdef object _unknown
116
122
_unknown = 'unknown'
117
 
_missing = 'missing'
118
123
 
119
124
# add a typedef struct dirent dirent to workaround pyrex
120
125
cdef extern from 'readdir.h':
159
164
                     self.st_mtime, self.st_ctime))
160
165
 
161
166
 
162
 
from bzrlib import osutils
 
167
from . import osutils
163
168
 
 
169
cdef object _safe_utf8
 
170
_safe_utf8 = osutils.safe_utf8
164
171
 
165
172
cdef class UTF8DirReader:
166
173
    """A dir reader for utf8 file systems."""
167
174
 
168
 
    cdef readonly object _safe_utf8
169
 
    cdef _directory, _chardev, _block, _file, _fifo, _symlink
170
 
    cdef _socket, _unknown
171
 
 
172
 
    def __init__(self):
173
 
        self._safe_utf8 = osutils.safe_utf8
174
 
        self._directory = _directory
175
 
        self._chardev = _chardev
176
 
        self._block = _block
177
 
        self._file = _file
178
 
        self._fifo = _fifo
179
 
        self._symlink = _symlink
180
 
        self._socket = _socket
181
 
        self._unknown = _unknown
182
 
 
183
175
    def kind_from_mode(self, int mode):
184
176
        """Get the kind of a path from a mode status."""
185
177
        return self._kind_from_mode(mode)
187
179
    cdef _kind_from_mode(self, int mode):
188
180
        # Files and directories are the most common - check them first.
189
181
        if S_ISREG(mode):
190
 
            return self._file
 
182
            return _file
191
183
        if S_ISDIR(mode):
192
 
            return self._directory
 
184
            return _directory
193
185
        if S_ISCHR(mode):
194
 
            return self._chardev
 
186
            return _chardev
195
187
        if S_ISBLK(mode):
196
 
            return self._block
 
188
            return _block
197
189
        if S_ISLNK(mode):
198
 
            return self._symlink
 
190
            return _symlink
199
191
        if S_ISFIFO(mode):
200
 
            return self._fifo
 
192
            return _fifo
201
193
        if S_ISSOCK(mode):
202
 
            return self._socket
203
 
        return self._unknown
 
194
            return _socket
 
195
        return _unknown
204
196
 
205
197
    def top_prefix_to_starting_dir(self, top, prefix=""):
206
198
        """See DirReader.top_prefix_to_starting_dir."""
207
 
        return (self._safe_utf8(prefix), None, None, None,
208
 
            self._safe_utf8(top))
 
199
        return (_safe_utf8(prefix), None, None, None, _safe_utf8(top))
209
200
 
210
201
    def read_dir(self, prefix, top):
211
202
        """Read a single directory from a utf8 file system.
227
218
        cdef object name
228
219
        cdef PyObject * new_val_obj
229
220
 
230
 
        if PyString_Size(prefix):
231
 
            relprefix = prefix + '/'
 
221
        if PyBytes_Size(prefix):
 
222
            relprefix = prefix + b'/'
232
223
        else:
233
 
            relprefix = ''
234
 
        top_slash = top + '/'
 
224
            relprefix = b''
 
225
        top_slash = top + b'/'
235
226
 
236
227
        # read_dir supplies in should-stat order.
237
228
        # for _, name in sorted(_listdir(top)):
247
238
            # direct concat - faster than operator +.
248
239
            new_val_obj = <PyObject *>relprefix
249
240
            Py_INCREF(relprefix)
250
 
            PyString_Concat(&new_val_obj, name)
 
241
            PyBytes_Concat(&new_val_obj, name)
251
242
            if NULL == new_val_obj:
252
 
                # PyString_Concat will have setup an exception, but how to get
 
243
                # PyBytes_Concat will have setup an exception, but how to get
253
244
                # at it?
254
245
                raise Exception("failed to strcat")
255
246
            PyTuple_SetItem_obj(atuple, 0, new_val_obj)
263
254
            # direct concat - faster than operator +.
264
255
            new_val_obj = <PyObject *>top_slash
265
256
            Py_INCREF(top_slash)
266
 
            PyString_Concat(&new_val_obj, name)
 
257
            PyBytes_Concat(&new_val_obj, name)
267
258
            if NULL == new_val_obj:
268
 
                # PyString_Concat will have setup an exception, but how to get
 
259
                # PyBytes_Concat will have setup an exception, but how to get
269
260
                # at it?
270
261
                raise Exception("failed to strcat")
271
262
            PyTuple_SetItem_obj(atuple, 4, new_val_obj)
299
290
 
300
291
    # Avoid chdir('') because it causes problems on Sun OS, and avoid this if
301
292
    # staying in .
302
 
    if path != "" and path != '.':
 
293
    if path != b"" and path != b'.':
303
294
        # we change into the requested directory before reading, and back at the
304
295
        # end, because that turns out to make the stat calls measurably faster than
305
296
        # passing full paths every time.
307
298
        if orig_dir_fd == -1:
308
299
            raise_os_error(errno, "open: ", ".")
309
300
        if -1 == chdir(path):
 
301
            # Ignore the return value, because we are already raising an
 
302
            # exception
 
303
            close(orig_dir_fd)
310
304
            raise_os_error(errno, "chdir: ", path)
311
305
    else:
312
306
        orig_dir_fd = -1
313
307
 
314
308
    try:
315
 
        the_dir = opendir(".")
 
309
        the_dir = opendir(b".")
316
310
        if NULL == the_dir:
317
311
            raise_os_error(errno, "opendir: ", path)
318
312
        try:
350
344
                    if stat_result != 0:
351
345
                        if errno != ENOENT:
352
346
                            raise_os_error(errno, "lstat: ",
353
 
                                path + "/" + entry.d_name)
 
347
                                path + b"/" + entry.d_name)
354
348
                        else:
355
349
                            # the file seems to have disappeared after being
356
350
                            # seen by readdir - perhaps a transient temporary