_msi.c 31.4 KB
Newer Older
Martin v. Löwis's avatar
Martin v. Löwis committed
1
/* Helper library for MSI creation with Python.
2
 * Copyright (C) 2005 Martin v. Löwis
3
 * Licensed to PSF under a contributor agreement.
Martin v. Löwis's avatar
Martin v. Löwis committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 */

#include <Python.h>
#include <fci.h>
#include <fcntl.h>
#include <windows.h>
#include <msi.h>
#include <msiquery.h>
#include <msidefs.h>
#include <rpc.h>

static PyObject *MSIError;

static PyObject*
uuidcreate(PyObject* obj, PyObject*args)
{
    UUID result;
Victor Stinner's avatar
Victor Stinner committed
21
    wchar_t *cresult;
Martin v. Löwis's avatar
Martin v. Löwis committed
22
    PyObject *oresult;
23

Martin v. Löwis's avatar
Martin v. Löwis committed
24 25 26 27 28
    /* May return ok, local only, and no address.
       For local only, the documentation says we still get a uuid.
       For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can
       use the result. */
    if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) {
29 30
        PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result");
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
31 32
    }

33
    if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) {
34 35
        PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen");
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
36 37
    }

Victor Stinner's avatar
Victor Stinner committed
38
    oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult));
39
    RpcStringFreeW(&cresult);
Martin v. Löwis's avatar
Martin v. Löwis committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    return oresult;

}

/* FCI callback functions */

static FNFCIALLOC(cb_alloc)
{
    return malloc(cb);
}

static FNFCIFREE(cb_free)
{
    free(memory);
}

static FNFCIOPEN(cb_open)
{
    int result = _open(pszFile, oflag, pmode);
    if (result == -1)
60
        *err = errno;
Martin v. Löwis's avatar
Martin v. Löwis committed
61 62 63 64 65 66 67
    return result;
}

static FNFCIREAD(cb_read)
{
    UINT result = (UINT)_read(hf, memory, cb);
    if (result != cb)
68
        *err = errno;
Martin v. Löwis's avatar
Martin v. Löwis committed
69 70 71 72 73 74 75
    return result;
}

static FNFCIWRITE(cb_write)
{
    UINT result = (UINT)_write(hf, memory, cb);
    if (result != cb)
76
        *err = errno;
Martin v. Löwis's avatar
Martin v. Löwis committed
77 78 79 80 81 82 83
    return result;
}

static FNFCICLOSE(cb_close)
{
    int result = _close(hf);
    if (result != 0)
84
        *err = errno;
Martin v. Löwis's avatar
Martin v. Löwis committed
85 86 87 88 89 90 91
    return result;
}

static FNFCISEEK(cb_seek)
{
    long result = (long)_lseek(hf, dist, seektype);
    if (result == -1)
92
        *err = errno;
Martin v. Löwis's avatar
Martin v. Löwis committed
93 94 95 96 97 98 99
    return result;
}

static FNFCIDELETE(cb_delete)
{
    int result = remove(pszFile);
    if (result != 0)
100
        *err = errno;
Martin v. Löwis's avatar
Martin v. Löwis committed
101 102 103 104 105 106 107 108 109 110 111 112
    return result;
}

static FNFCIFILEPLACED(cb_fileplaced)
{
    return 0;
}

static FNFCIGETTEMPFILE(cb_gettempfile)
{
    char *name = _tempnam("", "tmp");
    if ((name != NULL) && ((int)strlen(name) < cbTempName)) {
113 114 115
        strcpy(pszTempName, name);
        free(name);
        return TRUE;
Martin v. Löwis's avatar
Martin v. Löwis committed
116 117 118 119 120 121 122 123 124
    }

    if (name) free(name);
    return FALSE;
}

static FNFCISTATUS(cb_status)
{
    if (pv) {
125
        _Py_IDENTIFIER(status);
126 127

        PyObject *result = _PyObject_CallMethodId(pv, &PyId_status, "iii", typeStatus, cb1, cb2);
128 129 130
        if (result == NULL)
            return -1;
        Py_DECREF(result);
Martin v. Löwis's avatar
Martin v. Löwis committed
131 132 133 134 135 136 137
    }
    return 0;
}

static FNFCIGETNEXTCABINET(cb_getnextcabinet)
{
    if (pv) {
138
        _Py_IDENTIFIER(getnextcabinet);
139 140

        PyObject *result = _PyObject_CallMethodId(pv, &PyId_getnextcabinet, "i", pccab->iCab);
141 142 143 144 145 146 147 148 149 150 151
        if (result == NULL)
            return -1;
        if (!PyBytes_Check(result)) {
            PyErr_Format(PyExc_TypeError,
                "Incorrect return type %s from getnextcabinet",
                result->ob_type->tp_name);
            Py_DECREF(result);
            return FALSE;
        }
        strncpy(pccab->szCab, PyBytes_AsString(result), sizeof(pccab->szCab));
        return TRUE;
Martin v. Löwis's avatar
Martin v. Löwis committed
152 153 154 155 156 157 158 159 160 161 162 163
    }
    return FALSE;
}

