summaryrefslogtreecommitdiff
path: root/setup_native/source/win32/wintools/msiinfo/msiinfo.c
blob: f2d388b2fb10c5a0ab604624fc12245f7b428cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <msi.h>
#include <msiquery.h>
#include <msidefs.h>
#include <wchar.h>

#define WINE_NO_TRACE_MSGS 1
#define WINE_NO_DEBUG_MSGS 1

#include "wine/unicode.h"
#include "wine/debug.h"
#include "wtypes.h"

#define MAX_TABLES 50
#define MAX_STREAMS 10
#define MAX_STORAGES 10
#define MAX_TABLE_NAME 50
#define MAX_STREAM_NAME 62
#define MAX_STORAGE_NAME 31
#define COMMANDS 17

static const unsigned commandMap[COMMANDS][4] =
{{'c', PID_CODEPAGE, VT_I2},
 {'t', PID_TITLE, VT_LPSTR},
 {'j', PID_SUBJECT, VT_LPSTR},
 {'a', PID_AUTHOR, VT_LPSTR},
 {'k', PID_KEYWORDS, VT_LPSTR},
 {'o', PID_COMMENTS, VT_LPSTR},
 {'p', PID_TEMPLATE, VT_LPSTR},
 {'l', PID_LASTAUTHOR, VT_LPSTR},
 {'v', PID_REVNUMBER, VT_LPSTR},
 {'s', PID_LASTPRINTED, VT_FILETIME},
 {'r', PID_CREATE_DTM, VT_FILETIME},
 {'q', PID_LASTSAVE_DTM, VT_FILETIME},
 {'g', PID_PAGECOUNT, VT_I4},
 {'w', PID_WORDCOUNT, VT_I4},
 {'h', PID_CHARCOUNT, VT_I4},
 {'n', PID_APPNAME, VT_LPSTR},
 {'u', PID_SECURITY, VT_I4}};

static const char * commandNames[COMMANDS] =
{"Codepage", "Title", "Subject", "Author", "Keywords", "Comments",
 "Template", "Last Saved By", "Revision Number", "Last Printed",
 "Create Time/Date", "Last Save Time/Date", "Page Count", "Word Count",
 "Character Count", "Creating Application", "Security"};

static void parseDate(LPCWSTR value, SYSTEMTIME *time)
{
    LPWSTR field;
    unsigned len = 4*sizeof(WCHAR);

    if (lstrlenW(value) != 19) return;
    field = malloc(len + sizeof(WCHAR));
    if (field == NULL) return;
    memcpy(field, value, len);
    field[4] = L'\0';
    time->wYear = atoiW(field);

    len = 2 * sizeof(WCHAR);
    memcpy(field, &(value[5]), len);
    field[2] = '\0';
    time->wMonth = atoiW(field);

    memcpy(field, &(value[8]), len);
    time->wDay = atoiW(field);

    memcpy(field, &(value[11]), len);
    time->wHour = atoiW(field);

    memcpy(field, &(value[14]), len);
    time->wMinute = atoiW(field);

    memcpy(field, &(value[17]), len);
    time->wSecond = atoiW(field);

    free(field);
}

