~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_readdir_pyx.pyx

  • Committer: Martin Pool
  • Date: 2008-11-20 02:36:19 UTC
  • mto: This revision was merged to the branch mainline in revision 3842.
  • Revision ID: mbp@sourcefrog.net-20081120023619-kuhhai103uliqavo
Fix try/finally block after chdir in readdir_pyx

Show diffs side-by-side

added added

removed removed

Lines of Context:
285
285
    cwd = getcwd(NULL, 0)
286
286
    if -1 == chdir(path):
287
287
        raise OSError(errno, strerror(errno))
288
 
    the_dir = opendir(".")
289
 
    if NULL == the_dir:
290
 
        raise OSError(errno, strerror(errno))
291
 
    result = []
292
288
    try:
293
 
        entry = &sentinel
294
 
        while entry != NULL:
295
 
            # Unlike most libc functions, readdir needs errno set to 0
296
 
            # beforehand so that eof can be distinguished from errors.  See
297
 
            # <https://bugs.launchpad.net/bzr/+bug/279381>
298
 
            while True:
299
 
                errno = 0;
300
 
                entry = readdir(the_dir)
301
 
                if entry == NULL and (errno == EAGAIN or errno == EINTR):
302
 
                    # try again
303
 
                    continue
304
 
                else:
305
 
                    break
306
 
            if entry == NULL:
307
 
                if errno == ENOTDIR or errno == 0:
308
 
                    # We see ENOTDIR at the end of a normal directory.
309
 
                    # As ENOTDIR for read_dir(file) is triggered on opendir,
310
 
                    # we consider ENOTDIR to be 'no error'.
311
 
                    continue
312
 
                else:
313
 
                    raise OSError(errno, strerror(errno))
314
 
            name = entry.d_name
315
 
            if not (name[0] == c"." and (
316
 
                (name[1] == 0) or 
317
 
                (name[1] == c"." and name[2] == 0))
318
 
                ):
319
 
                statvalue = _Stat()
320
 
                stat_result = lstat(entry.d_name, &statvalue._st)
321
 
                if stat_result != 0:
322
 
                    if errno != ENOENT:
 
289
        the_dir = opendir(".")
 
290
        if NULL == the_dir:
 
291
            raise OSError(errno, strerror(errno))
 
292
        try:
 
293
            result = []
 
294
            entry = &sentinel
 
295
            while entry != NULL:
 
296
                # Unlike most libc functions, readdir needs errno set to 0
 
297
                # beforehand so that eof can be distinguished from errors.  See
 
298
                # <https://bugs.launchpad.net/bzr/+bug/279381>
 
299
                while True:
 
300
                    errno = 0;
 
301
                    entry = readdir(the_dir)
 
302
                    if entry == NULL and (errno == EAGAIN or errno == EINTR):
 
303
                        # try again
 
304
                        continue
 
305
                    else:
 
306
                        break
 
307
                if entry == NULL:
 
308
                    if errno == ENOTDIR or errno == 0:
 
309
                        # We see ENOTDIR at the end of a normal directory.
 
310
                        # As ENOTDIR for read_dir(file) is triggered on opendir,
 
311
                        # we consider ENOTDIR to be 'no error'.
 
312
                        continue
 
313
                    else:
323
314
                        raise OSError(errno, strerror(errno))
324
 
                    else:
325
 
                        kind = _missing
326
 
                        statvalue = None
327
 
                # We append a 5-tuple that can be modified in-place by the C
328
 
                # api:
329
 
                # inode to sort on (to replace with top_path)
330
 
                # name (to keep)
331
 
                # kind (None, to set)
332
 
                # statvalue (to keep)
333
 
                # abspath (None, to set)
334
 
                PyList_Append(result, (entry.d_ino, entry.d_name, None,
335
 
                    statvalue, None))
 
315
                name = entry.d_name
 
316
                if not (name[0] == c"." and (
 
317
                    (name[1] == 0) or 
 
318
                    (name[1] == c"." and name[2] == 0))
 
319
                    ):
 
320
                    statvalue = _Stat()
 
321
                    stat_result = lstat(entry.d_name, &statvalue._st)
 
322
                    if stat_result != 0:
 
323
                        if errno != ENOENT:
 
324
                            raise OSError(errno, strerror(errno))
 
325
                        else:
 
326
                            kind = _missing
 
327
                            statvalue = None
 
328
                    # We append a 5-tuple that can be modified in-place by the C
 
329
                    # api:
 
330
                    # inode to sort on (to replace with top_path)
 
331
                    # name (to keep)
 
332
                    # kind (None, to set)
 
333
                    # statvalue (to keep)
 
334
                    # abspath (None, to set)
 
335
                    PyList_Append(result, (entry.d_ino, entry.d_name, None,
 
336
                        statvalue, None))
 
337
        finally:
 
338
            if -1 == closedir(the_dir):
 
339
                raise OSError(errno, strerror(errno))
336
340
    finally:
337
341
        if -1 == chdir(cwd):
338
342
            free(cwd)
339
343
            raise OSError(errno, strerror(errno))
340
344
        free(cwd)
341
 
        if -1 == closedir(the_dir):
342
 
            raise OSError(errno, strerror(errno))
343
345
    return result
344
346
 
345
347