Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / blenkernel / intern / packedFile.c
1 /**
2  * blenkernel/packedFile.c - (cleaned up mar-01 nzc)
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <sys/stat.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifndef WIN32 
41 #include <unistd.h>
42 #else
43 #include <io.h>
44 #endif
45 #include <string.h>
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_image_types.h"
49 #include "DNA_sound_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_packedFile_types.h"
52 #include "DNA_scene_types.h"
53
54 #include "BLI_blenlib.h"
55
56 #include "BKE_utildefines.h"
57 #include "BKE_global.h"
58 #include "BKE_main.h"
59 #include "BKE_screen.h"
60 #include "BKE_sound.h"
61 #include "BKE_image.h"
62 #include "BKE_font.h"
63 #include "BKE_packedFile.h"
64 #include "BKE_report.h"
65
66 int seekPackedFile(PackedFile *pf, int offset, int whence)
67 {
68         int oldseek = -1, seek = 0;
69
70         if (pf) {
71                 oldseek = pf->seek;
72                 switch(whence) {
73                 case SEEK_CUR:
74                         seek = oldseek + offset;
75                         break;
76                 case SEEK_END:
77                         seek = pf->size + offset;
78                         break;
79                 case SEEK_SET:
80                         seek = offset;
81                         break;
82                 default:
83                         oldseek = -1;
84                 }
85                 if (seek < 0) {
86                         seek = 0;
87                 } else if (seek > pf->size) {
88                         seek = pf->size;
89                 }
90                 pf->seek = seek;
91         }
92
93         return(oldseek);
94 }
95         
96 void rewindPackedFile(PackedFile *pf)
97 {
98         seekPackedFile(pf, 0, SEEK_SET);
99 }
100
101 int readPackedFile(PackedFile *pf, void *data, int size)
102
103         if ((pf != NULL) && (size >= 0) && (data != NULL)) {
104                 if (size + pf->seek > pf->size) {
105                         size = pf->size - pf->seek;
106                 }
107
108                 if (size > 0) {
109                         memcpy(data, ((char *) pf->data) + pf->seek, size);
110                 } else {
111                         size = 0;
112                 }
113
114                 pf->seek += size;
115         } else {
116                 size = -1;
117         }
118
119         return(size);
120 }
121
122 int countPackedFiles(Main *bmain)
123 {
124         Image *ima;
125         VFont *vf;
126         bSound *sound;
127         int count = 0;
128         
129         // let's check if there are packed files...
130         for(ima=bmain->image.first; ima; ima=ima->id.next)
131                 if(ima->packedfile)
132                         count++;
133
134         for(vf=bmain->vfont.first; vf; vf=vf->id.next)
135                 if(vf->packedfile)
136                         count++;
137
138         for(sound=bmain->sound.first; sound; sound=sound->id.next)
139                 if(sound->packedfile)
140                         count++;
141
142         return count;
143 }
144
145 void freePackedFile(PackedFile *pf)
146 {
147         if(pf) {
148                 MEM_freeN(pf->data);
149                 MEM_freeN(pf);
150         }
151         else
152                 printf("freePackedFile: Trying to free a NULL pointer\n");
153 }
154         
155 PackedFile *newPackedFileMemory(void *mem, int memlen)
156 {
157         PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
158         pf->data = mem;
159         pf->size = memlen;
160         
161         return pf;
162 }
163
164 PackedFile *newPackedFile(ReportList *reports, char *filename)
165 {
166         PackedFile *pf = NULL;
167         int file, filelen;
168         char name[FILE_MAXDIR+FILE_MAXFILE];
169         void *data;
170         
171         //XXX waitcursor(1);
172         
173         // convert relative filenames to absolute filenames
174         
175         strcpy(name, filename);
176         BLI_convertstringcode(name, G.sce);
177         
178         // open the file
179         // and create a PackedFile structure
180
181         file= open(name, O_BINARY|O_RDONLY);
182         if (file <= 0) {
183                 BKE_reportf(reports, RPT_ERROR, "Can't open file: %s", name);
184         } else {
185                 filelen = BLI_filesize(file);
186
187                 if (filelen == 0) {
188                         // MEM_mallocN complains about MEM_mallocN(0, "bla");
189                         // we don't care....
190                         data = MEM_mallocN(1, "packFile");
191                 } else {
192                         data = MEM_mallocN(filelen, "packFile");
193                 }
194                 if (read(file, data, filelen) == filelen) {
195                         pf = newPackedFileMemory(data, filelen);
196                 }
197
198                 close(file);
199         }
200
201         //XXX waitcursor(0);
202                 
203         return (pf);
204 }
205
206 void packAll(Main *bmain, ReportList *reports)
207 {
208         Image *ima;
209         VFont *vf;
210         bSound *sound;
211
212         for(ima=bmain->image.first; ima; ima=ima->id.next)
213                 if(ima->packedfile == NULL)
214                         ima->packedfile = newPackedFile(reports, ima->name);
215
216         for(vf=bmain->vfont.first; vf; vf=vf->id.next)
217                 if(vf->packedfile == NULL)
218                         vf->packedfile = newPackedFile(reports, vf->name);
219
220         for(sound=bmain->sound.first; sound; sound=sound->id.next)
221                 if(sound->packedfile == NULL)
222                         sound->packedfile = newPackedFile(reports, sound->name);
223 }
224
225
226 /*
227
228 // attempt to create a function that generates an unique filename
229 // this will work when all funtions in fileops.c understand relative filenames...
230
231 char *find_new_name(char *name)
232 {
233         char tempname[FILE_MAXDIR + FILE_MAXFILE];
234         char *newname;
235         
236         if (fop_exists(name)) {
237                 for (number = 1; number <= 999; number++) {
238                         sprintf(tempname, "%s.%03d", name, number);
239                         if (! fop_exists(tempname)) {
240                                 break;
241                         }
242                 }
243         }
244         
245         newname = mallocN(strlen(tempname) + 1, "find_new_name");
246         strcpy(newname, tempname);
247         
248         return(newname);
249 }
250         
251 */
252
253 int writePackedFile(ReportList *reports, char *filename, PackedFile *pf, int guimode)
254 {
255         int file, number, remove_tmp = FALSE;
256         int ret_value = RET_OK;
257         char name[FILE_MAXDIR + FILE_MAXFILE];
258         char tempname[FILE_MAXDIR + FILE_MAXFILE];
259 /*      void *data; */
260         
261         if (guimode); //XXX  waitcursor(1);
262         
263         strcpy(name, filename);
264         BLI_convertstringcode(name, G.sce);
265         
266         if (BLI_exists(name)) {
267                 for (number = 1; number <= 999; number++) {
268                         sprintf(tempname, "%s.%03d_", name, number);
269                         if (! BLI_exists(tempname)) {
270                                 if (BLI_copy_fileops(name, tempname) == RET_OK) {
271                                         remove_tmp = TRUE;
272                                 }
273                                 break;
274                         }
275                 }
276         }
277         
278         // make sure the path to the file exists...
279         BLI_make_existing_file(name);
280         
281         file = open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
282         if (file >= 0) {
283                 if (write(file, pf->data, pf->size) != pf->size) {
284                         BKE_reportf(reports, RPT_ERROR, "Error writing file: %s", name);
285                         ret_value = RET_ERROR;
286                 }
287                 close(file);
288         } else {
289                 BKE_reportf(reports, RPT_ERROR, "Error creating file: %s", name);
290                 ret_value = RET_ERROR;
291         }
292         
293         if (remove_tmp) {
294                 if (ret_value == RET_ERROR) {
295                         if (BLI_rename(tempname, name) != 0) {
296                                 BKE_reportf(reports, RPT_ERROR, "Error restoring tempfile. Check files: '%s' '%s'", tempname, name);
297                         }
298                 } else {
299                         if (BLI_delete(tempname, 0, 0) != 0) {
300                                 BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
301                         }
302                 }
303         }
304         
305         if(guimode); //XXX waitcursor(0);
306
307         return (ret_value);
308 }
309         
310 /* 
311
312 This function compares a packed file to a 'real' file.
313 It returns an integer indicating if:
314
315 PF_EQUAL                - the packed file and original file are identical
316 PF_DIFFERENT    - the packed file and original file differ
317 PF_NOFILE               - the original file doens't exist
318
319 */
320
321 int checkPackedFile(char *filename, PackedFile *pf)
322 {
323         struct stat st;
324         int ret_val, i, len, file;
325         char buf[4096];
326         char name[FILE_MAXDIR + FILE_MAXFILE];
327         
328         strcpy(name, filename);
329         BLI_convertstringcode(name, G.sce);
330         
331         if (stat(name, &st)) {
332                 ret_val = PF_NOFILE;
333         } else if (st.st_size != pf->size) {
334                 ret_val = PF_DIFFERS;
335         } else {
336                 // we'll have to compare the two...
337                 
338                 file = open(name, O_BINARY | O_RDONLY);
339                 if (file < 0) {
340                         ret_val = PF_NOFILE;
341                 } else {
342                         ret_val = PF_EQUAL;
343                         
344                         for (i = 0; i < pf->size; i += sizeof(buf)) {
345                                 len = pf->size - i;
346                                 if (len > sizeof(buf)) {
347                                         len = sizeof(buf);
348                                 }
349                                 
350                                 if (read(file, buf, len) != len) {
351                                         // read error ...
352                                         ret_val = PF_DIFFERS;
353                                         break;
354                                 } else {
355                                         if (memcmp(buf, ((char *)pf->data) + i, len)) {
356                                                 ret_val = PF_DIFFERS;
357                                                 break;
358                                         }
359                                 }
360                         }
361                         
362                         close(file);
363                 }
364         }
365         
366         return(ret_val);
367 }
368
369 /*
370
371    unpackFile() looks at the existing files (abs_name, local_name) and a packed file.
372
373 It returns a char *to the existing file name / new file name or NULL when
374 there was an error or when the user desides to cancel the operation.
375
376 */
377
378 char *unpackFile(ReportList *reports, char *abs_name, char *local_name, PackedFile *pf, int how)
379 {
380         char *newname = NULL, *temp = NULL;
381         
382         // char newabs[FILE_MAXDIR + FILE_MAXFILE];
383         // char newlocal[FILE_MAXDIR + FILE_MAXFILE];
384         
385         if (pf != NULL) {
386                 switch (how) {
387                         case -1:
388                         case PF_KEEP:
389                                 break;
390                         case PF_REMOVE:
391                                 temp= abs_name;
392                                 break;
393                         case PF_USE_LOCAL:
394                                 // if file exists use it
395                                 if (BLI_exists(local_name)) {
396                                         temp = local_name;
397                                         break;
398                                 }
399                                 // else fall through and create it
400                         case PF_WRITE_LOCAL:
401                                 if (writePackedFile(reports, local_name, pf, 1) == RET_OK) {
402                                         temp = local_name;
403                                 }
404                                 break;
405                         case PF_USE_ORIGINAL:
406                                 // if file exists use it
407                                 if (BLI_exists(abs_name)) {
408                                         temp = abs_name;
409                                         break;
410                                 }
411                                 // else fall through and create it
412                         case PF_WRITE_ORIGINAL:
413                                 if (writePackedFile(reports, abs_name, pf, 1) == RET_OK) {
414                                         temp = abs_name;
415                                 }
416                                 break;
417                         default:
418                                 printf("unpackFile: unknown return_value %d\n", how);
419                                 break;
420                 }
421                 
422                 if (temp) {
423                         newname = MEM_mallocN(strlen(temp) + 1, "unpack_file newname");
424                         strcpy(newname, temp);
425                 }
426         }
427         
428         return (newname);
429 }
430
431
432 int unpackVFont(ReportList *reports, VFont *vfont, int how)
433 {
434         char localname[FILE_MAXDIR + FILE_MAXFILE], fi[FILE_MAXFILE];
435         char *newname;
436         int ret_value = RET_ERROR;
437         
438         if (vfont != NULL) {
439                 strcpy(localname, vfont->name);
440                 BLI_splitdirstring(localname, fi);
441                 
442                 sprintf(localname, "//fonts/%s", fi);
443                 
444                 newname = unpackFile(reports, vfont->name, localname, vfont->packedfile, how);
445                 if (newname != NULL) {
446                         ret_value = RET_OK;
447                         freePackedFile(vfont->packedfile);
448                         vfont->packedfile = 0;
449                         strcpy(vfont->name, newname);
450                         MEM_freeN(newname);
451                 }
452         }
453         
454         return (ret_value);
455 }
456
457 int unpackSound(ReportList *reports, bSound *sound, int how)
458 {
459         char localname[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
460         char *newname;
461         int ret_value = RET_ERROR;
462
463         if (sound != NULL) {
464                 strcpy(localname, sound->name);
465                 BLI_splitdirstring(localname, fi);
466                 sprintf(localname, "//sounds/%s", fi);
467
468                 newname = unpackFile(reports, sound->name, localname, sound->packedfile, how);
469                 if (newname != NULL) {
470                         strcpy(sound->name, newname);
471                         MEM_freeN(newname);
472
473                         freePackedFile(sound->packedfile);
474                         sound->packedfile = 0;
475
476                         sound_load(sound);
477
478                         ret_value = RET_OK;
479                 }
480         }
481         
482         return(ret_value);
483 }
484
485 int unpackImage(ReportList *reports, Image *ima, int how)
486 {
487         char localname[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
488         char *newname;
489         int ret_value = RET_ERROR;
490         
491         if (ima != NULL) {
492                 strcpy(localname, ima->name);
493                 BLI_splitdirstring(localname, fi);
494                 sprintf(localname, "//textures/%s", fi);
495                         
496                 newname = unpackFile(reports, ima->name, localname, ima->packedfile, how);
497                 if (newname != NULL) {
498                         ret_value = RET_OK;
499                         freePackedFile(ima->packedfile);
500                         ima->packedfile = NULL;
501                         strcpy(ima->name, newname);
502                         MEM_freeN(newname);
503                         BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
504                 }
505         }
506         
507         return(ret_value);
508 }
509
510 void unpackAll(Main *bmain, ReportList *reports, int how)
511 {
512         Image *ima;
513         VFont *vf;
514         bSound *sound;
515
516         for(ima=bmain->image.first; ima; ima=ima->id.next)
517                 if(ima->packedfile)
518                         unpackImage(reports, ima, how);
519
520         for(vf=bmain->vfont.first; vf; vf=vf->id.next)
521                 if(vf->packedfile)
522                         unpackVFont(reports, vf, how);
523
524         for(sound=bmain->sound.first; sound; sound=sound->id.next)
525                 if(sound->packedfile)
526                         unpackSound(reports, sound, how);
527 }
528