File: libjte.c

package info (click to toggle)
jigit 1.20-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,796 kB
  • ctags: 624
  • sloc: sh: 10,437; ansic: 5,866; perl: 566; makefile: 129
file content (517 lines) | stat: -rw-r--r-- 12,709 bytes parent folder | download | duplicates (3)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
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
/*
 * libjte.c
 *
 * Copyright (c) 2010-2011 Thomas Schmitt <scdbackup@gmx.net>
 *
 * API functions of libjte
 *
 * GNU LGPL v2.1 (including option for GPL v2 or later)
 *
 */

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#endif

#include <errno.h>


#include "jte.h"
#include "checksum.h"

#include "libjte_private.h"
#include "libjte.h"



/* @param flag bit0= do not free *dest before overwriting it
*/
static int libjte__set_string(char **dest, char *source, int flag)
{
    if (*dest != NULL && !(flag & 1))
        free(*dest);
    *dest = NULL;
    if (source == NULL)
        return 1;
    *dest = strdup(source);
    if (*dest == NULL)
        return -1;
    return 1;
}


/* Version inquiry API */

void libjte__version(int *major, int *minor, int *micro)
{
    *major = LIBJTE_VERSION_MAJOR;
    *minor = LIBJTE_VERSION_MINOR;
    *micro = LIBJTE_VERSION_MICRO;
}


int libjte__is_compatible(int major, int minor, int micro, int flag)
{
 int own_major, own_minor, own_micro;

 libjte__version(&own_major, &own_minor, &own_micro);
 return(own_major > major ||
        (own_major == major && (own_minor > minor ||
         (own_minor == minor && own_micro >= micro))));
}


/* Life cycle API */


int libjte_new(struct libjte_env **jte_handle, int flag)
{
    struct libjte_env *o;

    if (sizeof(off_t) < 8) {
        fprintf(stderr, "libjte: Fatal compile time misconfiguration. sizeof(off_t) = %d too small.\n\n", (int) sizeof(off_t));
        return -1;
    }

    *jte_handle = o = calloc(1, sizeof(struct libjte_env));
    if (o == NULL)
        return -1;
    o->jtemplate_out = NULL;
    o->jjigdo_out = NULL;
    o->jmd5_list = NULL;
    o->jtjigdo = NULL;
    o->jttemplate = NULL;
    o->jte_min_size = MIN_JIGDO_FILE_SIZE;
    o->checksum_algo_iso =  (CHECK_MD5_USED | \
                             CHECK_SHA1_USED | \
                             CHECK_SHA256_USED | \
                             CHECK_SHA512_USED);
    o->checksum_algo_tmpl = CHECK_MD5_USED;
    o->jte_template_compression = JTE_TEMP_GZIP;
    o->exclude_list = NULL;
    o->include_list = NULL;
    o->map_list = NULL;
    o->template_size = 0;
    o->image_size = 0;
    o->iso_context = NULL;
    o->template_context = NULL;
    o->entry_list = NULL;
    o->entry_last = NULL;
    o->t_file = NULL;
    o->j_file = NULL;
    o->num_matches = 0;
    o->num_chunks = 0;
    o->md5_list = NULL;
    o->md5_last = NULL;
    o->include_in_jigdo = 0;
    memset(o->message_buffer, 0, sizeof(o->message_buffer));
    o->error_behavior = 1; /* Print to stderr, do not exit but return -1 */
    o->msg_list = NULL;
    o->uncomp_buf = NULL;
    o->uncomp_size = 0;
    o->uncomp_buf_used = 0;
    return 1;
}

int libjte_destroy(struct libjte_env **jte_handle)
{
    struct libjte_env *o;

    o = *jte_handle;
    if (o == NULL)
        return 0;

    free(o->outfile);
    free(o->jtemplate_out);
    free(o->jjigdo_out);
    free(o->jmd5_list);
    if (o->jtjigdo != NULL)
        fclose(o->jtjigdo);
    if (o->jttemplate != NULL)
        fclose(o->jttemplate);

    libjte_destroy_path_match_list(o, 0); /* include_list */
    libjte_destroy_path_match_list(o, 1); /* exclude_list */
    libjte_destroy_path_mapping(o, 0);

    if (o->iso_context != NULL)
        checksum_free_context(o->iso_context);
    if (o->template_context != NULL)
        checksum_free_context(o->template_context);

    /* >>> TODO : get rid of the following odd situation
       o->j_file and o->t_file seem to be only copies of
       o->jttemplate and o->jtjigdo */

    libjte_destroy_entry_list(o, 0);
    libjte_destroy_md5_list(o, 0);

    libjte_clear_msg_list(o, 1 | 2); /* dump pending messages to stderr */
    free(o->uncomp_buf);
    free(o);
    *jte_handle = NULL;
    return 1;
}

/* Setup API */

