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