osutil: factor out creation and init of listdir_stat
authorBryan O'Sullivan <bryano@fb.com>
Fri, 30 Nov 2012 15:55:09 -0800
changeset 18019 e248bff2d8dd
parent 18018 0fed3fe45ea7
child 18020 ec02eef0b686
osutil: factor out creation and init of listdir_stat
mercurial/osutil.c
--- a/mercurial/osutil.c	Fri Nov 30 15:55:08 2012 -0800
+++ b/mercurial/osutil.c	Fri Nov 30 15:55:09 2012 -0800
@@ -276,6 +276,16 @@
 	return -1;
 }
 
+static PyObject *makestat(const struct stat *st)
+{
+        PyObject *stat;
+
+        stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
+        if (stat)
+                memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st));
+        return stat;
+}
+
 static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip)
 {
 	PyObject *list, *elem, *stat, *ret = NULL;
@@ -351,10 +361,9 @@
 		}
 
 		if (keepstat) {
-			stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
+			stat = makestat(&st);
 			if (!stat)
 				goto error;
-			memcpy(&((struct listdir_stat *)stat)->st, &st, sizeof(st));
 			elem = Py_BuildValue("siN", ent->d_name, kind, stat);
 		} else
 			elem = Py_BuildValue("si", ent->d_name, kind);