Revert "buildtools: Rename perl vendorarch configure option."
[Samba.git] / source3 / libsmb / pylibsmb.c
blobe3a5ac7d18acdd66763401c3b7596b5c6e995b14
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba-internal work in progress Python binding for libsmbclient
5 * Copyright (C) Volker Lendecke 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <Python.h>
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24 #include "libcli/security/security.h"
25 #include "system/select.h"
26 #include "source4/libcli/util/pyerrors.h"
27 #include "auth/credentials/pycredentials.h"
28 #include "trans2.h"
30 static PyTypeObject *get_pytype(const char *module, const char *type)
32 PyObject *mod;
33 PyTypeObject *result;
35 mod = PyImport_ImportModule(module);
36 if (mod == NULL) {
37 PyErr_Format(PyExc_RuntimeError,
38 "Unable to import %s to check type %s",
39 module, type);
40 return NULL;
42 result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
43 Py_DECREF(mod);
44 if (result == NULL) {
45 PyErr_Format(PyExc_RuntimeError,
46 "Unable to find type %s in module %s",
47 module, type);
48 return NULL;
50 return result;
54 * We're using "const char **" for keywords,
55 * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
56 * inevitable warnings to just one place.
58 static int ParseTupleAndKeywords(PyObject *args, PyObject *kw,
59 const char *format, const char **keywords,
60 ...)
62 va_list a;
63 int ret;
64 va_start(a, keywords);
65 ret = PyArg_VaParseTupleAndKeywords(args, kw, format,
66 (char **)keywords, a);
67 va_end(a);
68 return ret;
71 struct py_cli_thread;
73 struct py_cli_oplock_break {
74 uint16_t fnum;
75 uint8_t level;
78 struct py_cli_state {
79 PyObject_HEAD
80 struct cli_state *cli;
81 struct tevent_context *ev;
82 struct py_cli_thread *thread_state;
84 struct tevent_req *oplock_waiter;
85 struct py_cli_oplock_break *oplock_breaks;
86 struct py_tevent_cond *oplock_cond;
89 #if HAVE_PTHREAD
91 #include <pthread.h>
93 struct py_cli_thread {
96 * Pipe to make the poll thread wake up in our destructor, so
97 * that we can exit and join the thread.
99 int shutdown_pipe[2];
100 struct tevent_fd *shutdown_fde;
101 bool do_shutdown;
102 pthread_t id;
105 * Thread state to release the GIL during the poll(2) syscall
107 PyThreadState *py_threadstate;
110 static void *py_cli_state_poll_thread(void *private_data)
112 struct py_cli_state *self = (struct py_cli_state *)private_data;
113 struct py_cli_thread *t = self->thread_state;
114 PyGILState_STATE gstate;
116 gstate = PyGILState_Ensure();
118 while (!t->do_shutdown) {
119 int ret;
120 ret = tevent_loop_once(self->ev);
121 assert(ret == 0);
123 PyGILState_Release(gstate);
124 return NULL;
127 static void py_cli_state_trace_callback(enum tevent_trace_point point,
128 void *private_data)
130 struct py_cli_state *self = (struct py_cli_state *)private_data;
131 struct py_cli_thread *t = self->thread_state;
133 switch(point) {
134 case TEVENT_TRACE_BEFORE_WAIT:
135 assert(t->py_threadstate == NULL);
136 t->py_threadstate = PyEval_SaveThread();
137 break;
138 case TEVENT_TRACE_AFTER_WAIT:
139 assert(t->py_threadstate != NULL);
140 PyEval_RestoreThread(t->py_threadstate);
141 t->py_threadstate = NULL;
142 break;
143 default:
144 break;
148 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
149 struct tevent_fd *fde,
150 uint16_t flags,
151 void *private_data)
153 struct py_cli_state *self = (struct py_cli_state *)private_data;
154 struct py_cli_thread *t = self->thread_state;
156 if ((flags & TEVENT_FD_READ) == 0) {
157 return;
159 TALLOC_FREE(t->shutdown_fde);
160 t->do_shutdown = true;
163 static int py_cli_thread_destructor(struct py_cli_thread *t)
165 char c = 0;
166 ssize_t written;
167 int ret;
169 do {
171 * This will wake the poll thread from the poll(2)
173 written = write(t->shutdown_pipe[1], &c, 1);
174 } while ((written == -1) && (errno == EINTR));
177 * Allow the poll thread to do its own cleanup under the GIL
179 Py_BEGIN_ALLOW_THREADS
180 ret = pthread_join(t->id, NULL);
181 Py_END_ALLOW_THREADS
182 assert(ret == 0);
184 if (t->shutdown_pipe[0] != -1) {
185 close(t->shutdown_pipe[0]);
186 t->shutdown_pipe[0] = -1;
188 if (t->shutdown_pipe[1] != -1) {
189 close(t->shutdown_pipe[1]);
190 t->shutdown_pipe[1] = -1;
192 return 0;
195 static bool py_cli_state_setup_ev(struct py_cli_state *self)
197 struct py_cli_thread *t = NULL;
198 int ret;
200 self->ev = tevent_context_init_byname(NULL, "poll_mt");
201 if (self->ev == NULL) {
202 goto fail;
204 samba_tevent_set_debug(self->ev, "pylibsmb_tevent_mt");
205 tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
207 self->thread_state = talloc_zero(NULL, struct py_cli_thread);
208 if (self->thread_state == NULL) {
209 goto fail;
211 t = self->thread_state;
213 ret = pipe(t->shutdown_pipe);
214 if (ret == -1) {
215 goto fail;
217 t->shutdown_fde = tevent_add_fd(
218 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
219 py_cli_state_shutdown_handler, self);
220 if (t->shutdown_fde == NULL) {
221 goto fail;
224 PyEval_InitThreads();
226 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
227 if (ret != 0) {
228 goto fail;
230 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
231 return true;
233 fail:
234 if (t != NULL) {
235 TALLOC_FREE(t->shutdown_fde);
237 if (t->shutdown_pipe[0] != -1) {
238 close(t->shutdown_pipe[0]);
239 t->shutdown_pipe[0] = -1;
241 if (t->shutdown_pipe[1] != -1) {
242 close(t->shutdown_pipe[1]);
243 t->shutdown_pipe[1] = -1;
247 TALLOC_FREE(self->thread_state);
248 TALLOC_FREE(self->ev);
249 return false;
252 struct py_tevent_cond {
253 pthread_mutex_t mutex;
254 pthread_cond_t cond;
255 bool is_done;
258 static void py_tevent_signalme(struct tevent_req *req);
260 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
262 int ret, result;
264 result = pthread_mutex_init(&cond->mutex, NULL);
265 if (result != 0) {
266 goto fail;
268 result = pthread_cond_init(&cond->cond, NULL);
269 if (result != 0) {
270 goto fail_mutex;
273 result = pthread_mutex_lock(&cond->mutex);
274 if (result != 0) {
275 goto fail_cond;
278 cond->is_done = false;
280 while (!cond->is_done) {
282 Py_BEGIN_ALLOW_THREADS
283 result = pthread_cond_wait(&cond->cond, &cond->mutex);
284 Py_END_ALLOW_THREADS
286 if (result != 0) {
287 goto fail_unlock;
291 fail_unlock:
292 ret = pthread_mutex_unlock(&cond->mutex);
293 assert(ret == 0);
294 fail_cond:
295 ret = pthread_cond_destroy(&cond->cond);
296 assert(ret == 0);
297 fail_mutex:
298 ret = pthread_mutex_destroy(&cond->mutex);
299 assert(ret == 0);
300 fail:
301 return result;
304 static int py_tevent_req_wait(struct tevent_context *ev,
305 struct tevent_req *req)
307 struct py_tevent_cond cond;
308 tevent_req_set_callback(req, py_tevent_signalme, &cond);
309 return py_tevent_cond_wait(&cond);
312 static void py_tevent_cond_signal(struct py_tevent_cond *cond)
314 int ret;
316 ret = pthread_mutex_lock(&cond->mutex);
317 assert(ret == 0);
319 cond->is_done = true;
321 ret = pthread_cond_signal(&cond->cond);
322 assert(ret == 0);
323 ret = pthread_mutex_unlock(&cond->mutex);
324 assert(ret == 0);
327 static void py_tevent_signalme(struct tevent_req *req)
329 struct py_tevent_cond *cond = (struct py_tevent_cond *)
330 tevent_req_callback_data_void(req);
332 py_tevent_cond_signal(cond);
335 #else
337 static bool py_cli_state_setup_ev(struct py_cli_state *self)
339 self->ev = tevent_context_init(NULL);
340 if (self->ev == NULL) {
341 return false;
344 samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
346 return true;
349 static int py_tevent_req_wait(struct tevent_context *ev,
350 struct tevent_req *req)
352 while (tevent_req_is_in_progress(req)) {
353 int ret;
355 ret = tevent_loop_once(ev);
356 if (ret != 0) {
357 return ret;
360 return 0;
363 #endif
365 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
366 struct tevent_req *req)
368 int ret;
370 if (req == NULL) {
371 PyErr_NoMemory();
372 return false;
374 ret = py_tevent_req_wait(ev, req);
375 if (ret != 0) {
376 TALLOC_FREE(req);
377 errno = ret;
378 PyErr_SetFromErrno(PyExc_RuntimeError);
379 return false;
381 return true;
384 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
385 PyObject *kwds)
387 struct py_cli_state *self;
389 self = (struct py_cli_state *)type->tp_alloc(type, 0);
390 if (self == NULL) {
391 return NULL;
393 self->cli = NULL;
394 self->ev = NULL;
395 self->thread_state = NULL;
396 self->oplock_waiter = NULL;
397 self->oplock_cond = NULL;
398 self->oplock_breaks = NULL;
399 return (PyObject *)self;
402 static void py_cli_got_oplock_break(struct tevent_req *req);
404 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
405 PyObject *kwds)
407 NTSTATUS status;
408 char *host, *share;
409 PyObject *creds = NULL;
410 struct cli_credentials *cli_creds;
411 struct tevent_req *req;
412 bool ret;
414 static const char *kwlist[] = {
415 "host", "share", "credentials", NULL
418 PyTypeObject *py_type_Credentials = get_pytype(
419 "samba.credentials", "Credentials");
420 if (py_type_Credentials == NULL) {
421 return -1;
424 ret = ParseTupleAndKeywords(
425 args, kwds, "ss|O!", kwlist,
426 &host, &share, py_type_Credentials, &creds);
428 Py_DECREF(py_type_Credentials);
430 if (!ret) {
431 return -1;
434 if (!py_cli_state_setup_ev(self)) {
435 return -1;
438 if (creds == NULL) {
439 cli_creds = cli_credentials_init_anon(NULL);
440 } else {
441 cli_creds = PyCredentials_AsCliCredentials(creds);
444 req = cli_full_connection_send(
445 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
446 cli_credentials_get_username(cli_creds),
447 cli_credentials_get_domain(cli_creds),
448 cli_credentials_get_password(cli_creds),
449 0, 0);
450 if (!py_tevent_req_wait_exc(self->ev, req)) {
451 return NULL;
453 status = cli_full_connection_recv(req, &self->cli);
454 TALLOC_FREE(req);
456 if (!NT_STATUS_IS_OK(status)) {
457 PyErr_SetNTSTATUS(status);
458 return -1;
461 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
462 self->ev, self->ev, self->cli);
463 if (self->oplock_waiter == NULL) {
464 PyErr_NoMemory();
465 return -1;
467 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
468 self);
469 return 0;
472 static void py_cli_got_oplock_break(struct tevent_req *req)
474 struct py_cli_state *self = (struct py_cli_state *)
475 tevent_req_callback_data_void(req);
476 struct py_cli_oplock_break b;
477 struct py_cli_oplock_break *tmp;
478 size_t num_breaks;
479 NTSTATUS status;
481 status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
482 TALLOC_FREE(req);
483 self->oplock_waiter = NULL;
485 if (!NT_STATUS_IS_OK(status)) {
486 return;
489 num_breaks = talloc_array_length(self->oplock_breaks);
490 tmp = talloc_realloc(self->ev, self->oplock_breaks,
491 struct py_cli_oplock_break, num_breaks+1);
492 if (tmp == NULL) {
493 return;
495 self->oplock_breaks = tmp;
496 self->oplock_breaks[num_breaks] = b;
498 if (self->oplock_cond != NULL) {
499 py_tevent_cond_signal(self->oplock_cond);
502 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
503 self->ev, self->ev, self->cli);
504 if (self->oplock_waiter == NULL) {
505 return;
507 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
508 self);
511 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
512 PyObject *args)
514 size_t num_oplock_breaks;
516 if (!PyArg_ParseTuple(args, "")) {
517 return NULL;
520 if (self->oplock_cond != NULL) {
521 errno = EBUSY;
522 PyErr_SetFromErrno(PyExc_RuntimeError);
523 return NULL;
526 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
528 if (num_oplock_breaks == 0) {
529 struct py_tevent_cond cond;
530 int ret;
532 self->oplock_cond = &cond;
533 ret = py_tevent_cond_wait(&cond);
534 self->oplock_cond = NULL;
536 if (ret != 0) {
537 errno = ret;
538 PyErr_SetFromErrno(PyExc_RuntimeError);
539 return NULL;
543 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
544 if (num_oplock_breaks > 0) {
545 PyObject *result;
547 result = Py_BuildValue(
548 "{s:i,s:i}",
549 "fnum", self->oplock_breaks[0].fnum,
550 "level", self->oplock_breaks[0].level);
552 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
553 sizeof(self->oplock_breaks[0]) *
554 (num_oplock_breaks - 1));
555 self->oplock_breaks = talloc_realloc(
556 NULL, self->oplock_breaks, struct py_cli_oplock_break,
557 num_oplock_breaks - 1);
559 return result;
561 Py_RETURN_NONE;
564 static void py_cli_state_dealloc(struct py_cli_state *self)
566 TALLOC_FREE(self->thread_state);
567 TALLOC_FREE(self->oplock_waiter);
568 TALLOC_FREE(self->ev);
570 if (self->cli != NULL) {
571 cli_shutdown(self->cli);
572 self->cli = NULL;
574 self->ob_type->tp_free((PyObject *)self);
577 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
578 PyObject *kwds)
580 char *fname;
581 unsigned CreateFlags = 0;
582 unsigned DesiredAccess = FILE_GENERIC_READ;
583 unsigned FileAttributes = 0;
584 unsigned ShareAccess = 0;
585 unsigned CreateDisposition = FILE_OPEN;
586 unsigned CreateOptions = 0;
587 unsigned SecurityFlags = 0;
588 uint16_t fnum;
589 struct tevent_req *req;
590 NTSTATUS status;
592 static const char *kwlist[] = {
593 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
594 "ShareAccess", "CreateDisposition", "CreateOptions",
595 "SecurityFlags", NULL };
597 if (!ParseTupleAndKeywords(
598 args, kwds, "s|IIIIIII", kwlist,
599 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
600 &ShareAccess, &CreateDisposition, &CreateOptions,
601 &SecurityFlags)) {
602 return NULL;
605 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
606 DesiredAccess, FileAttributes, ShareAccess,
607 CreateDisposition, CreateOptions,
608 SecurityFlags);
609 if (!py_tevent_req_wait_exc(self->ev, req)) {
610 return NULL;
612 status = cli_ntcreate_recv(req, &fnum, NULL);
613 TALLOC_FREE(req);
615 if (!NT_STATUS_IS_OK(status)) {
616 PyErr_SetNTSTATUS(status);
617 return NULL;
619 return Py_BuildValue("I", (unsigned)fnum);
622 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
624 struct tevent_req *req;
625 int fnum;
626 NTSTATUS status;
628 if (!PyArg_ParseTuple(args, "i", &fnum)) {
629 return NULL;
632 req = cli_close_send(NULL, self->ev, self->cli, fnum);
633 if (!py_tevent_req_wait_exc(self->ev, req)) {
634 return NULL;
636 status = cli_close_recv(req);
637 TALLOC_FREE(req);
639 if (!NT_STATUS_IS_OK(status)) {
640 PyErr_SetNTSTATUS(status);
641 return NULL;
643 Py_RETURN_NONE;
646 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
647 PyObject *kwds)
649 int fnum;
650 unsigned mode = 0;
651 char *buf;
652 int buflen;
653 unsigned long long offset;
654 struct tevent_req *req;
655 NTSTATUS status;
656 size_t written;
658 static const char *kwlist[] = {
659 "fnum", "buffer", "offset", "mode", NULL };
661 if (!ParseTupleAndKeywords(
662 args, kwds, "Is#K|I", kwlist,
663 &fnum, &buf, &buflen, &offset, &mode)) {
664 return NULL;
667 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
668 (uint8_t *)buf, offset, buflen);
669 if (!py_tevent_req_wait_exc(self->ev, req)) {
670 return NULL;
672 status = cli_write_andx_recv(req, &written);
673 TALLOC_FREE(req);
675 if (!NT_STATUS_IS_OK(status)) {
676 PyErr_SetNTSTATUS(status);
677 return NULL;
679 return Py_BuildValue("K", (unsigned long long)written);
682 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
683 PyObject *kwds)
685 int fnum;
686 unsigned long long offset;
687 unsigned size;
688 struct tevent_req *req;
689 NTSTATUS status;
690 uint8_t *buf;
691 ssize_t buflen;
692 PyObject *result;
694 static const char *kwlist[] = {
695 "fnum", "offset", "size", NULL };
697 if (!ParseTupleAndKeywords(
698 args, kwds, "IKI", kwlist, &fnum, &offset,
699 &size)) {
700 return NULL;
703 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
704 offset, size);
705 if (!py_tevent_req_wait_exc(self->ev, req)) {
706 return NULL;
708 status = cli_read_andx_recv(req, &buflen, &buf);
710 if (!NT_STATUS_IS_OK(status)) {
711 TALLOC_FREE(req);
712 PyErr_SetNTSTATUS(status);
713 return NULL;
715 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
716 TALLOC_FREE(req);
717 return result;
720 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
721 PyObject *kwds)
723 int fnum;
724 unsigned long long size;
725 struct tevent_req *req;
726 NTSTATUS status;
728 static const char *kwlist[] = {
729 "fnum", "size", NULL };
731 if (!ParseTupleAndKeywords(
732 args, kwds, "IK", kwlist, &fnum, &size)) {
733 return NULL;
736 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
737 if (!py_tevent_req_wait_exc(self->ev, req)) {
738 return NULL;
740 status = cli_ftruncate_recv(req);
741 TALLOC_FREE(req);
743 if (!NT_STATUS_IS_OK(status)) {
744 PyErr_SetNTSTATUS(status);
745 return NULL;
747 Py_RETURN_NONE;
750 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
751 PyObject *args,
752 PyObject *kwds)
754 unsigned fnum, flag;
755 struct tevent_req *req;
756 NTSTATUS status;
758 static const char *kwlist[] = {
759 "fnum", "flag", NULL };
761 if (!ParseTupleAndKeywords(
762 args, kwds, "II", kwlist, &fnum, &flag)) {
763 return NULL;
766 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
767 flag);
768 if (!py_tevent_req_wait_exc(self->ev, req)) {
769 return NULL;
771 status = cli_nt_delete_on_close_recv(req);
772 TALLOC_FREE(req);
774 if (!NT_STATUS_IS_OK(status)) {
775 PyErr_SetNTSTATUS(status);
776 return NULL;
778 Py_RETURN_NONE;
781 static PyObject *py_cli_list(struct py_cli_state *self,
782 PyObject *args,
783 PyObject *kwds)
785 char *mask;
786 unsigned attribute =
787 FILE_ATTRIBUTE_DIRECTORY |
788 FILE_ATTRIBUTE_SYSTEM |
789 FILE_ATTRIBUTE_HIDDEN;
790 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
791 struct tevent_req *req;
792 NTSTATUS status;
793 struct file_info *finfos;
794 size_t i, num_finfos;
795 PyObject *result;
797 const char *kwlist[] = {
798 "mask", "attribute", "info_level", NULL
801 if (!ParseTupleAndKeywords(
802 args, kwds, "s|II", kwlist,
803 &mask, &attribute, &info_level)) {
804 return NULL;
807 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
808 info_level);
809 if (!py_tevent_req_wait_exc(self->ev, req)) {
810 return NULL;
812 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
813 TALLOC_FREE(req);
815 if (!NT_STATUS_IS_OK(status)) {
816 PyErr_SetNTSTATUS(status);
817 return NULL;
820 result = Py_BuildValue("[]");
821 if (result == NULL) {
822 return NULL;
825 for (i=0; i<num_finfos; i++) {
826 struct file_info *finfo = &finfos[i];
827 PyObject *file;
828 int ret;
830 file = Py_BuildValue(
831 "{s:s,s:i}",
832 "name", finfo->name,
833 "mode", (int)finfo->mode);
834 if (file == NULL) {
835 Py_XDECREF(result);
836 return NULL;
839 ret = PyList_Append(result, file);
840 if (ret == -1) {
841 Py_XDECREF(result);
842 return NULL;
846 return result;
849 static PyMethodDef py_cli_state_methods[] = {
850 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
851 "Open a file" },
852 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
853 "Close a file handle" },
854 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
855 "Write to a file handle" },
856 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
857 "Read from a file handle" },
858 { "truncate", (PyCFunction)py_cli_ftruncate,
859 METH_VARARGS|METH_KEYWORDS,
860 "Truncate a file" },
861 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
862 METH_VARARGS|METH_KEYWORDS,
863 "Set/Reset the delete on close flag" },
864 { "readdir", (PyCFunction)py_cli_list,
865 METH_VARARGS|METH_KEYWORDS,
866 "List a directory" },
867 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
868 METH_VARARGS, "Wait for an oplock break" },
869 { NULL, NULL, 0, NULL }
872 static PyTypeObject py_cli_state_type = {
873 PyObject_HEAD_INIT(NULL)
874 .tp_name = "libsmb_samba_internal.Conn",
875 .tp_basicsize = sizeof(struct py_cli_state),
876 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
877 .tp_doc = "libsmb connection",
878 .tp_new = py_cli_state_new,
879 .tp_init = (initproc)py_cli_state_init,
880 .tp_dealloc = (destructor)py_cli_state_dealloc,
881 .tp_methods = py_cli_state_methods,
884 static PyMethodDef py_libsmb_methods[] = {
885 { NULL },
888 void initlibsmb_samba_internal(void);
889 void initlibsmb_samba_internal(void)
891 PyObject *m;
893 talloc_stackframe();
895 m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
896 "libsmb wrapper");
898 if (PyType_Ready(&py_cli_state_type) < 0) {
899 return;
901 Py_INCREF(&py_cli_state_type);
902 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);