Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1 | /* Helper library for MSI creation with Python. |
Florent Xicluna | c934f32 | 2010-09-03 23:47:32 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005 Martin v. Löwis |
Martin v. Löwis | dd860ca | 2006-03-05 13:39:10 +0000 | [diff] [blame] | 3 | * Licensed to PSF under a contributor agreement. |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 4 | */ |
| 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 | |
| 15 | static PyObject *MSIError; |
| 16 | |
| 17 | static PyObject* |
| 18 | uuidcreate(PyObject* obj, PyObject*args) |
| 19 | { |
| 20 | UUID result; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 21 | wchar_t *cresult; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 22 | PyObject *oresult; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 23 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 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) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result"); |
| 30 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 33 | if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen"); |
| 35 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 38 | oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult)); |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 39 | RpcStringFreeW(&cresult); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 40 | return oresult; |
| 41 | |
| 42 | } |
| 43 | |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 44 | /* Helper for converting file names from UTF-8 to wchat_t*. */ |
| 45 | static wchar_t * |
| 46 | utf8_to_wchar(const char *s, int *err) |
| 47 | { |
| 48 | PyObject *obj = PyUnicode_FromString(s); |
| 49 | if (obj == NULL) { |
| 50 | if (PyErr_ExceptionMatches(PyExc_MemoryError)) { |
| 51 | *err = ENOMEM; |
| 52 | } |
| 53 | else { |
| 54 | *err = EINVAL; |
| 55 | } |
| 56 | PyErr_Clear(); |
| 57 | return NULL; |
| 58 | } |
| 59 | wchar_t *ws = PyUnicode_AsWideCharString(obj, NULL); |
| 60 | if (ws == NULL) { |
| 61 | *err = ENOMEM; |
| 62 | PyErr_Clear(); |
| 63 | } |
| 64 | Py_DECREF(obj); |
| 65 | return ws; |
| 66 | } |
| 67 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 68 | /* FCI callback functions */ |
| 69 | |
| 70 | static FNFCIALLOC(cb_alloc) |
| 71 | { |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 72 | return PyMem_RawMalloc(cb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static FNFCIFREE(cb_free) |
| 76 | { |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 77 | PyMem_RawFree(memory); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static FNFCIOPEN(cb_open) |
| 81 | { |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 82 | wchar_t *ws = utf8_to_wchar(pszFile, err); |
| 83 | if (ws == NULL) { |
| 84 | return -1; |
| 85 | } |
| 86 | int result = _wopen(ws, oflag | O_NOINHERIT, pmode); |
| 87 | PyMem_Free(ws); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 88 | if (result == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 90 | return result; |
| 91 | } |
| 92 | |
| 93 | static FNFCIREAD(cb_read) |
| 94 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 95 | UINT result = (UINT)_read((int)hf, memory, cb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 96 | if (result != cb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 98 | return result; |
| 99 | } |
| 100 | |
| 101 | static FNFCIWRITE(cb_write) |
| 102 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 103 | UINT result = (UINT)_write((int)hf, memory, cb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 104 | if (result != cb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 106 | return result; |
| 107 | } |
| 108 | |
| 109 | static FNFCICLOSE(cb_close) |
| 110 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 111 | int result = _close((int)hf); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 112 | if (result != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 114 | return result; |
| 115 | } |
| 116 | |
| 117 | static FNFCISEEK(cb_seek) |
| 118 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 119 | long result = (long)_lseek((int)hf, dist, seektype); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 120 | if (result == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 122 | return result; |
| 123 | } |
| 124 | |
| 125 | static FNFCIDELETE(cb_delete) |
| 126 | { |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 127 | wchar_t *ws = utf8_to_wchar(pszFile, err); |
| 128 | if (ws == NULL) { |
| 129 | return -1; |
| 130 | } |
| 131 | int result = _wremove(ws); |
| 132 | PyMem_Free(ws); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 133 | if (result != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 135 | return result; |
| 136 | } |
| 137 | |
| 138 | static FNFCIFILEPLACED(cb_fileplaced) |
| 139 | { |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static FNFCIGETTEMPFILE(cb_gettempfile) |
| 144 | { |
| 145 | char *name = _tempnam("", "tmp"); |
| 146 | if ((name != NULL) && ((int)strlen(name) < cbTempName)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | strcpy(pszTempName, name); |
| 148 | free(name); |
| 149 | return TRUE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (name) free(name); |
| 153 | return FALSE; |
| 154 | } |
| 155 | |
| 156 | static FNFCISTATUS(cb_status) |
| 157 | { |
| 158 | if (pv) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 159 | _Py_IDENTIFIER(status); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 160 | |
| 161 | PyObject *result = _PyObject_CallMethodId(pv, &PyId_status, "iii", typeStatus, cb1, cb2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | if (result == NULL) |
| 163 | return -1; |
| 164 | Py_DECREF(result); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static FNFCIGETNEXTCABINET(cb_getnextcabinet) |
| 170 | { |
| 171 | if (pv) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 172 | _Py_IDENTIFIER(getnextcabinet); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 173 | |
| 174 | PyObject *result = _PyObject_CallMethodId(pv, &PyId_getnextcabinet, "i", pccab->iCab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | if (result == NULL) |
| 176 | return -1; |
| 177 | if (!PyBytes_Check(result)) { |
| 178 | PyErr_Format(PyExc_TypeError, |
| 179 | "Incorrect return type %s from getnextcabinet", |
| 180 | result->ob_type->tp_name); |
| 181 | Py_DECREF(result); |
| 182 | return FALSE; |
| 183 | } |
| 184 | strncpy(pccab->szCab, PyBytes_AsString(result), sizeof(pccab->szCab)); |
| 185 | return TRUE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 186 | } |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
| 190 | static FNFCIGETOPENINFO(cb_getopeninfo) |
| 191 | { |
| 192 | BY_HANDLE_FILE_INFORMATION bhfi; |
| 193 | FILETIME filetime; |
| 194 | HANDLE handle; |
| 195 | |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 196 | wchar_t *ws = utf8_to_wchar(pszName, err); |
| 197 | if (ws == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | return -1; |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 199 | } |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 200 | |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 201 | /* Need Win32 handle to get time stamps */ |
| 202 | handle = CreateFileW(ws, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 203 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 204 | if (handle == INVALID_HANDLE_VALUE) { |
| 205 | PyMem_Free(ws); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | if (GetFileInformationByHandle(handle, &bhfi) == FALSE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | CloseHandle(handle); |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 211 | PyMem_Free(ws); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | return -1; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime); |
| 216 | FileTimeToDosDateTime(&filetime, pdate, ptime); |
| 217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | *pattribs = (int)(bhfi.dwFileAttributes & |
| 219 | (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH)); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 220 | |
| 221 | CloseHandle(handle); |
| 222 | |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 223 | int result = _wopen(ws, _O_RDONLY | _O_BINARY | O_NOINHERIT); |
| 224 | PyMem_Free(ws); |
| 225 | return result; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static PyObject* fcicreate(PyObject* obj, PyObject* args) |
| 229 | { |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 230 | char *cabname, *p; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 231 | PyObject *files; |
| 232 | CCAB ccab; |
| 233 | HFCI hfci; |
| 234 | ERF erf; |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 235 | Py_ssize_t i; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 236 | |
| 237 | |
| 238 | if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 240 | |
| 241 | if (!PyList_Check(files)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | PyErr_SetString(PyExc_TypeError, "FCICreate expects a list"); |
| 243 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | ccab.cb = INT_MAX; /* no need to split CAB into multiple media */ |
| 247 | ccab.cbFolderThresh = 1000000; /* flush directory after this many bytes */ |
| 248 | ccab.cbReserveCFData = 0; |
| 249 | ccab.cbReserveCFFolder = 0; |
| 250 | ccab.cbReserveCFHeader = 0; |
| 251 | |
| 252 | ccab.iCab = 1; |
| 253 | ccab.iDisk = 1; |
| 254 | |
| 255 | ccab.setID = 0; |
| 256 | ccab.szDisk[0] = '\0'; |
| 257 | |
Serhiy Storchaka | ba67d73 | 2020-06-30 11:56:03 +0300 | [diff] [blame^] | 258 | for (i = 0, p = cabname; *p; p++) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | if (*p == '\\' || *p == '/') |
| 260 | i = p - cabname + 1; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 261 | |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 262 | if (i >= sizeof(ccab.szCabPath) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | strlen(cabname+i) >= sizeof(ccab.szCab)) { |
| 264 | PyErr_SetString(PyExc_ValueError, "path name too long"); |
| 265 | return 0; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 268 | if (i > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | memcpy(ccab.szCabPath, cabname, i); |
| 270 | ccab.szCabPath[i] = '\0'; |
| 271 | strcpy(ccab.szCab, cabname+i); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 272 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | strcpy(ccab.szCabPath, ".\\"); |
| 274 | strcpy(ccab.szCab, cabname); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete, |
| 279 | cb_gettempfile, &ccab, NULL); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 280 | |
| 281 | if (hfci == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); |
| 283 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | for (i=0; i < PyList_GET_SIZE(files); i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | PyObject *item = PyList_GET_ITEM(files, i); |
| 288 | char *filename, *cabname; |
Zachary Ware | 0a29e89 | 2015-05-18 00:47:15 -0500 | [diff] [blame] | 289 | |
| 290 | if (!PyArg_ParseTuple(item, "ss", &filename, &cabname)) { |
| 291 | PyErr_SetString(PyExc_TypeError, "FCICreate expects a list of tuples containing two strings"); |
| 292 | FCIDestroy(hfci); |
| 293 | return NULL; |
| 294 | } |
| 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | if (!FCIAddFile(hfci, filename, cabname, FALSE, |
| 297 | cb_getnextcabinet, cb_status, cb_getopeninfo, |
| 298 | tcompTYPE_MSZIP)) |
| 299 | goto err; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | goto err; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 304 | |
| 305 | if (!FCIDestroy(hfci)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | goto err; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 307 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 308 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 309 | err: |
Zachary Ware | 0a29e89 | 2015-05-18 00:47:15 -0500 | [diff] [blame] | 310 | if(erf.fError) |
| 311 | PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); /* XXX better error type */ |
| 312 | else |
| 313 | PyErr_SetString(PyExc_ValueError, "FCI general error"); |
| 314 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 315 | FCIDestroy(hfci); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | typedef struct msiobj{ |
| 320 | PyObject_HEAD |
| 321 | MSIHANDLE h; |
| 322 | }msiobj; |
| 323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | static void |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 325 | msiobj_dealloc(msiobj* msidb) |
| 326 | { |
| 327 | MsiCloseHandle(msidb->h); |
| 328 | msidb->h = 0; |
Zackery Spytz | cb04f75 | 2017-11-07 03:03:09 -0700 | [diff] [blame] | 329 | PyObject_Del(msidb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static PyObject* |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 333 | msierror(int status) |
| 334 | { |
| 335 | int code; |
| 336 | char buf[2000]; |
| 337 | char *res = buf; |
| 338 | DWORD size = sizeof(buf); |
| 339 | MSIHANDLE err = MsiGetLastErrorRecord(); |
| 340 | |
| 341 | if (err == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | switch(status) { |
| 343 | case ERROR_ACCESS_DENIED: |
| 344 | PyErr_SetString(MSIError, "access denied"); |
| 345 | return NULL; |
| 346 | case ERROR_FUNCTION_FAILED: |
| 347 | PyErr_SetString(MSIError, "function failed"); |
| 348 | return NULL; |
| 349 | case ERROR_INVALID_DATA: |
| 350 | PyErr_SetString(MSIError, "invalid data"); |
| 351 | return NULL; |
| 352 | case ERROR_INVALID_HANDLE: |
| 353 | PyErr_SetString(MSIError, "invalid handle"); |
| 354 | return NULL; |
| 355 | case ERROR_INVALID_STATE: |
| 356 | PyErr_SetString(MSIError, "invalid state"); |
| 357 | return NULL; |
| 358 | case ERROR_INVALID_PARAMETER: |
| 359 | PyErr_SetString(MSIError, "invalid parameter"); |
| 360 | return NULL; |
Berker Peksag | 4864a61 | 2017-11-24 12:53:58 +0300 | [diff] [blame] | 361 | case ERROR_OPEN_FAILED: |
| 362 | PyErr_SetString(MSIError, "open failed"); |
| 363 | return NULL; |
| 364 | case ERROR_CREATE_FAILED: |
| 365 | PyErr_SetString(MSIError, "create failed"); |
| 366 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | default: |
| 368 | PyErr_Format(MSIError, "unknown error %x", status); |
| 369 | return NULL; |
| 370 | } |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | code = MsiRecordGetInteger(err, 1); /* XXX code */ |
| 374 | if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | res = malloc(size+1); |
Zackery Spytz | 4e51937 | 2018-09-07 16:02:56 -0600 | [diff] [blame] | 376 | if (res == NULL) { |
| 377 | MsiCloseHandle(err); |
| 378 | return PyErr_NoMemory(); |
| 379 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | MsiFormatRecord(0, err, res, &size); |
| 381 | res[size]='\0'; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 382 | } |
| 383 | MsiCloseHandle(err); |
| 384 | PyErr_SetString(MSIError, res); |
| 385 | if (res != buf) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | free(res); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
| 389 | |
Berker Peksag | a935654 | 2017-11-07 15:58:53 +0300 | [diff] [blame] | 390 | static PyObject* |
| 391 | msidb_close(msiobj* msidb, PyObject *args) |
| 392 | { |
| 393 | int status; |
| 394 | if ((status = MsiCloseHandle(msidb->h)) != ERROR_SUCCESS) { |
| 395 | return msierror(status); |
| 396 | } |
| 397 | msidb->h = 0; |
| 398 | Py_RETURN_NONE; |
| 399 | } |
| 400 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 401 | /*************************** Record objects **********************/ |
| 402 | |
| 403 | static PyObject* |
| 404 | record_getfieldcount(msiobj* record, PyObject* args) |
| 405 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 406 | return PyLong_FromLong(MsiRecordGetFieldCount(record->h)); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static PyObject* |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 410 | record_getinteger(msiobj* record, PyObject* args) |
| 411 | { |
| 412 | unsigned int field; |
| 413 | int status; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 415 | if (!PyArg_ParseTuple(args, "I:GetInteger", &field)) |
| 416 | return NULL; |
| 417 | status = MsiRecordGetInteger(record->h, field); |
| 418 | if (status == MSI_NULL_INTEGER){ |
| 419 | PyErr_SetString(MSIError, "could not convert record field to integer"); |
| 420 | return NULL; |
| 421 | } |
Martin v. Löwis | 704d8b1 | 2008-06-02 11:32:23 +0000 | [diff] [blame] | 422 | return PyLong_FromLong((long) status); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static PyObject* |
| 426 | record_getstring(msiobj* record, PyObject* args) |
| 427 | { |
| 428 | unsigned int field; |
| 429 | unsigned int status; |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 430 | WCHAR buf[2000]; |
| 431 | WCHAR *res = buf; |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 432 | DWORD size = sizeof(buf); |
| 433 | PyObject* string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 435 | if (!PyArg_ParseTuple(args, "I:GetString", &field)) |
| 436 | return NULL; |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 437 | status = MsiRecordGetStringW(record->h, field, res, &size); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 438 | if (status == ERROR_MORE_DATA) { |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 439 | res = (WCHAR*) malloc((size + 1)*sizeof(WCHAR)); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 440 | if (res == NULL) |
| 441 | return PyErr_NoMemory(); |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 442 | status = MsiRecordGetStringW(record->h, field, res, &size); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 443 | } |
| 444 | if (status != ERROR_SUCCESS) |
| 445 | return msierror((int) status); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 446 | string = PyUnicode_FromWideChar(res, size); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 447 | if (buf != res) |
| 448 | free(res); |
| 449 | return string; |
| 450 | } |
| 451 | |
| 452 | static PyObject* |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 453 | record_cleardata(msiobj* record, PyObject *args) |
| 454 | { |
| 455 | int status = MsiRecordClearData(record->h); |
| 456 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 458 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 459 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static PyObject* |
| 463 | record_setstring(msiobj* record, PyObject *args) |
| 464 | { |
| 465 | int status; |
| 466 | int field; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 467 | wchar_t *data; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 468 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 469 | if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 471 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 472 | if ((status = MsiRecordSetStringW(record->h, field, data)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 474 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 475 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static PyObject* |
| 479 | record_setstream(msiobj* record, PyObject *args) |
| 480 | { |
| 481 | int status; |
| 482 | int field; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 483 | wchar_t *data; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 484 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 485 | if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 487 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 488 | if ((status = MsiRecordSetStreamW(record->h, field, data)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 490 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 491 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static PyObject* |
| 495 | record_setinteger(msiobj* record, PyObject *args) |
| 496 | { |
| 497 | int status; |
| 498 | int field; |
| 499 | int data; |
| 500 | |
| 501 | if (!PyArg_ParseTuple(args, "ii:SetInteger", &field, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 503 | |
| 504 | if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 506 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 507 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
| 511 | |
| 512 | static PyMethodDef record_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS, |
| 514 | PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")}, |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 515 | { "GetInteger", (PyCFunction)record_getinteger, METH_VARARGS, |
| 516 | PyDoc_STR("GetInteger(field) -> int\nWraps MsiRecordGetInteger")}, |
| 517 | { "GetString", (PyCFunction)record_getstring, METH_VARARGS, |
| 518 | PyDoc_STR("GetString(field) -> string\nWraps MsiRecordGetString")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | { "SetString", (PyCFunction)record_setstring, METH_VARARGS, |
| 520 | PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")}, |
| 521 | { "SetStream", (PyCFunction)record_setstream, METH_VARARGS, |
| 522 | PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")}, |
| 523 | { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS, |
| 524 | PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")}, |
| 525 | { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS, |
| 526 | PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 527 | { NULL, NULL } |
| 528 | }; |
| 529 | |
| 530 | static PyTypeObject record_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | PyVarObject_HEAD_INIT(NULL, 0) |
| 532 | "_msi.Record", /*tp_name*/ |
| 533 | sizeof(msiobj), /*tp_basicsize*/ |
| 534 | 0, /*tp_itemsize*/ |
| 535 | /* methods */ |
| 536 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 537 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | 0, /*tp_getattr*/ |
| 539 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 540 | 0, /*tp_as_async*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | 0, /*tp_repr*/ |
| 542 | 0, /*tp_as_number*/ |
| 543 | 0, /*tp_as_sequence*/ |
| 544 | 0, /*tp_as_mapping*/ |
| 545 | 0, /*tp_hash*/ |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 546 | 0, /*tp_call*/ |
| 547 | 0, /*tp_str*/ |
| 548 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 549 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 550 | 0, /*tp_as_buffer*/ |
| 551 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 552 | 0, /*tp_doc*/ |
| 553 | 0, /*tp_traverse*/ |
| 554 | 0, /*tp_clear*/ |
| 555 | 0, /*tp_richcompare*/ |
| 556 | 0, /*tp_weaklistoffset*/ |
| 557 | 0, /*tp_iter*/ |
| 558 | 0, /*tp_iternext*/ |
| 559 | record_methods, /*tp_methods*/ |
| 560 | 0, /*tp_members*/ |
| 561 | 0, /*tp_getset*/ |
| 562 | 0, /*tp_base*/ |
| 563 | 0, /*tp_dict*/ |
| 564 | 0, /*tp_descr_get*/ |
| 565 | 0, /*tp_descr_set*/ |
| 566 | 0, /*tp_dictoffset*/ |
| 567 | 0, /*tp_init*/ |
| 568 | 0, /*tp_alloc*/ |
| 569 | 0, /*tp_new*/ |
| 570 | 0, /*tp_free*/ |
| 571 | 0, /*tp_is_gc*/ |
| 572 | }; |
| 573 | |
| 574 | static PyObject* |
| 575 | record_new(MSIHANDLE h) |
| 576 | { |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame] | 577 | msiobj *result = PyObject_New(struct msiobj, &record_Type); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 578 | |
| 579 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | MsiCloseHandle(h); |
| 581 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | result->h = h; |
| 585 | return (PyObject*)result; |
| 586 | } |
| 587 | |
| 588 | /*************************** SummaryInformation objects **************/ |
| 589 | |
| 590 | static PyObject* |
| 591 | summary_getproperty(msiobj* si, PyObject *args) |
| 592 | { |
| 593 | int status; |
| 594 | int field; |
| 595 | PyObject *result; |
| 596 | UINT type; |
| 597 | INT ival; |
| 598 | FILETIME fval; |
| 599 | char sbuf[1000]; |
| 600 | char *sval = sbuf; |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 601 | DWORD ssize = sizeof(sbuf); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 602 | |
| 603 | if (!PyArg_ParseTuple(args, "i:GetProperty", &field)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival, |
| 607 | &fval, sval, &ssize); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 608 | if (status == ERROR_MORE_DATA) { |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 609 | ssize++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | sval = malloc(ssize); |
Zackery Spytz | 4e51937 | 2018-09-07 16:02:56 -0600 | [diff] [blame] | 611 | if (sval == NULL) { |
| 612 | return PyErr_NoMemory(); |
| 613 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival, |
| 615 | &fval, sval, &ssize); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 616 | } |
Zackery Spytz | 549e55a | 2019-05-31 18:16:20 -0600 | [diff] [blame] | 617 | if (status != ERROR_SUCCESS) { |
| 618 | return msierror(status); |
| 619 | } |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 620 | |
| 621 | switch(type) { |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 622 | case VT_I2: |
| 623 | case VT_I4: |
| 624 | result = PyLong_FromLong(ival); |
| 625 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | case VT_FILETIME: |
| 627 | PyErr_SetString(PyExc_NotImplementedError, "FILETIME result"); |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 628 | result = NULL; |
| 629 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | case VT_LPSTR: |
| 631 | result = PyBytes_FromStringAndSize(sval, ssize); |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 632 | break; |
Berker Peksag | 19fb134 | 2017-11-24 18:11:18 +0300 | [diff] [blame] | 633 | case VT_EMPTY: |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 634 | Py_INCREF(Py_None); |
| 635 | result = Py_None; |
| 636 | break; |
| 637 | default: |
| 638 | PyErr_Format(PyExc_NotImplementedError, "result of type %d", type); |
| 639 | result = NULL; |
| 640 | break; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 641 | } |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 642 | if (sval != sbuf) |
| 643 | free(sval); |
| 644 | return result; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static PyObject* |
| 648 | summary_getpropertycount(msiobj* si, PyObject *args) |
| 649 | { |
| 650 | int status; |
| 651 | UINT result; |
| 652 | |
| 653 | status = MsiSummaryInfoGetPropertyCount(si->h, &result); |
| 654 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 656 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 657 | return PyLong_FromLong(result); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static PyObject* |
| 661 | summary_setproperty(msiobj* si, PyObject *args) |
| 662 | { |
| 663 | int status; |
| 664 | int field; |
| 665 | PyObject* data; |
| 666 | |
| 667 | if (!PyArg_ParseTuple(args, "iO:SetProperty", &field, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 669 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 670 | if (PyUnicode_Check(data)) { |
Serhiy Storchaka | ccdc09e | 2017-06-28 09:55:22 +0300 | [diff] [blame] | 671 | const WCHAR *value = _PyUnicode_AsUnicode(data); |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 672 | if (value == NULL) { |
| 673 | return NULL; |
| 674 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | status = MsiSummaryInfoSetPropertyW(si->h, field, VT_LPSTR, |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 676 | 0, NULL, value); |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 677 | } else if (PyLong_CheckExact(data)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | long value = PyLong_AsLong(data); |
| 679 | if (value == -1 && PyErr_Occurred()) { |
| 680 | return NULL; |
| 681 | } |
| 682 | status = MsiSummaryInfoSetProperty(si->h, field, VT_I4, |
| 683 | value, NULL, NULL); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 684 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | PyErr_SetString(PyExc_TypeError, "unsupported type"); |
| 686 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 687 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 689 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 691 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 692 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
| 696 | static PyObject* |
| 697 | summary_persist(msiobj* si, PyObject *args) |
| 698 | { |
| 699 | int status; |
| 700 | |
| 701 | status = MsiSummaryInfoPersist(si->h); |
| 702 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | return msierror(status); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 704 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static PyMethodDef summary_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS, |
| 709 | PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")}, |
| 710 | { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS, |
| 711 | PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")}, |
| 712 | { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS, |
| 713 | PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")}, |
| 714 | { "Persist", (PyCFunction)summary_persist, METH_NOARGS, |
| 715 | PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 716 | { NULL, NULL } |
| 717 | }; |
| 718 | |
| 719 | static PyTypeObject summary_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | PyVarObject_HEAD_INIT(NULL, 0) |
| 721 | "_msi.SummaryInformation", /*tp_name*/ |
| 722 | sizeof(msiobj), /*tp_basicsize*/ |
| 723 | 0, /*tp_itemsize*/ |
| 724 | /* methods */ |
| 725 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 726 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | 0, /*tp_getattr*/ |
| 728 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 729 | 0, /*tp_as_async*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | 0, /*tp_repr*/ |
| 731 | 0, /*tp_as_number*/ |
| 732 | 0, /*tp_as_sequence*/ |
| 733 | 0, /*tp_as_mapping*/ |
| 734 | 0, /*tp_hash*/ |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 735 | 0, /*tp_call*/ |
| 736 | 0, /*tp_str*/ |
| 737 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 738 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 739 | 0, /*tp_as_buffer*/ |
| 740 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 741 | 0, /*tp_doc*/ |
| 742 | 0, /*tp_traverse*/ |
| 743 | 0, /*tp_clear*/ |
| 744 | 0, /*tp_richcompare*/ |
| 745 | 0, /*tp_weaklistoffset*/ |
| 746 | 0, /*tp_iter*/ |
| 747 | 0, /*tp_iternext*/ |
| 748 | summary_methods, /*tp_methods*/ |
| 749 | 0, /*tp_members*/ |
| 750 | 0, /*tp_getset*/ |
| 751 | 0, /*tp_base*/ |
| 752 | 0, /*tp_dict*/ |
| 753 | 0, /*tp_descr_get*/ |
| 754 | 0, /*tp_descr_set*/ |
| 755 | 0, /*tp_dictoffset*/ |
| 756 | 0, /*tp_init*/ |
| 757 | 0, /*tp_alloc*/ |
| 758 | 0, /*tp_new*/ |
| 759 | 0, /*tp_free*/ |
| 760 | 0, /*tp_is_gc*/ |
| 761 | }; |
| 762 | |
| 763 | /*************************** View objects **************/ |
| 764 | |
| 765 | static PyObject* |
| 766 | view_execute(msiobj *view, PyObject*args) |
| 767 | { |
| 768 | int status; |
| 769 | MSIHANDLE params = 0; |
| 770 | PyObject *oparams = Py_None; |
| 771 | |
| 772 | if (!PyArg_ParseTuple(args, "O:Execute", &oparams)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 774 | |
| 775 | if (oparams != Py_None) { |
| 776 | if (oparams->ob_type != &record_Type) { |
| 777 | PyErr_SetString(PyExc_TypeError, "Execute argument must be a record"); |
| 778 | return NULL; |
| 779 | } |
| 780 | params = ((msiobj*)oparams)->h; |
| 781 | } |
| 782 | |
| 783 | status = MsiViewExecute(view->h, params); |
| 784 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 786 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 787 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static PyObject* |
| 791 | view_fetch(msiobj *view, PyObject*args) |
| 792 | { |
| 793 | int status; |
| 794 | MSIHANDLE result; |
| 795 | |
Berker Peksag | bdb8315 | 2017-11-23 15:47:30 +0300 | [diff] [blame] | 796 | status = MsiViewFetch(view->h, &result); |
| 797 | if (status == ERROR_NO_MORE_ITEMS) { |
| 798 | Py_RETURN_NONE; |
| 799 | } else if (status != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | return msierror(status); |
Berker Peksag | bdb8315 | 2017-11-23 15:47:30 +0300 | [diff] [blame] | 801 | } |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 802 | |
| 803 | return record_new(result); |
| 804 | } |
| 805 | |
| 806 | static PyObject* |
| 807 | view_getcolumninfo(msiobj *view, PyObject *args) |
| 808 | { |
| 809 | int status; |
| 810 | int kind; |
| 811 | MSIHANDLE result; |
| 812 | |
| 813 | if (!PyArg_ParseTuple(args, "i:GetColumnInfo", &kind)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 815 | |
| 816 | if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 818 | |
| 819 | return record_new(result); |
| 820 | } |
| 821 | |
| 822 | static PyObject* |
| 823 | view_modify(msiobj *view, PyObject *args) |
| 824 | { |
| 825 | int kind; |
| 826 | PyObject *data; |
| 827 | int status; |
| 828 | |
| 829 | if (!PyArg_ParseTuple(args, "iO:Modify", &kind, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 831 | |
| 832 | if (data->ob_type != &record_Type) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | PyErr_SetString(PyExc_TypeError, "Modify expects a record object"); |
| 834 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 839 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 840 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static PyObject* |
| 844 | view_close(msiobj *view, PyObject*args) |
| 845 | { |
| 846 | int status; |
| 847 | |
| 848 | if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 850 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 851 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static PyMethodDef view_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | { "Execute", (PyCFunction)view_execute, METH_VARARGS, |
| 856 | PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 857 | { "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 859 | { "Fetch", (PyCFunction)view_fetch, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 861 | { "Modify", (PyCFunction)view_modify, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 863 | { "Close", (PyCFunction)view_close, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | PyDoc_STR("Close() -> result\nWraps MsiViewClose")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 865 | { NULL, NULL } |
| 866 | }; |
| 867 | |
| 868 | static PyTypeObject msiview_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | PyVarObject_HEAD_INIT(NULL, 0) |
| 870 | "_msi.View", /*tp_name*/ |
| 871 | sizeof(msiobj), /*tp_basicsize*/ |
| 872 | 0, /*tp_itemsize*/ |
| 873 | /* methods */ |
| 874 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 875 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | 0, /*tp_getattr*/ |
| 877 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 878 | 0, /*tp_as_async*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | 0, /*tp_repr*/ |
| 880 | 0, /*tp_as_number*/ |
| 881 | 0, /*tp_as_sequence*/ |
| 882 | 0, /*tp_as_mapping*/ |
| 883 | 0, /*tp_hash*/ |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 884 | 0, /*tp_call*/ |
| 885 | 0, /*tp_str*/ |
| 886 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 887 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 888 | 0, /*tp_as_buffer*/ |
| 889 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 890 | 0, /*tp_doc*/ |
| 891 | 0, /*tp_traverse*/ |
| 892 | 0, /*tp_clear*/ |
| 893 | 0, /*tp_richcompare*/ |
| 894 | 0, /*tp_weaklistoffset*/ |
| 895 | 0, /*tp_iter*/ |
| 896 | 0, /*tp_iternext*/ |
| 897 | view_methods, /*tp_methods*/ |
| 898 | 0, /*tp_members*/ |
| 899 | 0, /*tp_getset*/ |
| 900 | 0, /*tp_base*/ |
| 901 | 0, /*tp_dict*/ |
| 902 | 0, /*tp_descr_get*/ |
| 903 | 0, /*tp_descr_set*/ |
| 904 | 0, /*tp_dictoffset*/ |
| 905 | 0, /*tp_init*/ |
| 906 | 0, /*tp_alloc*/ |
| 907 | 0, /*tp_new*/ |
| 908 | 0, /*tp_free*/ |
| 909 | 0, /*tp_is_gc*/ |
| 910 | }; |
| 911 | |
| 912 | /*************************** Database objects **************/ |
| 913 | |
| 914 | static PyObject* |
| 915 | msidb_openview(msiobj *msidb, PyObject *args) |
| 916 | { |
| 917 | int status; |
Serhiy Storchaka | 55939b1 | 2020-06-25 11:37:12 +0300 | [diff] [blame] | 918 | const wchar_t *sql; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 919 | MSIHANDLE hView; |
| 920 | msiobj *result; |
| 921 | |
Serhiy Storchaka | 55939b1 | 2020-06-25 11:37:12 +0300 | [diff] [blame] | 922 | if (!PyArg_ParseTuple(args, "u:OpenView", &sql)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 924 | |
Serhiy Storchaka | 55939b1 | 2020-06-25 11:37:12 +0300 | [diff] [blame] | 925 | if ((status = MsiDatabaseOpenViewW(msidb->h, sql, &hView)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 927 | |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame] | 928 | result = PyObject_New(struct msiobj, &msiview_Type); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 929 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | MsiCloseHandle(hView); |
| 931 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | result->h = hView; |
| 935 | return (PyObject*)result; |
| 936 | } |
| 937 | |
| 938 | static PyObject* |
| 939 | msidb_commit(msiobj *msidb, PyObject *args) |
| 940 | { |
| 941 | int status; |
| 942 | |
| 943 | if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 945 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 946 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | static PyObject* |
| 950 | msidb_getsummaryinformation(msiobj *db, PyObject *args) |
| 951 | { |
| 952 | int status; |
| 953 | int count; |
| 954 | MSIHANDLE result; |
| 955 | msiobj *oresult; |
| 956 | |
| 957 | if (!PyArg_ParseTuple(args, "i:GetSummaryInformation", &count)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 959 | |
| 960 | status = MsiGetSummaryInformation(db->h, NULL, count, &result); |
| 961 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 963 | |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame] | 964 | oresult = PyObject_New(struct msiobj, &summary_Type); |
Zackery Spytz | bf94cc7 | 2019-03-07 11:20:13 -0700 | [diff] [blame] | 965 | if (!oresult) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | MsiCloseHandle(result); |
| 967 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | oresult->h = result; |
| 971 | return (PyObject*)oresult; |
| 972 | } |
| 973 | |
| 974 | static PyMethodDef db_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS, |
| 976 | PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 977 | { "Commit", (PyCFunction)msidb_commit, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")}, |
| 979 | { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS, |
| 980 | PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")}, |
Berker Peksag | a935654 | 2017-11-07 15:58:53 +0300 | [diff] [blame] | 981 | { "Close", (PyCFunction)msidb_close, METH_NOARGS, |
| 982 | PyDoc_STR("Close() -> None\nWraps MsiCloseHandle")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 983 | { NULL, NULL } |
| 984 | }; |
| 985 | |
| 986 | static PyTypeObject msidb_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | PyVarObject_HEAD_INIT(NULL, 0) |
| 988 | "_msi.Database", /*tp_name*/ |
| 989 | sizeof(msiobj), /*tp_basicsize*/ |
| 990 | 0, /*tp_itemsize*/ |
| 991 | /* methods */ |
| 992 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 993 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | 0, /*tp_getattr*/ |
| 995 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 996 | 0, /*tp_as_async*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | 0, /*tp_repr*/ |
| 998 | 0, /*tp_as_number*/ |
| 999 | 0, /*tp_as_sequence*/ |
| 1000 | 0, /*tp_as_mapping*/ |
| 1001 | 0, /*tp_hash*/ |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1002 | 0, /*tp_call*/ |
| 1003 | 0, /*tp_str*/ |
| 1004 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 1005 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 1006 | 0, /*tp_as_buffer*/ |
| 1007 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 1008 | 0, /*tp_doc*/ |
| 1009 | 0, /*tp_traverse*/ |
| 1010 | 0, /*tp_clear*/ |
| 1011 | 0, /*tp_richcompare*/ |
| 1012 | 0, /*tp_weaklistoffset*/ |
| 1013 | 0, /*tp_iter*/ |
| 1014 | 0, /*tp_iternext*/ |
| 1015 | db_methods, /*tp_methods*/ |
| 1016 | 0, /*tp_members*/ |
| 1017 | 0, /*tp_getset*/ |
| 1018 | 0, /*tp_base*/ |
| 1019 | 0, /*tp_dict*/ |
| 1020 | 0, /*tp_descr_get*/ |
| 1021 | 0, /*tp_descr_set*/ |
| 1022 | 0, /*tp_dictoffset*/ |
| 1023 | 0, /*tp_init*/ |
| 1024 | 0, /*tp_alloc*/ |
| 1025 | 0, /*tp_new*/ |
| 1026 | 0, /*tp_free*/ |
| 1027 | 0, /*tp_is_gc*/ |
| 1028 | }; |
| 1029 | |
Steve Dower | 6ceda63 | 2016-09-09 11:56:34 -0700 | [diff] [blame] | 1030 | #define Py_NOT_PERSIST(x, flag) \ |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 1031 | (x != (SIZE_T)(flag) && \ |
| 1032 | x != ((SIZE_T)(flag) | MSIDBOPEN_PATCHFILE)) |
Steve Dower | 6ceda63 | 2016-09-09 11:56:34 -0700 | [diff] [blame] | 1033 | |
| 1034 | #define Py_INVALID_PERSIST(x) \ |
| 1035 | (Py_NOT_PERSIST(x, MSIDBOPEN_READONLY) && \ |
| 1036 | Py_NOT_PERSIST(x, MSIDBOPEN_TRANSACT) && \ |
| 1037 | Py_NOT_PERSIST(x, MSIDBOPEN_DIRECT) && \ |
| 1038 | Py_NOT_PERSIST(x, MSIDBOPEN_CREATE) && \ |
| 1039 | Py_NOT_PERSIST(x, MSIDBOPEN_CREATEDIRECT)) |
| 1040 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1041 | static PyObject* msiopendb(PyObject *obj, PyObject *args) |
| 1042 | { |
| 1043 | int status; |
Serhiy Storchaka | 55939b1 | 2020-06-25 11:37:12 +0300 | [diff] [blame] | 1044 | const wchar_t *path; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1045 | int persist; |
| 1046 | MSIHANDLE h; |
| 1047 | msiobj *result; |
Serhiy Storchaka | 55939b1 | 2020-06-25 11:37:12 +0300 | [diff] [blame] | 1048 | if (!PyArg_ParseTuple(args, "ui:MSIOpenDatabase", &path, &persist)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | return NULL; |
Steve Dower | 6ceda63 | 2016-09-09 11:56:34 -0700 | [diff] [blame] | 1050 | /* We need to validate that persist is a valid MSIDBOPEN_* value. Otherwise, |
| 1051 | MsiOpenDatabase may treat the value as a pointer, leading to unexpected |
| 1052 | behavior. */ |
| 1053 | if (Py_INVALID_PERSIST(persist)) |
| 1054 | return msierror(ERROR_INVALID_PARAMETER); |
Serhiy Storchaka | 55939b1 | 2020-06-25 11:37:12 +0300 | [diff] [blame] | 1055 | status = MsiOpenDatabaseW(path, (LPCWSTR)(SIZE_T)persist, &h); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1056 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1058 | |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame] | 1059 | result = PyObject_New(struct msiobj, &msidb_Type); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1060 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | MsiCloseHandle(h); |
| 1062 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1063 | } |
| 1064 | result->h = h; |
| 1065 | return (PyObject*)result; |
| 1066 | } |
| 1067 | |
| 1068 | static PyObject* |
| 1069 | createrecord(PyObject *o, PyObject *args) |
| 1070 | { |
| 1071 | int count; |
| 1072 | MSIHANDLE h; |
| 1073 | |
| 1074 | if (!PyArg_ParseTuple(args, "i:CreateRecord", &count)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | return NULL; |
| 1076 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1077 | h = MsiCreateRecord(count); |
| 1078 | if (h == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | return msierror(0); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1080 | |
| 1081 | return record_new(h); |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | static PyMethodDef msi_methods[] = { |
| 1086 | {"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | PyDoc_STR("UuidCreate() -> string")}, |
| 1088 | {"FCICreate", (PyCFunction)fcicreate, METH_VARARGS, |
| 1089 | PyDoc_STR("fcicreate(cabname,files) -> None")}, |
| 1090 | {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS, |
| 1091 | PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")}, |
| 1092 | {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS, |
| 1093 | PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")}, |
| 1094 | {NULL, NULL} /* sentinel */ |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1095 | }; |
| 1096 | |
| 1097 | static char msi_doc[] = "Documentation"; |
| 1098 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1099 | |
| 1100 | static struct PyModuleDef _msimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | PyModuleDef_HEAD_INIT, |
| 1102 | "_msi", |
| 1103 | msi_doc, |
| 1104 | -1, |
| 1105 | msi_methods, |
| 1106 | NULL, |
| 1107 | NULL, |
| 1108 | NULL, |
| 1109 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1110 | }; |
| 1111 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1112 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1113 | PyInit__msi(void) |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1114 | { |
| 1115 | PyObject *m; |
| 1116 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1117 | m = PyModule_Create(&_msimodule); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1118 | if (m == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1120 | |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 1121 | PyModule_AddIntConstant(m, "MSIDBOPEN_CREATEDIRECT", (long)(SIZE_T)MSIDBOPEN_CREATEDIRECT); |
| 1122 | PyModule_AddIntConstant(m, "MSIDBOPEN_CREATE", (long)(SIZE_T)MSIDBOPEN_CREATE); |
| 1123 | PyModule_AddIntConstant(m, "MSIDBOPEN_DIRECT", (long)(SIZE_T)MSIDBOPEN_DIRECT); |
| 1124 | PyModule_AddIntConstant(m, "MSIDBOPEN_READONLY", (long)(SIZE_T)MSIDBOPEN_READONLY); |
| 1125 | PyModule_AddIntConstant(m, "MSIDBOPEN_TRANSACT", (long)(SIZE_T)MSIDBOPEN_TRANSACT); |
| 1126 | PyModule_AddIntConstant(m, "MSIDBOPEN_PATCHFILE", (long)(SIZE_T)MSIDBOPEN_PATCHFILE); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1127 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 1128 | PyModule_AddIntMacro(m, MSICOLINFO_NAMES); |
| 1129 | PyModule_AddIntMacro(m, MSICOLINFO_TYPES); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1130 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 1131 | PyModule_AddIntMacro(m, MSIMODIFY_SEEK); |
| 1132 | PyModule_AddIntMacro(m, MSIMODIFY_REFRESH); |
| 1133 | PyModule_AddIntMacro(m, MSIMODIFY_INSERT); |
| 1134 | PyModule_AddIntMacro(m, MSIMODIFY_UPDATE); |
| 1135 | PyModule_AddIntMacro(m, MSIMODIFY_ASSIGN); |
| 1136 | PyModule_AddIntMacro(m, MSIMODIFY_REPLACE); |
| 1137 | PyModule_AddIntMacro(m, MSIMODIFY_MERGE); |
| 1138 | PyModule_AddIntMacro(m, MSIMODIFY_DELETE); |
| 1139 | PyModule_AddIntMacro(m, MSIMODIFY_INSERT_TEMPORARY); |
| 1140 | PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE); |
| 1141 | PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE_NEW); |
| 1142 | PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE_FIELD); |
| 1143 | PyModule_AddIntMacro(m, MSIMODIFY_VALIDATE_DELETE); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1144 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 1145 | PyModule_AddIntMacro(m, PID_CODEPAGE); |
| 1146 | PyModule_AddIntMacro(m, PID_TITLE); |
| 1147 | PyModule_AddIntMacro(m, PID_SUBJECT); |
| 1148 | PyModule_AddIntMacro(m, PID_AUTHOR); |
| 1149 | PyModule_AddIntMacro(m, PID_KEYWORDS); |
| 1150 | PyModule_AddIntMacro(m, PID_COMMENTS); |
| 1151 | PyModule_AddIntMacro(m, PID_TEMPLATE); |
| 1152 | PyModule_AddIntMacro(m, PID_LASTAUTHOR); |
| 1153 | PyModule_AddIntMacro(m, PID_REVNUMBER); |
| 1154 | PyModule_AddIntMacro(m, PID_LASTPRINTED); |
| 1155 | PyModule_AddIntMacro(m, PID_CREATE_DTM); |
| 1156 | PyModule_AddIntMacro(m, PID_LASTSAVE_DTM); |
| 1157 | PyModule_AddIntMacro(m, PID_PAGECOUNT); |
| 1158 | PyModule_AddIntMacro(m, PID_WORDCOUNT); |
| 1159 | PyModule_AddIntMacro(m, PID_CHARCOUNT); |
| 1160 | PyModule_AddIntMacro(m, PID_APPNAME); |
| 1161 | PyModule_AddIntMacro(m, PID_SECURITY); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1162 | |
| 1163 | MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL); |
| 1164 | if (!MSIError) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1166 | PyModule_AddObject(m, "MSIError", MSIError); |
Amaury Forgeot d'Arc | bf9e966 | 2008-06-17 21:39:46 +0000 | [diff] [blame] | 1167 | return m; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1168 | } |