summaryrefslogtreecommitdiff
path: root/app/bin/archive.c
blob: 4e82bd38097340c08a2e6be95f8b483087c48690 (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
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
/** \file archive.c
 *   ARCHIVE PROCESSING
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2018 Adam Richards and Martin Fischer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <errno.h>
#include <fcntl.h>

#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <zip.h>

#ifdef WINDOWS
    #include "include/dirent.h"
    #include <direct.h>
    #include <io.h>
    #include <process.h>
    #define unlink(a) _unlink((a))
    #define rmdir(a) _rmdir((a))
    #define open(name, flag, mode) _open((name), (flag), (mode))
    #define write(file, buffer, count) _write((file),(buffer), (count))
    #define close(file) _close((file))
    #define getpid() _getpid()
#else
    #include <dirent.h>
    #include <unistd.h>
#endif

#include <wlib.h>
#include "archive.h"
#include "directory.h"
#include "dynstring.h"
#include "i18n.h"
#include "messages.h"
#include "misc.h"
#include "misc2.h"
#include "paths.h"
#include "include/utf8convert.h"

int log_zip = 0;

//char *
//NativeToUtf8(const char *nativeString)
//{
//
//#ifdef WINDOWS
//
//	int cnt = 2 * (strlen(nativeString) + 1);
//	char *tempBuffer = MyMalloc( cnt );
//	char *destBuffer = MyMalloc( cnt );
//
//	//// find the 
//	//cnt = MultiByteToWideChar(CP_ACP,
//	//	0,
//	//	nativeString,
//	//	-1,
//	//	tempBuffer,
//	//	0);
//
//	//tempBuffer = realloc(tempBuffer, cnt * 2 + 4);
//
//	// convert to wide character (UTF16)
//	MultiByteToWideChar(CP_ACP,
//		0,
//		nativeString,
//		-1,
//		(LPWSTR)tempBuffer,
//		cnt);
//
//	// convert from wide char to UTF-8
//	WideCharToMultiByte(CP_UTF8,
//		0,
//		(LPCWCH)tempBuffer,
//		-1,
//		(LPSTR)destBuffer,
//		cnt,
//		NULL,
//		NULL);
//
//	MyFree(tempBuffer);
//#else
//	char * destBuffer = MyStrdup(nativeString);
//#endif
//
//	return(destBuffer);
//}

/**
 * Create the full path for temporary directories used in zip archive operations
 *
 * \param archive operation
 * \return pointer to full path, must be free'd by caller
 */

char *
GetZipDirectoryName(enum ArchiveOps op)
{
    char *opDesc;
    char *directory;
    DynString zipDirectory;

	DynStringMalloc(&zipDirectory, 0);

    switch (op) {
    case ARCHIVE_READ:
        opDesc = "in";
        break;
    case ARCHIVE_WRITE:
        opDesc = "out";
        break;
    default:
        opDesc = "err";
        break;
    }

    DynStringPrintf(&zipDirectory,
                    "%s" FILE_SEP_CHAR "zip_%s.%d",
                    workingDir,
                    opDesc,
                    getpid());

    directory = strdup(DynStringToCStr(&zipDirectory));
    DynStringFree(&zipDirectory);
    return (directory);
}

/*****************************************************************************
 * Add directory to archive
 *
 * \param IN zip 		The open zip archive handle
 * \param IN dir_path 	The path to add
 * \param IN prefix 	The prefix in the archive
 *
 * \returns 	TRUE if OK
 */

BOOL_T AddDirectoryToArchive(
    struct zip * za,
    const char * dir_path,
    const char * prefix)
{

    char *full_path;
    char *arch_path;
    DIR *dir;
    const char * buf;
    struct stat stat_path, stat_entry;
    struct dirent *entry;

    zip_source_t * zt;

    // stat for the path
    stat(dir_path, &stat_path);

    // if path does not exists or is not dir - exit with status -1
    if (S_ISDIR(stat_path.st_mode) == 0) {
        NoticeMessage(MSG_NOT_DIR_FAIL,
                      _("Continue"), NULL, dir_path);
        return FALSE;
    }

    // if not possible to read the directory for this user
    if ((dir = opendir(dir_path)) == NULL) {
        NoticeMessage(MSG_OPEN_DIR_FAIL,
                      _("Continue"), NULL, dir_path);
        return FALSE;
    }

    // iteration through entries in the directory
    while ((entry = readdir(dir)) != NULL) {
        // skip entries "." and ".."
        if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
            continue;
        }

        // determinate a full path of an entry
        MakeFullpath(&full_path, dir_path, entry->d_name, NULL);

        // stat for the entry
        stat(full_path, &stat_entry);

        if (prefix && prefix[0]) {
            MakeFullpath(&arch_path, prefix, entry->d_name, NULL);
        } else {
            MakeFullpath(&arch_path, entry->d_name, NULL);
        }

        // recursively add a nested directory
        if (S_ISDIR(stat_entry.st_mode) != 0) {
            if (zip_dir_add(za, arch_path, 0) < 0) {
                zip_error_t  *ziperr = zip_get_error(za);
                buf = zip_error_strerror(ziperr);
                NoticeMessage(MSG_ZIP_DIR_ADD_FAIL,
                              _("Continue"), NULL, arch_path, buf);
#if DEBUG
                printf("Added Directory %s \n", arch_path);
#endif
            }

            if (AddDirectoryToArchive(za, full_path, arch_path) != TRUE) {
                free(full_path);
                free(arch_path);
                return FALSE;
            }
            free(arch_path);
            continue;
        } else {
			char *archPathUtf8 = MyStrdup(arch_path);
			char *fullPathUtf8 = MyStrdup(full_path);
#ifdef WINDOWS
			archPathUtf8 = Convert2UTF8(archPathUtf8);
			fullPathUtf8 = Convert2UTF8(fullPathUtf8);
			ConvertPathForward(archPathUtf8);
#endif // WINDOWS
            zt = zip_source_file(za, fullPathUtf8, 0, -1);
            if (zip_file_add(za, archPathUtf8, zt, ZIP_FL_ENC_UTF_8) == -1) {
                zip_error_t  *ziperr = zip_get_error(za);
                buf = zip_error_strerror(ziperr);
                NoticeMessage(MSG_ZIP_FILE_ADD_FAIL, _("Continue"), NULL, full_path, arch_path,
                              buf);
                free(full_path);
                free(arch_path);
				MyFree(fullPathUtf8);
				MyFree(archPathUtf8);
                return FALSE;
            }
			MyFree(fullPathUtf8);
			MyFree(archPathUtf8);
#if DEBUG
            printf("Added File %s", full_path);
#endif
        }
        free(arch_path);
        free(full_path);
    }

    closedir(dir);
    return TRUE;
}

