Merge branch 'master' into blender2.8
[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, bmain->name);
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, bmain->name);
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
268 #if 0
269
270 // attempt to create a function that generates an unique filename
271 // this will work when all funtions in fileops.c understand relative filenames...
272
273 static char *find_new_name(char *name)
274 {
275         char tempname[FILE_MAX];
276         char *newname;
277         size_t len;
278         
279         if (fop_exists(name)) {
280                 for (number = 1; number <= 999; number++) {
281                         BLI_snprintf(tempname, sizeof(tempname), "%s.%03d", name, number);
282                         if (!fop_exists(tempname)) {
283                                 break;
284                         }
285                 }
286         }
287         len = strlen(tempname) + 1;
288         newname = MEM_mallocN(len, "find_new_name");
289         memcpy(newname, tempname, len * sizeof(char));
290         return newname;
291 }
292 #endif
293
294 int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, int guimode)
295 {
296         int file, number;
297         int ret_value = RET_OK;
298         bool remove_tmp = false;
299         char name[FILE_MAX];
300         char tempname[FILE_MAX];
301 /*      void *data; */
302         
303         if (guimode) {} //XXX  waitcursor(1);
304         
305         BLI_strncpy(name, filename, sizeof(name));
306         BLI_path_abs(name, G.main->name);
307         
308         if (BLI_exists(name)) {
309                 for (number = 1; number <= 999; number++) {
310                         BLI_snprintf(tempname, sizeof(tempname), "%s.%03d_", name, number);
311                         if (!BLI_exists(tempname)) {
312                                 if (BLI_copy(name, tempname) == RET_OK) {
313                                         remove_tmp = true;
314                                 }
315                                 break;
316                         }
317                 }
318         }
319         
320         /* make sure the path to the file exists... */
321         BLI_make_existing_file(name);
322         
323         file = BLI_open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
324         if (file == -1) {
325                 BKE_reportf(reports, RPT_ERROR, "Error creating file '%s'", name);
326                 ret_value = RET_ERROR;
327         }
328         else {
329                 if (write(file, pf->data, pf->size) != pf->size) {
330                         BKE_reportf(reports, RPT_ERROR, "Error writing file '%s'", name);
331                         ret_value = RET_ERROR;
332                 }
333                 else {
334                         BKE_reportf(reports, RPT_INFO, "Saved packed file to: %s", name);
335                 }
336                 
337                 close(file);
338         }
339         
340         if (remove_tmp) {
341                 if (ret_value == RET_ERROR) {
342                         if (BLI_rename(tempname, name) != 0) {
343                                 BKE_reportf(reports, RPT_ERROR, "Error restoring temp file (check files '%s' '%s')", tempname, name);
344                         }
345                 }
346                 else {
347                         if (BLI_delete(tempname, false, false) != 0) {
348                                 BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
349                         }
350                 }
351         }
352         
353         if (guimode) {} //XXX waitcursor(0);
354
355         return (ret_value);
356 }
357
358 /**
359  * This function compares a packed file to a 'real' file.
360  * It returns an integer indicating if:
361  *
362  * - PF_EQUAL:     the packed file and original file are identical
363  * - PF_DIFFERENT: the packed file and original file differ
364  * - PF_NOFILE:    the original file doens't exist
365  */
366 int checkPackedFile(const char *filename, PackedFile *pf)
367 {
368         BLI_stat_t st;
369         int ret_val, i, len, file;
370         char buf[4096];
371         char name[FILE_MAX];
372         
373         BLI_strncpy(name, filename, sizeof(name));
374         BLI_path_abs(name, G.main->name);
375         
376         if (BLI_stat(name, &st) == -1) {
377                 ret_val = PF_NOFILE;
378         }
379         else if (st.st_size != pf->size) {
380                 ret_val = PF_DIFFERS;
381         }
382         else {
383                 /* we'll have to compare the two... */
384
385                 file = BLI_open(name, O_BINARY | O_RDONLY, 0);
386                 if (file == -1) {
387                         ret_val = PF_NOFILE;
388                 }
389                 else {
390                         ret_val = PF_EQUAL;
391
392                         for (i = 0; i < pf->size; i += sizeof(buf)) {
393                                 len = pf->size - i;
394                                 if (len > sizeof(buf)) {
395                                         len = sizeof(buf);
396                                 }
397
398                                 if (read(file, buf, len) != len) {
399                                         /* read error ... */
400                                         ret_val = PF_DIFFERS;
401                                         break;
402                                 }
403                                 else {
404                                         if (memcmp(buf, ((char *)pf->data) + i, len)) {
405                                                 ret_val = PF_DIFFERS;
406                                                 break;
407                                         }
408                                 }
409                         }
410                         
411                         close(file);
412                 }
413         }
414         
415         return(ret_val);
416 }
417
418 /**
419  * unpackFile() looks at the existing files (abs_name, local_name) and a packed file.
420  *
421  * It returns a char *to the existing file name / new file name or NULL when
422  * there was an error or when the user decides to cancel the operation.
423  *
424  * \warning 'abs_name' may be relative still! (use a "//" prefix) be sure to run #BLI_path_abs on it first.
425  */
426 char *unpackFile(ReportList *reports, const char *abs_name, const char *local_name, PackedFile *pf, int how)
427 {
428         char *newname = NULL;
429         const char *temp = NULL;
430         
431         if (pf != NULL) {
432                 switch (how) {
433                         case -1:
434                         case PF_KEEP:
435                                 break;
436                         case PF_REMOVE:
437                                 temp = abs_name;
438                                 break;
439                         case PF_USE_LOCAL:
440                         {
441                                 char temp_abs[FILE_MAX];
442
443                                 BLI_strncpy(temp_abs, local_name, sizeof(temp_abs));
444                                 BLI_path_abs(temp_abs, G.main->name);
445
446                                 /* if file exists use it */
447                                 if (BLI_exists(temp_abs)) {
448                                         temp = local_name;
449                                         break;
450                                 }
451                                 /* else create it */
452                                 ATTR_FALLTHROUGH;
453                         }
454                         case PF_WRITE_LOCAL:
455                                 if (writePackedFile(reports, local_name, pf, 1) == RET_OK) {
456                                         temp = local_name;
457                                 }
458                                 break;
459                         case PF_USE_ORIGINAL:
460                         {
461                                 char temp_abs[FILE_MAX];
462
463                                 BLI_strncpy(temp_abs, abs_name, sizeof(temp_abs));
464                                 BLI_path_abs(temp_abs, G.main->name);
465
466                                 /* if file exists use it */
467                                 if (BLI_exists(temp_abs)) {
468                                         BKE_reportf(reports, RPT_INFO, "Use existing file (instead of packed): %s", abs_name);
469                                         temp = abs_name;
470                                         break;
471                                 }
472                                 /* else create it */
473                                 ATTR_FALLTHROUGH;
474                         }
475                         case PF_WRITE_ORIGINAL:
476                                 if (writePackedFile(reports, abs_name, pf, 1) == RET_OK) {
477                                         temp = abs_name;
478                                 }
479                                 break;
480                         default:
481                                 printf("unpackFile: unknown return_value %d\n", how);
482                                 break;
483                 }
484                 
485                 if (temp) {
486                         newname = BLI_strdup(temp);
487                 }
488         }
489         
490         return newname;
491 }
492
493 static void unpack_generate_paths(
494         const char *name, ID *id, char *r_abspath, char *r_relpath, size_t abspathlen, size_t relpathlen)
495 {
496         char tempname[FILE_MAX];
497         char tempdir[FILE_MAXDIR];
498
499         BLI_split_dirfile(name, tempdir, tempname, sizeof(tempdir), sizeof(tempname));
500
501         if (tempname[0] == '\0') {
502                 /* Note: we do not have any real way to re-create extension out of data... */
503                 BLI_strncpy(tempname, id->name + 2, sizeof(tempname));
504                 printf("%s\n", tempname);
505                 BLI_filename_make_safe(tempname);
506                 printf("%s\n", tempname);
507         }
508
509         if (tempdir[0] == '\0') {
510                 /* Fallback to relative dir. */
511                 BLI_strncpy(tempdir, "//", sizeof(tempdir));
512         }
513
514         switch (GS(id->name)) {
515                 case ID_VF:
516                         BLI_snprintf(r_relpath, relpathlen, "//fonts/%s", tempname);
517                         break;
518                 case ID_SO:
519                         BLI_snprintf(r_relpath, relpathlen, "//sounds/%s", tempname);
520                         break;
521                 case ID_IM:
522                         BLI_snprintf(r_relpath, relpathlen, "//textures/%s", tempname);
523                         break;
524                 default:
525                         break;
526         }
527
528         {
529                 size_t len = BLI_strncpy_rlen(r_abspath, tempdir, abspathlen);
530                 BLI_strncpy(r_abspath + len, tempname, abspathlen - len);
531         }
532 }
533
534 int unpackVFont(ReportList *reports, VFont *vfont, int how)
535 {
536         char localname[FILE_MAX], absname[FILE_MAX];
537         char *newname;
538         int ret_value = RET_ERROR;
539         
540         if (vfont != NULL) {
541                 unpack_generate_paths(vfont->name, (ID *)vfont, absname, localname, sizeof(absname), sizeof(localname));
542                 newname = unpackFile(reports, absname, localname, vfont->packedfile, how);
543                 if (newname != NULL) {
544                         ret_value = RET_OK;
545                         freePackedFile(vfont->packedfile);
546                         vfont->packedfile = NULL;
547                         BLI_strncpy(vfont->name, newname, sizeof(vfont->name));
548                         MEM_freeN(newname);
549                 }
550         }
551         
552         return (ret_value);
553 }
554
555 int unpackSound(Main *bmain, ReportList *reports, bSound *sound, int how)
556 {
557         char localname[FILE_MAX], absname[FILE_MAX];
558         char *newname;
559         int ret_value = RET_ERROR;
560
561         if (sound != NULL) {
562                 unpack_generate_paths(sound->name, (ID *)sound, absname, localname, sizeof(absname), sizeof(localname));
563                 newname = unpackFile(reports, absname, localname, sound->packedfile, how);
564                 if (newname != NULL) {
565                         BLI_strncpy(sound->name, newname, sizeof(sound->name));
566                         MEM_freeN(newname);
567
568                         freePackedFile(sound->packedfile);
569                         sound->packedfile = NULL;
570
571                         BKE_sound_load(bmain, sound);
572
573                         ret_value = RET_OK;
574                 }
575         }
576         
577         return(ret_value);
578 }
579
580 int unpackImage(ReportList *reports, Image *ima, int how)
581 {
582         int ret_value = RET_ERROR;
583
584         if (ima != NULL) {
585                 while (ima->packedfiles.last) {
586                         char localname[FILE_MAX], absname[FILE_MAX];
587                         char *newname;
588                         ImagePackedFile *imapf = ima->packedfiles.last;
589
590                         unpack_generate_paths(imapf->filepath, (ID *)ima, absname, localname, sizeof(absname), sizeof(localname));
591                         newname = unpackFile(reports, absname, localname, imapf->packedfile, how);
592
593                         if (newname != NULL) {
594                                 ImageView *iv;
595
596                                 ret_value = ret_value == RET_ERROR ? RET_ERROR : RET_OK;
597                                 freePackedFile(imapf->packedfile);
598                                 imapf->packedfile = NULL;
599
600                                 /* update the new corresponding view filepath */
601                                 iv = BLI_findstring(&ima->views, imapf->filepath, offsetof(ImageView, filepath));
602                                 if (iv) {
603                                         BLI_strncpy(iv->filepath, newname, sizeof(imapf->filepath));
604                                 }
605
606                                 /* keep the new name in the image for non-pack specific reasons */
607                                 if (how != PF_REMOVE) {
608                                         BLI_strncpy(ima->name, newname, sizeof(imapf->filepath));
609                                 }
610                                 MEM_freeN(newname);
611                         }
612                         else {
613                                 ret_value = RET_ERROR;
614                         }
615
616                         BLI_remlink(&ima->packedfiles, imapf);
617                         MEM_freeN(imapf);
618                 }
619         }
620
621         if (ret_value == RET_OK) {
622                 BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
623         }
624
625         return(ret_value);
626 }
627
628 int unpackLibraries(Main *bmain, ReportList *reports)
629 {
630         Library *lib;
631         char *newname;
632         int ret_value = RET_ERROR;
633         
634         for (lib = bmain->library.first; lib; lib = lib->id.next) {
635                 if (lib->packedfile && lib->name[0]) {
636                         
637                         newname = unpackFile(reports, lib->filepath, lib->filepath, lib->packedfile, PF_WRITE_ORIGINAL);
638                         if (newname != NULL) {
639                                 ret_value = RET_OK;
640                                 
641                                 printf("Unpacked .blend library: %s\n", newname);
642                                 
643                                 freePackedFile(lib->packedfile);
644                                 lib->packedfile = NULL;
645
646                                 MEM_freeN(newname);
647                         }
648                 }
649         }
650         
651         return(ret_value);
652 }
653
654 void packLibraries(Main *bmain, ReportList *reports)
655 {
656         Library *lib;
657         
658         /* test for relativenss */
659         for (lib = bmain->library.first; lib; lib = lib->id.next)
660                 if (!BLI_path_is_rel(lib->name))
661                         break;
662         
663         if (lib) {
664                 BKE_reportf(reports, RPT_ERROR, "Cannot pack absolute file: '%s'", lib->name);
665                 return;
666         }
667         
668         for (lib = bmain->library.first; lib; lib = lib->id.next)
669                 if (lib->packedfile == NULL)
670                         lib->packedfile = newPackedFile(reports, lib->name, bmain->name);
671 }
672
673 void unpackAll(Main *bmain, ReportList *reports, int how)
674 {
675         Image *ima;
676         VFont *vf;
677         bSound *sound;
678
679         for (ima = bmain->image.first; ima; ima = ima->id.next)
680                 if (BKE_image_has_packedfile(ima))
681                         unpackImage(reports, ima, how);
682
683         for (vf = bmain->vfont.first; vf; vf = vf->id.next)
684                 if (vf->packedfile)
685                         unpackVFont(reports, vf, how);
686
687         for (sound = bmain->sound.first; sound; sound = sound->id.next)
688                 if (sound->packedfile)
689                         unpackSound(bmain, reports, sound, how);
690 }
691
692 /* ID should be not NULL, return 1 if there's a packed file */
693 bool BKE_pack_check(ID *id)
694 {
695         switch (GS(id->name)) {
696                 case ID_IM:
697                 {
698                         Image *ima = (Image *)id;
699                         return BKE_image_has_packedfile(ima);
700                 }
701                 case ID_VF:
702                 {
703                         VFont *vf = (VFont *)id;
704                         return vf->packedfile != NULL;
705                 }
706                 case ID_SO:
707                 {
708                         bSound *snd = (bSound *)id;
709                         return snd->packedfile != NULL;
710                 }
711                 case ID_LI:
712                 {
713                         Library *li = (Library *)id;
714                         return li->packedfile != NULL;
715                 }
716                 default:
717                         break;
718         }
719         return false;
720 }
721
722 /* ID should be not NULL */
723 void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
724 {
725         switch (GS(id->name)) {
726                 case ID_IM:
727                 {
728                         Image *ima = (Image *)id;
729                         if (BKE_image_has_packedfile(ima)) {
730                                 unpackImage(reports, ima, how);
731                         }
732                         break;
733                 }
734                 case ID_VF:
735                 {
736                         VFont *vf = (VFont *)id;
737                         if (vf->packedfile) {
738                                 unpackVFont(reports, vf, how);
739                         }
740                         break;
741                 }
742                 case ID_SO:
743                 {
744                         bSound *snd = (bSound *)id;
745                         if (snd->packedfile) {
746                                 unpackSound(bmain, reports, snd, how);
747                         }
748                         break;
749                 }
750                 case ID_LI:
751                 {
752                         Library *li = (Library *)id;
753                         BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->name);
754                         break;
755                 }
756                 default:
757                         break;
758         }
759 }