blob: ae30acbc9b48d4c07f99f57e6a8986c85883daa5 [file] [log] [blame]
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001/* Helper library for MSI creation with Python.
Florent Xiclunac934f322010-09-03 23:47:32 +00002 * Copyright (C) 2005 Martin v. Löwis
Martin v. Löwisdd860ca2006-03-05 13:39:10 +00003 * Licensed to PSF under a contributor agreement.
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00004 */
5
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>
14
15static PyObject *MSIError;
16
17static PyObject*
18uuidcreate(PyObject* obj, PyObject*args)
19{
20 UUID result;
Victor Stinner9d3b93b2011-11-22 02:27:30 +010021 wchar_t *cresult;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000022 PyObject *oresult;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000024 /* 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) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result");
30 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000031 }
32
Martin v. Löwis371bb502008-08-16 13:02:57 +000033 if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen");
35 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000036 }
37
Victor Stinner9d3b93b2011-11-22 02:27:30 +010038 oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult));
Martin v. Löwis371bb502008-08-16 13:02:57 +000039 RpcStringFreeW(&cresult);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000040 return oresult;
41
42}
43
44/* FCI callback functions */
45
46static FNFCIALLOC(cb_alloc)
47{
48 return malloc(cb);
49}
50
51static FNFCIFREE(cb_free)
52{
53 free(memory);
54}
55
56static FNFCIOPEN(cb_open)
57{
Victor Stinnerdaf45552013-08-28 00:53:59 +020058 int result = _open(pszFile, oflag | O_NOINHERIT, pmode);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000059 if (result == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 *err = errno;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000061 return result;
62}
63
64static FNFCIREAD(cb_read)
65{
Victor Stinner67158282013-11-20 00:14:49 +010066 UINT result = (UINT)_read((int)hf, memory, cb);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000067 if (result != cb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 *err = errno;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000069 return result;
70}
71
72static FNFCIWRITE(cb_write)
73{
Victor Stinner67158282013-11-20 00:14:49 +010074 UINT result = (UINT)_write((int)hf, memory, cb);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000075 if (result != cb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 *err = errno;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000077 return result;
78}
79
80static FNFCICLOSE(cb_close)
81{
Victor Stinner67158282013-11-20 00:14:49 +010082 int result = _close((int)hf);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000083 if (result != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 *err = errno;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000085 return result;
86}
87
88static FNFCISEEK(cb_seek)
89{
Victor Stinner67158282013-11-20 00:14:49 +010090 long result = (long)_lseek((int)hf, dist, seektype);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000091 if (result == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 *err = errno;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +000093 return result;
94}
95
96static FNFCIDELETE(cb_delete)
97{
98 int result = remove(pszFile);
99 if (result != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 *err = errno;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000101 return result;
102}
103
104static FNFCIFILEPLACED(cb_fileplaced)
105{
106 return 0;
107}
108
109static FNFCIGETTEMPFILE(cb_gettempfile)
110{
111 char *name = _tempnam("", "tmp");
112 if ((name != NULL) && ((int)strlen(name) < cbTempName)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 strcpy(pszTempName, name);
114 free(name);
115 return TRUE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000116 }
117
118 if (name) free(name);
119 return FALSE;
120}
121
122static FNFCISTATUS(cb_status)
123{
124 if (pv) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200125 _Py_IDENTIFIER(status);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200126
127 PyObject *result = _PyObject_CallMethodId(pv, &PyId_status, "iii", typeStatus, cb1, cb2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (result == NULL)
129 return -1;
130 Py_DECREF(result);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000131 }
132 return 0;
133}
134
135static FNFCIGETNEXTCABINET(cb_getnextcabinet)
136{
137 if (pv) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200138 _Py_IDENTIFIER(getnextcabinet);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200139
140 PyObject *result = _PyObject_CallMethodId(pv, &PyId_getnextcabinet, "i", pccab->iCab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (result == NULL)
142 return -1;
143 if (!PyBytes_Check(result)) {
144 PyErr_Format(PyExc_TypeError,
145 "Incorrect return type %s from getnextcabinet",
146 result->ob_type->tp_name);
147 Py_DECREF(result);
148 return FALSE;
149 }
150 strncpy(pccab->szCab, PyBytes_AsString(result), sizeof(pccab->szCab));
151 return TRUE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000152 }
153 return FALSE;
154}
155
156static FNFCIGETOPENINFO(cb_getopeninfo)
157{
158 BY_HANDLE_FILE_INFORMATION bhfi;
159 FILETIME filetime;
160 HANDLE handle;
161
162 /* Need Win32 handle to get time stamps */
163 handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000165 if (handle == INVALID_HANDLE_VALUE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return -1;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000167
168 if (GetFileInformationByHandle(handle, &bhfi) == FALSE)
169 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 CloseHandle(handle);
171 return -1;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000172 }
173
174 FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime);
175 FileTimeToDosDateTime(&filetime, pdate, ptime);
176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 *pattribs = (int)(bhfi.dwFileAttributes &
178 (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000179
180 CloseHandle(handle);
181
Victor Stinnerdaf45552013-08-28 00:53:59 +0200182 return _open(pszName, _O_RDONLY | _O_BINARY | O_NOINHERIT);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000183}
184
185static PyObject* fcicreate(PyObject* obj, PyObject* args)
186{
Christian Heimes0bd4e112008-02-12 22:59:25 +0000187 char *cabname, *p;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000188 PyObject *files;
189 CCAB ccab;
190 HFCI hfci;
191 ERF erf;
Christian Heimes0bd4e112008-02-12 22:59:25 +0000192 Py_ssize_t i;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000193
194
195 if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000197
198 if (!PyList_Check(files)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 PyErr_SetString(PyExc_TypeError, "FCICreate expects a list");
200 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000201 }
202
203 ccab.cb = INT_MAX; /* no need to split CAB into multiple media */
204 ccab.cbFolderThresh = 1000000; /* flush directory after this many bytes */
205 ccab.cbReserveCFData = 0;
206 ccab.cbReserveCFFolder = 0;
207 ccab.cbReserveCFHeader = 0;
208
209 ccab.iCab = 1;
210 ccab.iDisk = 1;
211
212 ccab.setID = 0;
213 ccab.szDisk[0] = '\0';
214
Christian Heimes0bd4e112008-02-12 22:59:25 +0000215 for (i = 0, p = cabname; *p; p = CharNext(p))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (*p == '\\' || *p == '/')
217 i = p - cabname + 1;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000218
Christian Heimes0bd4e112008-02-12 22:59:25 +0000219 if (i >= sizeof(ccab.szCabPath) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 strlen(cabname+i) >= sizeof(ccab.szCab)) {
221 PyErr_SetString(PyExc_ValueError, "path name too long");
222 return 0;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000223 }
224
Christian Heimes0bd4e112008-02-12 22:59:25 +0000225 if (i > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 memcpy(ccab.szCabPath, cabname, i);
227 ccab.szCabPath[i] = '\0';
228 strcpy(ccab.szCab, cabname+i);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000229 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 strcpy(ccab.szCabPath, ".\\");
231 strcpy(ccab.szCab, cabname);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000232 }
233
234 hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete,
236 cb_gettempfile, &ccab, NULL);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000237
238 if (hfci == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper);
240 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000241 }
242
243 for (i=0; i < PyList_GET_SIZE(files); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyObject *item = PyList_GET_ITEM(files, i);
245 char *filename, *cabname;
Zachary Ware0a29e892015-05-18 00:47:15 -0500246
247 if (!PyArg_ParseTuple(item, "ss", &filename, &cabname)) {
248 PyErr_SetString(PyExc_TypeError, "FCICreate expects a list of tuples containing two strings");
249 FCIDestroy(hfci);
250 return NULL;
251 }
252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 if (!FCIAddFile(hfci, filename, cabname, FALSE,
254 cb_getnextcabinet, cb_status, cb_getopeninfo,
255 tcompTYPE_MSZIP))
256 goto err;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000257 }
258
259 if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 goto err;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000261
262 if (!FCIDestroy(hfci))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 goto err;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000264
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200265 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000266err:
Zachary Ware0a29e892015-05-18 00:47:15 -0500267 if(erf.fError)
268 PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); /* XXX better error type */
269 else
270 PyErr_SetString(PyExc_ValueError, "FCI general error");
271
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000272 FCIDestroy(hfci);
273 return NULL;
274}
275
276typedef struct msiobj{
277 PyObject_HEAD
278 MSIHANDLE h;
279}msiobj;
280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281static void
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000282msiobj_dealloc(msiobj* msidb)
283{
284 MsiCloseHandle(msidb->h);
285 msidb->h = 0;
Zackery Spytzcb04f752017-11-07 03:03:09 -0700286 PyObject_Del(msidb);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000287}
288
289static PyObject*
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000290msierror(int status)
291{
292 int code;
293 char buf[2000];
294 char *res = buf;
295 DWORD size = sizeof(buf);
296 MSIHANDLE err = MsiGetLastErrorRecord();
297
298 if (err == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 switch(status) {
300 case ERROR_ACCESS_DENIED:
301 PyErr_SetString(MSIError, "access denied");
302 return NULL;
303 case ERROR_FUNCTION_FAILED:
304 PyErr_SetString(MSIError, "function failed");
305 return NULL;
306 case ERROR_INVALID_DATA:
307 PyErr_SetString(MSIError, "invalid data");
308 return NULL;
309 case ERROR_INVALID_HANDLE:
310 PyErr_SetString(MSIError, "invalid handle");
311 return NULL;
312 case ERROR_INVALID_STATE:
313 PyErr_SetString(MSIError, "invalid state");
314 return NULL;
315 case ERROR_INVALID_PARAMETER:
316 PyErr_SetString(MSIError, "invalid parameter");
317 return NULL;
Berker Peksag4864a612017-11-24 12:53:58 +0300318 case ERROR_OPEN_FAILED:
319 PyErr_SetString(MSIError, "open failed");
320 return NULL;
321 case ERROR_CREATE_FAILED:
322 PyErr_SetString(MSIError, "create failed");
323 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 default:
325 PyErr_Format(MSIError, "unknown error %x", status);
326 return NULL;
327 }
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000328 }
329
330 code = MsiRecordGetInteger(err, 1); /* XXX code */
331 if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 res = malloc(size+1);
Zackery Spytz4e519372018-09-07 16:02:56 -0600333 if (res == NULL) {
334 MsiCloseHandle(err);
335 return PyErr_NoMemory();
336 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 MsiFormatRecord(0, err, res, &size);
338 res[size]='\0';
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000339 }
340 MsiCloseHandle(err);
341 PyErr_SetString(MSIError, res);
342 if (res != buf)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 free(res);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000344 return NULL;
345}
346
Berker Peksaga9356542017-11-07 15:58:53 +0300347static PyObject*
348msidb_close(msiobj* msidb, PyObject *args)
349{
350 int status;
351 if ((status = MsiCloseHandle(msidb->h)) != ERROR_SUCCESS) {
352 return msierror(status);
353 }
354 msidb->h = 0;
355 Py_RETURN_NONE;
356}
357
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000358/*************************** Record objects **********************/
359
360static PyObject*
361record_getfieldcount(msiobj* record, PyObject* args)
362{
Christian Heimes217cfd12007-12-02 14:31:20 +0000363 return PyLong_FromLong(MsiRecordGetFieldCount(record->h));
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000364}
365
366static PyObject*
Martin v. Löwise95593e2008-06-02 10:08:54 +0000367record_getinteger(msiobj* record, PyObject* args)
368{
369 unsigned int field;
370 int status;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371
Martin v. Löwise95593e2008-06-02 10:08:54 +0000372 if (!PyArg_ParseTuple(args, "I:GetInteger", &field))
373 return NULL;
374 status = MsiRecordGetInteger(record->h, field);
375 if (status == MSI_NULL_INTEGER){
376 PyErr_SetString(MSIError, "could not convert record field to integer");
377 return NULL;
378 }
Martin v. Löwis704d8b12008-06-02 11:32:23 +0000379 return PyLong_FromLong((long) status);
Martin v. Löwise95593e2008-06-02 10:08:54 +0000380}
381
382static PyObject*
383record_getstring(msiobj* record, PyObject* args)
384{
385 unsigned int field;
386 unsigned int status;
Martin v. Löwis371bb502008-08-16 13:02:57 +0000387 WCHAR buf[2000];
388 WCHAR *res = buf;
Martin v. Löwise95593e2008-06-02 10:08:54 +0000389 DWORD size = sizeof(buf);
390 PyObject* string;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391
Martin v. Löwise95593e2008-06-02 10:08:54 +0000392 if (!PyArg_ParseTuple(args, "I:GetString", &field))
393 return NULL;
Martin v. Löwis371bb502008-08-16 13:02:57 +0000394 status = MsiRecordGetStringW(record->h, field, res, &size);
Martin v. Löwise95593e2008-06-02 10:08:54 +0000395 if (status == ERROR_MORE_DATA) {
Martin v. Löwis371bb502008-08-16 13:02:57 +0000396 res = (WCHAR*) malloc((size + 1)*sizeof(WCHAR));
Martin v. Löwise95593e2008-06-02 10:08:54 +0000397 if (res == NULL)
398 return PyErr_NoMemory();
Martin v. Löwis371bb502008-08-16 13:02:57 +0000399 status = MsiRecordGetStringW(record->h, field, res, &size);
Martin v. Löwise95593e2008-06-02 10:08:54 +0000400 }
401 if (status != ERROR_SUCCESS)
402 return msierror((int) status);
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100403 string = PyUnicode_FromWideChar(res, size);
Martin v. Löwise95593e2008-06-02 10:08:54 +0000404 if (buf != res)
405 free(res);
406 return string;
407}
408
409static PyObject*
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000410record_cleardata(msiobj* record, PyObject *args)
411{
412 int status = MsiRecordClearData(record->h);
413 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000415
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200416 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000417}
418
419static PyObject*
420record_setstring(msiobj* record, PyObject *args)
421{
422 int status;
423 int field;
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100424 wchar_t *data;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000425
Martin v. Löwis371bb502008-08-16 13:02:57 +0000426 if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000428
Martin v. Löwis371bb502008-08-16 13:02:57 +0000429 if ((status = MsiRecordSetStringW(record->h, field, data)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000431
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200432 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000433}
434
435static PyObject*
436record_setstream(msiobj* record, PyObject *args)
437{
438 int status;
439 int field;
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100440 wchar_t *data;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000441
Martin v. Löwis371bb502008-08-16 13:02:57 +0000442 if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000444
Martin v. Löwis371bb502008-08-16 13:02:57 +0000445 if ((status = MsiRecordSetStreamW(record->h, field, data)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000447
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200448 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000449}
450
451static PyObject*
452record_setinteger(msiobj* record, PyObject *args)
453{
454 int status;
455 int field;
456 int data;
457
458 if (!PyArg_ParseTuple(args, "ii:SetInteger", &field, &data))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000460
461 if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000463
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200464 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000465}
466
467
468
469static PyMethodDef record_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS,
471 PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")},
Martin v. Löwise95593e2008-06-02 10:08:54 +0000472 { "GetInteger", (PyCFunction)record_getinteger, METH_VARARGS,
473 PyDoc_STR("GetInteger(field) -> int\nWraps MsiRecordGetInteger")},
474 { "GetString", (PyCFunction)record_getstring, METH_VARARGS,
475 PyDoc_STR("GetString(field) -> string\nWraps MsiRecordGetString")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 { "SetString", (PyCFunction)record_setstring, METH_VARARGS,
477 PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")},
478 { "SetStream", (PyCFunction)record_setstream, METH_VARARGS,
479 PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")},
480 { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS,
481 PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")},
482 { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS,
483 PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000484 { NULL, NULL }
485};
486
487static PyTypeObject record_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyVarObject_HEAD_INIT(NULL, 0)
489 "_msi.Record", /*tp_name*/
490 sizeof(msiobj), /*tp_basicsize*/
491 0, /*tp_itemsize*/
492 /* methods */
493 (destructor)msiobj_dealloc, /*tp_dealloc*/
494 0, /*tp_print*/
495 0, /*tp_getattr*/
496 0, /*tp_setattr*/
497 0, /*tp_reserved*/
498 0, /*tp_repr*/
499 0, /*tp_as_number*/
500 0, /*tp_as_sequence*/
501 0, /*tp_as_mapping*/
502 0, /*tp_hash*/
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000503 0, /*tp_call*/
504 0, /*tp_str*/
505 PyObject_GenericGetAttr,/*tp_getattro*/
506 PyObject_GenericSetAttr,/*tp_setattro*/
507 0, /*tp_as_buffer*/
508 Py_TPFLAGS_DEFAULT, /*tp_flags*/
509 0, /*tp_doc*/
510 0, /*tp_traverse*/
511 0, /*tp_clear*/
512 0, /*tp_richcompare*/
513 0, /*tp_weaklistoffset*/
514 0, /*tp_iter*/
515 0, /*tp_iternext*/
516 record_methods, /*tp_methods*/
517 0, /*tp_members*/
518 0, /*tp_getset*/
519 0, /*tp_base*/
520 0, /*tp_dict*/
521 0, /*tp_descr_get*/
522 0, /*tp_descr_set*/
523 0, /*tp_dictoffset*/
524 0, /*tp_init*/
525 0, /*tp_alloc*/
526 0, /*tp_new*/
527 0, /*tp_free*/
528 0, /*tp_is_gc*/
529};
530
531static PyObject*
532record_new(MSIHANDLE h)
533{
534 msiobj *result = PyObject_NEW(struct msiobj, &record_Type);
535
536 if (!result) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 MsiCloseHandle(h);
538 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000539 }
540
541 result->h = h;
542 return (PyObject*)result;
543}
544
545/*************************** SummaryInformation objects **************/
546
547static PyObject*
548summary_getproperty(msiobj* si, PyObject *args)
549{
550 int status;
551 int field;
552 PyObject *result;
553 UINT type;
554 INT ival;
555 FILETIME fval;
556 char sbuf[1000];
557 char *sval = sbuf;
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800558 DWORD ssize = sizeof(sbuf);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000559
560 if (!PyArg_ParseTuple(args, "i:GetProperty", &field))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
564 &fval, sval, &ssize);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565 if (status == ERROR_MORE_DATA) {
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800566 ssize++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 sval = malloc(ssize);
Zackery Spytz4e519372018-09-07 16:02:56 -0600568 if (sval == NULL) {
569 return PyErr_NoMemory();
570 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
572 &fval, sval, &ssize);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000573 }
574
575 switch(type) {
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800576 case VT_I2:
577 case VT_I4:
578 result = PyLong_FromLong(ival);
579 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 case VT_FILETIME:
581 PyErr_SetString(PyExc_NotImplementedError, "FILETIME result");
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800582 result = NULL;
583 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 case VT_LPSTR:
585 result = PyBytes_FromStringAndSize(sval, ssize);
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800586 break;
Berker Peksag19fb1342017-11-24 18:11:18 +0300587 case VT_EMPTY:
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800588 Py_INCREF(Py_None);
589 result = Py_None;
590 break;
591 default:
592 PyErr_Format(PyExc_NotImplementedError, "result of type %d", type);
593 result = NULL;
594 break;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000595 }
Tzu-ping Chung2de576e2019-02-03 01:13:23 +0800596 if (sval != sbuf)
597 free(sval);
598 return result;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000599}
600
601static PyObject*
602summary_getpropertycount(msiobj* si, PyObject *args)
603{
604 int status;
605 UINT result;
606
607 status = MsiSummaryInfoGetPropertyCount(si->h, &result);
608 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000610
Christian Heimes217cfd12007-12-02 14:31:20 +0000611 return PyLong_FromLong(result);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000612}
613
614static PyObject*
615summary_setproperty(msiobj* si, PyObject *args)
616{
617 int status;
618 int field;
619 PyObject* data;
620
621 if (!PyArg_ParseTuple(args, "iO:SetProperty", &field, &data))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000623
Martin v. Löwis371bb502008-08-16 13:02:57 +0000624 if (PyUnicode_Check(data)) {
Serhiy Storchakaccdc09e2017-06-28 09:55:22 +0300625 const WCHAR *value = _PyUnicode_AsUnicode(data);
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300626 if (value == NULL) {
627 return NULL;
628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 status = MsiSummaryInfoSetPropertyW(si->h, field, VT_LPSTR,
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300630 0, NULL, value);
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000631 } else if (PyLong_CheckExact(data)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 long value = PyLong_AsLong(data);
633 if (value == -1 && PyErr_Occurred()) {
634 return NULL;
635 }
636 status = MsiSummaryInfoSetProperty(si->h, field, VT_I4,
637 value, NULL, NULL);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000638 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyErr_SetString(PyExc_TypeError, "unsupported type");
640 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000641 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000643 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000645
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200646 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000647}
648
649
650static PyObject*
651summary_persist(msiobj* si, PyObject *args)
652{
653 int status;
654
655 status = MsiSummaryInfoPersist(si->h);
656 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return msierror(status);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200658 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000659}
660
661static PyMethodDef summary_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS,
663 PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")},
664 { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS,
665 PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")},
666 { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS,
667 PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")},
668 { "Persist", (PyCFunction)summary_persist, METH_NOARGS,
669 PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000670 { NULL, NULL }
671};
672
673static PyTypeObject summary_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 PyVarObject_HEAD_INIT(NULL, 0)
675 "_msi.SummaryInformation", /*tp_name*/
676 sizeof(msiobj), /*tp_basicsize*/
677 0, /*tp_itemsize*/
678 /* methods */
679 (destructor)msiobj_dealloc, /*tp_dealloc*/
680 0, /*tp_print*/
681 0, /*tp_getattr*/
682 0, /*tp_setattr*/
683 0, /*tp_reserved*/
684 0, /*tp_repr*/
685 0, /*tp_as_number*/
686 0, /*tp_as_sequence*/
687 0, /*tp_as_mapping*/
688 0, /*tp_hash*/
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000689 0, /*tp_call*/
690 0, /*tp_str*/
691 PyObject_GenericGetAttr,/*tp_getattro*/
692 PyObject_GenericSetAttr,/*tp_setattro*/
693 0, /*tp_as_buffer*/
694 Py_TPFLAGS_DEFAULT, /*tp_flags*/
695 0, /*tp_doc*/
696 0, /*tp_traverse*/
697 0, /*tp_clear*/
698 0, /*tp_richcompare*/
699 0, /*tp_weaklistoffset*/
700 0, /*tp_iter*/
701 0, /*tp_iternext*/
702 summary_methods, /*tp_methods*/
703 0, /*tp_members*/
704 0, /*tp_getset*/
705 0, /*tp_base*/
706 0, /*tp_dict*/
707 0, /*tp_descr_get*/
708 0, /*tp_descr_set*/
709 0, /*tp_dictoffset*/
710 0, /*tp_init*/
711 0, /*tp_alloc*/
712 0, /*tp_new*/
713 0, /*tp_free*/
714 0, /*tp_is_gc*/
715};
716
717/*************************** View objects **************/
718
719static PyObject*
720view_execute(msiobj *view, PyObject*args)
721{
722 int status;
723 MSIHANDLE params = 0;
724 PyObject *oparams = Py_None;
725
726 if (!PyArg_ParseTuple(args, "O:Execute", &oparams))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000728
729 if (oparams != Py_None) {
730 if (oparams->ob_type != &record_Type) {
731 PyErr_SetString(PyExc_TypeError, "Execute argument must be a record");
732 return NULL;
733 }
734 params = ((msiobj*)oparams)->h;
735 }
736
737 status = MsiViewExecute(view->h, params);
738 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000740
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200741 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000742}
743
744static PyObject*
745view_fetch(msiobj *view, PyObject*args)
746{
747 int status;
748 MSIHANDLE result;
749
Berker Peksagbdb83152017-11-23 15:47:30 +0300750 status = MsiViewFetch(view->h, &result);
751 if (status == ERROR_NO_MORE_ITEMS) {
752 Py_RETURN_NONE;
753 } else if (status != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 return msierror(status);
Berker Peksagbdb83152017-11-23 15:47:30 +0300755 }
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000756
757 return record_new(result);
758}
759
760static PyObject*
761view_getcolumninfo(msiobj *view, PyObject *args)
762{
763 int status;
764 int kind;
765 MSIHANDLE result;
766
767 if (!PyArg_ParseTuple(args, "i:GetColumnInfo", &kind))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000769
770 if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000772
773 return record_new(result);
774}
775
776static PyObject*
777view_modify(msiobj *view, PyObject *args)
778{
779 int kind;
780 PyObject *data;
781 int status;
782
783 if (!PyArg_ParseTuple(args, "iO:Modify", &kind, &data))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000785
786 if (data->ob_type != &record_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyErr_SetString(PyExc_TypeError, "Modify expects a record object");
788 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000789 }
790
791 if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000793
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200794 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000795}
796
797static PyObject*
798view_close(msiobj *view, PyObject*args)
799{
800 int status;
801
802 if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000804
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200805 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000806}
807
808static PyMethodDef view_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 { "Execute", (PyCFunction)view_execute, METH_VARARGS,
810 PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000811 { "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000813 { "Fetch", (PyCFunction)view_fetch, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000815 { "Modify", (PyCFunction)view_modify, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000817 { "Close", (PyCFunction)view_close, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 PyDoc_STR("Close() -> result\nWraps MsiViewClose")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000819 { NULL, NULL }
820};
821
822static PyTypeObject msiview_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyVarObject_HEAD_INIT(NULL, 0)
824 "_msi.View", /*tp_name*/
825 sizeof(msiobj), /*tp_basicsize*/
826 0, /*tp_itemsize*/
827 /* methods */
828 (destructor)msiobj_dealloc, /*tp_dealloc*/
829 0, /*tp_print*/
830 0, /*tp_getattr*/
831 0, /*tp_setattr*/
832 0, /*tp_reserved*/
833 0, /*tp_repr*/
834 0, /*tp_as_number*/
835 0, /*tp_as_sequence*/
836 0, /*tp_as_mapping*/
837 0, /*tp_hash*/
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000838 0, /*tp_call*/
839 0, /*tp_str*/
840 PyObject_GenericGetAttr,/*tp_getattro*/
841 PyObject_GenericSetAttr,/*tp_setattro*/
842 0, /*tp_as_buffer*/
843 Py_TPFLAGS_DEFAULT, /*tp_flags*/
844 0, /*tp_doc*/
845 0, /*tp_traverse*/
846 0, /*tp_clear*/
847 0, /*tp_richcompare*/
848 0, /*tp_weaklistoffset*/
849 0, /*tp_iter*/
850 0, /*tp_iternext*/
851 view_methods, /*tp_methods*/
852 0, /*tp_members*/
853 0, /*tp_getset*/
854 0, /*tp_base*/
855 0, /*tp_dict*/
856 0, /*tp_descr_get*/
857 0, /*tp_descr_set*/
858 0, /*tp_dictoffset*/
859 0, /*tp_init*/
860 0, /*tp_alloc*/
861 0, /*tp_new*/
862 0, /*tp_free*/
863 0, /*tp_is_gc*/
864};
865
866/*************************** Database objects **************/
867
868static PyObject*
869msidb_openview(msiobj *msidb, PyObject *args)
870{
871 int status;
872 char *sql;
873 MSIHANDLE hView;
874 msiobj *result;
875
876 if (!PyArg_ParseTuple(args, "s:OpenView", &sql))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000878
879 if ((status = MsiDatabaseOpenView(msidb->h, sql, &hView)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000881
882 result = PyObject_NEW(struct msiobj, &msiview_Type);
883 if (!result) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 MsiCloseHandle(hView);
885 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000886 }
887
888 result->h = hView;
889 return (PyObject*)result;
890}
891
892static PyObject*
893msidb_commit(msiobj *msidb, PyObject *args)
894{
895 int status;
896
897 if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000899
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200900 Py_RETURN_NONE;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000901}
902
903static PyObject*
904msidb_getsummaryinformation(msiobj *db, PyObject *args)
905{
906 int status;
907 int count;
908 MSIHANDLE result;
909 msiobj *oresult;
910
911 if (!PyArg_ParseTuple(args, "i:GetSummaryInformation", &count))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000913
914 status = MsiGetSummaryInformation(db->h, NULL, count, &result);
915 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000917
918 oresult = PyObject_NEW(struct msiobj, &summary_Type);
Zackery Spytzbf94cc72019-03-07 11:20:13 -0700919 if (!oresult) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 MsiCloseHandle(result);
921 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000922 }
923
924 oresult->h = result;
925 return (PyObject*)oresult;
926}
927
928static PyMethodDef db_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS,
930 PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000931 { "Commit", (PyCFunction)msidb_commit, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")},
933 { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS,
934 PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")},
Berker Peksaga9356542017-11-07 15:58:53 +0300935 { "Close", (PyCFunction)msidb_close, METH_NOARGS,
936 PyDoc_STR("Close() -> None\nWraps MsiCloseHandle")},
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000937 { NULL, NULL }
938};
939
940static PyTypeObject msidb_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyVarObject_HEAD_INIT(NULL, 0)
942 "_msi.Database", /*tp_name*/
943 sizeof(msiobj), /*tp_basicsize*/
944 0, /*tp_itemsize*/
945 /* methods */
946 (destructor)msiobj_dealloc, /*tp_dealloc*/
947 0, /*tp_print*/
948 0, /*tp_getattr*/
949 0, /*tp_setattr*/
950 0, /*tp_reserved*/
951 0, /*tp_repr*/
952 0, /*tp_as_number*/
953 0, /*tp_as_sequence*/
954 0, /*tp_as_mapping*/
955 0, /*tp_hash*/
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000956 0, /*tp_call*/
957 0, /*tp_str*/
958 PyObject_GenericGetAttr,/*tp_getattro*/
959 PyObject_GenericSetAttr,/*tp_setattro*/
960 0, /*tp_as_buffer*/
961 Py_TPFLAGS_DEFAULT, /*tp_flags*/
962 0, /*tp_doc*/
963 0, /*tp_traverse*/
964 0, /*tp_clear*/
965 0, /*tp_richcompare*/
966 0, /*tp_weaklistoffset*/
967 0, /*tp_iter*/
968 0, /*tp_iternext*/
969 db_methods, /*tp_methods*/
970 0, /*tp_members*/
971 0, /*tp_getset*/
972 0, /*tp_base*/
973 0, /*tp_dict*/
974 0, /*tp_descr_get*/
975 0, /*tp_descr_set*/
976 0, /*tp_dictoffset*/
977 0, /*tp_init*/
978 0, /*tp_alloc*/
979 0, /*tp_new*/
980 0, /*tp_free*/
981 0, /*tp_is_gc*/
982};
983
Steve Dower6ceda632016-09-09 11:56:34 -0700984#define Py_NOT_PERSIST(x, flag) \
Segev Finer679b5662017-07-27 01:17:57 +0300985 (x != (SIZE_T)(flag) && \
986 x != ((SIZE_T)(flag) | MSIDBOPEN_PATCHFILE))
Steve Dower6ceda632016-09-09 11:56:34 -0700987
988#define Py_INVALID_PERSIST(x) \
989 (Py_NOT_PERSIST(x, MSIDBOPEN_READONLY) && \
990 Py_NOT_PERSIST(x, MSIDBOPEN_TRANSACT) && \
991 Py_NOT_PERSIST(x, MSIDBOPEN_DIRECT) && \
992 Py_NOT_PERSIST(x, MSIDBOPEN_CREATE) && \
993 Py_NOT_PERSIST(x, MSIDBOPEN_CREATEDIRECT))
994
Martin v. Löwisfbab90e2006-03-05 13:36:04 +0000995static PyObject* msiopendb(PyObject *obj, PyObject *args)
996{
997 int status;
998 char *path;
999 int persist;
1000 MSIHANDLE h;
1001 msiobj *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist))
1003 return NULL;
Steve Dower6ceda632016-09-09 11:56:34 -07001004 /* We need to validate that persist is a valid MSIDBOPEN_* value. Otherwise,
1005 MsiOpenDatabase may treat the value as a pointer, leading to unexpected
1006 behavior. */
1007 if (Py_INVALID_PERSIST(persist))
1008 return msierror(ERROR_INVALID_PARAMETER);
Segev Finer679b5662017-07-27 01:17:57 +03001009 status = MsiOpenDatabase(path, (LPCSTR)(SIZE_T)persist, &h);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001010 if (status != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return msierror(status);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001012
1013 result = PyObject_NEW(struct msiobj, &msidb_Type);
1014 if (!result) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 MsiCloseHandle(h);
1016 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001017 }
1018 result->h = h;
1019 return (PyObject*)result;
1020}
1021
1022static PyObject*
1023createrecord(PyObject *o, PyObject *args)
1024{
1025 int count;
1026 MSIHANDLE h;
1027
1028 if (!PyArg_ParseTuple(args, "i:CreateRecord", &count))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 return NULL;
1030
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001031 h = MsiCreateRecord(count);
1032 if (h == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 return msierror(0);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001034
1035 return record_new(h);
1036}
1037
1038
1039static PyMethodDef msi_methods[] = {
1040 {"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyDoc_STR("UuidCreate() -> string")},
1042 {"FCICreate", (PyCFunction)fcicreate, METH_VARARGS,
1043 PyDoc_STR("fcicreate(cabname,files) -> None")},
1044 {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS,
1045 PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")},
1046 {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS,
1047 PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")},
1048 {NULL, NULL} /* sentinel */
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001049};
1050
1051static char msi_doc[] = "Documentation";
1052
Martin v. Löwis1a214512008-06-11 05:26:20 +00001053
1054static struct PyModuleDef _msimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyModuleDef_HEAD_INIT,
1056 "_msi",
1057 msi_doc,
1058 -1,
1059 msi_methods,
1060 NULL,
1061 NULL,
1062 NULL,
1063 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001064};
1065
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001066PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001067PyInit__msi(void)
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001068{
1069 PyObject *m;
1070
Martin v. Löwis1a214512008-06-11 05:26:20 +00001071 m = PyModule_Create(&_msimodule);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001072 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001074
Segev Finer679b5662017-07-27 01:17:57 +03001075 PyModule_AddIntConstant(m, "MSIDBOPEN_CREATEDIRECT", (long)(SIZE_T)MSIDBOPEN_CREATEDIRECT);
1076 PyModule_AddIntConstant(m, "MSIDBOPEN_CREATE", (long)(SIZE_T)MSIDBOPEN_CREATE);
1077 PyModule_AddIntConstant(m, "MSIDBOPEN_DIRECT", (long)(SIZE_T)MSIDBOPEN_DIRECT);
1078 PyModule_AddIntConstant(m, "MSIDBOPEN_READONLY", (long)(SIZE_T)MSIDBOPEN_READONLY);
1079 PyModule_AddIntConstant(m, "MSIDBOPEN_TRANSACT", (long)(SIZE_T)MSIDBOPEN_TRANSACT);
1080 PyModule_AddIntConstant(m, "MSIDBOPEN_PATCHFILE", (long)(SIZE_T)MSIDBOPEN_PATCHFILE);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001081
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001082 PyModule_AddIntMacro(m, MSICOLINFO_NAMES);
1083 PyModule_AddIntMacro(m, MSICOLINFO_TYPES);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001084
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001085 PyModule_AddIntMacro(m, MSIMODIFY_SEEK);
1086 PyModule_AddIntMacro(m, MSIMODIFY_REFRESH);
1087 PyModule_AddIntMacro(m, MSIMODIFY_INSERT);
1088 PyModule_AddIntMacro(m, MSIMODIFY_UPDATE);
1089 PyModule_AddIntMacro(m, MSIMODIFY_ASSIGN);
1090 PyModule_AddIntMacro(m, MSIMODIFY_REPLACE);
1091 PyModule_AddIntMacro(m, MSIMODIFY_MERGE);
1092 PyModule_AddIntMacro(m, MSIMODIFY_DELETE);
1093 PyModule_AddIntMacro(m, MSIMODIFY_INSERT_TEMPORARY);
1094 PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE);
1095 PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE_NEW);
1096 PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE_FIELD);
1097 PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE_DELETE);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001098
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001099 PyModule_AddIntMacro(m, PID_CODEPAGE);
1100 PyModule_AddIntMacro(m, PID_TITLE);
1101 PyModule_AddIntMacro(m, PID_SUBJECT);
1102 PyModule_AddIntMacro(m, PID_AUTHOR);
1103 PyModule_AddIntMacro(m, PID_KEYWORDS);
1104 PyModule_AddIntMacro(m, PID_COMMENTS);
1105 PyModule_AddIntMacro(m, PID_TEMPLATE);
1106 PyModule_AddIntMacro(m, PID_LASTAUTHOR);
1107 PyModule_AddIntMacro(m, PID_REVNUMBER);
1108 PyModule_AddIntMacro(m, PID_LASTPRINTED);
1109 PyModule_AddIntMacro(m, PID_CREATE_DTM);
1110 PyModule_AddIntMacro(m, PID_LASTSAVE_DTM);
1111 PyModule_AddIntMacro(m, PID_PAGECOUNT);
1112 PyModule_AddIntMacro(m, PID_WORDCOUNT);
1113 PyModule_AddIntMacro(m, PID_CHARCOUNT);
1114 PyModule_AddIntMacro(m, PID_APPNAME);
1115 PyModule_AddIntMacro(m, PID_SECURITY);
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001116
1117 MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL);
1118 if (!MSIError)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 return NULL;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001120 PyModule_AddObject(m, "MSIError", MSIError);
Amaury Forgeot d'Arcbf9e9662008-06-17 21:39:46 +00001121 return m;
Martin v. Löwisfbab90e2006-03-05 13:36:04 +00001122}