summaryrefslogtreecommitdiffstats
path: root/tools/msibuild.c
blob: 3f5d318e645a44347f7a8b0507cdf403f2076b36 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * winemsibuilder - tool to build MSI packages
 *
 * Copyright 2010 Hans Leidekker for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <libmsi.h>
#include <limits.h>

#ifdef HAVE_LIBUUID
#include <uuid.h>
#endif

#include "sqldelim.h"

static void init_suminfo(LibmsiSummaryInfo *si)
{
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_TITLE,
                                     LIBMSI_PROPERTY_TYPE_STRING, 0, NULL,
                                     "Installation Database");
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_KEYWORDS,
                                     LIBMSI_PROPERTY_TYPE_STRING, 0, NULL,
                                     "Installer, MSI");
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_TEMPLATE,
                                     LIBMSI_PROPERTY_TYPE_STRING, 0, NULL,
                                     ";1033");
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_APPNAME,
                                     LIBMSI_PROPERTY_TYPE_STRING, 0, NULL,
                                     "libmsi msibuild");
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_VERSION,
                                     LIBMSI_PROPERTY_TYPE_INT, 200, NULL, NULL);
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_SOURCE,
                                     LIBMSI_PROPERTY_TYPE_INT, 0, NULL, NULL);
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_RESTRICT,
                                     LIBMSI_PROPERTY_TYPE_INT, 0, NULL, NULL);

#ifdef HAVE_LIBUUID
    {
        uuid_t uu;
        char uustr[40];
        uuid_generate(uu);
        uustr[0] = '{';
        uuid_unparse_upper(uu, uustr + 1);
        strcat(uustr, "}");
        libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_UUID,
                                         LIBMSI_PROPERTY_TYPE_STRING, 0, NULL, uustr);
    }
#endif
}

static LibmsiResult open_database(const char *msifile, LibmsiDatabase **db,
                                  LibmsiSummaryInfo **si)
{
    LibmsiResult r;
    struct stat st;

    if (stat(msifile, &st) == -1)
    {
        r = libmsi_database_open(msifile, LIBMSI_DB_OPEN_CREATE, db);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to create package database %s (%u)\n", msifile, r);
            return r;
        }

        r = libmsi_database_get_summary_info(*db, INT_MAX, si);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to open summary info (%u)\n", r);
            return r;
        }

        init_suminfo(*si);

        r = libmsi_database_commit(*db);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to commit database (%u)\n", r);
            g_object_unref(*db);
            return r;
        }
    }
    else
    {
        r = libmsi_database_open(msifile, LIBMSI_DB_OPEN_TRANSACT, db);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to open package database %s (%u)\n", msifile, r);
            return r;
        }

        r = libmsi_database_get_summary_info(*db, INT_MAX, si);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to open summary info (%u)\n", r);
            return r;
        }
    }

    return r;
}

static LibmsiDatabase *db;
static LibmsiSummaryInfo *si;

static int import_table(char *table)
{
    LibmsiResult r;
    char dir[PATH_MAX];

    if (getcwd(dir, PATH_MAX) == NULL)
        return 1;

    r = libmsi_database_import(db, dir, table);
    if (r != LIBMSI_RESULT_SUCCESS)
    {
        fprintf(stderr, "failed to import table %s (%u)\n", table, r);
    }

    return (r != LIBMSI_RESULT_SUCCESS);
}

static int add_summary_info(const char *name, const char *author,
                            const char *template, const char *uuid)
{
    libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_SUBJECT,
                                     LIBMSI_PROPERTY_TYPE_STRING,
                                     0, NULL, name);
    if (author) {
        libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_AUTHOR,
                                         LIBMSI_PROPERTY_TYPE_STRING,
                                         0, NULL, author);
    }
    if (template) {
        libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_TEMPLATE,
                                         LIBMSI_PROPERTY_TYPE_STRING,
                                         0, NULL, template);
    }
    if (uuid) {
        libmsi_summary_info_set_property(si, LIBMSI_PROPERTY_UUID,
                                         LIBMSI_PROPERTY_TYPE_STRING,
                                         0, NULL, uuid);
    }
}

