File: cfilearchiver.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (411 lines) | stat: -rw-r--r-- 8,487 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/ 



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#include <conio.h>
#else
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif

#include "globalincs/pstypes.h"
#include "cfile/cfile.h"


static int data_error;
static int no_dir;

// right out of pstypes.h but we don't want to use the INTEL_INT macro here
// since it would require SDL which isn't used on WIN32 platforms
#if BYTE_ORDER == BIG_ENDIAN
#define INT_SWAP(x)	(								\
						(x << 24) |					\
						(((ulong)x) >> 24) |		\
						((x & 0x0000ff00) << 8) |	\
						((x & 0x00ff0000) >> 8)		\
						)
#else
#define INT_SWAP(x) (x)
#endif


size_t fswrite_int(int *wint, FILE *stream)
{
	int tmp = *wint;
	tmp = INT_SWAP(tmp);

	return fwrite(&tmp, 1, sizeof(int), stream);
}

unsigned int Total_size=16; // Start with size of header
unsigned int Num_files =0;
FILE *fp_out = NULL;
FILE *fp_out_hdr = NULL;

typedef struct vp_header {
	char id[4];
	int version;
	int index_offset;
	int num_files;
} vp_header;

//vp_header Vp_header;

char archive_dat[1024];
char archive_hdr[1024];

#define BLOCK_SIZE (1024*1024)
#define VERSION_NUMBER 2;

char tmp_data[BLOCK_SIZE];		// 1 MB

void write_header()
{
	int ver = VERSION_NUMBER;

	fseek(fp_out, 0, SEEK_SET);
	fwrite("VPVP", 1, 4, fp_out);
	fswrite_int(&ver, fp_out);
	fswrite_int((int*)&Total_size, fp_out);
	fswrite_int((int*)&Num_files, fp_out);
}

int write_index(char *hf, char *df)
{
	FILE *h = NULL, *d = NULL;
	unsigned int i;

	h = fopen(hf, "rb");
	d = fopen(df, "a+b");

	if ( (h == NULL) || (d == NULL) )
	{
		if ( h )
			fclose( h );
		if ( d )
			fclose( d );
		return 0;
	}

	for (i = 0; i < Num_files; i++) {
		fread(tmp_data, 32+4+4+4, 1, h);
		fwrite(tmp_data, 32+4+4+4, 1, d);
	}

	fclose(h);
	fclose(d);

	unlink(hf);

	return 1;
}

void pack_file( char *filespec, char *filename, int filesize, _fs_time_t time_write )
{
	char path[1024];

	if ( strstr( filename, ".vp" ))	{
		// Don't pack yourself!!
		return;
	}

	if ( strstr( filename, ".hdr" ))	{
		// Don't pack yourself!!
		return;
	}

	if ( filesize == 0 ) {
		// Don't pack 0 length files, screws up directory structure!
		return;
	}

	if ( strlen(filename) > 31 )	{
		printf( "Filename '%s' too long!  Skipping...\n", filename );
		return;
	}

	memset( path, 0, sizeof(path) );
	strcpy_s( path, filename );

	fswrite_int( (int*)&Total_size, fp_out_hdr );
	fswrite_int( &filesize, fp_out_hdr );
	fwrite( &path, 1, 32, fp_out_hdr );
	fswrite_int( (int*)&time_write, fp_out_hdr );

	Total_size += filesize;
	Num_files++;

	printf( "Packing %s%s%s...", filespec, DIR_SEPARATOR_STR, filename );

	sprintf( path, "%s%s%s", filespec, DIR_SEPARATOR_STR, filename );


	FILE *fp = fopen( path, "rb" );
	
	if ( fp == NULL )	{
		printf( "Error opening '%s'\n", path );
		exit(1);
	}

	int nbytes, nbytes_read=0;

	do	{
		nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
		if ( nbytes > 0 )	{
			fwrite( tmp_data, 1, nbytes, fp_out );
			nbytes_read += nbytes;

		}
	} while( nbytes > 0 );

	fclose(fp);

	printf( " %d bytes\n", nbytes_read );
}

// This function adds a directory marker to the header file
void add_directory( char * dirname)
{
	char path[256];
	char *pathptr = path;
	char *tmpptr;
	int i = 0;

	strcpy_s(path, dirname);

	fswrite_int( (int*)&Total_size, fp_out_hdr);
	fswrite_int( &i, fp_out_hdr);

	// strip out any directories that this dir is a subdir of
	while ( (tmpptr = strchr(pathptr, DIR_SEPARATOR_CHAR)) != NULL ) {
		pathptr = tmpptr+1;
	}

	fwrite(pathptr, 1, 32, fp_out_hdr);
	fswrite_int( &i, fp_out_hdr); // timestamp = 0

	Num_files++;
}