static BOOL msiinfoDisplayProperties(LPWSTR dbfile)
{
    MSIHANDLE dbhandle, infohandle;
    unsigned i, r, dataType;
    INT iVal;
    FILETIME ftVal;
    SYSTEMTIME sysTime;
    LPWSTR szVal = NULL;
    DWORD size;
    r = MsiOpenDatabaseW(dbfile, (LPWSTR) MSIDBOPEN_READONLY, &dbhandle);
    if (r != ERROR_SUCCESS)
    {
        return FALSE;
    }

    r = MsiGetSummaryInformationW(dbhandle, 0, 0, &infohandle);
    if (r != ERROR_SUCCESS)
    {
        return FALSE;
    }
    for (i = 0; i < COMMANDS; i++)
    {
        MsiSummaryInfoGetPropertyW(infohandle, commandMap[i][1], &dataType,
                                   &iVal, &ftVal, szVal, &size);

        wprintf(L"%-24s", commandNames[i]);
        if (dataType == VT_LPSTR)
        {
            if (szVal!=NULL) wprintf(L"%ls (%d)\n", szVal, lstrlenW(szVal));
            else wprintf(L"\n");
        }
        else if (dataType == VT_FILETIME)
        {
            FileTimeToSystemTime(&ftVal, &sysTime);
            wprintf(L"%04d/%02d/%02d %02d:%02d:%02d\n", sysTime.wYear, sysTime.wMonth,
                    sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
        }
        else
        {
            wprintf(L"%d\n", iVal);
        }
    }

    MsiCloseHandle(infohandle);
    return TRUE;
}

static BOOL msiinfoUpdateProperty(MSIHANDLE infoHandle, unsigned property, unsigned dataType, LPCWSTR value)
{
    unsigned r;
    int iVal = 0;
    FILETIME ftVal;
    SYSTEMTIME sysTime = {0};
    LPCWSTR szVal = NULL;

    if (dataType == VT_LPSTR) szVal = value;
    else if (dataType == VT_FILETIME)
    {
        parseDate(value, &sysTime);
        SystemTimeToFileTime(&sysTime, &ftVal);
    }
    else
        iVal = atoiW(value);

    r = MsiSummaryInfoSetPropertyW(infoHandle, property, dataType, iVal, &ftVal, szVal);
    if (r != ERROR_SUCCESS)
    {
        wprintf(L"Problem updating property: %d %d %d %d\n", r == ERROR_DATATYPE_MISMATCH, r == ERROR_FUNCTION_FAILED, ERROR_UNKNOWN_PROPERTY, ERROR_UNSUPPORTED_TYPE);
        return FALSE;
    }

    return TRUE;
}

static void usage(void)
{
    wprintf(
        L"Usage: msiinfo {database} [[-b]-d] {options} {data}\n"
        L"\nOptions:\n"
        L"  -c <cp>       Specify codepage\n"
        L"  -t <title>    Specify title\n"
        L"  -j <subject>  Specify subject\n"
        L"  -a <author>   Specify author\n"
        L"  -k <keywords> Specify keywords\n"
        L"  -o <comment>  Specify comments\n"
        L"  -p <template> Specify template\n"
        L"  -l <author>   Specify last author\n"
        L"  -v <revno>    Specify revision number\n"
        L"  -s <date>     Specify last printed date\n"
        L"  -r <date>     Specify creation date\n"
        L"  -q <date>     Specify date of last save\n"
        L"  -g <pages>    Specify page count\n"
        L"  -w <words>    Specify word count\n"
        L"  -h <chars>    Specify character count\n"
        L"  -n <appname>  Specify application which created the database\n"
        L"  -u <security> Specify security (0: none, 2: read only (rec.) 3: read only (enforced)\n");
}

int wmain(int argc, WCHAR *argv[])
{
    WCHAR *dbfile = NULL;
    unsigned i = 0;
    MSIHANDLE dbhandle, infohandle;
    unsigned r;
    LPWSTR value = 0;
    static const WCHAR h1[] = {'/', '?', 0};
    static const WCHAR h2[] = {'-', '?', 0};

    if (argc > 1)
    {
        dbfile = argv[1];
        argv++; argc--;
    }

    if (strcmpW(dbfile, h1) == 0 || strcmpW(dbfile, h2) == 0)
    {
        usage();
        return 0;
    }
    else if (argc == 1)
    {
        msiinfoDisplayProperties(dbfile);
        return 0;
    }

    r = MsiOpenDatabaseW(dbfile, (LPWSTR) MSIDBOPEN_TRANSACT, &dbhandle);
    if (r != ERROR_SUCCESS) return 1;
    r = MsiGetSummaryInformationW(dbhandle, 0, 20, &infohandle);
    if (r != ERROR_SUCCESS) return 2;

    while (argv[1] && argv[1][0] == '-')
    {
        switch (argv[1][1])
        {
        case '?':
        case 'h':
            usage();
            return 0;
        default:
            for (i = 0; i < COMMANDS; i++)
            {
                if (commandMap[i][0] == argv[1][1])
                {
                    argv++; argc--;
                    value = argv[1];
                    msiinfoUpdateProperty(infohandle, commandMap[i][1], commandMap[i][2], value);
                    break;
                }
            }
            break;
        }
        argv++; argc--;
    }

    MsiSummaryInfoPersist(infohandle);
    MsiDatabaseCommit(dbhandle);
    MsiCloseHandle(dbhandle);
    msiinfoDisplayProperties(dbfile);
    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */