diff options
author | Tim Potter <tpot@samba.org> | 2005-02-08 23:17:44 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 10:55:36 -0500 |
commit | b3d17b8b166a870f08edbe506b5f4f91e22f0b25 (patch) | |
tree | edf03725ea9ffde702a364e75fe788378aa253b8 /source3/python | |
parent | a66913aca1132a9cd8070c509fe02ab5682c028e (diff) | |
download | samba-b3d17b8b166a870f08edbe506b5f4f91e22f0b25.tar.gz samba-b3d17b8b166a870f08edbe506b5f4f91e22f0b25.tar.bz2 samba-b3d17b8b166a870f08edbe506b5f4f91e22f0b25.zip |
r5283: Merge -r5279 and-r5280 from trunk.
(This used to be commit 74d872287b5cf5b13512091827a9d065f1fef122)
Diffstat (limited to 'source3/python')
-rw-r--r-- | source3/python/py_smb.c | 104 | ||||
-rw-r--r-- | source3/python/py_spoolss.h | 3 | ||||
-rw-r--r-- | source3/python/py_spoolss_drivers.c | 2 | ||||
-rw-r--r-- | source3/python/py_spoolss_drivers_conv.c | 53 |
4 files changed, 154 insertions, 8 deletions
diff --git a/source3/python/py_smb.c b/source3/python/py_smb.c index 1347f79a9b..c3d59d4fb8 100644 --- a/source3/python/py_smb.c +++ b/source3/python/py_smb.c @@ -179,6 +179,83 @@ static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args, return PyInt_FromLong(result); } +static PyObject *py_smb_open(PyObject *self, PyObject *args, PyObject *kw) +{ + cli_state_object *cli = (cli_state_object *)self; + static char *kwlist[] = { "filename", "flags", + "share_mode", NULL }; + char *filename; + uint32 flags, share_mode = DENY_NONE; + int result; + + /* Parse parameters */ + + if (!PyArg_ParseTupleAndKeywords( + args, kw, "si|i", kwlist, &filename, &flags, &share_mode)) + return NULL; + + result = cli_open(cli->cli, filename, flags, share_mode); + + if (cli_is_error(cli->cli)) { + PyErr_SetString(PyExc_RuntimeError, "open failed"); + return NULL; + } + + /* Return FID */ + + return PyInt_FromLong(result); +} + +static PyObject *py_smb_read(PyObject *self, PyObject *args, PyObject *kw) +{ + cli_state_object *cli = (cli_state_object *)self; + static char *kwlist[] = { "fnum", "offset", "size", NULL }; + int fnum, offset=0, size=0; + ssize_t result; + size_t fsize; + char *data; + PyObject *ret; + + /* Parse parameters */ + + if (!PyArg_ParseTupleAndKeywords( + args, kw, "i|ii", kwlist, &fnum, &offset, &size)) + return NULL; + + if (!cli_qfileinfo(cli->cli, fnum, NULL, &fsize, NULL, NULL, + NULL, NULL, NULL) && + !cli_getattrE(cli->cli, fnum, NULL, &fsize, NULL, NULL, NULL)) { + PyErr_SetString(PyExc_RuntimeError, "getattrib failed"); + return NULL; + } + + if (offset < 0) + offset = 0; + + if (size < 1 || size > fsize - offset) + size = fsize - offset; + + if (!(data = (char *) malloc((size_t) size))) { + PyErr_SetString(PyExc_RuntimeError, "malloc failed"); + return NULL; + } + + result = cli_read(cli->cli, fnum, data, (off_t) offset, (size_t) size); + + if (result==-1 || cli_is_error(cli->cli)) { + SAFE_FREE(data); + PyErr_SetString(PyExc_RuntimeError, "read failed"); + return NULL; + } + + /* Return a python string */ + + ret = Py_BuildValue("s#", data, result); + SAFE_FREE(data); + + return ret; +} + static PyObject *py_smb_close(PyObject *self, PyObject *args, PyObject *kw) { @@ -323,6 +400,33 @@ static PyMethodDef smb_hnd_methods[] = { { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx, METH_VARARGS | METH_KEYWORDS, "NT Create&X" }, + { "open", (PyCFunction)py_smb_open, + METH_VARARGS | METH_KEYWORDS, + "Open a file\n" +"\n" +"This function returns a fnum handle to an open file. The file is\n" +"opened with flags and optional share mode. If unspecified, the\n" +"default share mode is DENY_NONE\n" +"\n" +"Example:\n" +"\n" +">>> fnum=conn.open(filename, os.O_RDONLY)" }, + + { "read", (PyCFunction)py_smb_read, + METH_VARARGS | METH_KEYWORDS, + "Read from an open file\n" +"\n" +"This function returns a string read from an open file starting at\n" +"offset for size bytes (until EOF is reached). If unspecified, the\n" +"default offset is 0, and default size is the remainder of the file.\n" +"\n" +"Example:\n" +"\n" +">>> conn.read(fnum) # read entire file\n" +">>> conn.read(fnum,5) # read entire file from offset 5\n" +">>> conn.read(fnum,size=64) # read 64 bytes from start of file\n" +">>> conn.read(fnum,4096,1024) # read 1024 bytes from offset 4096\n" }, + { "close", (PyCFunction)py_smb_close, METH_VARARGS | METH_KEYWORDS, "Close" }, diff --git a/source3/python/py_spoolss.h b/source3/python/py_spoolss.h index 34b48190cd..ec94de8fe4 100644 --- a/source3/python/py_spoolss.h +++ b/source3/python/py_spoolss.h @@ -67,7 +67,8 @@ BOOL py_to_DRIVER_INFO_1(DRIVER_INFO_1 *info, PyObject *dict); BOOL py_from_DRIVER_INFO_2(PyObject **dict, DRIVER_INFO_2 *info); BOOL py_to_DRIVER_INFO_2(DRIVER_INFO_2 *info, PyObject *dict); BOOL py_from_DRIVER_INFO_3(PyObject **dict, DRIVER_INFO_3 *info); -BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict); +BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict, + TALLOC_CTX *mem_ctx); BOOL py_from_DRIVER_INFO_6(PyObject **dict, DRIVER_INFO_6 *info); BOOL py_to_DRIVER_INFO_6(DRIVER_INFO_6 *info, PyObject *dict); BOOL py_from_DRIVER_DIRECTORY_1(PyObject **dict, DRIVER_DIRECTORY_1 *info); diff --git a/source3/python/py_spoolss_drivers.c b/source3/python/py_spoolss_drivers.c index 9424fe1527..310f978188 100644 --- a/source3/python/py_spoolss_drivers.c +++ b/source3/python/py_spoolss_drivers.c @@ -365,7 +365,7 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args, case 3: ctr.info3 = &dinfo.driver_3; - if (!py_to_DRIVER_INFO_3(&dinfo.driver_3, info)) { + if (!py_to_DRIVER_INFO_3(&dinfo.driver_3, info, mem_ctx)) { PyErr_SetString(spoolss_error, "error converting to driver info 3"); goto done; diff --git a/source3/python/py_spoolss_drivers_conv.c b/source3/python/py_spoolss_drivers_conv.c index 9bc8408052..ae196c1903 100644 --- a/source3/python/py_spoolss_drivers_conv.c +++ b/source3/python/py_spoolss_drivers_conv.c @@ -78,9 +78,49 @@ struct pyconv py_DRIVER_DIRECTORY_1[] = { { NULL } }; -static uint16 *to_dependentfiles(PyObject *dict) +static uint16 *to_dependentfiles(PyObject *list, TALLOC_CTX *mem_ctx) { - return (uint16 *)"abcd\0"; + uint32 elements, size=0, pos=0, i; + char *str; + uint16 *ret = NULL; + PyObject *borrowedRef; + + if (!PyList_Check(list)) { + goto done; + } + + /* calculate size for dependentfiles */ + elements=PyList_Size(list); + for (i = 0; i < elements; i++) { + borrowedRef=PyList_GetItem(list, i); + if (!PyString_Check(borrowedRef)) + /* non string found, return error */ + goto done; + size+=PyString_Size(borrowedRef)+1; + } + + if (!(ret = (uint16*) talloc(mem_ctx,(size+1)*sizeof(uint16)))) + goto done; + + /* create null terminated sequence of null terminated strings */ + for (i = 0; i < elements; i++) { + borrowedRef=PyList_GetItem(list, i); + str=PyString_AsString(borrowedRef); + do { + if (pos >= size) { + /* dependentfiles too small. miscalculated? */ + ret = NULL; + goto done; + } + SSVAL(&ret[pos], 0, str[0]); + pos++; + } while (*(str++)); + } + /* final null */ + ret[pos]='\0'; + +done: + return ret; } BOOL py_from_DRIVER_INFO_1(PyObject **dict, DRIVER_INFO_1 *info) @@ -122,16 +162,17 @@ BOOL py_from_DRIVER_INFO_3(PyObject **dict, DRIVER_INFO_3 *info) return True; } -BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict) +BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict, + TALLOC_CTX *mem_ctx) { PyObject *obj, *dict_copy = PyDict_Copy(dict); BOOL result = False; - if (!(obj = PyDict_GetItemString(dict_copy, "dependent_files")) || - !PyList_Check(obj)) + if (!(obj = PyDict_GetItemString(dict_copy, "dependent_files"))) goto done; - info->dependentfiles = to_dependentfiles(obj); + if (!(info->dependentfiles = to_dependentfiles(obj, mem_ctx))) + goto done; PyDict_DelItemString(dict_copy, "dependent_files"); |