void pack_directory( char * filespec)
{
#ifdef _WIN32
	int find_handle;
	_finddata_t find;
#endif
	char tmp[512];
	char tmp1[512];
	char *ts;

	// strip trailing slash
	ts = filespec + (strlen(filespec) - 1);
	while ( (*ts == DIR_SEPARATOR_CHAR) && (ts > filespec) )
		*ts = '\0';

	strcpy_s( tmp1, filespec );

	add_directory(filespec);
	strcat( tmp1, DIR_SEPARATOR_STR"*.*" );
	
	printf( "In dir '%s'\n", tmp1 );

#ifdef _WIN32
	find_handle = _findfirst( tmp1, &find );
	if( find_handle != -1 )	{
		if ( find.attrib & _A_SUBDIR )	{
			if (strcmp( "..", find.name) && strcmp( ".", find.name) && strcmp( ".svn", find.name))	{
				strcpy_s( tmp, filespec );
				strcat( tmp, "\\" );
				strcat( tmp, find.name );
				pack_directory(tmp);
			}
		} else {
			pack_file( filespec, find.name, find.size, find.time_write );
		}

		while( !_findnext( find_handle, &find ) )	{
			if ( find.attrib & _A_SUBDIR )	{
				if (strcmp( "..", find.name) && strcmp( ".", find.name) && strcmp( ".svn", find.name))	{
					strcpy_s( tmp, filespec );
					strcat( tmp, "\\" );
					strcat( tmp, find.name );
					pack_directory(tmp);

				}
			} else {
				pack_file( filespec, find.name, find.size, find.time_write );
			}
		}
	}
#else
	DIR *dirp;
	struct dirent *dir;

	dirp = opendir (filespec);
	if ( dirp ) {
		while ((dir = readdir(dirp)) != NULL) {

			char fn[MAX_PATH];
			snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
			fn[MAX_PATH-1] = 0;
			
			struct stat buf;
			if (stat(fn, &buf) == -1) {
				continue;
			}

			if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
				continue;
			}

			if ( (strcmp(dir->d_name, ".svn") == 0) ) {
				continue;
			}

			if (S_ISDIR(buf.st_mode)) {
				strcpy_s( tmp, filespec );
				strcat( tmp, "/" );
				strcat( tmp, dir->d_name );
				pack_directory(tmp);
			} else {
				pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
			}
		}
		closedir(dirp);
	} else {
		printf("Error: Source directory does not exist!\n");
		no_dir = 1;
	}
#endif
	add_directory("..");
}

int verify_directory( char *filespec )
{
	char *ts;
	char *dd;

	// strip trailing '/'
	ts = filespec+(strlen(filespec)-1);
	while ( (*ts == DIR_SEPARATOR_CHAR) && (ts > filespec) )
		*ts = '\0';

	// make sure last directory is named "data", ignoring case
	dd = filespec + (strlen(filespec) - 4);
	if ( stricmp( dd, "data" ) )
		data_error = 1;
	
	return data_error;
}

void print_instructions()
{
	printf("Creates a vp archive out of a FreeSpace data tree.\n\n");
	printf("Usage:     cfilearchiver archive_name src_dir\n");
#ifdef _WIN32
	printf("Example:   cfilearchiver freespace c:\\freespace\\data\n");
#else
	printf("Example:   cfilearchiver freespace /tmp/freespace/data\n\n");
#endif
	printf("Creates an archive named freespace out of the freespace data tree\n");
	printf("For information about the FS2 directory structure, please consult\n");
	printf("http://www.hard-light.net/wiki/index.php/FS2_Data_Structure\n");
	exit(0);
}

// we end up #include'ing SDL.h which on Windows and Mac will redefine main() which is something
// that we don't want since we don't actually link against SDL, this solves the problem...
#ifdef main
#undef main
#endif

int main(int argc, char *argv[] )
{
	char archive[1024];
	char *p;

	if ( argc < 3 )	{
		print_instructions();
	}

	strcpy_s( archive, argv[1] );
	p = strchr( archive, '.' );
	if (p) *p = 0;		// remove extension	

	strcpy_s( archive_dat, archive );
	strcat( archive_dat, ".vp" );

	strcpy_s( archive_hdr, archive );
	strcat( archive_hdr, ".hdr" );

	fp_out = fopen( archive_dat, "wb" );
	if ( !fp_out )	{
		printf( "Couldn't open '%s'!\n", archive_dat );
#ifdef _WIN32
		printf( "Press any key to exit...\n" );
		getch();
		return 1;
#else
		exit(1);
#endif
	}

	fp_out_hdr = fopen( archive_hdr, "wb" );
	if ( !fp_out_hdr )	{
		printf( "Couldn't open '%s'!\n", archive_hdr );
#ifdef _WIN32
		printf( "Press any key to exit...\n" );
		getch();
		return 1;
#else
		exit(2);
#endif
	}

	if ( verify_directory( argv[2] ) != 0 ) {
		printf("Warning! Last directory must be named \"data\" (not case sensitive)\n");
		exit(3);
	}

	write_header();

	pack_directory( argv[2] );

	// in case the directory doesn't exist
	if ( no_dir )
		exit(4);

	write_header();

	fclose(fp_out);
	fclose(fp_out_hdr);

	printf( "Data files written, appending index...\n" );

	if (!write_index(archive_hdr, archive_dat)) {
		printf("Error appending index!\n");
#ifdef _WIN32
		printf("Press any key to exit...\n");
		getch();
#endif
		return 1;
	}
	
	printf( "%d total KB.\n", Total_size/1024 );
	return 0;
}