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