Change to flush and close logic to fix #1760556.
[python.git] / PC / _msi.c
blob7d60d085dff642a23eac1f64256a03c16836fb52
1 /* Helper library for MSI creation with Python.
2 * Copyright (C) 2005 Martin v. Löwis
3 * Licensed to PSF under a contributor agreement.
4 */
6 #include <Python.h>
7 #include <fci.h>
8 #include <fcntl.h>
9 #include <windows.h>
10 #include <msi.h>
11 #include <msiquery.h>
12 #include <msidefs.h>
13 #include <rpc.h>
15 static PyObject *MSIError;
17 static PyObject*
18 uuidcreate(PyObject* obj, PyObject*args)
20 UUID result;
21 char *cresult;
22 PyObject *oresult;
24 /* May return ok, local only, and no address.
25 For local only, the documentation says we still get a uuid.
26 For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can
27 use the result. */
28 if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) {
29 PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result");
30 return NULL;
33 if (UuidToString(&result, &cresult) == RPC_S_OUT_OF_MEMORY) {
34 PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen");
35 return NULL;
38 oresult = PyString_FromString(cresult);
39 RpcStringFree(&cresult);
40 return oresult;
44 /* FCI callback functions */
46 static FNFCIALLOC(cb_alloc)
48 return malloc(cb);
51 static FNFCIFREE(cb_free)
53 free(memory);
56 static FNFCIOPEN(cb_open)
58 int result = _open(pszFile, oflag, pmode);
59 if (result == -1)
60 *err = errno;
61 return result;
64 static FNFCIREAD(cb_read)
66 UINT result = (UINT)_read(hf, memory, cb);
67 if (result != cb)
68 *err = errno;
69 return result;
72 static FNFCIWRITE(cb_write)
74 UINT result = (UINT)_write(hf, memory, cb);
75 if (result != cb)
76 *err = errno;
77 return result;
80 static FNFCICLOSE(cb_close)
82 int result = _close(hf);
83 if (result != 0)
84 *err = errno;
85 return result;
88 static FNFCISEEK(cb_seek)
90 long result = (long)_lseek(hf, dist, seektype);
91 if (result == -1)
92 *err = errno;
93 return result;
96 static FNFCIDELETE(cb_delete)
98 int result = remove(pszFile);
99 if (result != 0)
100 *err = errno;
101 return result;
104 static FNFCIFILEPLACED(cb_fileplaced)
106 return 0;
109 static FNFCIGETTEMPFILE(cb_gettempfile)
111 char *name = _tempnam("", "tmp");
112 if ((name != NULL) && ((int)strlen(name) < cbTempName)) {
113 strcpy(pszTempName, name);
114 free(name);
115 return TRUE;
118 if (name) free(name);
119 return FALSE;
122 static FNFCISTATUS(cb_status)
124 if (pv) {
125 PyObject *result = PyObject_CallMethod(pv, "status", "iii", typeStatus, cb1, cb2);
126 if (result == NULL)
127 return -1;
128 Py_DECREF(result);
130 return 0;
133 static FNFCIGETNEXTCABINET(cb_getnextcabinet)
135 if (pv) {
136 PyObject *result = PyObject_CallMethod(pv, "getnextcabinet", "i", pccab->iCab);
137 if (result == NULL)
138 return -1;
139 if (!PyString_Check(result)) {
140 PyErr_Format(PyExc_TypeError,
141 "Incorrect return type %s from getnextcabinet",
142 result->ob_type->tp_name);
143 Py_DECREF(result);
144 return FALSE;
146 strncpy(pccab->szCab, PyString_AsString(result), sizeof(pccab->szCab));
147 return TRUE;
149 return FALSE;
152 static FNFCIGETOPENINFO(cb_getopeninfo)
154 BY_HANDLE_FILE_INFORMATION bhfi;
155 FILETIME filetime;
156 HANDLE handle;
158 /* Need Win32 handle to get time stamps */
159 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
160 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
161 if (handle == INVALID_HANDLE_VALUE)
162 return -1;
164 if (GetFileInformationByHandle(handle, &bhfi) == FALSE)
166 CloseHandle(handle);
167 return -1;
170 FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime);
171 FileTimeToDosDateTime(&filetime, pdate, ptime);
173 *pattribs = (int)(bhfi.dwFileAttributes &
174 (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
176 CloseHandle(handle);
178 return _open(pszName, _O_RDONLY | _O_BINARY);
181 static PyObject* fcicreate(PyObject* obj, PyObject* args)
183 char *cabname;
184 PyObject *files;
185 CCAB ccab;
186 HFCI hfci;
187 ERF erf;
188 int i;
191 if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files))
192 return NULL;
194 if (!PyList_Check(files)) {
195 PyErr_SetString(PyExc_TypeError, "FCICreate expects a list");
196 return NULL;
199 ccab.cb = INT_MAX; /* no need to split CAB into multiple media */
200 ccab.cbFolderThresh = 1000000; /* flush directory after this many bytes */
201 ccab.cbReserveCFData = 0;
202 ccab.cbReserveCFFolder = 0;
203 ccab.cbReserveCFHeader = 0;
205 ccab.iCab = 1;
206 ccab.iDisk = 1;
208 ccab.setID = 0;
209 ccab.szDisk[0] = '\0';
211 for (i=0; cabname[i]; i++)
212 if (cabname[i] == '\\' || cabname[i] == '/')
213 break;
215 if (i > sizeof(ccab.szCabPath) ||
216 strlen(cabname+i) > sizeof(ccab.szCab)) {
217 PyErr_SetString(PyExc_ValueError, "path name too long");
218 return 0;
221 if (cabname[i]) {
222 memcpy(ccab.szCabPath, cabname, i);
223 ccab.szCabPath[i] = '\0';
224 strcpy(ccab.szCab, cabname+i);
225 } else {
226 strcpy(ccab.szCabPath, ".");
227 strcpy(ccab.szCab, cabname);
230 hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free,
231 cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete,
232 cb_gettempfile, &ccab, NULL);
234 if (hfci == NULL) {
235 PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper);
236 return NULL;
239 for (i=0; i < PyList_GET_SIZE(files); i++) {
240 PyObject *item = PyList_GET_ITEM(files, i);
241 char *filename, *cabname;
242 if (!PyArg_ParseTuple(item, "ss", &filename, &cabname))
243 goto err;
244 if (!FCIAddFile(hfci, filename, cabname, FALSE,
245 cb_getnextcabinet, cb_status, cb_getopeninfo,
246 tcompTYPE_MSZIP))
247 goto err;
250 if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status))
251 goto err;
253 if (!FCIDestroy(hfci))
254 goto err;
256 Py_INCREF(Py_None);
257 return Py_None;
258 err:
259 PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); /* XXX better error type */
260 FCIDestroy(hfci);
261 return NULL;
264 typedef struct msiobj{
265 PyObject_HEAD
266 MSIHANDLE h;
267 }msiobj;
269 static void
270 msiobj_dealloc(msiobj* msidb)
272 MsiCloseHandle(msidb->h);
273 msidb->h = 0;
276 static PyObject*
277 msiobj_close(msiobj* msidb, PyObject *args)
279 MsiCloseHandle(msidb->h);
280 msidb->h = 0;
281 Py_INCREF(Py_None);
282 return Py_None;
285 static PyObject*
286 msierror(int status)
288 int code;
289 char buf[2000];
290 char *res = buf;
291 DWORD size = sizeof(buf);
292 MSIHANDLE err = MsiGetLastErrorRecord();
294 if (err == 0) {
295 switch(status) {
296 case ERROR_ACCESS_DENIED:
297 PyErr_SetString(MSIError, "access denied");
298 return NULL;
299 case ERROR_FUNCTION_FAILED:
300 PyErr_SetString(MSIError, "function failed");
301 return NULL;
302 case ERROR_INVALID_DATA:
303 PyErr_SetString(MSIError, "invalid data");
304 return NULL;
305 case ERROR_INVALID_HANDLE:
306 PyErr_SetString(MSIError, "invalid handle");
307 return NULL;
308 case ERROR_INVALID_STATE:
309 PyErr_SetString(MSIError, "invalid state");
310 return NULL;
311 case ERROR_INVALID_PARAMETER:
312 PyErr_SetString(MSIError, "invalid parameter");
313 return NULL;
314 default:
315 PyErr_Format(MSIError, "unknown error %x", status);
316 return NULL;
320 code = MsiRecordGetInteger(err, 1); /* XXX code */
321 if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) {
322 res = malloc(size+1);
323 MsiFormatRecord(0, err, res, &size);
324 res[size]='\0';
326 MsiCloseHandle(err);
327 PyErr_SetString(MSIError, res);
328 if (res != buf)
329 free(res);
330 return NULL;
333 /*************************** Record objects **********************/
335 static PyObject*
336 record_getfieldcount(msiobj* record, PyObject* args)
338 return PyInt_FromLong(MsiRecordGetFieldCount(record->h));
341 static PyObject*
342 record_cleardata(msiobj* record, PyObject *args)
344 int status = MsiRecordClearData(record->h);
345 if (status != ERROR_SUCCESS)
346 return msierror(status);
348 Py_INCREF(Py_None);
349 return Py_None;
352 static PyObject*
353 record_setstring(msiobj* record, PyObject *args)
355 int status;
356 int field;
357 char *data;
359 if (!PyArg_ParseTuple(args, "is:SetString", &field, &data))
360 return NULL;
362 if ((status = MsiRecordSetString(record->h, field, data)) != ERROR_SUCCESS)
363 return msierror(status);
365 Py_INCREF(Py_None);
366 return Py_None;
369 static PyObject*
370 record_setstream(msiobj* record, PyObject *args)
372 int status;
373 int field;
374 char *data;
376 if (!PyArg_ParseTuple(args, "is:SetStream", &field, &data))
377 return NULL;
379 if ((status = MsiRecordSetStream(record->h, field, data)) != ERROR_SUCCESS)
380 return msierror(status);
382 Py_INCREF(Py_None);
383 return Py_None;
386 static PyObject*
387 record_setinteger(msiobj* record, PyObject *args)
389 int status;
390 int field;
391 int data;
393 if (!PyArg_ParseTuple(args, "ii:SetInteger", &field, &data))
394 return NULL;
396 if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS)
397 return msierror(status);
399 Py_INCREF(Py_None);
400 return Py_None;
405 static PyMethodDef record_methods[] = {
406 { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS,
407 PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")},
408 { "SetString", (PyCFunction)record_setstring, METH_VARARGS,
409 PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")},
410 { "SetStream", (PyCFunction)record_setstream, METH_VARARGS,
411 PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")},
412 { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS,
413 PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")},
414 { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS,
415 PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")},
416 { NULL, NULL }
419 static PyTypeObject record_Type = {
420 PyVarObject_HEAD_INIT(NULL, 0)
421 "_msi.Record", /*tp_name*/
422 sizeof(msiobj), /*tp_basicsize*/
423 0, /*tp_itemsize*/
424 /* methods */
425 (destructor)msiobj_dealloc, /*tp_dealloc*/
426 0, /*tp_print*/
427 0, /*tp_getattr*/
428 0, /*tp_setattr*/
429 0, /*tp_compare*/
430 0, /*tp_repr*/
431 0, /*tp_as_number*/
432 0, /*tp_as_sequence*/
433 0, /*tp_as_mapping*/
434 0, /*tp_hash*/
435 0, /*tp_call*/
436 0, /*tp_str*/
437 PyObject_GenericGetAttr,/*tp_getattro*/
438 PyObject_GenericSetAttr,/*tp_setattro*/
439 0, /*tp_as_buffer*/
440 Py_TPFLAGS_DEFAULT, /*tp_flags*/
441 0, /*tp_doc*/
442 0, /*tp_traverse*/
443 0, /*tp_clear*/
444 0, /*tp_richcompare*/
445 0, /*tp_weaklistoffset*/
446 0, /*tp_iter*/
447 0, /*tp_iternext*/
448 record_methods, /*tp_methods*/
449 0, /*tp_members*/
450 0, /*tp_getset*/
451 0, /*tp_base*/
452 0, /*tp_dict*/
453 0, /*tp_descr_get*/
454 0, /*tp_descr_set*/
455 0, /*tp_dictoffset*/
456 0, /*tp_init*/
457 0, /*tp_alloc*/
458 0, /*tp_new*/
459 0, /*tp_free*/
460 0, /*tp_is_gc*/
463 static PyObject*
464 record_new(MSIHANDLE h)
466 msiobj *result = PyObject_NEW(struct msiobj, &record_Type);
468 if (!result) {
469 MsiCloseHandle(h);
470 return NULL;
473 result->h = h;
474 return (PyObject*)result;
477 /*************************** SummaryInformation objects **************/
479 static PyObject*
480 summary_getproperty(msiobj* si, PyObject *args)
482 int status;
483 int field;
484 PyObject *result;
485 UINT type;
486 INT ival;
487 FILETIME fval;
488 char sbuf[1000];
489 char *sval = sbuf;
490 DWORD ssize = sizeof(sval);
492 if (!PyArg_ParseTuple(args, "i:GetProperty", &field))
493 return NULL;
495 status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
496 &fval, sval, &ssize);
497 if (status == ERROR_MORE_DATA) {
498 sval = malloc(ssize);
499 status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
500 &fval, sval, &ssize);
503 switch(type) {
504 case VT_I2: case VT_I4:
505 return PyInt_FromLong(ival);
506 case VT_FILETIME:
507 PyErr_SetString(PyExc_NotImplementedError, "FILETIME result");
508 return NULL;
509 case VT_LPSTR:
510 result = PyString_FromStringAndSize(sval, ssize);
511 if (sval != sbuf)
512 free(sval);
513 return result;
515 PyErr_Format(PyExc_NotImplementedError, "result of type %d", type);
516 return NULL;
519 static PyObject*
520 summary_getpropertycount(msiobj* si, PyObject *args)
522 int status;
523 UINT result;
525 status = MsiSummaryInfoGetPropertyCount(si->h, &result);
526 if (status != ERROR_SUCCESS)
527 return msierror(status);
529 return PyInt_FromLong(result);
532 static PyObject*
533 summary_setproperty(msiobj* si, PyObject *args)
535 int status;
536 int field;
537 PyObject* data;
539 if (!PyArg_ParseTuple(args, "iO:SetProperty", &field, &data))
540 return NULL;
542 if (PyString_Check(data)) {
543 status = MsiSummaryInfoSetProperty(si->h, field, VT_LPSTR,
544 0, NULL, PyString_AsString(data));
545 } else if (PyInt_Check(data)) {
546 status = MsiSummaryInfoSetProperty(si->h, field, VT_I4,
547 PyInt_AsLong(data), NULL, NULL);
548 } else {
549 PyErr_SetString(PyExc_TypeError, "unsupported type");
550 return NULL;
553 if (status != ERROR_SUCCESS)
554 return msierror(status);
556 Py_INCREF(Py_None);
557 return Py_None;
561 static PyObject*
562 summary_persist(msiobj* si, PyObject *args)
564 int status;
566 status = MsiSummaryInfoPersist(si->h);
567 if (status != ERROR_SUCCESS)
568 return msierror(status);
569 Py_INCREF(Py_None);
570 return Py_None;
573 static PyMethodDef summary_methods[] = {
574 { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS,
575 PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")},
576 { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS,
577 PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")},
578 { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS,
579 PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")},
580 { "Persist", (PyCFunction)summary_persist, METH_NOARGS,
581 PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")},
582 { NULL, NULL }
585 static PyTypeObject summary_Type = {
586 PyVarObject_HEAD_INIT(NULL, 0)
587 "_msi.SummaryInformation", /*tp_name*/
588 sizeof(msiobj), /*tp_basicsize*/
589 0, /*tp_itemsize*/
590 /* methods */
591 (destructor)msiobj_dealloc, /*tp_dealloc*/
592 0, /*tp_print*/
593 0, /*tp_getattr*/
594 0, /*tp_setattr*/
595 0, /*tp_compare*/
596 0, /*tp_repr*/
597 0, /*tp_as_number*/
598 0, /*tp_as_sequence*/
599 0, /*tp_as_mapping*/
600 0, /*tp_hash*/
601 0, /*tp_call*/
602 0, /*tp_str*/
603 PyObject_GenericGetAttr,/*tp_getattro*/
604 PyObject_GenericSetAttr,/*tp_setattro*/
605 0, /*tp_as_buffer*/
606 Py_TPFLAGS_DEFAULT, /*tp_flags*/
607 0, /*tp_doc*/
608 0, /*tp_traverse*/
609 0, /*tp_clear*/
610 0, /*tp_richcompare*/
611 0, /*tp_weaklistoffset*/
612 0, /*tp_iter*/
613 0, /*tp_iternext*/
614 summary_methods, /*tp_methods*/
615 0, /*tp_members*/
616 0, /*tp_getset*/
617 0, /*tp_base*/
618 0, /*tp_dict*/
619 0, /*tp_descr_get*/
620 0, /*tp_descr_set*/
621 0, /*tp_dictoffset*/
622 0, /*tp_init*/
623 0, /*tp_alloc*/
624 0, /*tp_new*/
625 0, /*tp_free*/
626 0, /*tp_is_gc*/
629 /*************************** View objects **************/
631 static PyObject*
632 view_execute(msiobj *view, PyObject*args)
634 int status;
635 MSIHANDLE params = 0;
636 PyObject *oparams = Py_None;
638 if (!PyArg_ParseTuple(args, "O:Execute", &oparams))
639 return NULL;
641 if (oparams != Py_None) {
642 if (oparams->ob_type != &record_Type) {
643 PyErr_SetString(PyExc_TypeError, "Execute argument must be a record");
644 return NULL;
646 params = ((msiobj*)oparams)->h;
649 status = MsiViewExecute(view->h, params);
650 if (status != ERROR_SUCCESS)
651 return msierror(status);
653 Py_INCREF(Py_None);
654 return Py_None;
657 static PyObject*
658 view_fetch(msiobj *view, PyObject*args)
660 int status;
661 MSIHANDLE result;
663 if ((status = MsiViewFetch(view->h, &result)) != ERROR_SUCCESS)
664 return msierror(status);
666 return record_new(result);
669 static PyObject*
670 view_getcolumninfo(msiobj *view, PyObject *args)
672 int status;
673 int kind;
674 MSIHANDLE result;
676 if (!PyArg_ParseTuple(args, "i:GetColumnInfo", &kind))
677 return NULL;
679 if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS)
680 return msierror(status);
682 return record_new(result);
685 static PyObject*
686 view_modify(msiobj *view, PyObject *args)
688 int kind;
689 PyObject *data;
690 int status;
692 if (!PyArg_ParseTuple(args, "iO:Modify", &kind, &data))
693 return NULL;
695 if (data->ob_type != &record_Type) {
696 PyErr_SetString(PyExc_TypeError, "Modify expects a record object");
697 return NULL;
700 if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS)
701 return msierror(status);
703 Py_INCREF(Py_None);
704 return Py_None;
707 static PyObject*
708 view_close(msiobj *view, PyObject*args)
710 int status;
712 if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS)
713 return msierror(status);
715 Py_INCREF(Py_None);
716 return Py_None;
719 static PyMethodDef view_methods[] = {
720 { "Execute", (PyCFunction)view_execute, METH_VARARGS,
721 PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")},
722 { "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS,
723 PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")},
724 { "Fetch", (PyCFunction)view_fetch, METH_NOARGS,
725 PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")},
726 { "Modify", (PyCFunction)view_modify, METH_VARARGS,
727 PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")},
728 { "Close", (PyCFunction)view_close, METH_NOARGS,
729 PyDoc_STR("Close() -> result\nWraps MsiViewClose")},
730 { NULL, NULL }
733 static PyTypeObject msiview_Type = {
734 PyVarObject_HEAD_INIT(NULL, 0)
735 "_msi.View", /*tp_name*/
736 sizeof(msiobj), /*tp_basicsize*/
737 0, /*tp_itemsize*/
738 /* methods */
739 (destructor)msiobj_dealloc, /*tp_dealloc*/
740 0, /*tp_print*/
741 0, /*tp_getattr*/
742 0, /*tp_setattr*/
743 0, /*tp_compare*/
744 0, /*tp_repr*/
745 0, /*tp_as_number*/
746 0, /*tp_as_sequence*/
747 0, /*tp_as_mapping*/
748 0, /*tp_hash*/
749 0, /*tp_call*/
750 0, /*tp_str*/
751 PyObject_GenericGetAttr,/*tp_getattro*/
752 PyObject_GenericSetAttr,/*tp_setattro*/
753 0, /*tp_as_buffer*/
754 Py_TPFLAGS_DEFAULT, /*tp_flags*/
755 0, /*tp_doc*/
756 0, /*tp_traverse*/
757 0, /*tp_clear*/
758 0, /*tp_richcompare*/
759 0, /*tp_weaklistoffset*/
760 0, /*tp_iter*/
761 0, /*tp_iternext*/
762 view_methods, /*tp_methods*/
763 0, /*tp_members*/
764 0, /*tp_getset*/
765 0, /*tp_base*/
766 0, /*tp_dict*/
767 0, /*tp_descr_get*/
768 0, /*tp_descr_set*/
769 0, /*tp_dictoffset*/
770 0, /*tp_init*/
771 0, /*tp_alloc*/
772 0, /*tp_new*/
773 0, /*tp_free*/
774 0, /*tp_is_gc*/
777 /*************************** Database objects **************/
779 static PyObject*
780 msidb_openview(msiobj *msidb, PyObject *args)
782 int status;
783 char *sql;
784 MSIHANDLE hView;
785 msiobj *result;
787 if (!PyArg_ParseTuple(args, "s:OpenView", &sql))
788 return NULL;
790 if ((status = MsiDatabaseOpenView(msidb->h, sql, &hView)) != ERROR_SUCCESS)
791 return msierror(status);
793 result = PyObject_NEW(struct msiobj, &msiview_Type);
794 if (!result) {
795 MsiCloseHandle(hView);
796 return NULL;
799 result->h = hView;
800 return (PyObject*)result;
803 static PyObject*
804 msidb_commit(msiobj *msidb, PyObject *args)
806 int status;
808 if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS)
809 return msierror(status);
811 Py_INCREF(Py_None);
812 return Py_None;
815 static PyObject*
816 msidb_getsummaryinformation(msiobj *db, PyObject *args)
818 int status;
819 int count;
820 MSIHANDLE result;
821 msiobj *oresult;
823 if (!PyArg_ParseTuple(args, "i:GetSummaryInformation", &count))
824 return NULL;
826 status = MsiGetSummaryInformation(db->h, NULL, count, &result);
827 if (status != ERROR_SUCCESS)
828 return msierror(status);
830 oresult = PyObject_NEW(struct msiobj, &summary_Type);
831 if (!result) {
832 MsiCloseHandle(result);
833 return NULL;
836 oresult->h = result;
837 return (PyObject*)oresult;
840 static PyMethodDef db_methods[] = {
841 { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS,
842 PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")},
843 { "Commit", (PyCFunction)msidb_commit, METH_NOARGS,
844 PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")},
845 { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS,
846 PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")},
847 { NULL, NULL }
850 static PyTypeObject msidb_Type = {
851 PyVarObject_HEAD_INIT(NULL, 0)
852 "_msi.Database", /*tp_name*/
853 sizeof(msiobj), /*tp_basicsize*/
854 0, /*tp_itemsize*/
855 /* methods */
856 (destructor)msiobj_dealloc, /*tp_dealloc*/
857 0, /*tp_print*/
858 0, /*tp_getattr*/
859 0, /*tp_setattr*/
860 0, /*tp_compare*/
861 0, /*tp_repr*/
862 0, /*tp_as_number*/
863 0, /*tp_as_sequence*/
864 0, /*tp_as_mapping*/
865 0, /*tp_hash*/
866 0, /*tp_call*/
867 0, /*tp_str*/
868 PyObject_GenericGetAttr,/*tp_getattro*/
869 PyObject_GenericSetAttr,/*tp_setattro*/
870 0, /*tp_as_buffer*/
871 Py_TPFLAGS_DEFAULT, /*tp_flags*/
872 0, /*tp_doc*/
873 0, /*tp_traverse*/
874 0, /*tp_clear*/
875 0, /*tp_richcompare*/
876 0, /*tp_weaklistoffset*/
877 0, /*tp_iter*/
878 0, /*tp_iternext*/
879 db_methods, /*tp_methods*/
880 0, /*tp_members*/
881 0, /*tp_getset*/
882 0, /*tp_base*/
883 0, /*tp_dict*/
884 0, /*tp_descr_get*/
885 0, /*tp_descr_set*/
886 0, /*tp_dictoffset*/
887 0, /*tp_init*/
888 0, /*tp_alloc*/
889 0, /*tp_new*/
890 0, /*tp_free*/
891 0, /*tp_is_gc*/
894 static PyObject* msiopendb(PyObject *obj, PyObject *args)
896 int status;
897 char *path;
898 int persist;
899 MSIHANDLE h;
900 msiobj *result;
902 if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist))
903 return NULL;
905 status = MsiOpenDatabase(path, (LPCSTR)persist, &h);
906 if (status != ERROR_SUCCESS)
907 return msierror(status);
909 result = PyObject_NEW(struct msiobj, &msidb_Type);
910 if (!result) {
911 MsiCloseHandle(h);
912 return NULL;
914 result->h = h;
915 return (PyObject*)result;
918 static PyObject*
919 createrecord(PyObject *o, PyObject *args)
921 int count;
922 MSIHANDLE h;
924 if (!PyArg_ParseTuple(args, "i:CreateRecord", &count))
925 return NULL;
927 h = MsiCreateRecord(count);
928 if (h == 0)
929 return msierror(0);
931 return record_new(h);
935 static PyMethodDef msi_methods[] = {
936 {"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS,
937 PyDoc_STR("UuidCreate() -> string")},
938 {"FCICreate", (PyCFunction)fcicreate, METH_VARARGS,
939 PyDoc_STR("fcicreate(cabname,files) -> None")},
940 {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS,
941 PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")},
942 {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS,
943 PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")},
944 {NULL, NULL} /* sentinel */
947 static char msi_doc[] = "Documentation";
949 PyMODINIT_FUNC
950 init_msi(void)
952 PyObject *m;
954 m = Py_InitModule3("_msi", msi_methods, msi_doc);
955 if (m == NULL)
956 return;
958 PyModule_AddIntConstant(m, "MSIDBOPEN_CREATEDIRECT", (int)MSIDBOPEN_CREATEDIRECT);
959 PyModule_AddIntConstant(m, "MSIDBOPEN_CREATE", (int)MSIDBOPEN_CREATE);
960 PyModule_AddIntConstant(m, "MSIDBOPEN_DIRECT", (int)MSIDBOPEN_DIRECT);
961 PyModule_AddIntConstant(m, "MSIDBOPEN_READONLY", (int)MSIDBOPEN_READONLY);
962 PyModule_AddIntConstant(m, "MSIDBOPEN_TRANSACT", (int)MSIDBOPEN_TRANSACT);
963 PyModule_AddIntConstant(m, "MSIDBOPEN_PATCHFILE", (int)MSIDBOPEN_PATCHFILE);
965 PyModule_AddIntConstant(m, "MSICOLINFO_NAMES", MSICOLINFO_NAMES);
966 PyModule_AddIntConstant(m, "MSICOLINFO_TYPES", MSICOLINFO_TYPES);
968 PyModule_AddIntConstant(m, "MSIMODIFY_SEEK", MSIMODIFY_SEEK);
969 PyModule_AddIntConstant(m, "MSIMODIFY_REFRESH", MSIMODIFY_REFRESH);
970 PyModule_AddIntConstant(m, "MSIMODIFY_INSERT", MSIMODIFY_INSERT);
971 PyModule_AddIntConstant(m, "MSIMODIFY_UPDATE", MSIMODIFY_UPDATE);
972 PyModule_AddIntConstant(m, "MSIMODIFY_ASSIGN", MSIMODIFY_ASSIGN);
973 PyModule_AddIntConstant(m, "MSIMODIFY_REPLACE", MSIMODIFY_REPLACE);
974 PyModule_AddIntConstant(m, "MSIMODIFY_MERGE", MSIMODIFY_MERGE);
975 PyModule_AddIntConstant(m, "MSIMODIFY_DELETE", MSIMODIFY_DELETE);
976 PyModule_AddIntConstant(m, "MSIMODIFY_INSERT_TEMPORARY", MSIMODIFY_INSERT_TEMPORARY);
977 PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE", MSIMODIFY_VALIDATE);
978 PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_NEW", MSIMODIFY_VALIDATE_NEW);
979 PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_FIELD", MSIMODIFY_VALIDATE_FIELD);
980 PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_DELETE", MSIMODIFY_VALIDATE_DELETE);
982 PyModule_AddIntConstant(m, "PID_CODEPAGE", PID_CODEPAGE);
983 PyModule_AddIntConstant(m, "PID_TITLE", PID_TITLE);
984 PyModule_AddIntConstant(m, "PID_SUBJECT", PID_SUBJECT);
985 PyModule_AddIntConstant(m, "PID_AUTHOR", PID_AUTHOR);
986 PyModule_AddIntConstant(m, "PID_KEYWORDS", PID_KEYWORDS);
987 PyModule_AddIntConstant(m, "PID_COMMENTS", PID_COMMENTS);
988 PyModule_AddIntConstant(m, "PID_TEMPLATE", PID_TEMPLATE);
989 PyModule_AddIntConstant(m, "PID_LASTAUTHOR", PID_LASTAUTHOR);
990 PyModule_AddIntConstant(m, "PID_REVNUMBER", PID_REVNUMBER);
991 PyModule_AddIntConstant(m, "PID_LASTPRINTED", PID_LASTPRINTED);
992 PyModule_AddIntConstant(m, "PID_CREATE_DTM", PID_CREATE_DTM);
993 PyModule_AddIntConstant(m, "PID_LASTSAVE_DTM", PID_LASTSAVE_DTM);
994 PyModule_AddIntConstant(m, "PID_PAGECOUNT", PID_PAGECOUNT);
995 PyModule_AddIntConstant(m, "PID_WORDCOUNT", PID_WORDCOUNT);
996 PyModule_AddIntConstant(m, "PID_CHARCOUNT", PID_CHARCOUNT);
997 PyModule_AddIntConstant(m, "PID_APPNAME", PID_APPNAME);
998 PyModule_AddIntConstant(m, "PID_SECURITY", PID_SECURITY);
1000 MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL);
1001 if (!MSIError)
1002 return;
1003 PyModule_AddObject(m, "MSIError", MSIError);