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
58 #include <sys/types.h>
61 #define SDL_MAIN_HANDLED
66 #define strcasecmp stricmp
69 static int data_error;
72 unsigned int Total_size=16; // Start with size of header
73 unsigned int Num_files =0;
75 FILE * fp_out_hdr = NULL;
77 typedef struct vp_header {
84 //vp_header Vp_header;
86 char archive_dat[1024];
87 char archive_hdr[1024];
89 #define BLOCK_SIZE (1024*1024)
90 #define VERSION_NUMBER 2;
92 char tmp_data[BLOCK_SIZE]; // 1 MB
96 fseek(fp_out, 0, SEEK_SET);
97 fwrite("VPVP", 1, 4, fp_out);
98 int ver = VERSION_NUMBER;
99 fwrite(&ver, 1, 4, fp_out);
100 fwrite(&Total_size, 1, 4, fp_out);
101 fwrite(&Num_files, 1, 4, fp_out);
104 int write_index(char *hf, char *df)
106 FILE *h = fopen(hf, "rb");
108 FILE *d = fopen(df, "a+b");
110 for (unsigned int i=0;i<Num_files;i++) {
111 fread(tmp_data, 32+4+4+4, 1, h);
112 fwrite(tmp_data, 32+4+4+4, 1, d);
119 void pack_file(const char *filespec, const char *filename, int filesize, fs_time_t time_write )
123 if ( strstr( filename, ".vp" )) {
124 // Don't pack yourself!!
128 if ( strstr( filename, ".hdr" )) {
129 // Don't pack yourself!!
133 if ( filesize == 0 ) {
134 // Don't pack 0 length files, screws up directory structure!
138 memset( path, 0, sizeof(path));
139 strcpy( path, filename );
140 if ( strlen(filename)>31 ) {
141 printf( "Filename '%s' too long\n", filename );
144 fwrite( &Total_size, 1, 4, fp_out_hdr );
145 fwrite( &filesize, 1, 4, fp_out_hdr );
146 fwrite( &path, 1, 32, fp_out_hdr );
147 fwrite( &time_write, 1, sizeof(fs_time_t), fp_out_hdr);
149 Total_size += filesize;
152 printf( "Packing %s%s%s...", filespec, DIR_SEPARATOR_STR, filename );
155 sprintf( path, "%s%s%s", filespec, DIR_SEPARATOR_STR, filename );
158 FILE *fp = fopen( path, "rb" );
161 printf( "Error opening '%s'\n", path );
165 int nbytes, nbytes_read=0;
168 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
170 fwrite( tmp_data, 1, nbytes, fp_out );
171 nbytes_read += nbytes;
174 } while( nbytes > 0 );
178 printf( " %d bytes\n", nbytes_read );
181 // This function adds a directory marker to the header file
182 void add_directory(const char * dirname)
185 char *pathptr = path;
188 if ( strlen(dirname) >= sizeof(path) )
191 strcpy(path, dirname);
192 fwrite(&Total_size, 1, 4, fp_out_hdr);
194 fwrite(&i, 1, 4, fp_out_hdr);
195 // strip out any directories that this dir is a subdir of
196 while ((tmpptr = strchr(pathptr, DIR_SEPARATOR_CHAR)) != NULL) {
199 fwrite(pathptr, 1, 32, fp_out_hdr);
200 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
204 void pack_directory(const char * filespec)
215 if ( strlen(filespec) >= (sizeof(tmp1) + 5) )
222 last_slash = strrchr(filespec, '\\');
224 strcpy(dir_name, last_slash+1);
226 strcpy(dir_name, filespec);
229 if ( !strcasecmp(dir_name, "voice") ) {
234 strcpy( tmp1, filespec );
236 // strip trailing slash
237 ts = tmp1+(strlen(tmp1)-1);
238 if (*ts == DIR_SEPARATOR_CHAR)
242 strcat( tmp1, DIR_SEPARATOR_STR );
243 strcat( tmp1, "*.*" );
245 printf( "In dir '%s'\n", tmp1 );
248 find_handle = _findfirst( tmp1, &find );
249 if( find_handle != -1 ) {
250 if ( find.attrib & _A_SUBDIR ) {
251 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
252 strcpy( tmp, filespec );
254 strcat( tmp, find.name );
258 pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
261 while( !_findnext( find_handle, &find ) ) {
262 if ( find.attrib & _A_SUBDIR ) {
263 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
264 strcpy( tmp, filespec );
266 strcat( tmp, find.name );
271 pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
279 dirp = opendir (filespec);
281 while ((dir = readdir(dirp)) != NULL) {
283 char fn[MAX_PATH_LEN];
284 snprintf(fn, MAX_PATH_LEN-1, "%s/%s", filespec, dir->d_name);
285 fn[MAX_PATH_LEN-1] = 0;
288 if (stat(fn, &buf) == -1) {
292 if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
296 if (S_ISDIR(buf.st_mode)) {
297 strcpy( tmp, filespec );
299 strcat( tmp, dir->d_name );
302 pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
307 printf("Error: Source directory does not exist!\n");
314 int verify_directory(const char *filespec )
316 char tmp[512] = { 0 };
320 // strip trailing slash
321 strncpy(tmp, filespec, sizeof(tmp)-1);
323 ts = tmp+(strlen(tmp)-1);
324 if (*ts == DIR_SEPARATOR_CHAR)
327 // make sure last directory is named "data", ignoring case
328 dd = tmp+(strlen(tmp)-4);
329 if ( strcasecmp( dd, "data" ) )
335 int main(int argc, char *argv[] )
341 printf("Creates a vp archive out of a FreeSpace data tree.\n\n");
342 printf("Usage: cfilearchiver archive_name src_dir\n");
344 printf("Example: cfilearchiver freespace /tmp/freespace/data\n\n");
346 printf("Example: cfilearchiver freespace c:\\freespace\\data\n\n");
347 printf("Press any key to exit...\n");
353 strcpy( archive, argv[1] );
354 p = strchr( archive, '.' );
355 if (p) *p = 0; // remove extension
357 strcpy( archive_dat, archive );
358 strcat( archive_dat, ".vp" );
360 strcpy( archive_hdr, archive );
361 strcat( archive_hdr, ".hdr" );
363 fp_out = fopen( archive_dat, "wb" );
365 printf( "Couldn't open '%s'!\n", archive_dat );
367 printf( "Press any key to exit...\n" );
373 fp_out_hdr = fopen( archive_hdr, "wb" );
375 printf( "Couldn't open '%s'!\n", archive_hdr );
377 printf( "Press any key to exit...\n" );
383 if ( verify_directory( argv[2] ) != 0 ) {
384 printf("Warning! Last directory must be named \"data\" (not case sensitive)\n");
390 pack_directory( argv[2] );
392 // in case the directory doesn't exist
401 printf( "Data files written, appending index...\n" );
403 if (!write_index(archive_hdr, archive_dat)) {
404 printf("Error appending index!\n");
406 printf("Press any key to exit...\n");
412 printf( "%d total KB.\n", Total_size/1024 );