int libjte_set_outfile(struct libjte_env *o, char *outfile)
{
    return libjte__set_string(&(o->outfile), outfile, 0);
}

int libjte_set_verbose(struct libjte_env *o, int verbose)
{
    o->verbose = !!verbose;
    return 1;
}

int libjte_set_template_path(struct libjte_env *o, char *jtemplate_out)
{
    return libjte__set_string(&(o->jtemplate_out), jtemplate_out, 0);
}

int libjte_set_jigdo_path(struct libjte_env *o, char *jjigdo_out)
{
    return libjte__set_string(&(o->jjigdo_out), jjigdo_out, 0);
}

int libjte_set_md5_path(struct libjte_env *o, char *jmd5_list)
{
    return libjte__set_string(&(o->jmd5_list), jmd5_list, 0);
}

int libjte_set_min_size(struct libjte_env *o, int jte_min_size)
{
    o->jte_min_size = jte_min_size;
    return 1;
}

static int libjte_decode_checksum_string(struct libjte_env *o,
                                         char *checksum_code, int default_algo,
                                         int *algo)
{

#ifdef NIX

    *algo = 0;
    if (strcmp(checksum_code, "default") != 0) {
        if (strstr(checksum_code, "md5") != NULL)
            *algo |= CHECK_MD5_USED;
        if (strstr(checksum_code, "sha1") != NULL)
            *algo |= CHECK_SHA1_USED;
        if (strstr(checksum_code, "sha256") != NULL)
            *algo |= CHECK_SHA256_USED;
        if (strstr(checksum_code, "sha512") != NULL)
            *algo |= CHECK_SHA512_USED;
    }
    if (*algo == 0)
        *algo = default_algo;

#else /* NIX */

    int ret;

    ret = parse_checksum_algo(checksum_code, algo);
    if (ret) {
        *algo = default_algo;
        sprintf(o->message_buffer, "Invalid checksum algorithm name in '%s'",
                checksum_code);
        libjte_add_msg_entry(o, o->message_buffer, 0);
        return 0;
    }

#endif /* ! NIX */

    return 1;
}

int libjte_set_checksum_iso(struct libjte_env *o, char *checksum_code)
{
    int ret;
    int checksum_algo_iso = (CHECK_MD5_USED | 
                             CHECK_SHA1_USED | 
                             CHECK_SHA256_USED |
                             CHECK_SHA512_USED);

    ret = libjte_decode_checksum_string(o, checksum_code, checksum_algo_iso,
                                        &checksum_algo_iso);
    if (ret <= 0)
        return ret;
    o->checksum_algo_iso = checksum_algo_iso;
    return 1;
}

int libjte_set_checksum_template(struct libjte_env *o, char *checksum_code)
{
    int ret;
    int checksum_algo_tmpl = CHECK_MD5_USED;

    ret = libjte_decode_checksum_string(o, checksum_code, checksum_algo_tmpl,
                                        &checksum_algo_tmpl);
    if (ret <= 0)
        return ret;
    o->checksum_algo_tmpl = checksum_algo_tmpl;
    return 1;
}

int libjte_set_compression(struct libjte_env *o, char *compression_code)
{
    jtc_t compr = JTE_TEMP_GZIP;

    if (strcmp(compression_code, "default") != 0) {
        if (strcmp(compression_code, "gzip") == 0)
            compr = JTE_TEMP_GZIP;
        else if (strcmp(compression_code, "bzip2") ==0)
            compr = JTE_TEMP_BZIP2;
        else {
            sprintf(o->message_buffer,
                    "libjte: Unknown compression code. Known: gzip bzip2");
            libjte_add_msg_entry(o, o->message_buffer, 0);
            return 0;
        }
    }

#ifndef LIBJTE_WITH_LIBBZ2

    if (compr == JTE_TEMP_BZIP2) {
        sprintf(o->message_buffer,
                "libjte: Usage of libbz2 not enabled at compile time\n");
        libjte_add_msg_entry(o, o->message_buffer, 0);
        return 0;
    }

#endif /* LIBJTE_WITH_LIBBZ2 */
 
    o->jte_template_compression = compr;
    return 1;
}

int libjte_add_exclude(struct libjte_env *o, char *pattern)
{
    int ret;

    ret = jte_add_exclude(o, pattern);
    return !ret;
}

int libjte_add_md5_demand(struct libjte_env *o, char *pattern)
{
    int ret;

    ret = jte_add_include(o, pattern);
    return !ret;
}

int libjte_add_mapping(struct libjte_env *o, char *arg)
{
    int ret;

    ret = jte_add_mapping(o, arg);
    return !ret;
}

int libjte_set_image_size(struct libjte_env *o, off_t image_size)
{
    o->image_size = image_size;
    return 1;
}


/* Operation API */