static FNFCIGETOPENINFO(cb_getopeninfo)
{
    BY_HANDLE_FILE_INFORMATION bhfi;
    FILETIME filetime;
    HANDLE handle;

    /* Need Win32 handle to get time stamps */
    handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
164
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Martin v. Löwis's avatar
Martin v. Löwis committed
165
    if (handle == INVALID_HANDLE_VALUE)
166
        return -1;
Martin v. Löwis's avatar
Martin v. Löwis committed
167 168 169

    if (GetFileInformationByHandle(handle, &bhfi) == FALSE)
    {
170 171
        CloseHandle(handle);
        return -1;
Martin v. Löwis's avatar
Martin v. Löwis committed
172 173 174 175 176
    }

    FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime);
    FileTimeToDosDateTime(&filetime, pdate, ptime);

177 178
    *pattribs = (int)(bhfi.dwFileAttributes &
        (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
Martin v. Löwis's avatar
Martin v. Löwis committed
179 180 181 182 183 184 185 186

    CloseHandle(handle);

    return _open(pszName, _O_RDONLY | _O_BINARY);
}

static PyObject* fcicreate(PyObject* obj, PyObject* args)
{
Christian Heimes's avatar
Christian Heimes committed
187
    char *cabname, *p;
Martin v. Löwis's avatar
Martin v. Löwis committed
188 189 190 191
    PyObject *files;
    CCAB ccab;
    HFCI hfci;
    ERF erf;
Christian Heimes's avatar
Christian Heimes committed
192
    Py_ssize_t i;
Martin v. Löwis's avatar
Martin v. Löwis committed
193 194 195


    if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files))
196
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
197 198

    if (!PyList_Check(files)) {
199 200
        PyErr_SetString(PyExc_TypeError, "FCICreate expects a list");
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }

    ccab.cb = INT_MAX; /* no need to split CAB into multiple media */
    ccab.cbFolderThresh = 1000000; /* flush directory after this many bytes */
    ccab.cbReserveCFData = 0;
    ccab.cbReserveCFFolder = 0;
    ccab.cbReserveCFHeader = 0;

    ccab.iCab = 1;
    ccab.iDisk = 1;

    ccab.setID = 0;
    ccab.szDisk[0] = '\0';

Christian Heimes's avatar
Christian Heimes committed
215
    for (i = 0, p = cabname; *p; p = CharNext(p))
216 217
        if (*p == '\\' || *p == '/')
            i = p - cabname + 1;
Martin v. Löwis's avatar
Martin v. Löwis committed
218

Christian Heimes's avatar
Christian Heimes committed
219
    if (i >= sizeof(ccab.szCabPath) ||
220 221 222
        strlen(cabname+i) >= sizeof(ccab.szCab)) {
        PyErr_SetString(PyExc_ValueError, "path name too long");
        return 0;
Martin v. Löwis's avatar
Martin v. Löwis committed
223 224
    }

Christian Heimes's avatar
Christian Heimes committed
225
    if (i > 0) {
226 227 228
        memcpy(ccab.szCabPath, cabname, i);
        ccab.szCabPath[i] = '\0';
        strcpy(ccab.szCab, cabname+i);
Martin v. Löwis's avatar
Martin v. Löwis committed
229
    } else {
230 231
        strcpy(ccab.szCabPath, ".\\");
        strcpy(ccab.szCab, cabname);
Martin v. Löwis's avatar
Martin v. Löwis committed
232 233 234
    }

    hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free,
235 236
        cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete,
        cb_gettempfile, &ccab, NULL);
Martin v. Löwis's avatar
Martin v. Löwis committed
237 238

    if (hfci == NULL) {
239 240
        PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper);
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
241 242 243
    }

    for (i=0; i < PyList_GET_SIZE(files); i++) {
244 245 246 247 248 249 250 251
        PyObject *item = PyList_GET_ITEM(files, i);
        char *filename, *cabname;
        if (!PyArg_ParseTuple(item, "ss", &filename, &cabname))
            goto err;
        if (!FCIAddFile(hfci, filename, cabname, FALSE,
            cb_getnextcabinet, cb_status, cb_getopeninfo,
            tcompTYPE_MSZIP))
            goto err;
Martin v. Löwis's avatar
Martin v. Löwis committed
252 253 254
    }

    if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status))
255
        goto err;
Martin v. Löwis's avatar
Martin v. Löwis committed
256 257

    if (!FCIDestroy(hfci))
258
        goto err;
Martin v. Löwis's avatar
Martin v. Löwis committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272

    Py_INCREF(Py_None);
    return Py_None;
err:
    PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); /* XXX better error type */
    FCIDestroy(hfci);
    return NULL;
}

typedef struct msiobj{
    PyObject_HEAD
    MSIHANDLE h;
}msiobj;

