3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): Campbell barton
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/blenlib/intern/bpath.c
39 /* path/file handeling stuff */
45 #include "BLI_winstuff.h"
48 #include "MEM_guardedalloc.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_scene_types.h" /* to get the current frame */
52 #include "DNA_image_types.h"
53 #include "DNA_texture_types.h"
54 #include "DNA_text_types.h"
55 #include "DNA_sound_types.h"
56 #include "DNA_sequence_types.h"
57 #include "DNA_vfont_types.h"
58 #include "DNA_windowmanager_types.h"
60 #include "BLI_blenlib.h"
61 #include "BLI_bpath.h"
62 #include "BLI_utildefines.h"
64 #include "BKE_global.h"
65 #include "BKE_image.h" /* so we can check the image's type */
66 #include "BKE_sequencer.h"
68 #include "BKE_utildefines.h"
69 #include "BKE_report.h"
71 typedef struct BPathIteratorSeqData
75 struct Sequence **seqar; /* Sequence */
76 struct Scene *scene; /* Current scene */
77 } BPathIteratorSeqData;
79 typedef struct BPathIterator
81 char* _path; /* never access directly, use BLI_bpathIterator_getPath */
87 int flag; /* iterator options */
89 void (*setpath_callback)(struct BPathIterator *, const char *);
90 void (*getpath_callback)(struct BPathIterator *, char *);
92 const char* base_path; /* base path, the directory the blend file is in - normally bmain->name */
96 /* only for seq data */
97 struct BPathIteratorSeqData seqdata;
103 /* TODO - BPATH_PLUGIN, BPATH_SEQ */
117 void BLI_bpathIterator_init(struct BPathIterator **bpi_pt, Main *bmain, const char *basedir, const int flag)
121 bpi= MEM_mallocN(sizeof(BPathIterator), "BLI_bpathIterator_init");
124 bpi->type= BPATH_IMAGE;
127 bpi->getpath_callback= NULL;
128 bpi->setpath_callback= NULL;
130 /* Sequencer specific */
131 bpi->seqdata.totseq= 0;
133 bpi->seqdata.seqar= NULL;
134 bpi->seqdata.scene= NULL;
138 bpi->base_path= basedir; /* normally bmain->name */
141 BLI_bpathIterator_step(bpi);
145 static void BLI_bpathIterator_alloc(struct BPathIterator **bpi)
147 *bpi= MEM_mallocN(sizeof(BPathIterator), "BLI_bpathIterator_alloc");
151 void BLI_bpathIterator_free(struct BPathIterator *bpi)
153 if (bpi->seqdata.seqar)
154 MEM_freeN((void *)bpi->seqdata.seqar);
155 bpi->seqdata.seqar= NULL;
156 bpi->seqdata.scene= NULL;
161 void BLI_bpathIterator_getPath(struct BPathIterator *bpi, char *path)
163 if (bpi->getpath_callback) {
164 bpi->getpath_callback(bpi, path);
167 strcpy(path, bpi->_path); /* warning, we assume 'path' are long enough */
171 void BLI_bpathIterator_setPath(struct BPathIterator *bpi, const char *path)
173 if (bpi->setpath_callback) {
174 bpi->setpath_callback(bpi, path);
177 strcpy(bpi->_path, path); /* warning, we assume 'path' are long enough */
181 void BLI_bpathIterator_getPathExpanded(struct BPathIterator *bpi, char *path_expanded)
185 BLI_bpathIterator_getPath(bpi, path_expanded);
186 libpath= BLI_bpathIterator_getLib(bpi);
188 if (libpath) { /* check the files location relative to its library path */
189 BLI_path_abs(path_expanded, libpath);
191 else { /* local data, use the blend files path */
192 BLI_path_abs(path_expanded, bpi->base_path);
194 BLI_cleanup_file(NULL, path_expanded);
196 const char* BLI_bpathIterator_getLib(struct BPathIterator *bpi)
200 const char* BLI_bpathIterator_getName(struct BPathIterator *bpi)
204 int BLI_bpathIterator_getType(struct BPathIterator *bpi)
208 unsigned int BLI_bpathIterator_getPathMaxLen(struct BPathIterator *bpi)
212 const char* BLI_bpathIterator_getBasePath(struct BPathIterator *bpi)
214 return bpi->base_path;
217 /* gets the first or the next image that has a path - not a viewer node or generated image */
218 static struct Image *ima_stepdata__internal(struct Image *ima, const int step_next, const int flag)
227 if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
228 if(ima->packedfile==NULL || (flag & BPATH_USE_PACKED)) {
232 /* image is not a image with a path, skip it */
238 static struct Tex *tex_stepdata__internal(struct Tex *tex, const int step_next, const int UNUSED(flag))
247 if (tex->type == TEX_VOXELDATA && tex->vd && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format))
249 /* image is not a image with a path, skip it */
255 static struct Text *text_stepdata__internal(struct Text *text, const int step_next, const int UNUSED(flag))
266 /* image is not a image with a path, skip it */
272 static struct VFont *vf_stepdata__internal(struct VFont *vf, const int step_next, const int flag)
281 if (strcmp(vf->name, FO_BUILTIN_NAME)!=0) {
282 if(vf->packedfile==NULL || (flag & BPATH_USE_PACKED)) {
287 /* font with no path, skip it */
293 static struct bSound *snd_stepdata__internal(struct bSound *snd, int step_next, const int flag)
302 if(snd->packedfile==NULL || (flag & BPATH_USE_PACKED)) {
306 /* font with no path, skip it */
312 static struct Sequence *seq_stepdata__internal(struct BPathIterator *bpi, int step_next)
318 if (bpi->seqdata.scene==NULL) {
319 bpi->seqdata.scene= bpi->bmain->scene.first;
326 while (bpi->seqdata.scene) {
327 ed= seq_give_editing(bpi->seqdata.scene, 0);
329 if (bpi->seqdata.seqar == NULL) {
330 /* allocate the sequencer array */
331 seq_array(ed, &bpi->seqdata.seqar, &bpi->seqdata.totseq, 0);
335 if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
339 seq= bpi->seqdata.seqar[bpi->seqdata.seq];
340 while (!SEQ_HAS_PATH(seq) && seq->plugin==NULL) {
342 if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
346 seq= bpi->seqdata.seqar[bpi->seqdata.seq];
353 /* keep looking through the next scene, reallocate seq array */
354 if (bpi->seqdata.seqar) {
355 MEM_freeN((void *)bpi->seqdata.seqar);
356 bpi->seqdata.seqar= NULL;
358 bpi->seqdata.scene= bpi->seqdata.scene->id.next;
362 /* no seq data in this scene, next */
363 bpi->seqdata.scene= bpi->seqdata.scene->id.next;
370 static void seq_getpath(struct BPathIterator *bpi, char *path)
372 Sequence *seq= (Sequence *)bpi->data;
375 path[0]= '\0'; /* incase we cant get the path */
376 if (seq==NULL) return;
377 if (SEQ_HAS_PATH(seq)) {
378 if (ELEM3(seq->type, SEQ_IMAGE, SEQ_MOVIE, SEQ_SOUND)) {
379 BLI_strncpy(path, seq->strip->dir, FILE_MAX);
380 BLI_add_slash(path); /* incase its missing */
381 if (seq->strip->stripdata) { /* should always be true! */
382 /* Using the first image is weak for image sequences */
383 strcat(path, seq->strip->stripdata->name);
388 BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
391 else if (seq->plugin) {
392 BLI_strncpy(seq->plugin->name, path, sizeof(seq->plugin->name));
396 static void seq_setpath(struct BPathIterator *bpi, const char *path)
398 Sequence *seq= (Sequence *)bpi->data;
399 if (seq==NULL) return;
401 if (SEQ_HAS_PATH(seq)) {
402 if (ELEM3(seq->type, SEQ_IMAGE, SEQ_MOVIE, SEQ_SOUND)) {
403 BLI_split_dirfile(path, seq->strip->dir, seq->strip->stripdata->name);
407 BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
410 else if (seq->plugin) {
411 BLI_strncpy(seq->plugin->name, path, sizeof(seq->plugin->name));
415 static void text_getpath(struct BPathIterator *bpi, char *path)
417 Text *text= (Text *)bpi->data;
418 path[0]= '\0'; /* incase we cant get the path */
420 strcpy(path, text->name);
424 static void text_setpath(struct BPathIterator *bpi, const char *path)
426 Text *text= (Text *)bpi->data;
427 if (text==NULL) return;
430 MEM_freeN(text->name);
433 text->name= BLI_strdup(path);
436 static struct Mesh *cdata_stepdata__internal(struct Mesh *me, int step_next)
445 if (me->fdata.external) {
454 static void bpi_type_step__internal(struct BPathIterator *bpi)
456 bpi->type++; /* advance to the next type */
461 bpi->getpath_callback= seq_getpath;
462 bpi->setpath_callback= seq_setpath;
464 case BPATH_TEXT: /* path is malloc'd */
465 bpi->getpath_callback= text_getpath;
466 bpi->setpath_callback= text_setpath;
469 bpi->getpath_callback= NULL;
470 bpi->setpath_callback= NULL;
475 void BLI_bpathIterator_step(struct BPathIterator *bpi)
477 while (bpi->type != BPATH_DONE) {
479 if ((bpi->type) == BPATH_IMAGE) {
480 /*if (bpi->data) bpi->data= ((ID *)bpi->data)->next;*/
481 if (bpi->data) bpi->data= ima_stepdata__internal((Image *)bpi->data, 1, bpi->flag); /* must skip images that have no path */
482 else bpi->data= ima_stepdata__internal(bpi->bmain->image.first, 0, bpi->flag);
485 /* get the path info from this datatype */
486 Image *ima= (Image *)bpi->data;
488 bpi->_lib= ima->id.lib ? ima->id.lib->filepath : NULL;
489 bpi->_path= ima->name;
490 bpi->_name= ima->id.name+2;
491 bpi->len= sizeof(ima->name);
493 /* we are done, advancing to the next item, this type worked fine */
498 bpi_type_step__internal(bpi);
502 if ((bpi->type) == BPATH_TEXTURE) {
503 /*if (bpi->data) bpi->data= ((ID *)bpi->data)->next;*/
504 if (bpi->data) bpi->data= tex_stepdata__internal( (Tex *)bpi->data, 1, bpi->flag); /* must skip images that have no path */
505 else bpi->data= tex_stepdata__internal(bpi->bmain->tex.first, 0, bpi->flag);
508 /* get the path info from this datatype */
509 Tex *tex= (Tex *)bpi->data;
511 if(tex->type == TEX_VOXELDATA) {
512 bpi->_lib= tex->id.lib ? tex->id.lib->filepath : NULL;
513 bpi->_path= tex->vd->source_path;
514 bpi->_name= tex->id.name+2;
515 bpi->len= sizeof(tex->vd->source_path);
518 assert(!"Texture has no path, incorrect step 'tex_stepdata__internal'");
521 /* we are done, advancing to the next item, this type worked fine */
526 bpi_type_step__internal(bpi);
530 if ((bpi->type) == BPATH_TEXT) {
531 /*if (bpi->data) bpi->data= ((ID *)bpi->data)->next;*/
532 if (bpi->data) bpi->data= text_stepdata__internal((Text *)bpi->data, 1, bpi->flag); /* must skip images that have no path */
533 else bpi->data= text_stepdata__internal(bpi->bmain->text.first, 0, bpi->flag);
536 /* get the path info from this datatype */
537 Text *text= (Text *)bpi->data;
539 bpi->_lib= text->id.lib ? text->id.lib->filepath : NULL;
540 bpi->_path= NULL; /* bpi->path= text->name; */ /* get/set functions override. */
541 bpi->_name= text->id.name+2;
542 bpi->len= FILE_MAX; /* malloc'd but limit anyway since large paths may mess up other areas */
544 /* we are done, advancing to the next item, this type worked fine */
549 bpi_type_step__internal(bpi);
552 else if ((bpi->type) == BPATH_SOUND) {
553 if (bpi->data) bpi->data= snd_stepdata__internal((bSound *)bpi->data, 1, bpi->flag); /* must skip images that have no path */
554 else bpi->data= snd_stepdata__internal(bpi->bmain->sound.first, 0, bpi->flag);
557 /* get the path info from this datatype */
558 bSound *snd= (bSound *)bpi->data;
560 bpi->_lib= snd->id.lib ? snd->id.lib->filepath : NULL;
561 bpi->_path= snd->name;
562 bpi->_name= snd->id.name+2;
563 bpi->len= sizeof(snd->name);
565 /* we are done, advancing to the next item, this type worked fine */
569 bpi_type_step__internal(bpi);
572 else if ((bpi->type) == BPATH_FONT) {
574 if (bpi->data) bpi->data= vf_stepdata__internal((VFont *)bpi->data, 1, bpi->flag);
575 else bpi->data= vf_stepdata__internal(bpi->bmain->vfont.first, 0, bpi->flag);
578 /* get the path info from this datatype */
579 VFont *vf= (VFont *)bpi->data;
581 bpi->_lib= vf->id.lib ? vf->id.lib->filepath : NULL;
582 bpi->_path= vf->name;
583 bpi->_name= vf->id.name+2;
584 bpi->len= sizeof(vf->name);
586 /* we are done, advancing to the next item, this type worked fine */
590 bpi_type_step__internal(bpi);
594 else if ((bpi->type) == BPATH_LIB) {
595 if (bpi->data) bpi->data= ((ID *)bpi->data)->next;
596 else bpi->data= bpi->bmain->library.first;
599 /* get the path info from this datatype */
600 Library *lib= (Library *)bpi->data;
603 bpi->_path= lib->name;
605 bpi->len= sizeof(lib->name);
607 /* we are done, advancing to the next item, this type worked fine */
611 bpi_type_step__internal(bpi);
614 else if ((bpi->type) == BPATH_SEQ) {
615 if (bpi->data) bpi->data= seq_stepdata__internal( bpi, 1 );
616 else bpi->data= seq_stepdata__internal( bpi, 0 );
618 Sequence *seq= (Sequence *)bpi->data;
620 bpi->_name= seq->name+2;
621 bpi->len= seq->plugin ? sizeof(seq->plugin->name) : sizeof(seq->strip->dir) + sizeof(seq->strip->stripdata->name);
625 bpi_type_step__internal(bpi);
628 else if ((bpi->type) == BPATH_CDATA) {
629 if (bpi->data) bpi->data= cdata_stepdata__internal( bpi->data, 1 );
630 else bpi->data= cdata_stepdata__internal( bpi->bmain->mesh.first, 0 );
633 Mesh *me= (Mesh *)bpi->data;
634 bpi->_lib= me->id.lib ? me->id.lib->filepath : NULL;
635 bpi->_path= me->fdata.external->filename;
636 bpi->_name= me->id.name+2;
637 bpi->len= sizeof(me->fdata.external->filename);
641 bpi_type_step__internal(bpi);
647 int BLI_bpathIterator_isDone( struct BPathIterator *bpi) {
648 return bpi->type==BPATH_DONE;
651 /* include the path argument */
652 static void bpath_as_report(struct BPathIterator *bpi, const char *message, ReportList *reports)
656 char path_expanded[FILE_MAXDIR*2];
661 switch(BLI_bpathIterator_getType(bpi)) {
691 name= BLI_bpathIterator_getName(bpi);
692 BLI_bpathIterator_getPathExpanded(bpi, path_expanded);
695 if (name) BKE_reportf(reports, RPT_WARNING, "%s \"%s\", \"%s\": %s", prefix, name, path_expanded, message);
696 else BKE_reportf(reports, RPT_WARNING, "%s \"%s\": %s", prefix, path_expanded, message);
701 /* high level function */
702 void checkMissingFiles(Main *bmain, ReportList *reports) {
703 struct BPathIterator *bpi;
705 /* be sure there is low chance of the path being too short */
706 char filepath_expanded[FILE_MAXDIR*2];
708 BLI_bpathIterator_init(&bpi, bmain, bmain->name, 0);
709 while (!BLI_bpathIterator_isDone(bpi)) {
710 BLI_bpathIterator_getPathExpanded(bpi, filepath_expanded);
712 if (!BLI_exists(filepath_expanded))
713 bpath_as_report(bpi, "file not found", reports);
715 BLI_bpathIterator_step(bpi);
717 BLI_bpathIterator_free(bpi);
720 /* dont log any errors at the moment, should probably do this */
721 void makeFilesRelative(Main *bmain, const char *basedir, ReportList *reports) {
722 int tot= 0, changed= 0, failed= 0, linked= 0;
723 struct BPathIterator *bpi;
724 char filepath[FILE_MAX];
727 /* be sure there is low chance of the path being too short */
728 char filepath_relative[(FILE_MAXDIR * 2) + FILE_MAXFILE];
730 if(basedir[0] == '\0') {
731 printf("makeFilesRelative: basedir='', this is a bug\n");
735 BLI_bpathIterator_init(&bpi, bmain, basedir, 0);
736 while (!BLI_bpathIterator_isDone(bpi)) {
737 BLI_bpathIterator_getPath(bpi, filepath);
738 libpath= BLI_bpathIterator_getLib(bpi);
740 if(strncmp(filepath, "//", 2)) {
741 if (libpath) { /* cant make relative if we are library - TODO, LOG THIS */
744 else { /* local data, use the blend files path */
745 BLI_strncpy(filepath_relative, filepath, sizeof(filepath_relative));
746 /* Important BLI_cleanup_dir runs before the path is made relative
747 * because it wont work for paths that start with "//../" */
748 BLI_cleanup_file(bpi->base_path, filepath_relative); /* fix any /foo/../foo/ */
749 BLI_path_rel(filepath_relative, bpi->base_path);
750 /* be safe and check the length */
751 if (BLI_bpathIterator_getPathMaxLen(bpi) <= strlen(filepath_relative)) {
752 bpath_as_report(bpi, "couldn't make path relative (too long)", reports);
756 if(strncmp(filepath_relative, "//", 2)==0) {
757 BLI_bpathIterator_setPath(bpi, filepath_relative);
761 bpath_as_report(bpi, "couldn't make path relative", reports);
767 BLI_bpathIterator_step(bpi);
770 BLI_bpathIterator_free(bpi);
773 BKE_reportf(reports, failed ? RPT_ERROR : RPT_INFO, "Total files %i|Changed %i|Failed %i|Linked %i", tot, changed, failed, linked);
776 /* dont log any errors at the moment, should probably do this -
777 * Verry similar to makeFilesRelative - keep in sync! */
778 void makeFilesAbsolute(Main *bmain, const char *basedir, ReportList *reports)
780 int tot= 0, changed= 0, failed= 0, linked= 0;
782 struct BPathIterator *bpi;
783 char filepath[FILE_MAX];
786 /* be sure there is low chance of the path being too short */
787 char filepath_absolute[(FILE_MAXDIR * 2) + FILE_MAXFILE];
789 if(basedir[0] == '\0') {
790 printf("makeFilesAbsolute: basedir='', this is a bug\n");
794 BLI_bpathIterator_init(&bpi, bmain, basedir, 0);
795 while (!BLI_bpathIterator_isDone(bpi)) {
796 BLI_bpathIterator_getPath(bpi, filepath);
797 libpath= BLI_bpathIterator_getLib(bpi);
799 if(strncmp(filepath, "//", 2)==0) {
800 if (libpath) { /* cant make absolute if we are library - TODO, LOG THIS */
803 else { /* get the expanded path and check it is relative or too long */
804 BLI_bpathIterator_getPathExpanded(bpi, filepath_absolute);
805 BLI_cleanup_file(bpi->base_path, filepath_absolute); /* fix any /foo/../foo/ */
806 /* to be safe, check the length */
807 if (BLI_bpathIterator_getPathMaxLen(bpi) <= strlen(filepath_absolute)) {
808 bpath_as_report(bpi, "couldn't make absolute (too long)", reports);
812 if(strncmp(filepath_absolute, "//", 2)) {
813 BLI_bpathIterator_setPath(bpi, filepath_absolute);
817 bpath_as_report(bpi, "couldn't make absolute", reports);
823 BLI_bpathIterator_step(bpi);
826 BLI_bpathIterator_free(bpi);
829 BKE_reportf(reports, failed ? RPT_ERROR : RPT_INFO, "Total files %i|Changed %i|Failed %i|Linked %i", tot, changed, failed, linked);
833 /* find this file recursively, use the biggest file so thumbnails dont get used by mistake
834 - dir: subdir to search
835 - filename: set this filename
836 - filesize: filesize for the file
839 static int findFileRecursive(char *filename_new, const char *dirname, const char *filename, int *filesize, int *recur_depth)
841 /* file searching stuff */
848 dir= opendir(dirname);
854 *filesize= 0; /* dir opened fine */
856 while ((de= readdir(dir)) != NULL) {
858 if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0)
861 BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
863 if (stat(path, &status) != 0)
864 continue; /* cant stat, dont bother with this file, could print debug info here */
866 if (S_ISREG(status.st_mode)) { /* is file */
867 if (strncmp(filename, de->d_name, FILE_MAX)==0) { /* name matches */
868 /* open the file to read its size */
869 size= status.st_size;
870 if ((size > 0) && (size > *filesize)) { /* find the biggest file */
872 BLI_strncpy(filename_new, path, FILE_MAX);
876 else if (S_ISDIR(status.st_mode)) { /* is subdir */
877 if (*recur_depth <= MAX_RECUR) {
879 findFileRecursive(filename_new, path, filename, filesize, recur_depth);
888 /* high level function - call from fileselector */
889 void findMissingFiles(Main *bmain, const char *str) {
890 struct BPathIterator *bpi;
892 /* be sure there is low chance of the path being too short */
893 char filepath_expanded[FILE_MAXDIR*2];
894 char filepath[FILE_MAX];
896 int filesize, recur_depth;
898 char dirname[FILE_MAX], filename_new[FILE_MAX];
900 //XXX waitcursor( 1 );
902 BLI_split_dirfile(str, dirname, NULL);
904 BLI_bpathIterator_init(&bpi, bmain, bmain->name, 0);
906 while (!BLI_bpathIterator_isDone(bpi)) {
907 BLI_bpathIterator_getPath(bpi, filepath);
908 libpath= BLI_bpathIterator_getLib(bpi);
910 /* Check if esc was pressed because searching files can be slow */
911 /*XXX if (blender_test_break()) {
917 BLI_bpathIterator_getPathExpanded(bpi, filepath_expanded);
919 if (!BLI_exists(filepath_expanded)) {
920 /* can the dir be opened? */
924 findFileRecursive(filename_new, dirname, BLI_path_basename(filepath), &filesize, &recur_depth);
925 if (filesize == -1) { /* could not open dir */
926 printf("Could not open dir \"%s\"\n", dirname);
932 if (BLI_bpathIterator_getPathMaxLen(bpi) < strlen(filename_new)) {
933 printf("cannot set path \"%s\" too long!", filename_new);
936 /* copy the found path into the old one */
938 BLI_path_rel(filename_new, bpi->base_path);
940 BLI_bpathIterator_setPath(bpi, filename_new);
945 BLI_bpathIterator_step(bpi);
947 BLI_bpathIterator_free(bpi);
949 //XXX waitcursor( 0 );