##// END OF EJS Templates
osutil: fix some braindamage...
Matt Mackall -
r7033:892d27fb default
parent child Browse files
Show More
@@ -96,12 +96,6 b' static PyTypeObject listdir_stat_type = '
96 96 listdir_stat_new, /* tp_new */
97 97 };
98 98
99 #ifdef AT_SYMLINK_NOFOLLOW
100 #define USEFDOPEN 1
101 #else
102 #define USEFDOPEN 0
103 #endif
104
105 99 int entkind(struct dirent *ent)
106 100 {
107 101 #ifdef DT_REG
@@ -115,6 +109,7 b' int entkind(struct dirent *ent)'
115 109 case DT_SOCK: return S_IFSOCK;
116 110 }
117 111 #endif
112 return -1;
118 113 }
119 114
120 115 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
@@ -122,7 +117,7 b' static PyObject *listdir(PyObject *self,'
122 117 static char *kwlist[] = { "path", "stat", NULL };
123 118 PyObject *statflag = NULL, *list, *elem, *stat, *ret = NULL;
124 119 char fullpath[PATH_MAX + 10], *path;
125 int pathlen, keepstat, kind, dfd, err;
120 int pathlen, keepstat, kind, dfd = -1, err;
126 121 struct stat st;
127 122 struct dirent *ent;
128 123 DIR *dir;
@@ -130,20 +125,23 b' static PyObject *listdir(PyObject *self,'
130 125 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
131 126 &path, &pathlen, &statflag))
132 127 goto error_parse;
128 if (pathlen >= PATH_MAX)
129 goto error_parse;
133 130
134 131 strncpy(fullpath, path, PATH_MAX);
135 132 fullpath[pathlen] = '/';
136 133 keepstat = statflag && PyObject_IsTrue(statflag);
137 134
138 if (USEFDOPEN) {
139 dfd = open(path, O_RDONLY);
140 if (dfd == -1) {
141 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
142 goto error_parse;
143 }
144 dir = fdopendir(dfd);
145 } else
146 dir = opendir(path);
135 #ifdef AT_SYMLINK_NOFOLLOW
136 dfd = open(path, O_RDONLY);
137 if (dfd == -1) {
138 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
139 goto error_parse;
140 }
141 dir = fdopendir(dfd);
142 #else
143 dir = opendir(path);
144 #endif
147 145 if (!dir) {
148 146 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
149 147 goto error_dir;
@@ -159,15 +157,15 b' static PyObject *listdir(PyObject *self,'
159 157
160 158 kind = entkind(ent);
161 159 if (kind == -1 || keepstat) {
162 if (USEFDOPEN)
163 err = fstatat(dfd, ent->d_name, &st,
164 AT_SYMLINK_NOFOLLOW);
165 else {
166 strncpy(fullpath + pathlen + 1, ent->d_name,
167 PATH_MAX - pathlen);
168 fullpath[PATH_MAX] = 0;
169 err = lstat(fullpath, &st);
170 }
160 #ifdef AT_SYMLINK_NOFOLLOW
161 err = fstatat(dfd, ent->d_name, &st,
162 AT_SYMLINK_NOFOLLOW);
163 #else
164 strncpy(fullpath + pathlen + 1, ent->d_name,
165 PATH_MAX - pathlen);
166 fullpath[PATH_MAX] = 0;
167 err = lstat(fullpath, &st);
168 #endif
171 169 if (err == -1) {
172 170 strncpy(fullpath + pathlen + 1, ent->d_name,
173 171 PATH_MAX - pathlen);
@@ -203,8 +201,9 b' error:'
203 201 error_list:
204 202 closedir(dir);
205 203 error_dir:
206 if (USEFDOPEN)
207 close(dfd);
204 #ifdef AT_SYMLINK_NOFOLLOW
205 close(dfd);
206 #endif
208 207 error_parse:
209 208 return ret;
210 209 }
General Comments 0
You need to be logged in to leave comments. Login now