Source

dark-hammer / src / paki / paki.c

The branch '0.4.7' does not exist.
  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
/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/


#include <stdio.h>
#include "core/core.h"
#include "core/zip.h"
#include "core/pak-file.h"

#if defined(_LINUX_) || defined(_OSX_)
#include <dirent.h>
#endif

#if defined(_WIN_)
#include "core/win.h"
#endif

#define VERSION     "1.0b"
#define FILE_SIZE_WARNING_THRESHOLD     (64*1024*1024)

/* application input arguments */
enum PAKI_USAGE
{
    PAKI_USAGE_EXTRACT = (1 << 0),
    PAKI_USAGE_COMPRESS = (1 << 1),
    PAKI_USAGE_VERBOSE = (1 << 2),
    PAKI_USAGE_LIST = (1 << 3)
};

struct paki_args
{
    uint32 usage;  /* PAKI_USAGE combined */
    enum compress_mode compress_mode;
    char path[DH_PATH_MAX];
    char pakfile[DH_PATH_MAX];
    uint32 err_cnt;
    uint32 warn_cnt;
    uint32 file_cnt;
};

/* fwd declarations */
void show_help();
result_t archive_put(struct pak_file* pak, struct paki_args* args,
                     const char* srcfilepath, const char* destfilealias);
result_t compress_directory(struct pak_file* pak, struct paki_args* args, const char* subdir);
void save_pak(struct paki_args* args);
void load_pak(struct paki_args* args);
void list_pak(struct paki_args* args);

//
int main(int argc, char** argv)
{
    struct paki_args args;

    core_init(TRUE);
    log_outputconsole(TRUE);
    
    if (argc == 1)  {
        core_release(FALSE);
        return -1;
    }
    
    // read arguments
    memset(&args, 0x00, sizeof(args));
    
    args.compress_mode = COMPRESS_NORMAL;
    bool_t invalid_args = FALSE;
    
    for (int32 i = 1; i < argc; i++)    {
        if (str_isequal(argv[i], "-x"))      BIT_ADD(args.usage, PAKI_USAGE_EXTRACT);
        else if(str_isequal(argv[i], "-c"))  BIT_ADD(args.usage, PAKI_USAGE_COMPRESS);
        else if(str_isequal(argv[i], "-z0")) args.compress_mode = COMPRESS_NONE;
        else if(str_isequal(argv[i], "-z1")) args.compress_mode = COMPRESS_FAST;
        else if(str_isequal(argv[i], "-z2")) args.compress_mode = COMPRESS_NORMAL;
        else if(str_isequal(argv[i], "-z3")) args.compress_mode = COMPRESS_BEST;
        else if(str_isequal(argv[i], "-v"))  BIT_ADD(args.usage, PAKI_USAGE_VERBOSE);
        else if(str_isequal(argv[i], "-l"))	 BIT_ADD(args.usage, PAKI_USAGE_LIST);
        else if(str_isequal(argv[i], "-h"))  {
        	show_help();
        	core_release(FALSE);
        	return 0;
        }
        else    { 
            if (argv[i][0] == '-' || args.path[0] != 0)  {
                /* argument is an unknown parameter */
                invalid_args = TRUE;     
                break;
            }
            
            if (args.pakfile[0] == 0)   strcpy(args.pakfile, argv[i]);
            else                        strcpy(args.path, argv[i]);
        }
    }
    
    /* check for arguments validity */
    if (invalid_args || args.pakfile[0] == 0 ||
        ((BIT_CHECK(args.usage, PAKI_USAGE_EXTRACT) + 
          BIT_CHECK(args.usage, PAKI_USAGE_COMPRESS) +
          BIT_CHECK(args.usage, PAKI_USAGE_LIST)) != 1))
    {
        printf(TERM_BOLDRED "Invalid arguments\n" TERM_RESET);
        core_release(FALSE);
        return -1;
    }
    
    /* creating archive (-c flag) */
    if (BIT_CHECK(args.usage, PAKI_USAGE_COMPRESS))     {
        save_pak(&args);
    }   else if(BIT_CHECK(args.usage, PAKI_USAGE_EXTRACT))  {
        load_pak(&args);
    } else if(BIT_CHECK(args.usage, PAKI_USAGE_LIST))	{
    	list_pak(&args);
    }
    
#if defined(_DEBUG_)
    core_release(TRUE);
#else
    core_release(FALSE);
#endif
    return 0;
}

