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 | |
| 44 | /* FCI callback functions */ |
| 45 | |
| 46 | static FNFCIALLOC(cb_alloc) |
| 47 | { |
| 48 | return malloc(cb); |
| 49 | } |
| 50 | |
| 51 | static FNFCIFREE(cb_free) |
| 52 | { |
| 53 | free(memory); |
| 54 | } |
| 55 | |
| 56 | static FNFCIOPEN(cb_open) |
| 57 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 58 | int result = _open(pszFile, oflag | O_NOINHERIT, pmode); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 59 | if (result == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 61 | return result; |
| 62 | } |
| 63 | |
| 64 | static FNFCIREAD(cb_read) |
| 65 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 66 | UINT result = (UINT)_read((int)hf, memory, cb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 67 | if (result != cb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 69 | return result; |
| 70 | } |
| 71 | |
| 72 | static FNFCIWRITE(cb_write) |
| 73 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 74 | UINT result = (UINT)_write((int)hf, memory, cb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 75 | if (result != cb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 77 | return result; |
| 78 | } |
| 79 | |
| 80 | static FNFCICLOSE(cb_close) |
| 81 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 82 | int result = _close((int)hf); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 83 | if (result != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 85 | return result; |
| 86 | } |
| 87 | |
| 88 | static FNFCISEEK(cb_seek) |
| 89 | { |
Victor Stinner | 6715828 | 2013-11-20 00:14:49 +0100 | [diff] [blame] | 90 | long result = (long)_lseek((int)hf, dist, seektype); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 91 | if (result == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 93 | return result; |
| 94 | } |
| 95 | |
| 96 | static FNFCIDELETE(cb_delete) |
| 97 | { |
| 98 | int result = remove(pszFile); |
| 99 | if (result != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | *err = errno; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 101 | return result; |
| 102 | } |
| 103 | |
| 104 | static FNFCIFILEPLACED(cb_fileplaced) |
| 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static FNFCIGETTEMPFILE(cb_gettempfile) |
| 110 | { |
| 111 | char *name = _tempnam("", "tmp"); |
| 112 | if ((name != NULL) && ((int)strlen(name) < cbTempName)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | strcpy(pszTempName, name); |
| 114 | free(name); |
| 115 | return TRUE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (name) free(name); |
| 119 | return FALSE; |
| 120 | } |
| 121 | |
| 122 | static FNFCISTATUS(cb_status) |
| 123 | { |
| 124 | if (pv) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 125 | _Py_IDENTIFIER(status); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 126 | |
| 127 | PyObject *result = _PyObject_CallMethodId(pv, &PyId_status, "iii", typeStatus, cb1, cb2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | if (result == NULL) |
| 129 | return -1; |
| 130 | Py_DECREF(result); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static FNFCIGETNEXTCABINET(cb_getnextcabinet) |
| 136 | { |
| 137 | if (pv) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 138 | _Py_IDENTIFIER(getnextcabinet); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 139 | |
| 140 | PyObject *result = _PyObject_CallMethodId(pv, &PyId_getnextcabinet, "i", pccab->iCab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 152 | } |
| 153 | return FALSE; |
| 154 | } |
| 155 | |
| 156 | static 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 165 | if (handle == INVALID_HANDLE_VALUE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | return -1; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 167 | |
| 168 | if (GetFileInformationByHandle(handle, &bhfi) == FALSE) |
| 169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | CloseHandle(handle); |
| 171 | return -1; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime); |
| 175 | FileTimeToDosDateTime(&filetime, pdate, ptime); |
| 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | *pattribs = (int)(bhfi.dwFileAttributes & |
| 178 | (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH)); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 179 | |
| 180 | CloseHandle(handle); |
| 181 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 182 | return _open(pszName, _O_RDONLY | _O_BINARY | O_NOINHERIT); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static PyObject* fcicreate(PyObject* obj, PyObject* args) |
| 186 | { |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 187 | char *cabname, *p; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 188 | PyObject *files; |
| 189 | CCAB ccab; |
| 190 | HFCI hfci; |
| 191 | ERF erf; |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 192 | Py_ssize_t i; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 197 | |
| 198 | if (!PyList_Check(files)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | PyErr_SetString(PyExc_TypeError, "FCICreate expects a list"); |
| 200 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 201 | } |
| 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 Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 215 | for (i = 0, p = cabname; *p; p = CharNext(p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (*p == '\\' || *p == '/') |
| 217 | i = p - cabname + 1; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 218 | |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 219 | if (i >= sizeof(ccab.szCabPath) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | strlen(cabname+i) >= sizeof(ccab.szCab)) { |
| 221 | PyErr_SetString(PyExc_ValueError, "path name too long"); |
| 222 | return 0; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 225 | if (i > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | memcpy(ccab.szCabPath, cabname, i); |
| 227 | ccab.szCabPath[i] = '\0'; |
| 228 | strcpy(ccab.szCab, cabname+i); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 229 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | strcpy(ccab.szCabPath, ".\\"); |
| 231 | strcpy(ccab.szCab, cabname); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete, |
| 236 | cb_gettempfile, &ccab, NULL); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 237 | |
| 238 | if (hfci == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); |
| 240 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | for (i=0; i < PyList_GET_SIZE(files); i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | PyObject *item = PyList_GET_ITEM(files, i); |
| 245 | char *filename, *cabname; |
Zachary Ware | 0a29e89 | 2015-05-18 00:47:15 -0500 | [diff] [blame] | 246 | |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | if (!FCIAddFile(hfci, filename, cabname, FALSE, |
| 254 | cb_getnextcabinet, cb_status, cb_getopeninfo, |
| 255 | tcompTYPE_MSZIP)) |
| 256 | goto err; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | goto err; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 261 | |
| 262 | if (!FCIDestroy(hfci)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | goto err; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 264 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 265 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 266 | err: |
Zachary Ware | 0a29e89 | 2015-05-18 00:47:15 -0500 | [diff] [blame] | 267 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 272 | FCIDestroy(hfci); |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | typedef struct msiobj{ |
| 277 | PyObject_HEAD |
| 278 | MSIHANDLE h; |
| 279 | }msiobj; |
| 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | static void |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 282 | msiobj_dealloc(msiobj* msidb) |
| 283 | { |
| 284 | MsiCloseHandle(msidb->h); |
| 285 | msidb->h = 0; |
Zackery Spytz | cb04f75 | 2017-11-07 03:03:09 -0700 | [diff] [blame] | 286 | PyObject_Del(msidb); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static PyObject* |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 290 | msierror(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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | 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 Peksag | 4864a61 | 2017-11-24 12:53:58 +0300 | [diff] [blame] | 318 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | default: |
| 325 | PyErr_Format(MSIError, "unknown error %x", status); |
| 326 | return NULL; |
| 327 | } |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | code = MsiRecordGetInteger(err, 1); /* XXX code */ |
| 331 | if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | res = malloc(size+1); |
Zackery Spytz | 4e51937 | 2018-09-07 16:02:56 -0600 | [diff] [blame] | 333 | if (res == NULL) { |
| 334 | MsiCloseHandle(err); |
| 335 | return PyErr_NoMemory(); |
| 336 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | MsiFormatRecord(0, err, res, &size); |
| 338 | res[size]='\0'; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 339 | } |
| 340 | MsiCloseHandle(err); |
| 341 | PyErr_SetString(MSIError, res); |
| 342 | if (res != buf) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | free(res); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 344 | return NULL; |
| 345 | } |
| 346 | |
Berker Peksag | a935654 | 2017-11-07 15:58:53 +0300 | [diff] [blame] | 347 | static PyObject* |
| 348 | msidb_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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 358 | /*************************** Record objects **********************/ |
| 359 | |
| 360 | static PyObject* |
| 361 | record_getfieldcount(msiobj* record, PyObject* args) |
| 362 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 363 | return PyLong_FromLong(MsiRecordGetFieldCount(record->h)); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static PyObject* |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 367 | record_getinteger(msiobj* record, PyObject* args) |
| 368 | { |
| 369 | unsigned int field; |
| 370 | int status; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 372 | 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öwis | 704d8b1 | 2008-06-02 11:32:23 +0000 | [diff] [blame] | 379 | return PyLong_FromLong((long) status); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static PyObject* |
| 383 | record_getstring(msiobj* record, PyObject* args) |
| 384 | { |
| 385 | unsigned int field; |
| 386 | unsigned int status; |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 387 | WCHAR buf[2000]; |
| 388 | WCHAR *res = buf; |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 389 | DWORD size = sizeof(buf); |
| 390 | PyObject* string; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 392 | if (!PyArg_ParseTuple(args, "I:GetString", &field)) |
| 393 | return NULL; |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 394 | status = MsiRecordGetStringW(record->h, field, res, &size); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 395 | if (status == ERROR_MORE_DATA) { |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 396 | res = (WCHAR*) malloc((size + 1)*sizeof(WCHAR)); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 397 | if (res == NULL) |
| 398 | return PyErr_NoMemory(); |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 399 | status = MsiRecordGetStringW(record->h, field, res, &size); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 400 | } |
| 401 | if (status != ERROR_SUCCESS) |
| 402 | return msierror((int) status); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 403 | string = PyUnicode_FromWideChar(res, size); |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 404 | if (buf != res) |
| 405 | free(res); |
| 406 | return string; |
| 407 | } |
| 408 | |
| 409 | static PyObject* |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 410 | record_cleardata(msiobj* record, PyObject *args) |
| 411 | { |
| 412 | int status = MsiRecordClearData(record->h); |
| 413 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 415 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 416 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static PyObject* |
| 420 | record_setstring(msiobj* record, PyObject *args) |
| 421 | { |
| 422 | int status; |
| 423 | int field; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 424 | wchar_t *data; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 425 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 426 | if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 428 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 429 | if ((status = MsiRecordSetStringW(record->h, field, data)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 431 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 432 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static PyObject* |
| 436 | record_setstream(msiobj* record, PyObject *args) |
| 437 | { |
| 438 | int status; |
| 439 | int field; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 440 | wchar_t *data; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 441 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 442 | if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 444 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 445 | if ((status = MsiRecordSetStreamW(record->h, field, data)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 447 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 448 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static PyObject* |
| 452 | record_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 460 | |
| 461 | if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 463 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 464 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
| 468 | |
| 469 | static PyMethodDef record_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS, |
| 471 | PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")}, |
Martin v. Löwis | e95593e | 2008-06-02 10:08:54 +0000 | [diff] [blame] | 472 | { "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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | { "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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 484 | { NULL, NULL } |
| 485 | }; |
| 486 | |
| 487 | static PyTypeObject record_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 503 | 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 | |
| 531 | static PyObject* |
| 532 | record_new(MSIHANDLE h) |
| 533 | { |
| 534 | msiobj *result = PyObject_NEW(struct msiobj, &record_Type); |
| 535 | |
| 536 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | MsiCloseHandle(h); |
| 538 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | result->h = h; |
| 542 | return (PyObject*)result; |
| 543 | } |
| 544 | |
| 545 | /*************************** SummaryInformation objects **************/ |
| 546 | |
| 547 | static PyObject* |
| 548 | summary_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 Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 558 | DWORD ssize = sizeof(sbuf); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 559 | |
| 560 | if (!PyArg_ParseTuple(args, "i:GetProperty", &field)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival, |
| 564 | &fval, sval, &ssize); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 565 | if (status == ERROR_MORE_DATA) { |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 566 | ssize++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | sval = malloc(ssize); |
Zackery Spytz | 4e51937 | 2018-09-07 16:02:56 -0600 | [diff] [blame] | 568 | if (sval == NULL) { |
| 569 | return PyErr_NoMemory(); |
| 570 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival, |
| 572 | &fval, sval, &ssize); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | switch(type) { |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 576 | case VT_I2: |
| 577 | case VT_I4: |
| 578 | result = PyLong_FromLong(ival); |
| 579 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | case VT_FILETIME: |
| 581 | PyErr_SetString(PyExc_NotImplementedError, "FILETIME result"); |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 582 | result = NULL; |
| 583 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | case VT_LPSTR: |
| 585 | result = PyBytes_FromStringAndSize(sval, ssize); |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 586 | break; |
Berker Peksag | 19fb134 | 2017-11-24 18:11:18 +0300 | [diff] [blame] | 587 | case VT_EMPTY: |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 588 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 595 | } |
Tzu-ping Chung | 2de576e | 2019-02-03 01:13:23 +0800 | [diff] [blame] | 596 | if (sval != sbuf) |
| 597 | free(sval); |
| 598 | return result; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static PyObject* |
| 602 | summary_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 610 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 611 | return PyLong_FromLong(result); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static PyObject* |
| 615 | summary_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 623 | |
Martin v. Löwis | 371bb50 | 2008-08-16 13:02:57 +0000 | [diff] [blame] | 624 | if (PyUnicode_Check(data)) { |
Serhiy Storchaka | ccdc09e | 2017-06-28 09:55:22 +0300 | [diff] [blame] | 625 | const WCHAR *value = _PyUnicode_AsUnicode(data); |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 626 | if (value == NULL) { |
| 627 | return NULL; |
| 628 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | status = MsiSummaryInfoSetPropertyW(si->h, field, VT_LPSTR, |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 630 | 0, NULL, value); |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 631 | } else if (PyLong_CheckExact(data)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 638 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | PyErr_SetString(PyExc_TypeError, "unsupported type"); |
| 640 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 641 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 643 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 645 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 646 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | static PyObject* |
| 651 | summary_persist(msiobj* si, PyObject *args) |
| 652 | { |
| 653 | int status; |
| 654 | |
| 655 | status = MsiSummaryInfoPersist(si->h); |
| 656 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | return msierror(status); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 658 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static PyMethodDef summary_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | { "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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 670 | { NULL, NULL } |
| 671 | }; |
| 672 | |
| 673 | static PyTypeObject summary_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 689 | 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 | |
| 719 | static PyObject* |
| 720 | view_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 728 | |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 740 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 741 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static PyObject* |
| 745 | view_fetch(msiobj *view, PyObject*args) |
| 746 | { |
| 747 | int status; |
| 748 | MSIHANDLE result; |
| 749 | |
Berker Peksag | bdb8315 | 2017-11-23 15:47:30 +0300 | [diff] [blame] | 750 | status = MsiViewFetch(view->h, &result); |
| 751 | if (status == ERROR_NO_MORE_ITEMS) { |
| 752 | Py_RETURN_NONE; |
| 753 | } else if (status != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | return msierror(status); |
Berker Peksag | bdb8315 | 2017-11-23 15:47:30 +0300 | [diff] [blame] | 755 | } |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 756 | |
| 757 | return record_new(result); |
| 758 | } |
| 759 | |
| 760 | static PyObject* |
| 761 | view_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 769 | |
| 770 | if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 772 | |
| 773 | return record_new(result); |
| 774 | } |
| 775 | |
| 776 | static PyObject* |
| 777 | view_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 785 | |
| 786 | if (data->ob_type != &record_Type) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | PyErr_SetString(PyExc_TypeError, "Modify expects a record object"); |
| 788 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 793 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 794 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static PyObject* |
| 798 | view_close(msiobj *view, PyObject*args) |
| 799 | { |
| 800 | int status; |
| 801 | |
| 802 | if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 804 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 805 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static PyMethodDef view_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | { "Execute", (PyCFunction)view_execute, METH_VARARGS, |
| 810 | PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 811 | { "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 813 | { "Fetch", (PyCFunction)view_fetch, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 815 | { "Modify", (PyCFunction)view_modify, METH_VARARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 817 | { "Close", (PyCFunction)view_close, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | PyDoc_STR("Close() -> result\nWraps MsiViewClose")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 819 | { NULL, NULL } |
| 820 | }; |
| 821 | |
| 822 | static PyTypeObject msiview_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 838 | 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 | |
| 868 | static PyObject* |
| 869 | msidb_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 878 | |
| 879 | if ((status = MsiDatabaseOpenView(msidb->h, sql, &hView)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 881 | |
| 882 | result = PyObject_NEW(struct msiobj, &msiview_Type); |
| 883 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | MsiCloseHandle(hView); |
| 885 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | result->h = hView; |
| 889 | return (PyObject*)result; |
| 890 | } |
| 891 | |
| 892 | static PyObject* |
| 893 | msidb_commit(msiobj *msidb, PyObject *args) |
| 894 | { |
| 895 | int status; |
| 896 | |
| 897 | if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 899 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 900 | Py_RETURN_NONE; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static PyObject* |
| 904 | msidb_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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 913 | |
| 914 | status = MsiGetSummaryInformation(db->h, NULL, count, &result); |
| 915 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 917 | |
| 918 | oresult = PyObject_NEW(struct msiobj, &summary_Type); |
Zackery Spytz | bf94cc7 | 2019-03-07 11:20:13 -0700 | [diff] [blame] | 919 | if (!oresult) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | MsiCloseHandle(result); |
| 921 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | oresult->h = result; |
| 925 | return (PyObject*)oresult; |
| 926 | } |
| 927 | |
| 928 | static PyMethodDef db_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS, |
| 930 | PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 931 | { "Commit", (PyCFunction)msidb_commit, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")}, |
| 933 | { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS, |
| 934 | PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")}, |
Berker Peksag | a935654 | 2017-11-07 15:58:53 +0300 | [diff] [blame] | 935 | { "Close", (PyCFunction)msidb_close, METH_NOARGS, |
| 936 | PyDoc_STR("Close() -> None\nWraps MsiCloseHandle")}, |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 937 | { NULL, NULL } |
| 938 | }; |
| 939 | |
| 940 | static PyTypeObject msidb_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 956 | 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 Dower | 6ceda63 | 2016-09-09 11:56:34 -0700 | [diff] [blame] | 984 | #define Py_NOT_PERSIST(x, flag) \ |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 985 | (x != (SIZE_T)(flag) && \ |
| 986 | x != ((SIZE_T)(flag) | MSIDBOPEN_PATCHFILE)) |
Steve Dower | 6ceda63 | 2016-09-09 11:56:34 -0700 | [diff] [blame] | 987 | |
| 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 995 | static PyObject* msiopendb(PyObject *obj, PyObject *args) |
| 996 | { |
| 997 | int status; |
| 998 | char *path; |
| 999 | int persist; |
| 1000 | MSIHANDLE h; |
| 1001 | msiobj *result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist)) |
| 1003 | return NULL; |
Steve Dower | 6ceda63 | 2016-09-09 11:56:34 -0700 | [diff] [blame] | 1004 | /* 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 Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 1009 | status = MsiOpenDatabase(path, (LPCSTR)(SIZE_T)persist, &h); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1010 | if (status != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | return msierror(status); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1012 | |
| 1013 | result = PyObject_NEW(struct msiobj, &msidb_Type); |
| 1014 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | MsiCloseHandle(h); |
| 1016 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1017 | } |
| 1018 | result->h = h; |
| 1019 | return (PyObject*)result; |
| 1020 | } |
| 1021 | |
| 1022 | static PyObject* |
| 1023 | createrecord(PyObject *o, PyObject *args) |
| 1024 | { |
| 1025 | int count; |
| 1026 | MSIHANDLE h; |
| 1027 | |
| 1028 | if (!PyArg_ParseTuple(args, "i:CreateRecord", &count)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | return NULL; |
| 1030 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1031 | h = MsiCreateRecord(count); |
| 1032 | if (h == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | return msierror(0); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1034 | |
| 1035 | return record_new(h); |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | static PyMethodDef msi_methods[] = { |
| 1040 | {"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | static char msi_doc[] = "Documentation"; |
| 1052 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1053 | |
| 1054 | static struct PyModuleDef _msimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | PyModuleDef_HEAD_INIT, |
| 1056 | "_msi", |
| 1057 | msi_doc, |
| 1058 | -1, |
| 1059 | msi_methods, |
| 1060 | NULL, |
| 1061 | NULL, |
| 1062 | NULL, |
| 1063 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1064 | }; |
| 1065 | |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1066 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1067 | PyInit__msi(void) |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1068 | { |
| 1069 | PyObject *m; |
| 1070 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1071 | m = PyModule_Create(&_msimodule); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1072 | if (m == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | return NULL; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1074 | |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 1075 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1081 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 1082 | PyModule_AddIntMacro(m, MSICOLINFO_NAMES); |
| 1083 | PyModule_AddIntMacro(m, MSICOLINFO_TYPES); |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1084 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 1085 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1098 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 1099 | 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öwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1116 | |
| 1117 | MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL); |
| 1118 | if (!MSIError) |
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 | PyModule_AddObject(m, "MSIError", MSIError); |
Amaury Forgeot d'Arc | bf9e966 | 2008-06-17 21:39:46 +0000 | [diff] [blame] | 1121 | return m; |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1122 | } |