mercurial/osutil.c
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Thu, 11 Oct 2007 16:19:12 +0200
changeset 5454 f2ca8d2c988f
parent 5432 0d154bce2341
child 5457 7372b6bbc5e6
permissions -rw-r--r--
explicitely use integer division
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
/*
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
 osutil.c - native operating system services
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
 Copyright 2007 Matt Mackall and others
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
 This software may be used and distributed according to the terms of
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
 the GNU General Public License, incorporated herein by reference.
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     8
*/
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
#define _ATFILE_SOURCE
5397
11caa374f497 osutil.c: include Python.h before the other headers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5396
diff changeset
    11
#include <Python.h>
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
#include <alloca.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
#include <dirent.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    14
#include <fcntl.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    15
#include <string.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
#include <sys/stat.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
#include <sys/types.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
#include <unistd.h>
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    19
5432
0d154bce2341 osutil: Solaris build fix
Bryan O'Sullivan <bos@serpentine.com>
parents: 5431
diff changeset
    20
#if defined(__sun)
0d154bce2341 osutil: Solaris build fix
Bryan O'Sullivan <bos@serpentine.com>
parents: 5431
diff changeset
    21
#define dirfd(dir) ((dir)->dd_fd)
0d154bce2341 osutil: Solaris build fix
Bryan O'Sullivan <bos@serpentine.com>
parents: 5431
diff changeset
    22
#endif
0d154bce2341 osutil: Solaris build fix
Bryan O'Sullivan <bos@serpentine.com>
parents: 5431
diff changeset
    23
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    24
struct listdir_stat {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    25
	PyObject_HEAD
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    26
	struct stat st;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    27
};
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    28
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    29
#define listdir_slot(name) \
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    30
    static PyObject *listdir_stat_##name(PyObject *self, void *x) \
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    31
    { \
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    32
        return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    33
    }
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    34
5431
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
    35
listdir_slot(st_dev)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
    36
listdir_slot(st_mode)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
    37
listdir_slot(st_nlink)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
    38
listdir_slot(st_size)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
    39
listdir_slot(st_mtime)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
    40
