2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/packedFile.c
43 #include "MEM_guardedalloc.h"
45 #include "DNA_image_types.h"
47 #include "DNA_packedFile_types.h"
48 #include "DNA_sound_types.h"
49 #include "DNA_vfont_types.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
55 #include "BKE_global.h"
56 #include "BKE_image.h"
58 #include "BKE_packedFile.h"
59 #include "BKE_report.h"
60 #include "BKE_sound.h"
62 int seekPackedFile(PackedFile *pf, int offset, int whence)
64 int oldseek = -1, seek = 0;
70 seek = oldseek + offset;
73 seek = pf->size + offset;
85 else if (seek > pf->size) {
94 void rewindPackedFile(PackedFile *pf)
96 seekPackedFile(pf, 0, SEEK_SET);
99 int readPackedFile(PackedFile *pf, void *data, int size)
101 if ((pf != NULL) && (size >= 0) && (data != NULL)) {
102 if (size + pf->seek > pf->size) {
103 size = pf->size - pf->seek;
107 memcpy(data, ((char *) pf->data) + pf->seek, size);
122 int countPackedFiles(Main *bmain)
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))
134 for (vf = bmain->vfont.first; vf; vf = vf->id.next)
138 for (sound = bmain->sound.first; sound; sound = sound->id.next)
139 if (sound->packedfile)
145 void freePackedFile(PackedFile *pf)
152 printf("freePackedFile: Trying to free a NULL pointer\n");
155 PackedFile *dupPackedFile(const PackedFile *pf_src)
159 pf_dst = MEM_dupallocN(pf_src);
160 pf_dst->data = MEM_dupallocN(pf_src->data);
165 PackedFile *newPackedFileMemory(void *mem, int memlen)
167 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
174 PackedFile *newPackedFile(ReportList *reports, const char *filename, const char *basepath)
176 PackedFile *pf = NULL;
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')
188 /* convert relative filenames to absolute filenames */
190 BLI_strncpy(name, filename, sizeof(name));
191 BLI_path_abs(name, basepath);
194 * and create a PackedFile structure */
196 file = BLI_open(name, O_BINARY | O_RDONLY, 0);
198 BKE_reportf(reports, RPT_ERROR, "Unable to pack file, source path '%s' not found", name);
201 filelen = BLI_file_descriptor_size(file);
204 /* MEM_mallocN complains about MEM_mallocN(0, "bla");
205 * we don't care.... */
206 data = MEM_mallocN(1, "packFile");
209 data = MEM_mallocN(filelen, "packFile");
211 if (read(file, data, filelen) == filelen) {
212 pf = newPackedFileMemory(data, filelen);
226 /* no libraries for now */
227 void packAll(Main *bmain, ReportList *reports, bool verbose)
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));
240 else if (BKE_image_is_animated(ima) && verbose) {
241 BKE_reportf(reports, RPT_WARNING, "Image '%s' skipped, movies and image sequences not supported",
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);
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);
262 BKE_reportf(reports, RPT_INFO, "Packed %d files", tot);
264 BKE_report(reports, RPT_INFO, "No new files have been packed");
270 // attempt to create a function that generates an unique filename
271 // this will work when all funtions in fileops.c understand relative filenames...
273 static char *find_new_name(char *name)
275 char tempname[FILE_MAX];
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)) {
287 len = strlen(tempname) + 1;
288 newname = MEM_mallocN(len, "find_new_name");
289 memcpy(newname, tempname, len * sizeof(char));
294 int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, int guimode)
297 int ret_value = RET_OK;
298 bool remove_tmp = false;
300 char tempname[FILE_MAX];
303 if (guimode) {} //XXX waitcursor(1);
305 BLI_strncpy(name, filename, sizeof(name));
306 BLI_path_abs(name, G.main->name);
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) {
320 /* make sure the path to the file exists... */
321 BLI_make_existing_file(name);
323 file = BLI_open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
325 BKE_reportf(reports, RPT_ERROR, "Error creating file '%s'", name);
326 ret_value = RET_ERROR;
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;
334 BKE_reportf(reports, RPT_INFO, "Saved packed file to: %s", name);
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);
347 if (BLI_delete(tempname, false, false) != 0) {
348 BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
353 if (guimode) {} //XXX waitcursor(0);
359 * This function compares a packed file to a 'real' file.
360 * It returns an integer indicating if:
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
367 int checkPackedFile(const char *filename, PackedFile *pf)
370 int ret_val, i, len, file;
374 BLI_strncpy(name, filename, sizeof(name));
375 BLI_path_abs(name, G.main->name);
377 if (BLI_stat(name, &st) == -1) {
380 else if (st.st_size != pf->size) {
381 ret_val = PF_DIFFERS;
384 /* we'll have to compare the two... */
386 file = BLI_open(name, O_BINARY | O_RDONLY, 0);
393 for (i = 0; i < pf->size; i += sizeof(buf)) {
395 if (len > sizeof(buf)) {
399 if (read(file, buf, len) != len) {
401 ret_val = PF_DIFFERS;
405 if (memcmp(buf, ((char *)pf->data) + i, len)) {
406 ret_val = PF_DIFFERS;
420 * unpackFile() looks at the existing files (abs_name, local_name) and a packed file.
422 * It returns a char *to the existing file name / new file name or NULL when
423 * there was an error or when the user decides to cancel the operation.
425 * \warning 'abs_name' may be relative still! (use a "//" prefix) be sure to run #BLI_path_abs on it first.
427 char *unpackFile(ReportList *reports, const char *abs_name, const char *local_name, PackedFile *pf, int how)
429 char *newname = NULL;
430 const char *temp = NULL;
442 char temp_abs[FILE_MAX];
444 BLI_strncpy(temp_abs, local_name, sizeof(temp_abs));
445 BLI_path_abs(temp_abs, G.main->name);
447 /* if file exists use it */
448 if (BLI_exists(temp_abs)) {
456 if (writePackedFile(reports, local_name, pf, 1) == RET_OK) {
460 case PF_USE_ORIGINAL:
462 char temp_abs[FILE_MAX];
464 BLI_strncpy(temp_abs, abs_name, sizeof(temp_abs));
465 BLI_path_abs(temp_abs, G.main->name);
467 /* if file exists use it */
468 if (BLI_exists(temp_abs)) {
469 BKE_reportf(reports, RPT_INFO, "Use existing file (instead of packed): %s", abs_name);
476 case PF_WRITE_ORIGINAL:
477 if (writePackedFile(reports, abs_name, pf, 1) == RET_OK) {
482 printf("unpackFile: unknown return_value %d\n", how);
487 newname = BLI_strdup(temp);
494 static void unpack_generate_paths(
495 const char *name, ID *id, char *r_abspath, char *r_relpath, size_t abspathlen, size_t relpathlen)
497 char tempname[FILE_MAX];
498 char tempdir[FILE_MAXDIR];
500 BLI_split_dirfile(name, tempdir, tempname, sizeof(tempdir), sizeof(tempname));
502 if (tempname[0] == '\0') {
503 /* Note: we do not have any real way to re-create extension out of data... */
504 BLI_strncpy(tempname, id->name + 2, sizeof(tempname));
505 printf("%s\n", tempname);
506 BLI_filename_make_safe(tempname);
507 printf("%s\n", tempname);
510 if (tempdir[0] == '\0') {
511 /* Fallback to relative dir. */
512 BLI_strncpy(tempdir, "//", sizeof(tempdir));
515 switch (GS(id->name)) {
517 BLI_snprintf(r_relpath, relpathlen, "//fonts/%s", tempname);
520 BLI_snprintf(r_relpath, relpathlen, "//sounds/%s", tempname);
523 BLI_snprintf(r_relpath, relpathlen, "//textures/%s", tempname);
530 size_t len = BLI_strncpy_rlen(r_abspath, tempdir, abspathlen);
531 BLI_strncpy(r_abspath + len, tempname, abspathlen - len);
535 int unpackVFont(ReportList *reports, VFont *vfont, int how)
537 char localname[FILE_MAX], absname[FILE_MAX];
539 int ret_value = RET_ERROR;
542 unpack_generate_paths(vfont->name, (ID *)vfont, absname, localname, sizeof(absname), sizeof(localname));
543 newname = unpackFile(reports, absname, localname, vfont->packedfile, how);
544 if (newname != NULL) {
546 freePackedFile(vfont->packedfile);
547 vfont->packedfile = NULL;
548 BLI_strncpy(vfont->name, newname, sizeof(vfont->name));
556 int unpackSound(Main *bmain, ReportList *reports, bSound *sound, int how)
558 char localname[FILE_MAX], absname[FILE_MAX];
560 int ret_value = RET_ERROR;
563 unpack_generate_paths(sound->name, (ID *)sound, absname, localname, sizeof(absname), sizeof(localname));
564 newname = unpackFile(reports, absname, localname, sound->packedfile, how);
565 if (newname != NULL) {
566 BLI_strncpy(sound->name, newname, sizeof(sound->name));
569 freePackedFile(sound->packedfile);
570 sound->packedfile = NULL;
572 BKE_sound_load(bmain, sound);
581 int unpackImage(ReportList *reports, Image *ima, int how)
583 int ret_value = RET_ERROR;
586 while (ima->packedfiles.last) {
587 char localname[FILE_MAX], absname[FILE_MAX];
589 ImagePackedFile *imapf = ima->packedfiles.last;
591 unpack_generate_paths(imapf->filepath, (ID *)ima, absname, localname, sizeof(absname), sizeof(localname));
592 newname = unpackFile(reports, absname, localname, imapf->packedfile, how);
594 if (newname != NULL) {
597 ret_value = ret_value == RET_ERROR ? RET_ERROR : RET_OK;
598 freePackedFile(imapf->packedfile);
599 imapf->packedfile = NULL;
601 /* update the new corresponding view filepath */
602 iv = BLI_findstring(&ima->views, imapf->filepath, offsetof(ImageView, filepath));
604 BLI_strncpy(iv->filepath, newname, sizeof(imapf->filepath));
607 /* keep the new name in the image for non-pack specific reasons */
608 if (how != PF_REMOVE) {
609 BLI_strncpy(ima->name, newname, sizeof(imapf->filepath));
614 ret_value = RET_ERROR;
617 BLI_remlink(&ima->packedfiles, imapf);
622 if (ret_value == RET_OK) {
623 BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
629 int unpackLibraries(Main *bmain, ReportList *reports)
633 int ret_value = RET_ERROR;
635 for (lib = bmain->library.first; lib; lib = lib->id.next) {
636 if (lib->packedfile && lib->name[0]) {
638 newname = unpackFile(reports, lib->filepath, lib->filepath, lib->packedfile, PF_WRITE_ORIGINAL);
639 if (newname != NULL) {
642 printf("Unpacked .blend library: %s\n", newname);
644 freePackedFile(lib->packedfile);
645 lib->packedfile = NULL;
655 void packLibraries(Main *bmain, ReportList *reports)
659 /* test for relativenss */
660 for (lib = bmain->library.first; lib; lib = lib->id.next)
661 if (!BLI_path_is_rel(lib->name))
665 BKE_reportf(reports, RPT_ERROR, "Cannot pack absolute file: '%s'", lib->name);
669 for (lib = bmain->library.first; lib; lib = lib->id.next)
670 if (lib->packedfile == NULL)
671 lib->packedfile = newPackedFile(reports, lib->name, bmain->name);
674 void unpackAll(Main *bmain, ReportList *reports, int how)
680 for (ima = bmain->image.first; ima; ima = ima->id.next)
681 if (BKE_image_has_packedfile(ima))
682 unpackImage(reports, ima, how);
684 for (vf = bmain->vfont.first; vf; vf = vf->id.next)
686 unpackVFont(reports, vf, how);
688 for (sound = bmain->sound.first; sound; sound = sound->id.next)
689 if (sound->packedfile)
690 unpackSound(bmain, reports, sound, how);
693 /* ID should be not NULL, return 1 if there's a packed file */
694 bool BKE_pack_check(ID *id)
696 switch (GS(id->name)) {
699 Image *ima = (Image *)id;
700 return BKE_image_has_packedfile(ima);
704 VFont *vf = (VFont *)id;
705 return vf->packedfile != NULL;
709 bSound *snd = (bSound *)id;
710 return snd->packedfile != NULL;
714 Library *li = (Library *)id;
715 return li->packedfile != NULL;
723 /* ID should be not NULL */
724 void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
726 switch (GS(id->name)) {
729 Image *ima = (Image *)id;
730 if (BKE_image_has_packedfile(ima)) {
731 unpackImage(reports, ima, how);
737 VFont *vf = (VFont *)id;
738 if (vf->packedfile) {
739 unpackVFont(reports, vf, how);
745 bSound *snd = (bSound *)id;
746 if (snd->packedfile) {
747 unpackSound(bmain, reports, snd, how);
753 Library *li = (Library *)id;
754 BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->name);