/* show arguments help */
void show_help()
{
    printf("paki - 'pak' file archiver for dark-hammer engine v%s\n", VERSION);
    printf("Usage:\n"
              "  paki [-x] [-c] [-l] [-v] [pakfile] [path]\n"
              "  [-x]: extracts a file defined by [path] from [pak-file]\n"
              "  [-c]: create pak file from [path] including subdirectories.\n"
              "  [-zN]: compression mode. ([-z0]:no-compr [-z1]:fast [-z2]:normal [-z3]:best)\n"
              "  [-l]: list files in the [packfile]\n"
              "  [-v]: verbose mode\n\n");
}

void save_pak(struct paki_args* args)
{
    result_t r;
    struct pak_file pak;

    path_norm(args->pakfile, args->pakfile);
    path_norm(args->path, args->path);

    r = pak_create(&pak, mem_heap(), args->pakfile, args->compress_mode, 0);
    if (IS_FAIL(r))     {
        err_sendtolog(FALSE);
        return;
    }
    
    
    r = compress_directory(&pak, args, "");
    if (IS_FAIL(r))     {
        err_sendtolog(FALSE);
        pak_close(&pak);
        return;
    }
    
    pak_close(&pak);

    // report
    printf(TERM_BOLDWHITE "Saved pak: '%s'\nTotal %d file(s) - %d Error(s), %d Warning(s)\n" TERM_RESET,
               args->pakfile, args->file_cnt, args->err_cnt, args->warn_cnt);
}

#if defined(_LINUX_) || defined(_OSX_)
result_t compress_directory(struct pak_file* pak, struct paki_args* args, const char* subdir)
{
    result_t r;
    char directory[DH_PATH_MAX];
    char filepath[DH_PATH_MAX];
    char fullfilepath[DH_PATH_MAX];

    strcpy(directory, args->path);
    if (subdir[0] != 0)     {
        path_join(directory, path_norm(directory, directory), subdir, NULL);
    }
    
    DIR* dir = opendir(directory);
    if (dir == NULL)    {
        printf(TERM_BOLDRED "Creating pak failed: directory '%s' does not exist.\n" TERM_RESET,
        		directory);
        return RET_FAIL;
    }
    
    /* read directory recuresively, and compress files into pak */
    struct dirent* ent = readdir(dir);
    while (ent != NULL)     {
        if (!str_isequal(ent->d_name, ".") && !str_isequal(ent->d_name, ".."))    {
            filepath[0] = 0;
            if (subdir[0] != 0) 
               strcpy(filepath, subdir);
            
            if (ent->d_type != DT_DIR)  {
                // put the file into the archive
                path_join(filepath, filepath, ent->d_name, NULL);
                path_join(fullfilepath, directory, ent->d_name, NULL);
                r = archive_put(pak, args, fullfilepath, filepath);
                if (IS_OK(r) && BIT_CHECK(args->usage, PAKI_USAGE_VERBOSE))     {
                    puts(filepath);
                }   else if (IS_FAIL(r))  {
                    err_sendtolog(FALSE);
                    args->err_cnt ++;
                }
                args->file_cnt ++;
            }   else    {
                // it's a directory, recurse
                path_join(filepath, filepath, ent->d_name, NULL);
                compress_directory(pak, args, filepath);
            }
        }
        ent = readdir(dir);
    }
    
    closedir(dir);
    return RET_OK;
}
#endif

#if defined(_WIN_)
result_t compress_directory(struct pak_file* pak, struct paki_args* args, const char* subdir)
{
    result_t r;
    char directory[DH_PATH_MAX];
    char filepath[DH_PATH_MAX];
    char fullfilepath[DH_PATH_MAX];
    
    strcpy(directory, args->path);
    if (subdir[0] != 0)     {
        path_join(directory, directory, subdir, NULL);
    }

    WIN32_FIND_DATA fdata;
    char filter[DH_PATH_MAX];
    path_join(filter, directory, "*", NULL);
    HANDLE find_hdl = FindFirstFile(filter, &fdata);
    if (find_hdl == INVALID_HANDLE_VALUE)    {
        printf(TERM_BOLDRED "Creating pak failed: directory '%s' does not exist.\n" TERM_RESET,
        		directory);
        return RET_FAIL;
    }

    /* read directory recuresively, and compress files into pak */
    BOOL fr = TRUE;
    while (fr)     {
        if (!str_isequal(fdata.cFileName, ".") && !str_isequal(fdata.cFileName, ".."))    {
            filepath[0] = 0;
            if (subdir[0] != 0)     {
                strcpy(filepath, subdir);
            }

            if (!BIT_CHECK(fdata.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY))  {
                /* put the file into the archive */
                path_join(filepath, filepath, fdata.cFileName, NULL);
                path_join(fullfilepath, directory, fdata.cFileName, NULL);

                r = archive_put(pak, args, fullfilepath, filepath);
                if (IS_OK(r) && BIT_CHECK(args->usage, PAKI_USAGE_VERBOSE))     {
                    puts(filepath);
                }   else if (IS_FAIL(r))  {
                    err_sendtolog(FALSE);
                    args->err_cnt ++;
                }
                args->file_cnt ++;
            }   else    {
                /* it's a directory, recurse */
                path_join(filepath, filepath, fdata.cFileName, NULL);
                compress_directory(pak, args, filepath);
            }
        }
        fr = FindNextFile(find_hdl, &fdata);
    }

    FindClose(find_hdl);
    return RET_OK;
}
#endif

