Cleanup: Remove more #if 0 blocks
[blender.git] / source / blender / blenkernel / intern / packedFile.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/packedFile.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <sys/stat.h>
36
37 #ifndef WIN32
38 #include <unistd.h>
39 #else
40 #include <io.h>
41 #endif
42 #include <string.h>
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_image_types.h"
46 #include "DNA_ID.h"
47 #include "DNA_packedFile_types.h"
48 #include "DNA_sound_types.h"
49 #include "DNA_vfont_types.h"
50
51 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
53
54 #include "BKE_font.h"
55 #include "BKE_global.h"
56 #include "BKE_image.h"
57 #include "BKE_main.h"
58 #include "BKE_packedFile.h"
59 #include "BKE_report.h"
60 #include "BKE_sound.h"
61
62 int seekPackedFile(PackedFile *pf, int offset, int whence)
63 {
64         int oldseek = -1, seek = 0;
65
66         if (pf) {
67                 oldseek = pf->seek;
68                 switch (whence) {
69                         case SEEK_CUR:
70                                 seek = oldseek + offset;
71                                 break;
72                         case SEEK_END:
73                                 seek = pf->size + offset;
74                                 break;
75                         case SEEK_SET:
76                                 seek = offset;
77                                 break;
78                         default:
79                                 oldseek = -1;
80                                 break;
81                 }
82                 if (seek < 0) {
83                         seek = 0;
84                 }
85                 else if (seek > pf->size) {
86                         seek = pf->size;
87                 }
88                 pf->seek = seek;
89         }
90
91         return(oldseek);
92 }
93
94 void rewindPackedFile(PackedFile *pf)
95 {
96         seekPackedFile(pf, 0, SEEK_SET);
97 }
98
99 int readPackedFile(PackedFile *pf, void *data, int size)
100 {
101         if ((pf != NULL) && (size >= 0) && (data != NULL)) {
102                 if (size + pf->seek > pf->size) {
103                         size = pf->size - pf->seek;
104                 }
105
106                 if (size > 0) {
107                         memcpy(data, ((char *) pf->data) + pf->seek, size);
108                 }
109                 else {
110                         size = 0;
111                 }
112
113                 pf->seek += size;
114         }
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 (BKE_image_has_packedfile(ima))
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 *dupPackedFile(const PackedFile *pf_src)
156 {
157         PackedFile *pf_dst;
158
159         pf_dst       = MEM_dupallocN(pf_src);
160         pf_dst->data = MEM_dupallocN(pf_src->data);
161
162         return pf_dst;
163 }
164
165 PackedFile *newPackedFileMemory(void *mem, int memlen)
166 {
167         PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
168         pf->data = mem;
169         pf->size = memlen;
170
171         return pf;
172 }
173
174 PackedFile *newPackedFile(ReportList *reports, const char *filename, const char *basepath)
175 {
176         PackedFile *pf = NULL;
177         int file, filelen;
178         char name[FILE_MAX];
179         void *data;
180
181         /* render result has no filename and can be ignored
182          * any other files with no name can be ignored too */
183         if (filename[0] == '\0')
184                 return NULL;
185
186         //XXX waitcursor(1);
187
188         /* convert relative filenames to absolute filenames */
189
190         BLI_strncpy(name, filename, sizeof(name));
191         BLI_path_abs(name, basepath);
192
193         /* open the file
194          * and create a PackedFile structure */
195
196         file = BLI_open(name, O_BINARY | O_RDONLY, 0);
197         if (file == -1) {
198                 BKE_reportf(reports, RPT_ERROR, "Unable to pack file, source path '%s' not found", name);
199         }
200         else {
201                 filelen = BLI_file_descriptor_size(file);
202
203                 if (filelen == 0) {
204                         /* MEM_mallocN complains about MEM_mallocN(0, "bla");
205                          * we don't care.... */
206                         data = MEM_mallocN(1, "packFile");
207                 }
208                 else {
209                         data = MEM_mallocN(filelen, "packFile");
210                 }
211                 if (read(file, data, filelen) == filelen) {
212                         pf = newPackedFileMemory(data, filelen);
213                 }
214                 else {
215                         MEM_freeN(data);
216                 }
217
218                 close(file);
219         }
220
221         //XXX waitcursor(0);
222
223         return (pf);
224 }
225
226 /* no libraries for now */
227 void packAll(Main *bmain, ReportList *reports, bool verbose)
228 {
229         Image *ima;
230         VFont *vfont;
231         bSound *sound;
232         int tot = 0;
233
234         for (ima = bmain->image.first; ima; ima = ima->id.next) {
235                 if (BKE_image_has_packedfile(ima) == false && !ID_IS_LINKED(ima)) {
236                         if (ima->source == IMA_SRC_FILE) {
237                                 BKE_image_packfiles(reports, ima, ID_BLEND_PATH(bmain, &ima->id));
238                                 tot ++;
239                         }
240                         else if (BKE_image_is_animated(ima) && verbose) {
241                                 BKE_reportf(reports, RPT_WARNING, "Image '%s' skipped, movies and image sequences not supported",
242                                             ima->id.name + 2);
243                         }
244                 }
245         }
246
247         for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) {
248                 if (vfont->packedfile == NULL && !ID_IS_LINKED(vfont) && BKE_vfont_is_builtin(vfont) == false) {
249                         vfont->packedfile = newPackedFile(reports, vfont->name, BKE_main_blendfile_path(bmain));
250                         tot ++;
251                 }
252         }
253
254         for (sound = bmain->sound.first; sound; sound = sound->id.next) {
255                 if (sound->packedfile == NULL && !ID_IS_LINKED(sound)) {
256                         sound->packedfile = newPackedFile(reports, sound->name, BKE_main_blendfile_path(bmain));
257                         tot++;
258                 }
259         }
260
261         if (tot > 0)
262                 BKE_reportf(reports, RPT_INFO, "Packed %d files", tot);
263         else if (verbose)
264                 BKE_report(reports, RPT_INFO, "No new files have been packed");
265 }
266
267 int writePackedFile(
268         ReportList *reports, const char *ref_file_name, const char *filename, PackedFile *pf, const bool guimode)
269 {
270         int file, number;
271         int ret_value = RET_OK;
272         bool remove_tmp = false;
273         char name[FILE_MAX];
274         char tempname[FILE_MAX];
275 /*      void *data; */
276
277         if (guimode) {} //XXX  waitcursor(1);
278
279         BLI_strncpy(name, filename, sizeof(name));
280         BLI_path_abs(name, ref_file_name);
281
282         if (BLI_exists(name)) {
283                 for (number = 1; number <= 999; number++) {
284                         BLI_snprintf(tempname, sizeof(tempname), "%s.%03d_", name, number);
285                         if (!BLI_exists(tempname)) {
286                                 if (BLI_copy(name, tempname) == RET_OK) {
287                                         remove_tmp = true;
288                                 }
289                                 break;
290                         }
291                 }
292         }
293
294         /* make sure the path to the file exists... */
295         BLI_make_existing_file(name);
296
297         file = BLI_open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
298         if (file == -1) {
299                 BKE_reportf(reports, RPT_ERROR, "Error creating file '%s'", name);
300                 ret_value = RET_ERROR;
301         }
302         else {
303                 if (write(file, pf->data, pf->size) != pf->size) {
304                         BKE_reportf(reports, RPT_ERROR, "Error writing file '%s'", name);
305                         ret_value = RET_ERROR;
306                 }
307                 else {
308                         BKE_reportf(reports, RPT_INFO, "Saved packed file to: %s", name);
309                 }
310
311                 close(file);
312         }
313
314         if (remove_tmp) {
315                 if (ret_value == RET_ERROR) {
316                         if (BLI_rename(tempname, name) != 0) {
317                                 BKE_reportf(reports, RPT_ERROR, "Error restoring temp file (check files '%s' '%s')", tempname, name);
318                         }
319                 }
320                 else {
321                         if (BLI_delete(tempname, false, false) != 0) {
322                                 BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
323                         }
324                 }
325         }
326
327         if (guimode) {} //XXX waitcursor(0);
328
329         return (ret_value);
330 }
331
332 /**
333  * This function compares a packed file to a 'real' file.
334  * It returns an integer indicating if:
335  *
336  * - PF_EQUAL:     the packed file and original file are identical
337  * - PF_DIFFERENT: the packed file and original file differ
338  * - PF_NOFILE:    the original file doesn't exist
339  */
340 int checkPackedFile(const char *ref_file_name, const char *filename, PackedFile *pf)
341 {
342         BLI_stat_t st;
343         int ret_val, i, len, file;
344         char buf[4096];
345         char name[FILE_MAX];
346
347         BLI_strncpy(name, filename, sizeof(name));
348         BLI_path_abs(name, ref_file_name);
349
350         if (BLI_stat(name, &st) == -1) {
351                 ret_val = PF_NOFILE;
352         }
353         else if (st.st_size != pf->size) {
354                 ret_val = PF_DIFFERS;
355         }
356         else {
357                 /* we'll have to compare the two... */
358
359                 file = BLI_open(name, O_BINARY | O_RDONLY, 0);
360                 if (file == -1) {
361                         ret_val = PF_NOFILE;
362                 }
363                 else {
364                         ret_val = PF_EQUAL;
365
366                         for (i = 0; i < pf->size; i += sizeof(buf)) {
367                                 len = pf->size - i;
368                                 if (len > sizeof(buf)) {
369                                         len = sizeof(buf);
370                                 }
371
372                                 if (read(file, buf, len) != len) {
373                                         /* read error ... */
374                                         ret_val = PF_DIFFERS;
375                                         break;
376                                 }
377                                 else {
378                                         if (memcmp(buf, ((char *)pf->data) + i, len)) {
379                                                 ret_val = PF_DIFFERS;
380                                                 break;
381                                         }
382                                 }
383                         }
384
385                         close(file);
386                 }
387         }
388
389         return(ret_val);
390 }
391
392 /**
393  * unpackFile() looks at the existing files (abs_name, local_name) and a packed file.
394  *
395  * It returns a char *to the existing file name / new file name or NULL when
396  * there was an error or when the user decides to cancel the operation.
397  *
398  * \warning 'abs_name' may be relative still! (use a "//" prefix) be sure to run #BLI_path_abs on it first.
399  */
400 char *unpackFile(
401         ReportList *reports, const char *ref_file_name,
402         const char *abs_name, const char *local_name, PackedFile *pf, int how)
403 {
404         char *newname = NULL;
405         const char *temp = NULL;
406
407         if (pf != NULL) {
408                 switch (how) {
409                         case -1:
410                         case PF_KEEP:
411                                 break;
412                         case PF_REMOVE:
413                                 temp = abs_name;
414                                 break;
415                         case PF_USE_LOCAL:
416                         {
417                                 char temp_abs[FILE_MAX];
418
419                                 BLI_strncpy(temp_abs, local_name, sizeof(temp_abs));
420                                 BLI_path_abs(temp_abs, ref_file_name);
421
422                                 /* if file exists use it */
423                                 if (BLI_exists(temp_abs)) {
424                                         temp = local_name;
425                                         break;
426                                 }
427                                 /* else create it */
428                                 ATTR_FALLTHROUGH;
429                         }
430                         case PF_WRITE_LOCAL:
431                                 if (writePackedFile(reports, ref_file_name, local_name, pf, 1) == RET_OK) {
432                                         temp = local_name;
433                                 }
434                                 break;
435                         case PF_USE_ORIGINAL:
436                         {
437                                 char temp_abs[FILE_MAX];
438
439                                 BLI_strncpy(temp_abs, abs_name, sizeof(temp_abs));
440                                 BLI_path_abs(temp_abs, ref_file_name);
441
442                                 /* if file exists use it */
443                                 if (BLI_exists(temp_abs)) {
444                                         BKE_reportf(reports, RPT_INFO, "Use existing file (instead of packed): %s", abs_name);
445                                         temp = abs_name;
446                                         break;
447                                 }
448                                 /* else create it */
449                                 ATTR_FALLTHROUGH;
450                         }
451                         case PF_WRITE_ORIGINAL:
452                                 if (writePackedFile(reports, ref_file_name, abs_name, pf, 1) == RET_OK) {
453                                         temp = abs_name;
454                                 }
455                                 break;
456                         default:
457                                 printf("unpackFile: unknown return_value %d\n", how);
458                                 break;
459                 }
460
461                 if (temp) {
462                         newname = BLI_strdup(temp);
463                 }
464         }
465
466         return newname;
467 }
468
469 static void unpack_generate_paths(
470         const char *name, ID *id, char *r_abspath, char *r_relpath, size_t abspathlen, size_t relpathlen)
471 {
472         char tempname[FILE_MAX];
473         char tempdir[FILE_MAXDIR];
474
475         BLI_split_dirfile(name, tempdir, tempname, sizeof(tempdir), sizeof(tempname));
476
477         if (tempname[0] == '\0') {
478                 /* Note: we do not have any real way to re-create extension out of data... */
479                 BLI_strncpy(tempname, id->name + 2, sizeof(tempname));
480                 printf("%s\n", tempname);
481                 BLI_filename_make_safe(tempname);
482                 printf("%s\n", tempname);
483         }
484
485         if (tempdir[0] == '\0') {
486                 /* Fallback to relative dir. */
487                 BLI_strncpy(tempdir, "//", sizeof(tempdir));
488         }
489
490         switch (GS(id->name)) {
491                 case ID_VF:
492                         BLI_snprintf(r_relpath, relpathlen, "//fonts/%s", tempname);
493                         break;
494                 case ID_SO:
495                         BLI_snprintf(r_relpath, relpathlen, "//sounds/%s", tempname);
496                         break;
497                 case ID_IM:
498                         BLI_snprintf(r_relpath, relpathlen, "//textures/%s", tempname);
499                         break;
500                 default:
501                         break;
502         }
503
504         {
505                 size_t len = BLI_strncpy_rlen(r_abspath, tempdir, abspathlen);
506                 BLI_strncpy(r_abspath + len, tempname, abspathlen - len);
507         }
508 }
509
510 int unpackVFont(Main *bmain, ReportList *reports, VFont *vfont, int how)
511 {
512         char localname[FILE_MAX], absname[FILE_MAX];
513         char *newname;
514         int ret_value = RET_ERROR;
515
516         if (vfont != NULL) {
517                 unpack_generate_paths(vfont->name, (ID *)vfont, absname, localname, sizeof(absname), sizeof(localname));
518                 newname = unpackFile(reports, BKE_main_blendfile_path(bmain), absname, localname, vfont->packedfile, how);
519                 if (newname != NULL) {
520                         ret_value = RET_OK;
521                         freePackedFile(vfont->packedfile);
522                         vfont->packedfile = NULL;
523                         BLI_strncpy(vfont->name, newname, sizeof(vfont->name));
524                         MEM_freeN(newname);
525                 }
526         }
527
528         return (ret_value);
529 }
530
531 int unpackSound(Main *bmain, ReportList *reports, bSound *sound, int how)
532 {
533         char localname[FILE_MAX], absname[FILE_MAX];
534         char *newname;
535         int ret_value = RET_ERROR;
536
537         if (sound != NULL) {
538                 unpack_generate_paths(sound->name, (ID *)sound, absname, localname, sizeof(absname), sizeof(localname));
539                 newname = unpackFile(reports, BKE_main_blendfile_path(bmain), absname, localname, sound->packedfile, how);
540                 if (newname != NULL) {
541                         BLI_strncpy(sound->name, newname, sizeof(sound->name));
542                         MEM_freeN(newname);
543
544                         freePackedFile(sound->packedfile);
545                         sound->packedfile = NULL;
546
547                         BKE_sound_load(bmain, sound);
548
549                         ret_value = RET_OK;
550                 }
551         }
552
553         return(ret_value);
554 }
555
556 int unpackImage(Main *bmain, ReportList *reports, Image *ima, int how)
557 {
558         int ret_value = RET_ERROR;
559
560         if (ima != NULL) {
561                 while (ima->packedfiles.last) {
562                         char localname[FILE_MAX], absname[FILE_MAX];
563                         char *newname;
564                         ImagePackedFile *imapf = ima->packedfiles.last;
565
566                         unpack_generate_paths(imapf->filepath, (ID *)ima, absname, localname, sizeof(absname), sizeof(localname));
567                         newname = unpackFile(reports, BKE_main_blendfile_path(bmain), absname, localname, imapf->packedfile, how);
568
569                         if (newname != NULL) {
570                                 ImageView *iv;
571
572                                 ret_value = ret_value == RET_ERROR ? RET_ERROR : RET_OK;
573                                 freePackedFile(imapf->packedfile);
574                                 imapf->packedfile = NULL;
575
576                                 /* update the new corresponding view filepath */
577                                 iv = BLI_findstring(&ima->views, imapf->filepath, offsetof(ImageView, filepath));
578                                 if (iv) {
579                                         BLI_strncpy(iv->filepath, newname, sizeof(imapf->filepath));
580                                 }
581
582                                 /* keep the new name in the image for non-pack specific reasons */
583                                 if (how != PF_REMOVE) {
584                                         BLI_strncpy(ima->name, newname, sizeof(imapf->filepath));
585                                 }
586                                 MEM_freeN(newname);
587                         }
588                         else {
589                                 ret_value = RET_ERROR;
590                         }
591
592                         BLI_remlink(&ima->packedfiles, imapf);
593                         MEM_freeN(imapf);
594                 }
595         }
596
597         if (ret_value == RET_OK) {
598                 BKE_image_signal(bmain, ima, NULL, IMA_SIGNAL_RELOAD);
599         }
600
601         return(ret_value);
602 }
603
604 int unpackLibraries(Main *bmain, ReportList *reports)
605 {
606         Library *lib;
607         char *newname;
608         int ret_value = RET_ERROR;
609
610         for (lib = bmain->library.first; lib; lib = lib->id.next) {
611                 if (lib->packedfile && lib->name[0]) {
612
613                         newname = unpackFile(reports, BKE_main_blendfile_path(bmain), lib->filepath, lib->filepath, lib->packedfile, PF_WRITE_ORIGINAL);
614                         if (newname != NULL) {
615                                 ret_value = RET_OK;
616
617                                 printf("Unpacked .blend library: %s\n", newname);
618
619                                 freePackedFile(lib->packedfile);
620                                 lib->packedfile = NULL;
621
622                                 MEM_freeN(newname);
623                         }
624                 }
625         }
626
627         return(ret_value);
628 }
629
630 void packLibraries(Main *bmain, ReportList *reports)
631 {
632         Library *lib;
633
634         /* test for relativenss */
635         for (lib = bmain->library.first; lib; lib = lib->id.next)
636                 if (!BLI_path_is_rel(lib->name))
637                         break;
638
639         if (lib) {
640                 BKE_reportf(reports, RPT_ERROR, "Cannot pack absolute file: '%s'", lib->name);
641                 return;
642         }
643
644         for (lib = bmain->library.first; lib; lib = lib->id.next)
645                 if (lib->packedfile == NULL)
646                         lib->packedfile = newPackedFile(reports, lib->name, BKE_main_blendfile_path(bmain));
647 }
648
649 void unpackAll(Main *bmain, ReportList *reports, int how)
650 {
651         Image *ima;
652         VFont *vf;
653         bSound *sound;
654
655         for (ima = bmain->image.first; ima; ima = ima->id.next)
656                 if (BKE_image_has_packedfile(ima))
657                         unpackImage(bmain, reports, ima, how);
658
659         for (vf = bmain->vfont.first; vf; vf = vf->id.next)
660                 if (vf->packedfile)
661                         unpackVFont(bmain, reports, vf, how);
662
663         for (sound = bmain->sound.first; sound; sound = sound->id.next)
664                 if (sound->packedfile)
665                         unpackSound(bmain, reports, sound, how);
666 }
667
668 /* ID should be not NULL, return 1 if there's a packed file */
669 bool BKE_pack_check(ID *id)
670 {
671         switch (GS(id->name)) {
672                 case ID_IM:
673                 {
674                         Image *ima = (Image *)id;
675                         return BKE_image_has_packedfile(ima);
676                 }
677                 case ID_VF:
678                 {
679                         VFont *vf = (VFont *)id;
680                         return vf->packedfile != NULL;
681                 }
682                 case ID_SO:
683                 {
684                         bSound *snd = (bSound *)id;
685                         return snd->packedfile != NULL;
686                 }
687                 case ID_LI:
688                 {
689                         Library *li = (Library *)id;
690                         return li->packedfile != NULL;
691                 }
692                 default:
693                         break;
694         }
695         return false;
696 }
697
698 /* ID should be not NULL */
699 void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
700 {
701         switch (GS(id->name)) {
702                 case ID_IM:
703                 {
704                         Image *ima = (Image *)id;
705                         if (BKE_image_has_packedfile(ima)) {
706                                 unpackImage(bmain, reports, ima, how);
707                         }
708                         break;
709                 }
710                 case ID_VF:
711                 {
712                         VFont *vf = (VFont *)id;
713                         if (vf->packedfile) {
714                                 unpackVFont(bmain, reports, vf, how);
715                         }
716                         break;
717                 }
718                 case ID_SO:
719                 {
720                         bSound *snd = (bSound *)id;
721                         if (snd->packedfile) {
722                                 unpackSound(bmain, reports, snd, how);
723                         }
724                         break;
725                 }
726                 case ID_LI:
727                 {
728                         Library *li = (Library *)id;
729                         BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->name);
730                         break;
731                 }
732                 default:
733                         break;
734         }
735 }