int libjte_write_header(struct libjte_env *o)
{
    int ret;

    if (o->jtemplate_out == NULL || o->jjigdo_out == NULL ||
        o->outfile == NULL || o->jmd5_list == NULL) {
        sprintf(o->message_buffer,
               "Undefined: template_path, jigdo_path, md5_paths, or outfile.");
        libjte_add_msg_entry(o, o->message_buffer, 0);
        return 0;
    }
    
    o->jttemplate = fopen(o->jtemplate_out, "wb");
    if (o->jttemplate == NULL) {
        sprintf(o->message_buffer,
                "Cannot open template file '%1.1024s' for writing. errno=%d",
                o->jtemplate_out, errno);
        libjte_add_msg_entry(o, o->message_buffer, 0);
        return 0;
    }
    o->jtjigdo = fopen(o->jjigdo_out, "wb");
    if (o->jtjigdo == NULL) {
        sprintf(o->message_buffer,
                "Cannot open jigdo file '%1.1024s' for writing. errno=%d",
                o->jjigdo_out, errno);
        libjte_add_msg_entry(o, o->message_buffer, 0);
        return 0;
    }

    ret = write_jt_header(o, o->jttemplate, o->jtjigdo);
    if (ret <= 0)
        return ret;
    return 1;
}

int libjte_write_footer(struct libjte_env *o)
{
    int ret;

    ret = write_jt_footer(o);
    if (o->jtjigdo != NULL)
        fclose(o->jtjigdo);
    if (o->jttemplate != NULL)
        fclose(o->jttemplate);
    o->jtjigdo = NULL;
    o->jttemplate = NULL;
    if (ret <= 0)
        return ret;
    return 1;
}

/* Traditional Data File API */

int libjte_write_unmatched(struct libjte_env *o, void *buffer, int size,
                            int count)
{
    int ret;

    ret = jtwrite(o, buffer, size, count);
    return ret;
}

int libjte_write_match_record(struct libjte_env *o,
                            char *filename, char *mirror_name, int sector_size,
                            off_t size, unsigned char md5[16])
{
    int ret;

    ret = write_jt_match_record(o, filename, mirror_name, sector_size, size,
                                md5);
    if (ret <= 0)
        return ret;
    return 1;
}

int libjte_decide_file_jigdo(struct libjte_env *o,
                             char *filename, off_t size, char **mirror_name,
                             unsigned char md5[16])
{
    int ret;
    char *md5_name;

    *mirror_name = NULL;
    ret = list_file_in_jigdo(o, filename, size, &md5_name, md5);
    if (ret <= 0)
        return ret;
    *mirror_name = strdup(md5_name);
    if (*mirror_name == NULL) {
        libjte_report_no_mem(o, strlen(md5_name) + 1, 0);
        return -1;
    }
    return 1;
}

/* Simplified Data File API */

int libjte_begin_data_file(struct libjte_env *o, char *filename,
                           int sector_size, off_t size)
{
    int ret;
    char *mirror_name;
    unsigned char md5[16];

    o->include_in_jigdo = 0;
    ret = list_file_in_jigdo(o, filename, size, &mirror_name, md5);
    if (ret < 0)
        return ret;
    if (ret == 0)
        return 2;
    write_jt_match_record(o, filename, mirror_name, sector_size,
                          size, md5);
    o->include_in_jigdo = 1;
    return 1;
}

int libjte_show_data_chunk(struct libjte_env *o,
                           void *buffer, int sector_size,
                           int count)
{
    o->image_size += count * sector_size;
    if (o->include_in_jigdo)
        return 2;
    jtwrite(o, buffer, sector_size, count);
    return 1;
}

int libjte_end_data_file(struct libjte_env *o)
{
    o->include_in_jigdo = 0;
    return 1;
}


/* Error reporting API */

int libjte_set_error_behavior(struct libjte_env *o,
                              int to_stderr, int with_exit)
{
    o->error_behavior = 0;
    if (to_stderr > 0)
        o->error_behavior |= 1;
    if (with_exit> 0)
        o->error_behavior |= 2;
    return 1;
}

char *libjte_get_next_message(struct libjte_env *o)
{
    jigdo_msg_entry_t *old_entry;
    char *msg;

    if (o->msg_list == NULL)
        return NULL;
    old_entry = o->msg_list;
    o->msg_list = old_entry->next;
    msg = old_entry->message;
    free(old_entry);
    return msg;
}

/* @param flag bit0= print pending messages to stderr
*/
int libjte_clear_msg_list(struct libjte_env *o, int flag)
{
    char *msg;

    if ((flag & 2) && o->msg_list != NULL)
        fprintf(stderr,
                    "libjte: -- have to dump error messages to stderr --\n");
    while (1) {
        msg = libjte_get_next_message(o);
        if (msg == NULL)
    break;
        if (flag & 1)
            fprintf(stderr, "libjte: %s\n", msg);
        free(msg);
    }
    return 1;
}