/***********************************************************************
 * Create Archive
 *
 * \param IN dir_path The place to create the archive
 * \param IN fileName The name of the archive
 *
 * \return TRUE if ok
 */

BOOL_T CreateArchive(
    const char * dir_path,
    const char * fileName)
{
    struct zip *za;
    int err;
    char buf[100];

    char * archive = MyStrdup(fileName);  	// Because of const char
    char * archive_name = FindFilename(archive);
    char * archive_path;
	char * archiveUtf8;

    MakeFullpath(&archive_path, workingDir, archive_name, NULL);
	    
	archiveUtf8 = MyStrdup(archive_path);
#ifdef WINDOWS
	archiveUtf8 = Convert2UTF8(archiveUtf8);
#endif // WINDOWS

	MyFree(archive);

    if ((za = zip_open(archiveUtf8, ZIP_CREATE, &err)) == NULL) {
        zip_error_to_str(buf, sizeof(buf), err, errno);
        NoticeMessage(MSG_ZIP_CREATE_FAIL, _("Continue"), NULL, archiveUtf8, buf);
        MyFree(archiveUtf8);
        return FALSE;
    }
#if DEBUG
    printf("====================== \n");
    printf("Started Archive %s", archive_path);
#endif

    AddDirectoryToArchive(za, dir_path, "");

    if (zip_close(za) == -1) {
        zip_error_to_str(buf, sizeof(buf), err, errno);
        NoticeMessage(MSG_ZIP_CLOSE_FAIL, _("Continue"), NULL, archiveUtf8, buf);
		free(archive_path);
		MyFree(archiveUtf8);
        return FALSE;
    }

    unlink(fileName); 							//Delete Old
    if (rename(archive_path, fileName) == -1) {	//Move zip into place
        NoticeMessage(MSG_ZIP_RENAME_FAIL, _("Continue"), NULL, archiveUtf8, fileName,
                      strerror(errno));
		free(archive_path);
		MyFree(archiveUtf8);
        return FALSE;
    }
	free(archive_path);
	MyFree(archiveUtf8);

#if DEBUG
    printf("Moved Archive to %s", fileName);
    printf("====================== \n");
#endif
    return TRUE;
}

