s3:pylibsmb: Split out code into list_helper()
authorTim Beale <timbeale@catalyst.net.nz>
Tue, 4 Dec 2018 02:43:53 +0000 (15:43 +1300)
committerTim Beale <timbeale@samba.org>
Mon, 7 Jan 2019 00:23:07 +0000 (01:23 +0100)
Refactor out the code so we can re-use it for the synchronous API as
well.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13676

Signed-off-by: Tim Beale <timbeale@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source3/libsmb/pylibsmb.c

index 9423fd23dd31b4981332b4261e15b01ab8d61003..b5cb573230c81a19e4b5bc0eb202ad6303727c6a 100644 (file)
@@ -897,6 +897,31 @@ static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
        Py_RETURN_NONE;
 }
 
+/*
+ * Helper to add directory listing entries to an overall Python list
+ */
+static NTSTATUS list_helper(const char *mntpoint, struct file_info *finfo,
+                           const char *mask, void *state)
+{
+       PyObject *result = (PyObject *)state;
+       PyObject *file = NULL;
+       int ret;
+
+       file = Py_BuildValue("{s:s,s:i}",
+                            "name", finfo->name,
+                            "mode", (int)finfo->mode);
+       if (file == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       ret = PyList_Append(result, file);
+       if (ret == -1) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       return NT_STATUS_OK;
+}
+
 static PyObject *py_cli_list(struct py_cli_state *self,
                             PyObject *args,
                             PyObject *kwds)
@@ -953,21 +978,8 @@ static PyObject *py_cli_list(struct py_cli_state *self,
        }
 
        for (i=0; i<num_finfos; i++) {
-               struct file_info *finfo = &finfos[i];
-               PyObject *file;
-               int ret;
-
-               file = Py_BuildValue(
-                       "{s:s,s:i}",
-                       "name", finfo->name,
-                       "mode", (int)finfo->mode);
-               if (file == NULL) {
-                       Py_XDECREF(result);
-                       return NULL;
-               }
-
-               ret = PyList_Append(result, file);
-               if (ret == -1) {
+               status = list_helper(base_dir, &finfos[i], user_mask, result);
+               if (!NT_STATUS_IS_OK(status)) {
                        Py_XDECREF(result);
                        return NULL;
                }