result_t archive_put(struct pak_file* pak, struct paki_args* args,
                     const char* srcfilepath, const char* destfilealias)
{
    result_t r;
    
    file_t f = io_file_opendisk(srcfilepath, TRUE);
    if (f == FILE_NULL)     {
        printf(TERM_BOLDRED "Packing file '%s' failed: file may not exist or locked.\n" TERM_RESET,
        		srcfilepath);
        return RET_FILE_ERROR;
    }
    
    if (io_file_getsize(f) > FILE_SIZE_WARNING_THRESHOLD)   {
        printf(TERM_BOLDYELLOW "File '%s' have %dmb of size, which may be too large.\n" TERM_RESET,
        		srcfilepath, (uint32)io_file_getsize(f)/(1024*1024));
        args->warn_cnt ++;
    }
    
    char alias[DH_PATH_MAX];
    r = pak_putfile(pak, mem_heap(), f, path_tounix(alias, destfilealias));
    io_file_close(f);
    
    return r;
}

void load_pak(struct paki_args* args)
{
    result_t r;
    struct pak_file pak;
    char filename[DH_PATH_MAX];

    path_norm(args->pakfile, args->pakfile);
    path_tounix(args->path, args->path);

    r = pak_open(&pak, mem_heap(), args->pakfile, 0);
    if (IS_FAIL(r))     {
        err_sendtolog(FALSE);
        return;
    }
    
    uint32 file_id = pak_findfile(&pak, args->path);
    if (file_id == INVALID_INDEX)   {
        printf(TERM_BOLDRED "Extract failed: file '%s' not found in pak.\n" TERM_RESET, args->path);
        pak_close(&pak);
        return;
    }
    
    path_getfullfilename(filename, args->path);
    file_t f = io_file_createdisk(filename);
    if (f == FILE_NULL)     {
        printf(TERM_BOLDRED "Extract failed: could not create '%s' for writing.\n" TERM_RESET,
        		filename);
        pak_close(&pak);
        err_sendtolog(FALSE);
        return;
    }
    
    file_t src_file = pak_getfile(&pak, mem_heap(), mem_heap(), file_id, 0);
    if (src_file == FILE_NULL)   {
        pak_close(&pak);
        io_file_close(f);
        err_sendtolog(FALSE);
        return;
    }
    
    size_t size;
    struct allocator* alloc;
    void* buffer = io_file_detachmem(src_file, &size, &alloc);
    io_file_write(f, buffer, size, 1);    
    A_FREE(alloc, buffer);
    io_file_close(f);
    
    pak_close(&pak);
    
    if (BIT_CHECK(args->usage, PAKI_USAGE_VERBOSE)) {
        printf(TERM_WHITE "%s -> %s\n" TERM_RESET, args->path, filename);
    }
    args->file_cnt ++;
    
    // report
    printf(TERM_BOLDWHITE "Finished: total %d file(s) - %d error(s), %d warning(s)\n" TERM_RESET,
               args->file_cnt, args->err_cnt, args->warn_cnt);
}

void list_pak(struct paki_args* args)
{
	result_t r;
    struct pak_file pak;
    r = pak_open(&pak, mem_heap(), args->pakfile, 0);
    if (IS_FAIL(r))     {
        err_sendtolog(FALSE);
        return;
    }

    uint32 cnt = INVALID_INDEX;
	char* filelist = pak_createfilelist(&pak, mem_heap(), &cnt);
    pak_close(&pak);
	if (cnt == 0)	{
		printf(TERM_BOLDWHITE "There are no files in the pak '%s'.\n" TERM_RESET, args->pakfile);
		return;
	}

	if (filelist == NULL)	{
		printf(TERM_BOLDRED "Not enough memory.\n" TERM_RESET);
		return;
	}

	for (uint32 i = 0; i < cnt; i++)	{
		printf(TERM_WHITE "%s\n" TERM_RESET, filelist + i*DH_PATH_MAX);
	}
	printf(TERM_BOLDWHITE "Total %d files in '%s'.\n" TERM_RESET, cnt, args->pakfile);
	FREE(filelist);

    
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.