2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
15 * Program to create an archive file for use with cfile stuff
18 * Revision 1.6 2004/12/15 04:10:45 taylor
19 * outwnd_unix.cpp from fs2_open for logging to file in debug mode
20 * fixes for default function values
21 * always use vm_* functions for sanity sake
22 * make cfilearchiver 64-bit compatible
23 * fix crash on exit from double free()
24 * fix crash on startup from extra long GL extension string in debug
26 * Revision 1.5 2003/05/04 04:49:48 taylor
27 * improve error handling, instructions
29 * Revision 1.4 2003/01/30 20:01:46 relnev
30 * ported (Taylor Richards)
32 * Revision 1.3 2002/06/09 04:41:15 relnev
33 * added copyright header
35 * Revision 1.2 2002/05/07 03:16:43 theoddone33
36 * The Great Newline Fix
38 * Revision 1.1.1.1 2002/05/03 03:28:08 root
42 * 2 10/23/98 6:15p Dave
57 #include <sys/types.h>
60 #define SDL_MAIN_HANDLED
65 #define strcasecmp stricmp
68 static int data_error;
71 unsigned int Total_size=16; // Start with size of header
72 unsigned int Num_files =0;
74 FILE * fp_out_hdr = NULL;
76 typedef struct vp_header {
83 //vp_header Vp_header;
85 char archive_dat[1024];
86 char archive_hdr[1024];
88 #define BLOCK_SIZE (1024*1024)
89 #define VERSION_NUMBER 2;
91 char tmp_data[BLOCK_SIZE]; // 1 MB
95 fseek(fp_out, 0, SEEK_SET);
96 fwrite("VPVP", 1, 4, fp_out);
97 int ver = VERSION_NUMBER;
98 fwrite(&ver, 1, 4, fp_out);
99 fwrite(&Total_size, 1, 4, fp_out);
100 fwrite(&Num_files, 1, 4, fp_out);
103 int write_index(char *hf, char *df)
105 FILE *h = fopen(hf, "rb");
111 FILE *d = fopen(df, "a+b");
118 for (unsigned int i=0;i<Num_files;i++) {
119 if ( !fread(tmp_data, 32+4+4+4, 1, h) ) {
125 fwrite(tmp_data, 32+4+4+4, 1, d);
134 void pack_file(const char *filespec, const char *filename, int filesize, fs_time_t time_write )
138 if ( strstr( filename, ".vp" )) {
139 // Don't pack yourself!!
143 if ( strstr( filename, ".hdr" )) {
144 // Don't pack yourself!!
148 if ( filesize == 0 ) {
149 // Don't pack 0 length files, screws up directory structure!
153 memset( path, 0, sizeof(path));
154 strcpy( path, filename );
155 if ( strlen(filename)>31 ) {
156 printf( "Filename '%s' too long\n", filename );
159 fwrite( &Total_size, 1, 4, fp_out_hdr );
160 fwrite( &filesize, 1, 4, fp_out_hdr );
161 fwrite( &path, 1, 32, fp_out_hdr );
162 fwrite( &time_write, 1, sizeof(fs_time_t), fp_out_hdr);
164 Total_size += filesize;
167 printf( "Packing %s%s%s...", filespec, DIR_SEPARATOR_STR, filename );
170 sprintf( path, "%s%s%s", filespec, DIR_SEPARATOR_STR, filename );
173 FILE *fp = fopen( path, "rb" );
176 printf( "Error opening '%s'\n", path );
180 int nbytes, nbytes_read=0;
183 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
185 fwrite( tmp_data, 1, nbytes, fp_out );
186 nbytes_read += nbytes;
189 } while( nbytes > 0 );
193 printf( " %d bytes\n", nbytes_read );
196 // This function adds a directory marker to the header file
197 void add_directory(const char * dirname)
200 char *pathptr = path;
203 if ( strlen(dirname) >= sizeof(path) )
206 strcpy(path, dirname);
207 fwrite(&Total_size, 1, 4, fp_out_hdr);
209 fwrite(&i, 1, 4, fp_out_hdr);
210 // strip out any directories that this dir is a subdir of
211 while ((tmpptr = strchr(pathptr, DIR_SEPARATOR_CHAR)) != NULL) {
214 fwrite(pathptr, 1, 32, fp_out_hdr);
215 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
219 void pack_directory(const char * filespec)
230 if ( strlen(filespec) >= (sizeof(tmp1) + 5) )
237 last_slash = strrchr(filespec, '\\');
239 strcpy(dir_name, last_slash+1);
241 strcpy(dir_name, filespec);
244 if ( !strcasecmp(dir_name, "voice") ) {
249 strcpy( tmp1, filespec );
251 // strip trailing slash
252 ts = tmp1+(strlen(tmp1)-1);
253 if (*ts == DIR_SEPARATOR_CHAR)
257 strcat( tmp1, DIR_SEPARATOR_STR );
258 strcat( tmp1, "*.*" );
260 printf( "In dir '%s'\n", tmp1 );
263 find_handle = _findfirst( tmp1, &find );
264 if( find_handle != -1 ) {
265 if ( find.attrib & _A_SUBDIR ) {
266 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
267 strcpy( tmp, filespec );
269 strcat( tmp, find.name );
273 pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
276 while( !_findnext( find_handle, &find ) ) {
277 if ( find.attrib & _A_SUBDIR ) {
278 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
279 strcpy( tmp, filespec );
281 strcat( tmp, find.name );
286 pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
294 dirp = opendir (filespec);
296 while ((dir = readdir(dirp)) != NULL) {
298 char fn[MAX_PATH_LEN];
299 snprintf(fn, MAX_PATH_LEN-1, "%s/%s", filespec, dir->d_name);
300 fn[MAX_PATH_LEN-1] = 0;
303 if (stat(fn, &buf) == -1) {
307 if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
311 if (S_ISDIR(buf.st_mode)) {
312 strcpy( tmp, filespec );
314 strcat( tmp, dir->d_name );
317 pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
322 printf("Error: Source directory does not exist!\n");
329 int verify_directory(const char *filespec )
331 char tmp[512] = { 0 };
335 // strip trailing slash
336 strncpy(tmp, filespec, sizeof(tmp)-1);
338 ts = tmp+(strlen(tmp)-1);
339 if (*ts == DIR_SEPARATOR_CHAR)
342 // make sure last directory is named "data", ignoring case
343 dd = tmp+(strlen(tmp)-4);
344 if ( strcasecmp( dd, "data" ) )
350 int main(int argc, char *argv[] )
356 printf("Creates a vp archive out of a FreeSpace data tree.\n\n");
357 printf("Usage: cfilearchiver archive_name src_dir\n");
359 printf("Example: cfilearchiver freespace /tmp/freespace/data\n\n");
361 printf("Example: cfilearchiver freespace c:\\freespace\\data\n\n");
362 printf("Press any key to exit...\n");
368 strcpy( archive, argv[1] );
369 p = strchr( archive, '.' );
370 if (p) *p = 0; // remove extension
372 strcpy( archive_dat, archive );
373 strcat( archive_dat, ".vp" );
375 strcpy( archive_hdr, archive );
376 strcat( archive_hdr, ".hdr" );
378 fp_out = fopen( archive_dat, "wb" );
380 printf( "Couldn't open '%s'!\n", archive_dat );
382 printf( "Press any key to exit...\n" );
388 fp_out_hdr = fopen( archive_hdr, "wb" );
390 printf( "Couldn't open '%s'!\n", archive_hdr );
392 printf( "Press any key to exit...\n" );
398 if ( verify_directory( argv[2] ) != 0 ) {
399 printf("Warning! Last directory must be named \"data\" (not case sensitive)\n");
405 pack_directory( argv[2] );
407 // in case the directory doesn't exist
416 printf( "Data files written, appending index...\n" );
418 if (!write_index(archive_hdr, archive_dat)) {
419 printf("Error appending index!\n");
421 printf("Press any key to exit...\n");
427 printf( "%d total KB.\n", Total_size/1024 );