]> icculus.org git repositories - taylor/freespace2.git/blob - src/cfilearchiver/cfilearchiver.cpp
silence various compiler warnings (clang:rel)
[taylor/freespace2.git] / src / cfilearchiver / cfilearchiver.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Program to create an archive file for use with cfile stuff
16  *
17  * $Log$
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
25  *
26  * Revision 1.5  2003/05/04 04:49:48  taylor
27  * improve error handling, instructions
28  *
29  * Revision 1.4  2003/01/30 20:01:46  relnev
30  * ported (Taylor Richards)
31  *
32  * Revision 1.3  2002/06/09 04:41:15  relnev
33  * added copyright header
34  *
35  * Revision 1.2  2002/05/07 03:16:43  theoddone33
36  * The Great Newline Fix
37  *
38  * Revision 1.1.1.1  2002/05/03 03:28:08  root
39  * Initial import.
40  *
41  * 
42  * 2     10/23/98 6:15p Dave
43  *
44  * $NoKeywords: $
45  */
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #ifndef PLAT_UNIX
51 #include <direct.h>
52 #include <io.h>
53 #include <conio.h>
54 #else
55 #include <dirent.h>
56 #include <unistd.h>
57 #include <sys/stat.h>
58 #include <sys/types.h>
59 #endif
60
61 #define SDL_MAIN_HANDLED
62
63 #include "pstypes.h"
64
65 #ifndef PLAT_UNIX
66 #define strcasecmp stricmp
67 #endif
68
69 static int data_error;
70 static int no_dir;
71
72 unsigned int Total_size=16; // Start with size of header
73 unsigned int Num_files =0;
74 FILE * fp_out = NULL;
75 FILE * fp_out_hdr = NULL;
76
77 typedef struct vp_header {
78         char id[4];
79         int version;
80         int index_offset;
81         int num_files;
82 } vp_header;
83
84 //vp_header Vp_header;
85
86 char archive_dat[1024];
87 char archive_hdr[1024];
88
89 #define BLOCK_SIZE (1024*1024)
90 #define VERSION_NUMBER 2;
91
92 char tmp_data[BLOCK_SIZE];              // 1 MB
93
94 void write_header()
95 {
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);
102 }
103
104 int write_index(char *hf, char *df)
105 {
106         FILE *h = fopen(hf, "rb");
107
108         if ( !h ) {
109                 return 0;
110         }
111
112         FILE *d = fopen(df, "a+b");
113
114         if ( !d ) {
115                 fclose(h);
116                 return 0;
117         }
118
119         for (unsigned int i=0;i<Num_files;i++) {
120                 if ( !fread(tmp_data, 32+4+4+4, 1, h) ) {
121                         fclose(h);
122                         fclose(d);
123                         return 0;
124                 }
125
126                 fwrite(tmp_data, 32+4+4+4, 1, d);
127         }
128
129         fclose(h);
130         fclose(d);
131
132         return 1;
133 }
134
135 void pack_file(const char *filespec, const char *filename, int filesize, fs_time_t time_write )
136 {
137         char path[1024];
138
139         if ( strstr( filename, ".vp" )) {
140                 // Don't pack yourself!!
141                 return;
142         }
143
144         if ( strstr( filename, ".hdr" ))        {
145                 // Don't pack yourself!!
146                 return;
147         }
148
149         if ( filesize == 0 ) {
150                 // Don't pack 0 length files, screws up directory structure!
151                 return;
152         }
153
154         memset( path, 0, sizeof(path));
155         strcpy( path, filename );
156         if ( strlen(filename)>31 )      {
157                 printf( "Filename '%s' too long\n", filename );
158                 exit(1);
159         }
160         fwrite( &Total_size, 1, 4, fp_out_hdr );
161         fwrite( &filesize, 1, 4, fp_out_hdr );
162         fwrite( &path, 1, 32, fp_out_hdr );
163         fwrite( &time_write, 1, sizeof(fs_time_t), fp_out_hdr);
164
165         Total_size += filesize;
166         Num_files++;
167
168         printf( "Packing %s%s%s...", filespec, DIR_SEPARATOR_STR, filename );
169
170
171         sprintf( path, "%s%s%s", filespec, DIR_SEPARATOR_STR, filename );
172
173
174         FILE *fp = fopen( path, "rb" );
175         
176         if ( fp == NULL )       {
177                 printf( "Error opening '%s'\n", path );
178                 exit(1);
179         }
180
181         int nbytes, nbytes_read=0;
182
183         do      {
184                 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
185                 if ( nbytes > 0 )       {
186                         fwrite( tmp_data, 1, nbytes, fp_out );
187                         nbytes_read += nbytes;
188
189                 }
190         } while( nbytes > 0 );
191
192         fclose(fp);
193
194         printf( " %d bytes\n", nbytes_read );
195 }
196
197 // This function adds a directory marker to the header file
198 void add_directory(const char * dirname)
199 {
200         char path[256];
201         char *pathptr = path;
202         char *tmpptr;
203
204         if ( strlen(dirname) >= sizeof(path) )
205                 return;
206
207         strcpy(path, dirname);
208         fwrite(&Total_size, 1, 4, fp_out_hdr);
209         int i = 0;
210         fwrite(&i, 1, 4, fp_out_hdr);
211         // strip out any directories that this dir is a subdir of
212         while ((tmpptr = strchr(pathptr, DIR_SEPARATOR_CHAR)) != NULL) {
213                 pathptr = tmpptr+1;
214         }
215         fwrite(pathptr, 1, 32, fp_out_hdr);
216         fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
217         Num_files++;
218 }
219
220 void pack_directory(const char * filespec)
221 {
222 #ifndef PLAT_UNIX
223         int find_handle;
224         _finddata_t find;
225 #endif
226         char tmp[512];
227         char tmp1[512];
228         char *ts;
229
230         // size safety check
231         if ( strlen(filespec) >= (sizeof(tmp1) + 5) )
232                 return;
233
234 /*
235         char dir_name[512];
236         char *last_slash;
237
238         last_slash = strrchr(filespec, '\\');
239         if ( last_slash ) {
240                 strcpy(dir_name, last_slash+1);
241         } else {
242                 strcpy(dir_name, filespec);
243         }
244
245         if ( !strcasecmp(dir_name, "voice") ) {
246                 return;
247         }
248 */
249
250         strcpy( tmp1, filespec );
251
252         // strip trailing slash
253         ts = tmp1+(strlen(tmp1)-1);
254         if (*ts == DIR_SEPARATOR_CHAR)
255                 *ts = '\0';
256
257         add_directory(tmp1);
258         strcat( tmp1, DIR_SEPARATOR_STR );
259         strcat( tmp1, "*.*" );
260
261         printf( "In dir '%s'\n", tmp1 );
262
263 #ifndef PLAT_UNIX
264         find_handle = _findfirst( tmp1, &find );
265         if( find_handle != -1 ) {
266                 if ( find.attrib & _A_SUBDIR )  {
267                         if (strcmp( "..", find.name) && strcmp( ".", find.name))        {
268                                 strcpy( tmp, filespec );
269                                 strcat( tmp, "\\" );
270                                 strcat( tmp, find.name );
271                                 pack_directory(tmp);
272                         }
273                 } else {
274                         pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
275                 }
276
277                 while( !_findnext( find_handle, &find ) )       {
278                         if ( find.attrib & _A_SUBDIR )  {
279                                 if (strcmp( "..", find.name) && strcmp( ".", find.name))        {
280                                         strcpy( tmp, filespec );
281                                         strcat( tmp, "\\" );
282                                         strcat( tmp, find.name );
283                                         pack_directory(tmp);
284
285                                 }
286                         } else {
287                                 pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
288                         }
289                 }
290         }
291 #else
292         DIR *dirp;
293         struct dirent *dir;
294
295         dirp = opendir (filespec);
296         if ( dirp ) {
297                 while ((dir = readdir(dirp)) != NULL) {
298
299                         char fn[MAX_PATH_LEN];
300                         snprintf(fn, MAX_PATH_LEN-1, "%s/%s", filespec, dir->d_name);
301                         fn[MAX_PATH_LEN-1] = 0;
302                         
303                         struct stat buf;
304                         if (stat(fn, &buf) == -1) {
305                                 continue;
306                         }
307
308                         if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
309                                 continue;
310                         }
311
312                         if (S_ISDIR(buf.st_mode)) {
313                                 strcpy( tmp, filespec );
314                                 strcat( tmp, "/" );
315                                 strcat( tmp, dir->d_name );
316                                 pack_directory(tmp);
317                         } else {
318                                 pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
319                         }
320                 }
321                 closedir(dirp);
322         } else {
323                 printf("Error: Source directory does not exist!\n");
324                 no_dir = 1;
325         }
326 #endif
327         add_directory("..");
328 }
329
330 int verify_directory(const char *filespec )
331 {
332         char tmp[512] = { 0 };
333         char *ts;
334         char *dd;
335
336         // strip trailing slash
337         strncpy(tmp, filespec, sizeof(tmp)-1);
338
339         ts = tmp+(strlen(tmp)-1);
340         if (*ts == DIR_SEPARATOR_CHAR)
341                 *ts = '\0';
342
343         // make sure last directory is named "data", ignoring case
344         dd = tmp+(strlen(tmp)-4);
345         if ( strcasecmp( dd, "data" ) )
346                 data_error = 1;
347
348         return data_error;
349 }
350
351 int main(int argc, char *argv[] )
352 {
353         char archive[1024];
354         char *p;
355
356         if ( argc < 3 ) {
357                 printf("Creates a vp archive out of a FreeSpace data tree.\n\n");
358                 printf("Usage:          cfilearchiver archive_name src_dir\n");
359 #ifdef PLAT_UNIX
360                 printf("Example:        cfilearchiver freespace /tmp/freespace/data\n\n");
361 #else
362                 printf("Example:        cfilearchiver freespace c:\\freespace\\data\n\n");
363                 printf("Press any key to exit...\n");
364                 getch();
365 #endif
366                 return 1;
367         }
368
369         strcpy( archive, argv[1] );
370         p = strchr( archive, '.' );
371         if (p) *p = 0;          // remove extension     
372
373         strcpy( archive_dat, archive );
374         strcat( archive_dat, ".vp" );
375
376         strcpy( archive_hdr, archive );
377         strcat( archive_hdr, ".hdr" );
378
379         fp_out = fopen( archive_dat, "wb" );
380         if ( !fp_out )  {
381                 printf( "Couldn't open '%s'!\n", archive_dat );
382 #ifndef PLAT_UNIX
383                 printf( "Press any key to exit...\n" );
384                 getch();
385 #endif
386                 return 2;
387         }
388
389         fp_out_hdr = fopen( archive_hdr, "wb" );
390         if ( !fp_out_hdr )      {
391                 printf( "Couldn't open '%s'!\n", archive_hdr );
392 #ifndef PLAT_UNIX
393                 printf( "Press any key to exit...\n" );
394                 getch();
395 #endif
396                 return 3;
397         }
398
399         if ( verify_directory( argv[2] ) != 0 ) {
400                 printf("Warning! Last directory must be named \"data\" (not case sensitive)\n");
401                 return 4;
402         }
403
404         write_header();
405
406         pack_directory( argv[2] );
407
408         // in case the directory doesn't exist
409         if ( no_dir )
410                 return 5;
411
412         write_header();
413
414         fclose(fp_out);
415         fclose(fp_out_hdr);
416
417         printf( "Data files written, appending index...\n" );
418
419         if (!write_index(archive_hdr, archive_dat)) {
420                 printf("Error appending index!\n");
421 #ifndef PLAT_UNIX
422                 printf("Press any key to exit...\n");
423                 getch();
424 #endif
425                 return 6;
426         }
427         
428         printf( "%d total KB.\n", Total_size/1024 );
429         return 0;
430 }