static int add_stream(const char *stream, const char *file)
{
    LibmsiResult r;
    LibmsiRecord *rec;
    LibmsiQuery *query;

    rec = libmsi_record_new(2);
    libmsi_record_set_string(rec, 1, stream);
    r = libmsi_record_load_stream(rec, 2, file);
    if (r != LIBMSI_RESULT_SUCCESS)
        fprintf(stderr, "failed to load stream (%u)\n", r);

    r = libmsi_database_open_query(db,
            "INSERT INTO `_Streams` (`Name`, `Data`) VALUES (?, ?)", &query);
    if (r != LIBMSI_RESULT_SUCCESS)
        fprintf(stderr, "failed to open query (%u)\n", r);

    r = libmsi_query_execute(query, rec);
    if (r != LIBMSI_RESULT_SUCCESS)
        fprintf(stderr, "failed to execute query (%u)\n", r);

    g_object_unref(rec);
    libmsi_query_close(query);
    g_object_unref(query);
    return r;
}

static int do_query(const char *sql, void *opaque)
{
    LibmsiResult r;
    LibmsiQuery *query;

    r = libmsi_database_open_query(db, sql, &query);
    if (r != LIBMSI_RESULT_SUCCESS) {
        fprintf(stderr, "failed to open query (%u)\n", r);
        return r;
    }

    r = libmsi_query_execute(query, NULL);
    if (r != LIBMSI_RESULT_SUCCESS)
        fprintf(stderr, "failed to execute query (%u)\n", r);

    libmsi_query_close(query);
    g_object_unref(query);
    return r;
}

static void show_usage(void)
{
    printf(
        "Usage: msibuild MSIFILE [OPTION]...\n"
        "Options:\n"
        "  -s name [author] [template] [uuid] Set summary information.\n"
        "  -q query         Execute SQL query/queries.\n"
        "  -i table1.idt    Import one table into the database.\n"
        "  -a stream file   Add 'stream' to storage with contents of 'file'.\n"
        "\nExisting tables or streams will be overwritten. If package.msi does not exist a new file\n"
        "will be created with an empty database.\n"
  );
}

int main(int argc, char *argv[])
{
    int r;
    int n;

    g_type_init();
    if (argc <= 2 )
    {
        show_usage();
        return 1;
    }

    /* Accept package after first option for winemsibuilder compatibility.  */
    if (argc >= 3 && argv[1][0] == '-') {
        r = open_database(argv[2], &db, &si);
        argv[2] = argv[1];
    } else {
        r = open_database(argv[1], &db, &si);
    }
    if (r != LIBMSI_RESULT_SUCCESS) return 1;

    argc -= 2, argv += 2;
    while (argc > 0) {
        int ret;
        if (argc < 2 || argv[0][0] != '-' || argv[0][2])
        {
            show_usage();
            return 1;
        }

        switch (argv[0][1])
        {
        case 's':
            n = 2;
            if (argv[2] && argv[2][0] != '-') n++;
            if (n > 2 && argv[3] && argv[3][0] != '-') n++;
            if (n > 3 && argv[4] && argv[4][0] != '-') n++;
            ret = add_summary_info(argv[1],
                                   n > 2 ? argv[2] : NULL,
                                   n > 3 ? argv[3] : NULL,
                                   n > 4 ? argv[4] : NULL);
            argc -= 3, argv += 3;
            break;
        case 'i':
            do {
                ret = import_table(argv[1]);
                if (ret) {
                    break;
                }
                argc--, argv++;
            } while (argv[1] && argv[1][0] != '-');
            argc--, argv++;
            break;
        case 'q':
            do {
                ret = sql_get_statement(argv[1], do_query, NULL);
                if (ret == 0) {
                    ret = sql_get_statement("", do_query, NULL);
                }
                if (ret) {
                    break;
                }
                argc--, argv++;
            } while (argv[1] && argv[1][0] != '-');
            argc--, argv++;
            break;
        case 'a':
            if (argc < 3) break;
            ret = add_stream(argv[1], argv[2]);
            argc -= 3, argv += 3;
            break;
        default:
            fprintf(stdout, "unknown option\n");
            show_usage();
            break;
        }
        if (r != LIBMSI_RESULT_SUCCESS) {
            break;
        }
    }

    if (r == LIBMSI_RESULT_SUCCESS) {
        r = libmsi_summary_info_persist(si);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to commit summary info (%u)\n", r);
            exit(1);
        }
        r = libmsi_database_commit(db);
        if (r != LIBMSI_RESULT_SUCCESS)
        {
            fprintf(stderr, "failed to commit database (%u)\n", r);
            exit(1);
        }
    }
    g_object_unref(si);
    g_object_unref(db);
    return r != LIBMSI_RESULT_SUCCESS;
}