273
static void
Martin v. Löwis's avatar
Martin v. Löwis committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
msiobj_dealloc(msiobj* msidb)
{
    MsiCloseHandle(msidb->h);
    msidb->h = 0;
}

static PyObject*
msiobj_close(msiobj* msidb, PyObject *args)
{
    MsiCloseHandle(msidb->h);
    msidb->h = 0;
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
msierror(int status)
{
    int code;
    char buf[2000];
    char *res = buf;
    DWORD size = sizeof(buf);
    MSIHANDLE err = MsiGetLastErrorRecord();

    if (err == 0) {
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        switch(status) {
        case ERROR_ACCESS_DENIED:
            PyErr_SetString(MSIError, "access denied");
            return NULL;
        case ERROR_FUNCTION_FAILED:
            PyErr_SetString(MSIError, "function failed");
            return NULL;
        case ERROR_INVALID_DATA:
            PyErr_SetString(MSIError, "invalid data");
            return NULL;
        case ERROR_INVALID_HANDLE:
            PyErr_SetString(MSIError, "invalid handle");
            return NULL;
        case ERROR_INVALID_STATE:
            PyErr_SetString(MSIError, "invalid state");
            return NULL;
        case ERROR_INVALID_PARAMETER:
            PyErr_SetString(MSIError, "invalid parameter");
            return NULL;
        default:
            PyErr_Format(MSIError, "unknown error %x", status);
            return NULL;
        }
Martin v. Löwis's avatar
Martin v. Löwis committed
322 323 324 325
    }

    code = MsiRecordGetInteger(err, 1); /* XXX code */
    if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) {
326 327 328
        res = malloc(size+1);
        MsiFormatRecord(0, err, res, &size);
        res[size]='\0';
Martin v. Löwis's avatar
Martin v. Löwis committed
329 330 331 332
    }
    MsiCloseHandle(err);
    PyErr_SetString(MSIError, res);
    if (res != buf)
333
        free(res);
Martin v. Löwis's avatar
Martin v. Löwis committed
334 335 336 337 338 339 340 341
    return NULL;
}

/*************************** Record objects **********************/

static PyObject*
record_getfieldcount(msiobj* record, PyObject* args)
{
342
    return PyLong_FromLong(MsiRecordGetFieldCount(record->h));
Martin v. Löwis's avatar
Martin v. Löwis committed
343 344
}

345 346 347 348 349
static PyObject*
record_getinteger(msiobj* record, PyObject* args)
{
    unsigned int field;
    int status;
350

351 352 353 354 355 356 357
    if (!PyArg_ParseTuple(args, "I:GetInteger", &field))
        return NULL;
    status = MsiRecordGetInteger(record->h, field);
    if (status == MSI_NULL_INTEGER){
        PyErr_SetString(MSIError, "could not convert record field to integer");
        return NULL;
    }
358
    return PyLong_FromLong((long) status);
359 360 361 362 363 364 365
}

static PyObject*
record_getstring(msiobj* record, PyObject* args)
{
    unsigned int field;
    unsigned int status;
366 367
    WCHAR buf[2000];
    WCHAR *res = buf;
368 369
    DWORD size = sizeof(buf);
    PyObject* string;
370

371 372
    if (!PyArg_ParseTuple(args, "I:GetString", &field))
        return NULL;
373
    status = MsiRecordGetStringW(record->h, field, res, &size);
374
    if (status == ERROR_MORE_DATA) {
375
        res = (WCHAR*) malloc((size + 1)*sizeof(WCHAR));
376 377
        if (res == NULL)
            return PyErr_NoMemory();
378
        status = MsiRecordGetStringW(record->h, field, res, &size);
379 380 381
    }
    if (status != ERROR_SUCCESS)
        return msierror((int) status);
Victor Stinner's avatar
Victor Stinner committed
382
    string = PyUnicode_FromWideChar(res, size);
383 384 385 386 387
    if (buf != res)
        free(res);
    return string;
}

Martin v. Löwis's avatar
Martin v. Löwis committed
388 389 390 391 392
static PyObject*
record_cleardata(msiobj* record, PyObject *args)
{
    int status = MsiRecordClearData(record->h);
    if (status != ERROR_SUCCESS)
393
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
394 395 396 397 398 399 400 401 402 403

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
record_setstring(msiobj* record, PyObject *args)
{
    int status;
    int field;
Victor Stinner's avatar
Victor Stinner committed
404
    wchar_t *data;
Martin v. Löwis's avatar
Martin v. Löwis committed
405

406
    if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data))
407
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
408

409
    if ((status = MsiRecordSetStringW(record->h, field, data)) != ERROR_SUCCESS)
410
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
411 412 413 414 415 416 417 418 419 420

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
record_setstream(msiobj* record, PyObject *args)
{
    int status;
    int field;
Victor Stinner's avatar
Victor Stinner committed
421
    wchar_t *data;
Martin v. Löwis's avatar
Martin v. Löwis committed
422

423
    if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data))