/**************************************************************************
 * Unpack_Archive_for
 *
 * \param IN pathName the name of the archive
 * \param IN fileName just the filename and extension of the layout
 * \param IN tempDir The directory to use to unpack into
 *
 * \returns TRUE if all worked
 */
BOOL_T UnpackArchiveFor(
    const char * pathName,  /*Full name of archive*/
    const char * fileName,  /*Layout name and extension */
    const char * tempDir,   /*Directory to unpack into */
    BOOL_T file_only)
{
    char *dirName;
    struct zip *za;
    struct zip_file *zf;
    struct zip_stat sb;
    char buf[100];
    int err;
    int i;
    int64_t len;
    FILE  *fd;
    long long sum;

	char *destBuffer = MyStrdup(pathName);
#ifdef WINDOWS
	destBuffer = Convert2UTF8(destBuffer);
#endif // WINDOWS


    if ((za = zip_open(destBuffer, 0, &err)) == NULL) {
        zip_error_to_str(buf, sizeof(buf), err, errno);
        NoticeMessage(MSG_ZIP_OPEN_FAIL, _("Continue"), NULL, pathName, buf);
        fprintf(stderr, "xtrkcad: can't open xtrkcad zip archive `%s': %s \n",
                pathName, buf);

		MyFree(destBuffer);
        return FALSE;
    }

    for (i = 0; i < zip_get_num_entries(za, 0); i++) {
        if (zip_stat_index(za, i, 0, &sb) == 0) {
            len = strlen(sb.name);

#if DEBUG
            printf("==================\n");
            printf("Name: [%s], ", sb.name);
            printf("Size: [%llu], ", sb.size);
            printf("mtime: [%u]\n", (unsigned int)sb.mtime);
            printf("mtime: [%u]\n", (unsigned int)sb.mtime);
#endif

            LOG(log_zip, 1, ("================= \n"))
            LOG(log_zip, 1, ("Zip-Name [%s] \n", sb.name))
            LOG(log_zip, 1, ("Zip-Size [%llu] \n", sb.size))
            LOG(log_zip, 1, ("Zip-mtime [%u] \n", (unsigned int)sb.mtime))

            if (sb.name[len - 1] == '/' && !file_only) {
                MakeFullpath(&dirName, tempDir, &sb.name[0], NULL);
                if (SafeCreateDir(dirName) != TRUE) {
                    free(dirName);
                    return FALSE;
                }
                free(dirName);
            } else {
                zf = zip_fopen_index(za, i, 0);
                if (!zf) {
                    NoticeMessage(MSG_ZIP_INDEX_FAIL, _("Continue"), NULL);
                    fprintf(stderr, "xtrkcad zip archive open index error \n");
                    return FALSE;
                }

                if (file_only) {
                    if (strncmp(sb.name, fileName, strlen(fileName)) != 0) {
                        continue; /* Ignore any other files than the one we asked for */
                    }
                }
                MakeFullpath(&dirName, tempDir, &sb.name[0], NULL);
#ifdef WINDOWS
				ConvertUTF8ToSystem(dirName);
#endif // WINDOWS
				fd = fopen(dirName, "wb");
                if (!fd) {
                    NoticeMessage(MSG_ZIP_FILE_OPEN_FAIL, _("Continue"), NULL, dirName,
                                  strerror(errno));
                    free(dirName);
                    return FALSE;
                }

                sum = 0;
                while (sum != sb.size) {
                    len = zip_fread(zf, buf, 100);
                    if (len < 0) {
                        NoticeMessage(MSG_ZIP_READ_FAIL, _("Continue"), NULL, dirName, &sb.name[0]);
                        free(dirName);
						fclose(fd);
                        return FALSE;
                    }
                    fwrite(buf, 1, (unsigned int)len, fd);
                    sum += len;
                }
                fclose(fd);
                free(dirName);
                zip_fclose(zf);
            }
        } else {
            LOG(log_zip, 1, ("Zip-Unknown File[%s] Line[%d] \n", __FILE__, __LINE__))
#if DEBUG
            printf("File[%s] Line[%d]\n", __FILE__, __LINE__);
#endif
        }
    }

	MyFree(destBuffer);

    if (zip_close(za) == -1) {
        NoticeMessage(MSG_ZIP_CLOSE_FAIL, _("Continue"), NULL, dirName, &sb.name[0]);
        return FALSE;
    }
    return TRUE;
}