listdir_slot(st_ctime)
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    41
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    42
static struct PyGetSetDef listdir_stat_getsets[] = {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    43
	{"st_dev", listdir_stat_st_dev, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    44
	{"st_mode", listdir_stat_st_mode, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    45
	{"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    46
	{"st_size", listdir_stat_st_size, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    47
	{"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    48
	{"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    49
	{0, 0, 0, 0, 0}
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
};
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    51
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    52
static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    53
{
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    54
	return t->tp_alloc(t, 0);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    55
}
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    56
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    57
static void listdir_stat_dealloc(PyObject *o)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
{
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    59
	o->ob_type->tp_free(o);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    60
}
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    61
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    62
static PyTypeObject listdir_stat_type = {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    63
	PyObject_HEAD_INIT(NULL)
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    64
	0,                         /*ob_size*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    65
	"osutil.stat",             /*tp_name*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    66
	sizeof(struct listdir_stat), /*tp_basicsize*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    67
	0,                         /*tp_itemsize*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    68
	(destructor)listdir_stat_dealloc, /*tp_dealloc*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    69
	0,                         /*tp_print*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    70
	0,                         /*tp_getattr*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    71
	0,                         /*tp_setattr*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    72
	0,                         /*tp_compare*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    73
	0,                         /*tp_repr*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    74
	0,                         /*tp_as_number*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    75
	0,                         /*tp_as_sequence*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    76
	0,                         /*tp_as_mapping*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    77
	0,                         /*tp_hash */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    78
	0,                         /*tp_call*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    79
	0,                         /*tp_str*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    80
	0,                         /*tp_getattro*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    81
	0,                         /*tp_setattro*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    82
	0,                         /*tp_as_buffer*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    83
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    84
	"stat objects",            /* tp_doc */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    85
	0,                         /* tp_traverse */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    86
	0,                         /* tp_clear */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    87
	0,                         /* tp_richcompare */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    88
	0,                         /* tp_weaklistoffset */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    89
	0,                         /* tp_iter */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    90
	0,                         /* tp_iternext */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    91
	0,                         /* tp_methods */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    92
	0,                         /* tp_members */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    93
	listdir_stat_getsets,      /* tp_getset */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    94
	0,                         /* tp_base */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    95
	0,                         /* tp_dict */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    96
	0,                         /* tp_descr_get */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    97
	0,                         /* tp_descr_set */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    98
	0,                         /* tp_dictoffset */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
    99
	0,                         /* tp_init */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   100
	0,                         /* tp_alloc */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   101
	listdir_stat_new,          /* tp_new */
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   102
};
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   103
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   104
static inline int mode_to_kind(int mode)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   105
{
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   106
	if (S_ISREG(mode)) return S_IFREG;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   107
	if (S_ISDIR(mode)) return S_IFDIR;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   108
	if (S_ISLNK(mode)) return S_IFLNK;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   109
	if (S_ISBLK(mode)) return S_IFBLK;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   110
	if (S_ISCHR(mode)) return S_IFCHR;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   111
	if (S_ISFIFO(mode)) return S_IFIFO;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   112
	if (S_ISSOCK(mode)) return S_IFSOCK;
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   113
	return mode;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   114
}
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   115
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   116
static PyObject *listfiles(PyObject *list, DIR *dir,
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   117
			   int keep_stat, int *need_stat)
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   118
{
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   119
	struct dirent *ent;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   120
	PyObject *name, *py_kind, *val;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   121
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   122
#ifdef DT_REG
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   123
	*need_stat = 0;
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   124
#else
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   125
	*need_stat = 1;
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   126
#endif
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   127
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   128
	for (ent = readdir(dir); ent; ent = readdir(dir)) {
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   129
		int kind = -1;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   130
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   131
		if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   132
			continue;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   133
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   134
#ifdef DT_REG
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   135
		if (!keep_stat)
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   136
			switch (ent->d_type) {
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   137
			case DT_REG: kind = S_IFREG; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   138
			case DT_DIR: kind = S_IFDIR; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   139
			case DT_LNK: kind = S_IFLNK; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   140
			case DT_BLK: kind = S_IFBLK; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   141
			case DT_CHR: kind = S_IFCHR; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   142
			case DT_FIFO: kind = S_IFIFO; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   143
			case DT_SOCK: kind = S_IFSOCK; break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   144
			default:
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   145
				*need_stat = 0;
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   146
				break;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   147
			}
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   148
#endif
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   149
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   150
		if (kind != -1)
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   151
			py_kind = PyInt_FromLong(kind);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   152
		else {
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   153
			py_kind = Py_None;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   154
			Py_INCREF(Py_None);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   155
		}
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   156
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   157
		val = PyTuple_New(keep_stat ? 3 : 2);
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   158
		name = PyString_FromString(ent->d_name);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   159
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   160
		if (!name || !py_kind || !val) {
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   161
			Py_XDECREF(name);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   162
			Py_XDECREF(py_kind);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   163
			Py_XDECREF(val);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   164
			return PyErr_NoMemory();
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   165
		}
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   166
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   167
		PyTuple_SET_ITEM(val, 0, name);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   168
		PyTuple_SET_ITEM(val, 1, py_kind);
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   169
		if (keep_stat) {
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   170
			PyTuple_SET_ITEM(val, 2, Py_None);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   171
			Py_INCREF(Py_None);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   172
		}
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   173
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   174
		PyList_Append(list, val);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   175
		Py_DECREF(val);
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   176
	}
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   177
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   178
	return 0;
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   179
}
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   180
5425
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   181
static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep,
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   182
			   char *path, int len, DIR *dir)
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   183
{
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   184
	struct stat buf;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   185
	struct stat *stp = &buf;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   186
	int kind;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   187
	int ret;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   188
	ssize_t i;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   189
	ssize_t size = PyList_Size(list);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   190
#ifdef AT_SYMLINK_NOFOLLOW
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   191
	int dfd = dirfd(dir);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   192
#endif
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   193
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   194
	for (i = 0; i < size; i++) {
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   195
		PyObject *elt = PyList_GetItem(list, i);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   196
		char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0));
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   197
		PyObject *py_st = NULL;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   198
		PyObject *py_kind = PyTuple_GET_ITEM(elt, 1);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   199
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   200
		kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   201
		if (kind != -1 && !keep)
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   202
			continue;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   203
5430
0bdea0abe62e osutil.c: use strncpy instead of strncat
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5428
diff changeset
   204
		strncpy(path + len + 1, name, PATH_MAX - len);
5425
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   205
		path[PATH_MAX] = 0;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   206
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   207
		if (keep) {
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   208
			py_st = PyObject_CallObject(
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   209
				(PyObject *)&listdir_stat_type, ctor_args);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   210
			if (!py_st)
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   211
				return PyErr_NoMemory();
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   212
			stp = &((struct listdir_stat *)py_st)->st;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   213
			PyTuple_SET_ITEM(elt, 2, py_st);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   214
		}
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   215
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   216
#ifdef AT_SYMLINK_NOFOLLOW
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   217
		ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   218
#else
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   219
		ret = lstat(path, stp);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   220
#endif
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   221
		if (ret == -1)
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   222
			return PyErr_SetFromErrnoWithFilename(PyExc_OSError,
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   223
							      path);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   224
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   225
		if (kind == -1)
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   226
			kind = mode_to_kind(stp->st_mode);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   227
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   228
		if (py_kind == Py_None && kind != -1) {
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   229
			py_kind = PyInt_FromLong(kind);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   230
			if (!py_kind)
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   231
				return PyErr_NoMemory();
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   232
			Py_XDECREF(Py_None);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   233
			PyTuple_SET_ITEM(elt, 1, py_kind);
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   234
		}
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   235
	}
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   236
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   237
	return 0;
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   238
}
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   239
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   240
static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   241
{
5416
ca890c0c3f1f osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents: 5398
diff changeset
   242
	static char *kwlist[] = { "path", "stat", NULL };
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   243
	DIR *dir = NULL;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   244
	PyObject *statobj = NULL;
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   245
	PyObject *list = NULL;
5425
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   246
	PyObject *err = NULL;
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   247
	PyObject *ctor_args = NULL;
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   248
	char *path;
5422
a3ba7ef98c94 osutil: eliminate alloca call
Matt Mackall <mpm@selenic.com>
parents: 5421
diff changeset
   249
	char full_path[PATH_MAX + 10];
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   250
	int path_len;
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   251
	int need_stat, keep_stat;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   252
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   253
	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   254
					 &path, &path_len, &statobj))
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   255
		goto bail;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   256
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   257
	keep_stat = statobj && PyObject_IsTrue(statobj);
5416
ca890c0c3f1f osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents: 5398
diff changeset
   258
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   259
	dir = opendir(path);
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   260
	if (!dir) {
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   261
		err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   262
		goto bail;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   263
	}
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   264
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   265
	list = PyList_New(0);
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   266
	ctor_args = PyTuple_New(0);
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   267
	if (!list || !ctor_args)
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   268
		goto bail;
5416
ca890c0c3f1f osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents: 5398
diff changeset
   269
5422
a3ba7ef98c94 osutil: eliminate alloca call
Matt Mackall <mpm@selenic.com>
parents: 5421
diff changeset
   270
	strncpy(full_path, path, PATH_MAX);
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   271
	full_path[path_len] = '/';
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   272
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   273
	err = listfiles(list, dir, keep_stat, &need_stat);
5427
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   274
	if (err)
dae6188e8c9e osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents: 5426
diff changeset
   275
		goto bail;
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   276
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   277
	PyList_Sort(list);
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   278
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   279
	if (!keep_stat && !need_stat)
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   280
		goto done;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   281
5428
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   282
	err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dir);
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   283
	if (!err)
eb1b6aaeb32e osutil: more tidying
Matt Mackall <mpm@selenic.com>
parents: 5427
diff changeset
   284
		goto done;
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   285
5423
e5f238a8b0d2 osutil: more cleanups
Matt Mackall <mpm@selenic.com>
parents: 5422
diff changeset
   286
 bail:
e5f238a8b0d2 osutil: more cleanups
Matt Mackall <mpm@selenic.com>
parents: 5422
diff changeset
   287
	Py_XDECREF(list);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   288
5423
e5f238a8b0d2 osutil: more cleanups
Matt Mackall <mpm@selenic.com>
parents: 5422
diff changeset
   289
 done:
e5f238a8b0d2 osutil: more cleanups
Matt Mackall <mpm@selenic.com>
parents: 5422
diff changeset
   290
	Py_XDECREF(ctor_args);
e5f238a8b0d2 osutil: more cleanups
Matt Mackall <mpm@selenic.com>
parents: 5422
diff changeset
   291
	if (dir)
e5f238a8b0d2 osutil: more cleanups
Matt Mackall <mpm@selenic.com>
parents: 5422
diff changeset
   292
		closedir(dir);
5425
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
   293
	return err ? err : list;
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   294
}
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   295
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   296
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   297
static char osutil_doc[] = "Native operating system services.";
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   298
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   299
static PyMethodDef methods[] = {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   300
	{"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   301
	 "list a directory\n"},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   302
	{NULL, NULL}
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   303
};
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   304
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   305
PyMODINIT_FUNC initosutil(void)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   306
{
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   307
	if (PyType_Ready(&listdir_stat_type) == -1)
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   308
		return;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   309
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
   310
	Py_InitModule3("osutil", methods, osutil_doc);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   311
}