424
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
425

426
    if ((status = MsiRecordSetStreamW(record->h, field, data)) != ERROR_SUCCESS)
427
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
428 429 430 431 432 433 434 435 436 437 438 439 440

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
record_setinteger(msiobj* record, PyObject *args)
{
    int status;
    int field;
    int data;

    if (!PyArg_ParseTuple(args, "ii:SetInteger", &field, &data))
441
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
442 443

    if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS)
444
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
445 446 447 448 449 450 451 452

    Py_INCREF(Py_None);
    return Py_None;
}



static PyMethodDef record_methods[] = {
453 454
    { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS,
        PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")},
455 456 457 458
    { "GetInteger", (PyCFunction)record_getinteger, METH_VARARGS,
    PyDoc_STR("GetInteger(field) -> int\nWraps MsiRecordGetInteger")},
    { "GetString", (PyCFunction)record_getstring, METH_VARARGS,
    PyDoc_STR("GetString(field) -> string\nWraps MsiRecordGetString")},
459 460 461 462 463 464 465 466
    { "SetString", (PyCFunction)record_setstring, METH_VARARGS,
        PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")},
    { "SetStream", (PyCFunction)record_setstream, METH_VARARGS,
        PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")},
    { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS,
        PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")},
    { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS,
        PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")},
Martin v. Löwis's avatar
Martin v. Löwis committed
467 468 469 470
    { NULL, NULL }
};

static PyTypeObject record_Type = {
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
        PyVarObject_HEAD_INIT(NULL, 0)
        "_msi.Record",          /*tp_name*/
        sizeof(msiobj), /*tp_basicsize*/
        0,                      /*tp_itemsize*/
        /* methods */
        (destructor)msiobj_dealloc, /*tp_dealloc*/
        0,                      /*tp_print*/
        0,                      /*tp_getattr*/
        0,                      /*tp_setattr*/
        0,                      /*tp_reserved*/
        0,                      /*tp_repr*/
        0,                      /*tp_as_number*/
        0,                      /*tp_as_sequence*/
        0,                      /*tp_as_mapping*/
        0,                      /*tp_hash*/
Martin v. Löwis's avatar
Martin v. Löwis committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        0,                      /*tp_call*/
        0,                      /*tp_str*/
        PyObject_GenericGetAttr,/*tp_getattro*/
        PyObject_GenericSetAttr,/*tp_setattro*/
        0,                      /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,     /*tp_flags*/
        0,                      /*tp_doc*/
        0,                      /*tp_traverse*/
        0,                      /*tp_clear*/
        0,                      /*tp_richcompare*/
        0,                      /*tp_weaklistoffset*/
        0,                      /*tp_iter*/
        0,                      /*tp_iternext*/
        record_methods,           /*tp_methods*/
        0,                      /*tp_members*/
        0,                      /*tp_getset*/
        0,                      /*tp_base*/
        0,                      /*tp_dict*/
        0,                      /*tp_descr_get*/
        0,                      /*tp_descr_set*/
        0,                      /*tp_dictoffset*/
        0,                      /*tp_init*/
        0,                      /*tp_alloc*/
        0,                      /*tp_new*/
        0,                      /*tp_free*/
        0,                      /*tp_is_gc*/
};

static PyObject*
record_new(MSIHANDLE h)
{
    msiobj *result = PyObject_NEW(struct msiobj, &record_Type);

    if (!result) {
520 521
        MsiCloseHandle(h);
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    }

    result->h = h;
    return (PyObject*)result;
}

/*************************** SummaryInformation objects **************/

static PyObject*
summary_getproperty(msiobj* si, PyObject *args)
{
    int status;
    int field;
    PyObject *result;
    UINT type;
    INT ival;
    FILETIME fval;
    char sbuf[1000];
    char *sval = sbuf;
    DWORD ssize = sizeof(sval);

    if (!PyArg_ParseTuple(args, "i:GetProperty", &field))
544
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
545

546 547
    status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
        &fval, sval, &ssize);
548
    if (status == ERROR_MORE_DATA) {
549 550 551
        sval = malloc(ssize);
        status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
            &fval, sval, &ssize);
Martin v. Löwis's avatar
Martin v. Löwis committed
552 553 554
    }

    switch(type) {
555 556 557 558 559 560 561 562 563 564
        case VT_I2: case VT_I4:
            return PyLong_FromLong(ival);
        case VT_FILETIME:
            PyErr_SetString(PyExc_NotImplementedError, "FILETIME result");
            return NULL;
        case VT_LPSTR:
            result = PyBytes_FromStringAndSize(sval, ssize);
            if (sval != sbuf)
                free(sval);
            return result;
Martin v. Löwis's avatar
Martin v. Löwis committed
565 566 567 568 569 570 571 572 573 574 575 576 577
    }
    PyErr_Format(PyExc_NotImplementedError, "result of type %d", type);
    return NULL;
}

static PyObject*
summary_getpropertycount(msiobj* si, PyObject *args)
{
    int status;
    UINT result;

    status = MsiSummaryInfoGetPropertyCount(si->h, &result);
    if (status != ERROR_SUCCESS)
578
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
579

580
    return PyLong_FromLong(result);
Martin v. Löwis's avatar
Martin v. Löwis committed
581 582 583 584 585 586 587 588 589 590
}

static PyObject*
summary_setproperty(msiobj* si, PyObject *args)
{
    int status;
    int field;
    PyObject* data;

    if (!PyArg_ParseTuple(args, "iO:SetProperty", &field, &data))
591
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
592

593
    if (PyUnicode_Check(data)) {
594 595
        status = MsiSummaryInfoSetPropertyW(si->h, field, VT_LPSTR,
            0, NULL, PyUnicode_AsUnicode(data));
596
    } else if (PyLong_CheckExact(data)) {
597 598 599 600 601 602
        long value = PyLong_AsLong(data);
        if (value == -1 && PyErr_Occurred()) {
            return NULL;
        }
        status = MsiSummaryInfoSetProperty(si->h, field, VT_I4,
            value, NULL, NULL);
Martin v. Löwis's avatar
Martin v. Löwis committed
603
    } else {
604 605
        PyErr_SetString(PyExc_TypeError, "unsupported type");
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
606
    }
607

Martin v. Löwis's avatar
Martin v. Löwis committed
608
    if (status != ERROR_SUCCESS)
609
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
610 611 612 613 614 615 616 617 618 619 620 621 622

    Py_INCREF(Py_None);
    return Py_None;
}


static PyObject*
summary_persist(msiobj* si, PyObject *args)
{
    int status;

    status = MsiSummaryInfoPersist(si->h);
    if (status != ERROR_SUCCESS)
623
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
624 625 626 627 628
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef summary_methods[] = {
629 630 631 632 633 634 635 636
    { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS,
        PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")},
    { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS,
        PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")},
    { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS,
        PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")},
    { "Persist", (PyCFunction)summary_persist, METH_NOARGS,
        PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")},
Martin v. Löwis's avatar
Martin v. Löwis committed
637 638 639 640
    { NULL, NULL }
};

static PyTypeObject summary_Type = {
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        PyVarObject_HEAD_INIT(NULL, 0)
        "_msi.SummaryInformation",              /*tp_name*/
        sizeof(msiobj), /*tp_basicsize*/
        0,                      /*tp_itemsize*/
        /* methods */
        (destructor)msiobj_dealloc, /*tp_dealloc*/
        0,                      /*tp_print*/
        0,                      /*tp_getattr*/
        0,                      /*tp_setattr*/
        0,                      /*tp_reserved*/
        0,                      /*tp_repr*/
        0,                      /*tp_as_number*/
        0,                      /*tp_as_sequence*/
        0,                      /*tp_as_mapping*/
        0,                      /*tp_hash*/
Martin v. Löwis's avatar
Martin v. Löwis committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
        0,                      /*tp_call*/
        0,                      /*tp_str*/
        PyObject_GenericGetAttr,/*tp_getattro*/
        PyObject_GenericSetAttr,/*tp_setattro*/
        0,                      /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,     /*tp_flags*/
        0,                      /*tp_doc*/
        0,                      /*tp_traverse*/
        0,                      /*tp_clear*/
        0,                      /*tp_richcompare*/
        0,                      /*tp_weaklistoffset*/
        0,                      /*tp_iter*/
        0,                      /*tp_iternext*/
        summary_methods,        /*tp_methods*/
        0,                      /*tp_members*/
        0,                      /*tp_getset*/
        0,                      /*tp_base*/
        0,                      /*tp_dict*/
        0,                      /*tp_descr_get*/
        0,                      /*tp_descr_set*/
        0,                      /*tp_dictoffset*/
        0,                      /*tp_init*/
        0,                      /*tp_alloc*/
        0,                      /*tp_new*/
        0,                      /*tp_free*/
        0,                      /*tp_is_gc*/
};

/*************************** View objects **************/

static PyObject*
view_execute(msiobj *view, PyObject*args)
{
    int status;
    MSIHANDLE params = 0;
    PyObject *oparams = Py_None;

    if (!PyArg_ParseTuple(args, "O:Execute", &oparams))
694
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
695 696 697 698 699 700 701 702 703 704 705

    if (oparams != Py_None) {
        if (oparams->ob_type != &record_Type) {
            PyErr_SetString(PyExc_TypeError, "Execute argument must be a record");
            return NULL;
        }
        params = ((msiobj*)oparams)->h;
    }

    status = MsiViewExecute(view->h, params);
    if (status != ERROR_SUCCESS)
706
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
707 708 709 710 711 712 713 714 715 716 717 718

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
view_fetch(msiobj *view, PyObject*args)
{
    int status;
    MSIHANDLE result;

    if ((status = MsiViewFetch(view->h, &result)) != ERROR_SUCCESS)
719
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
720 721 722 723 724 725 726 727 728 729 730 731

    return record_new(result);
}

static PyObject*
view_getcolumninfo(msiobj *view, PyObject *args)
{
    int status;
    int kind;
    MSIHANDLE result;

    if (!PyArg_ParseTuple(args, "i:GetColumnInfo", &kind))
732
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
733 734

    if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS)
735
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
736 737 738 739 740 741 742 743 744 745 746 747

    return record_new(result);
}

static PyObject*
view_modify(msiobj *view, PyObject *args)
{
    int kind;
    PyObject *data;
    int status;

    if (!PyArg_ParseTuple(args, "iO:Modify", &kind, &data))
748
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
749 750

    if (data->ob_type != &record_Type) {
751 752
        PyErr_SetString(PyExc_TypeError, "Modify expects a record object");
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
753 754 755
    }

    if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS)
756
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
757 758 759 760 761 762 763 764 765 766 767

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
view_close(msiobj *view, PyObject*args)
{
    int status;

    if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS)
768
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
769 770 771 772 773 774

    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef view_methods[] = {
775 776
    { "Execute", (PyCFunction)view_execute, METH_VARARGS,
        PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")},
Martin v. Löwis's avatar
Martin v. Löwis committed
777
    { "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS,
778
        PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")},
Martin v. Löwis's avatar
Martin v. Löwis committed
779
    { "Fetch", (PyCFunction)view_fetch, METH_NOARGS,
780
        PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")},
Martin v. Löwis's avatar
Martin v. Löwis committed
781
    { "Modify", (PyCFunction)view_modify, METH_VARARGS,
782
        PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")},
Martin v. Löwis's avatar
Martin v. Löwis committed
783
    { "Close", (PyCFunction)view_close, METH_NOARGS,
784
        PyDoc_STR("Close() -> result\nWraps MsiViewClose")},
Martin v. Löwis's avatar
Martin v. Löwis committed
785 786 787 788
    { NULL, NULL }
};

static PyTypeObject msiview_Type = {
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
        PyVarObject_HEAD_INIT(NULL, 0)
        "_msi.View",            /*tp_name*/
        sizeof(msiobj), /*tp_basicsize*/
        0,                      /*tp_itemsize*/
        /* methods */
        (destructor)msiobj_dealloc, /*tp_dealloc*/
        0,                      /*tp_print*/
        0,                      /*tp_getattr*/
        0,                      /*tp_setattr*/
        0,                      /*tp_reserved*/
        0,                      /*tp_repr*/
        0,                      /*tp_as_number*/
        0,                      /*tp_as_sequence*/
        0,                      /*tp_as_mapping*/
        0,                      /*tp_hash*/
Martin v. Löwis's avatar
Martin v. Löwis committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
        0,                      /*tp_call*/
        0,                      /*tp_str*/
        PyObject_GenericGetAttr,/*tp_getattro*/
        PyObject_GenericSetAttr,/*tp_setattro*/
        0,                      /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,     /*tp_flags*/
        0,                      /*tp_doc*/
        0,                      /*tp_traverse*/
        0,                      /*tp_clear*/
        0,                      /*tp_richcompare*/
        0,                      /*tp_weaklistoffset*/
        0,                      /*tp_iter*/
        0,                      /*tp_iternext*/
        view_methods,           /*tp_methods*/
        0,                      /*tp_members*/
        0,                      /*tp_getset*/
        0,                      /*tp_base*/
        0,                      /*tp_dict*/
        0,                      /*tp_descr_get*/
        0,                      /*tp_descr_set*/
        0,                      /*tp_dictoffset*/
        0,                      /*tp_init*/
        0,                      /*tp_alloc*/
        0,                      /*tp_new*/
        0,                      /*tp_free*/
        0,                      /*tp_is_gc*/
};

/*************************** Database objects **************/

static PyObject*
msidb_openview(msiobj *msidb, PyObject *args)
{
    int status;
    char *sql;
    MSIHANDLE hView;
    msiobj *result;

    if (!PyArg_ParseTuple(args, "s:OpenView", &sql))
843
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
844 845

    if ((status = MsiDatabaseOpenView(msidb->h, sql, &hView)) != ERROR_SUCCESS)
846
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
847 848 849

    result = PyObject_NEW(struct msiobj, &msiview_Type);
    if (!result) {
850 851
        MsiCloseHandle(hView);
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
852 853 854 855 856 857 858 859 860 861 862 863
    }

    result->h = hView;
    return (PyObject*)result;
}

static PyObject*
msidb_commit(msiobj *msidb, PyObject *args)
{
    int status;

    if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS)
864
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
msidb_getsummaryinformation(msiobj *db, PyObject *args)
{
    int status;
    int count;
    MSIHANDLE result;
    msiobj *oresult;

    if (!PyArg_ParseTuple(args, "i:GetSummaryInformation", &count))
879
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
880 881 882

    status = MsiGetSummaryInformation(db->h, NULL, count, &result);
    if (status != ERROR_SUCCESS)
883
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
884 885 886

    oresult = PyObject_NEW(struct msiobj, &summary_Type);
    if (!result) {
887 888
        MsiCloseHandle(result);
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
889 890 891 892 893 894 895
    }

    oresult->h = result;
    return (PyObject*)oresult;
}

static PyMethodDef db_methods[] = {
896 897
    { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS,
        PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")},
Martin v. Löwis's avatar
Martin v. Löwis committed
898
    { "Commit", (PyCFunction)msidb_commit, METH_NOARGS,
899 900 901
        PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")},
    { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS,
        PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")},
Martin v. Löwis's avatar
Martin v. Löwis committed
902 903 904 905
    { NULL, NULL }
};

static PyTypeObject msidb_Type = {
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
        PyVarObject_HEAD_INIT(NULL, 0)
        "_msi.Database",                /*tp_name*/
        sizeof(msiobj), /*tp_basicsize*/
        0,                      /*tp_itemsize*/
        /* methods */
        (destructor)msiobj_dealloc, /*tp_dealloc*/
        0,                      /*tp_print*/
        0,                      /*tp_getattr*/
        0,                      /*tp_setattr*/
        0,                      /*tp_reserved*/
        0,                      /*tp_repr*/
        0,                      /*tp_as_number*/
        0,                      /*tp_as_sequence*/
        0,                      /*tp_as_mapping*/
        0,                      /*tp_hash*/
Martin v. Löwis's avatar
Martin v. Löwis committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
        0,                      /*tp_call*/
        0,                      /*tp_str*/
        PyObject_GenericGetAttr,/*tp_getattro*/
        PyObject_GenericSetAttr,/*tp_setattro*/
        0,                      /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,     /*tp_flags*/
        0,                      /*tp_doc*/
        0,                      /*tp_traverse*/
        0,                      /*tp_clear*/
        0,                      /*tp_richcompare*/
        0,                      /*tp_weaklistoffset*/
        0,                      /*tp_iter*/
        0,                      /*tp_iternext*/
        db_methods,             /*tp_methods*/
        0,                      /*tp_members*/
        0,                      /*tp_getset*/
        0,                      /*tp_base*/
        0,                      /*tp_dict*/
        0,                      /*tp_descr_get*/
        0,                      /*tp_descr_set*/
        0,                      /*tp_dictoffset*/
        0,                      /*tp_init*/
        0,                      /*tp_alloc*/
        0,                      /*tp_new*/
        0,                      /*tp_free*/
        0,                      /*tp_is_gc*/
};

static PyObject* msiopendb(PyObject *obj, PyObject *args)
{
    int status;
    char *path;
    int persist;
    MSIHANDLE h;
    msiobj *result;
956

Martin v. Löwis's avatar
Martin v. Löwis committed
957
    if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist))
958
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
959

960
        status = MsiOpenDatabase(path, (LPCSTR)persist, &h);
Martin v. Löwis's avatar
Martin v. Löwis committed
961
    if (status != ERROR_SUCCESS)
962
        return msierror(status);
Martin v. Löwis's avatar
Martin v. Löwis committed
963 964 965

    result = PyObject_NEW(struct msiobj, &msidb_Type);
    if (!result) {
966 967
        MsiCloseHandle(h);
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
968 969 970 971 972 973 974 975 976 977 978 979
    }
    result->h = h;
    return (PyObject*)result;
}

static PyObject*
createrecord(PyObject *o, PyObject *args)
{
    int count;
    MSIHANDLE h;

    if (!PyArg_ParseTuple(args, "i:CreateRecord", &count))
980 981
        return NULL;

Martin v. Löwis's avatar
Martin v. Löwis committed
982 983
    h = MsiCreateRecord(count);
    if (h == 0)
984
        return msierror(0);
Martin v. Löwis's avatar
Martin v. Löwis committed
985 986 987 988 989 990 991

    return record_new(h);
}


static PyMethodDef msi_methods[] = {
        {"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS,
992 993 994 995 996 997 998 999
                PyDoc_STR("UuidCreate() -> string")},
        {"FCICreate",   (PyCFunction)fcicreate, METH_VARARGS,
                PyDoc_STR("fcicreate(cabname,files) -> None")},
        {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS,
        PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")},
        {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS,
        PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")},
        {NULL,          NULL}           /* sentinel */
Martin v. Löwis's avatar
Martin v. Löwis committed
1000 1001 1002 1003
};

static char msi_doc[] = "Documentation";

1004 1005

static struct PyModuleDef _msimodule = {
1006 1007 1008 1009 1010 1011 1012 1013 1014
        PyModuleDef_HEAD_INIT,
        "_msi",
        msi_doc,
        -1,
        msi_methods,
        NULL,
        NULL,
        NULL,
        NULL
1015 1016
};

Martin v. Löwis's avatar
Martin v. Löwis committed
1017
PyMODINIT_FUNC
1018
PyInit__msi(void)
Martin v. Löwis's avatar
Martin v. Löwis committed
1019 1020 1021
{
    PyObject *m;

1022
    m = PyModule_Create(&_msimodule);
Martin v. Löwis's avatar
Martin v. Löwis committed
1023
    if (m == NULL)
1024
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

    PyModule_AddIntConstant(m, "MSIDBOPEN_CREATEDIRECT", (int)MSIDBOPEN_CREATEDIRECT);
    PyModule_AddIntConstant(m, "MSIDBOPEN_CREATE", (int)MSIDBOPEN_CREATE);
    PyModule_AddIntConstant(m, "MSIDBOPEN_DIRECT", (int)MSIDBOPEN_DIRECT);
    PyModule_AddIntConstant(m, "MSIDBOPEN_READONLY", (int)MSIDBOPEN_READONLY);
    PyModule_AddIntConstant(m, "MSIDBOPEN_TRANSACT", (int)MSIDBOPEN_TRANSACT);
    PyModule_AddIntConstant(m, "MSIDBOPEN_PATCHFILE", (int)MSIDBOPEN_PATCHFILE);

    PyModule_AddIntConstant(m, "MSICOLINFO_NAMES", MSICOLINFO_NAMES);
    PyModule_AddIntConstant(m, "MSICOLINFO_TYPES", MSICOLINFO_TYPES);

    PyModule_AddIntConstant(m, "MSIMODIFY_SEEK", MSIMODIFY_SEEK);
    PyModule_AddIntConstant(m, "MSIMODIFY_REFRESH", MSIMODIFY_REFRESH);
    PyModule_AddIntConstant(m, "MSIMODIFY_INSERT", MSIMODIFY_INSERT);
    PyModule_AddIntConstant(m, "MSIMODIFY_UPDATE", MSIMODIFY_UPDATE);
    PyModule_AddIntConstant(m, "MSIMODIFY_ASSIGN", MSIMODIFY_ASSIGN);
    PyModule_AddIntConstant(m, "MSIMODIFY_REPLACE", MSIMODIFY_REPLACE);
    PyModule_AddIntConstant(m, "MSIMODIFY_MERGE", MSIMODIFY_MERGE);
    PyModule_AddIntConstant(m, "MSIMODIFY_DELETE", MSIMODIFY_DELETE);
    PyModule_AddIntConstant(m, "MSIMODIFY_INSERT_TEMPORARY", MSIMODIFY_INSERT_TEMPORARY);
    PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE", MSIMODIFY_VALIDATE);
    PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_NEW", MSIMODIFY_VALIDATE_NEW);
    PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_FIELD", MSIMODIFY_VALIDATE_FIELD);
    PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_DELETE", MSIMODIFY_VALIDATE_DELETE);

    PyModule_AddIntConstant(m, "PID_CODEPAGE", PID_CODEPAGE);
    PyModule_AddIntConstant(m, "PID_TITLE", PID_TITLE);
    PyModule_AddIntConstant(m, "PID_SUBJECT", PID_SUBJECT);
    PyModule_AddIntConstant(m, "PID_AUTHOR", PID_AUTHOR);
    PyModule_AddIntConstant(m, "PID_KEYWORDS", PID_KEYWORDS);
    PyModule_AddIntConstant(m, "PID_COMMENTS", PID_COMMENTS);
    PyModule_AddIntConstant(m, "PID_TEMPLATE", PID_TEMPLATE);
    PyModule_AddIntConstant(m, "PID_LASTAUTHOR", PID_LASTAUTHOR);
    PyModule_AddIntConstant(m, "PID_REVNUMBER", PID_REVNUMBER);
    PyModule_AddIntConstant(m, "PID_LASTPRINTED", PID_LASTPRINTED);
    PyModule_AddIntConstant(m, "PID_CREATE_DTM", PID_CREATE_DTM);
    PyModule_AddIntConstant(m, "PID_LASTSAVE_DTM", PID_LASTSAVE_DTM);
    PyModule_AddIntConstant(m, "PID_PAGECOUNT", PID_PAGECOUNT);
    PyModule_AddIntConstant(m, "PID_WORDCOUNT", PID_WORDCOUNT);
    PyModule_AddIntConstant(m, "PID_CHARCOUNT", PID_CHARCOUNT);
    PyModule_AddIntConstant(m, "PID_APPNAME", PID_APPNAME);
    PyModule_AddIntConstant(m, "PID_SECURITY", PID_SECURITY);

    MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL);
    if (!MSIError)
1070
        return NULL;
Martin v. Löwis's avatar
Martin v. Löwis committed
1071
    PyModule_AddObject(m, "MSIError", MSIError);
1072
    return m;
Martin v. Löwis's avatar
Martin v. Löwis committed
1073
}