MERGING TRUNK
[blender.git] / source / blender / blenlib / intern / bpath.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
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.
9  *
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.
14  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): Campbell barton
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "BLI_bpath.h"
30 #include "BKE_global.h"
31 #include "BIF_screen.h" /* only for wait cursor */
32 #include "DNA_ID.h" /* Library */
33 #include "DNA_vfont_types.h"
34 #include "DNA_image_types.h"
35 #include "DNA_sound_types.h"
36 #include "DNA_scene_types.h" /* to get the current frame */
37 #include "DNA_sequence_types.h"
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "BKE_main.h" /* so we can access G.main->*.first */
42 #include "BKE_image.h" /* so we can check the image's type */
43
44 #include "blendef.h"
45 #include "BKE_utildefines.h"
46 #include "MEM_guardedalloc.h"
47
48 /* for sequence */
49 #include "BSE_sequence.h"
50
51 /* for writing to a textblock */
52 #include "BKE_text.h" 
53 #include "BLI_blenlib.h"
54 #include "DNA_text_types.h"
55
56 /* path/file handeling stuff */
57 #ifndef WIN32
58   #include <dirent.h>
59   #include <unistd.h>
60 #else
61   #include "BLI_winstuff.h"
62   #include <io.h>
63 #endif
64
65 #include <sys/stat.h>
66 #include <sys/types.h>
67
68 #include <fcntl.h>
69 #include <stdio.h>
70 #include <string.h>
71 #include <stdlib.h>
72
73
74 #define FILE_MAX                        240
75
76 /* TODO - BPATH_PLUGIN, BPATH_SEQ */
77 enum BPathTypes {
78         BPATH_IMAGE = 0,
79         BPATH_SOUND,
80         BPATH_FONT,
81         BPATH_LIB,
82         BPATH_SEQ,
83
84         BPATH_DONE
85 };
86
87 void BLI_bpathIterator_init( struct BPathIterator *bpi ) {
88         bpi->type = BPATH_IMAGE;
89         bpi->data = NULL;
90         
91         bpi->getpath_callback = NULL;
92         bpi->setpath_callback = NULL;
93         
94         /* Sequencer spesific */
95         bpi->seqdata.totseq = 0;
96         bpi->seqdata.seq = 0;
97         bpi->seqdata.seqar = NULL;
98         bpi->seqdata.scene = NULL;
99         
100         BLI_bpathIterator_step(bpi);
101 }
102
103 void BLI_bpathIterator_free( struct BPathIterator *bpi ) {
104         if (bpi->seqdata.seqar)
105                 MEM_freeN((void *)bpi->seqdata.seqar);
106         bpi->seqdata.seqar = NULL;
107         bpi->seqdata.scene = NULL;
108 }
109
110 void BLI_bpathIterator_getPath( struct BPathIterator *bpi, char *path) {
111         if (bpi->getpath_callback) {
112                 bpi->getpath_callback( bpi, path );
113         } else {
114                 strcpy(path, bpi->path); /* warning, we assume 'path' are long enough */
115         }
116 }
117
118 void BLI_bpathIterator_setPath( struct BPathIterator *bpi, char *path) {
119         if (bpi->setpath_callback) {
120                 bpi->setpath_callback( bpi, path );
121         } else {
122                 strcpy(bpi->path, path); /* warning, we assume 'path' are long enough */
123         }
124 }
125
126 void BLI_bpathIterator_getPathExpanded( struct BPathIterator *bpi, char *path_expanded) {
127         char *libpath;
128         
129         BLI_bpathIterator_getPath(bpi, path_expanded);
130         libpath = BLI_bpathIterator_getLib(bpi);
131         
132         if (libpath) { /* check the files location relative to its library path */
133                 BLI_convertstringcode(path_expanded, libpath);
134         } else { /* local data, use the blend files path */
135                 BLI_convertstringcode(path_expanded, G.sce);
136         }
137 }
138 char* BLI_bpathIterator_getLib( struct BPathIterator *bpi) {
139         return bpi->lib;
140 }
141 char* BLI_bpathIterator_getName( struct BPathIterator *bpi) {
142         return bpi->name;
143 }
144 int     BLI_bpathIterator_getType( struct BPathIterator *bpi) {
145         return bpi->type;
146 }
147 int     BLI_bpathIterator_getPathMaxLen( struct BPathIterator *bpi) {
148         return bpi->len;
149 }
150
151 /* gets the first or the next image that has a path - not a viewer node or generated image */
152 static struct Image *ima_stepdata__internal(struct Image *ima, int step_next) {
153         if (ima==NULL)
154                 return NULL;
155         
156         if (step_next)
157                 ima = ima->id.next;
158         
159         while (ima) {
160                 if (ima->packedfile==NULL && ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
161                         break;
162                 /* image is not a image with a path, skip it */
163                 ima = ima->id.next;
164         }       
165         return ima;
166 }
167
168 static struct VFont *vf_stepdata__internal(struct VFont *vf, int step_next) {
169         if (vf==NULL)
170                 return NULL;
171         
172         if (step_next)
173                 vf = vf->id.next;
174         
175         while (vf) {
176                 if (vf->packedfile==NULL && BLI_streq(vf->name, "<builtin>")==0) {
177                         break;
178                 }
179                 
180                 /* font with no path, skip it */
181                 vf = vf->id.next;
182         }       
183         return vf;
184 }
185
186 static struct bSound *snd_stepdata__internal(struct bSound *snd, int step_next) {
187         if (snd==NULL)
188                 return NULL;
189         
190         if (step_next)
191                 snd = snd->id.next;
192         
193         while (snd) {
194                 if (snd->packedfile==NULL) {
195                         break;
196                 }
197                 
198                 /* font with no path, skip it */
199                 snd = snd->id.next;
200         }       
201         return snd;
202 }
203
204 static struct Sequence *seq_stepdata__internal(struct BPathIterator *bpi, int step_next) {
205         Sequence *seq;
206         
207         /* Initializing */
208         if (bpi->seqdata.scene==NULL) {
209                 bpi->seqdata.scene= G.main->scene.first;
210         }
211         
212         if (step_next) {
213                 bpi->seqdata.seq++;
214         }
215         
216         while (bpi->seqdata.scene) {
217                 
218                 if (bpi->seqdata.scene->ed) {
219                         if (bpi->seqdata.seqar == NULL) {
220                                 /* allocate the sequencer array */
221                                 build_seqar( &(((Editing *)bpi->seqdata.scene->ed)->seqbase), &bpi->seqdata.seqar, &bpi->seqdata.totseq);               
222                                 bpi->seqdata.seq = 0;
223                         }
224                         
225                         if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
226                                 seq = NULL;
227                         } else {
228                                 seq = bpi->seqdata.seqar[bpi->seqdata.seq];
229                                 while (!SEQ_HAS_PATH(seq)) {
230                                         bpi->seqdata.seq++;
231                                         if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
232                                                 seq = NULL;
233                                                 break;
234                                         }
235                                         seq = bpi->seqdata.seqar[bpi->seqdata.seq];
236                                 }
237                         }
238                         if (seq) {
239                                 return seq;
240                         } else {
241                                 /* keep looking through the next scene, reallocate seq array */
242                                 if (bpi->seqdata.seqar) {
243                                         MEM_freeN((void *)bpi->seqdata.seqar);
244                                         bpi->seqdata.seqar = NULL;
245                                 }
246                                 bpi->seqdata.scene = bpi->seqdata.scene->id.next;
247                         }
248                 } else {
249                         /* no seq data in this scene, next */
250                         bpi->seqdata.scene = bpi->seqdata.scene->id.next;
251                 }
252         }
253         
254         return NULL;
255 }
256
257 void seq_getpath(struct BPathIterator *bpi, char *path) {
258         Sequence *seq = (Sequence *)bpi->data;
259
260         
261         path[0] = '\0'; /* incase we cant get the path */
262         if (seq==NULL) return;
263         if (SEQ_HAS_PATH(seq)) {
264                 if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
265                         BLI_strncpy(path, seq->strip->dir, FILE_MAX);
266                         BLI_add_slash(path); /* incase its missing */
267                         if (seq->strip->stripdata) { /* should always be true! */
268                                 /* Using the first image is weak for image sequences */
269                                 strcat(path, seq->strip->stripdata->name);
270                         } 
271                 } else {
272                         /* simple case */
273                         BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
274                 }
275         }
276 }
277
278 void seq_setpath(struct BPathIterator *bpi, char *path) {
279         Sequence *seq = (Sequence *)bpi->data;
280         if (seq==NULL) return; 
281         
282         if (SEQ_HAS_PATH(seq)) {
283                 if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
284                         BLI_split_dirfile_basic(path, seq->strip->dir, seq->strip->stripdata->name);
285                 } else {
286                         /* simple case */
287                         BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
288                 }
289         }
290 }
291
292 static void bpi_type_step__internal( struct BPathIterator *bpi) {
293         bpi->type++; /* advance to the next type */
294         bpi->data = NULL;
295         
296         switch (bpi->type) {
297         case BPATH_SEQ:
298                 bpi->getpath_callback = seq_getpath;
299                 bpi->setpath_callback = seq_setpath;
300                 break;
301         default:
302                 bpi->getpath_callback = NULL;
303                 bpi->setpath_callback = NULL;
304                 break;
305         }
306 }
307
308 void BLI_bpathIterator_step( struct BPathIterator *bpi) {
309         while (bpi->type != BPATH_DONE) {
310                 
311                 if  ((bpi->type) == BPATH_IMAGE) {
312                         /*if (bpi->data)        bpi->data = ((ID *)bpi->data)->next;*/
313                         if (bpi->data)  bpi->data = ima_stepdata__internal( (Image *)bpi->data, 1 ); /* must skip images that have no path */
314                         else                    bpi->data = ima_stepdata__internal(G.main->image.first, 0);
315                         
316                         if (bpi->data) {
317                                 /* get the path info from this datatype */
318                                 Image *ima = (Image *)bpi->data;
319                                 
320                                 bpi->lib = ima->id.lib ? ima->id.lib->filename : NULL;
321                                 bpi->path = ima->name;
322                                 bpi->name = ima->id.name+2;
323                                 bpi->len = sizeof(ima->name);
324                                 
325                                 /* we are done, advancing to the next item, this type worked fine */
326                                 break;
327                                 
328                         } else {
329                                 bpi_type_step__internal(bpi);
330                         }
331                         
332                         
333                 } else if  ((bpi->type) == BPATH_SOUND) {
334                         if (bpi->data)  bpi->data = snd_stepdata__internal( (bSound *)bpi->data, 1 ); /* must skip images that have no path */
335                         else                    bpi->data = snd_stepdata__internal(G.main->sound.first, 0);
336                         
337                         if (bpi->data) {
338                                 /* get the path info from this datatype */
339                                 bSound *snd = (bSound *)bpi->data;
340                                 
341                                 bpi->lib = snd->id.lib ? snd->id.lib->filename : NULL;
342                                 bpi->path = snd->sample->name;
343                                 bpi->name = snd->id.name+2;
344                                 bpi->len = sizeof(snd->sample->name);
345                                 
346                                 /* we are done, advancing to the next item, this type worked fine */
347                                 break;
348                         } else {
349                                 bpi_type_step__internal(bpi);
350                         }
351                         
352                         
353                 } else if  ((bpi->type) == BPATH_FONT) {
354                         
355                         if (bpi->data)  bpi->data = vf_stepdata__internal( (VFont *)bpi->data, 1 );
356                         else                    bpi->data = vf_stepdata__internal( G.main->vfont.first, 0 );
357                         
358                         if (bpi->data) {
359                                 /* get the path info from this datatype */
360                                 VFont *vf = (VFont *)bpi->data;
361                                 
362                                 bpi->lib = vf->id.lib ? vf->id.lib->filename : NULL;
363                                 bpi->path = vf->name;
364                                 bpi->name = vf->id.name+2;
365                                 bpi->len = sizeof(vf->name);
366                                 
367                                 /* we are done, advancing to the next item, this type worked fine */
368                                 break;
369                         } else {
370                                 bpi_type_step__internal(bpi);
371                         }
372                         
373                 } else if  ((bpi->type) == BPATH_LIB) {
374                         if (bpi->data)  bpi->data = ((ID *)bpi->data)->next;
375                         else                    bpi->data = G.main->library.first;
376                         
377                         if (bpi->data) {
378                                 /* get the path info from this datatype */
379                                 Library *lib = (Library *)bpi->data;
380                                 
381                                 bpi->lib = NULL;
382                                 bpi->path = lib->name;
383                                 bpi->name = NULL;
384                                 bpi->len = sizeof(lib->name);
385                                 
386                                 /* we are done, advancing to the next item, this type worked fine */
387                                 break;
388                         } else {
389                                 bpi_type_step__internal(bpi);
390                         }
391                 } else if  ((bpi->type) == BPATH_SEQ) {
392                         if (bpi->data)  bpi->data = seq_stepdata__internal( bpi, 1 );
393                         else                    bpi->data = seq_stepdata__internal( bpi, 0 );
394                         if (bpi->data) {
395                                 Sequence *seq = (Sequence *)bpi->data;
396                                 bpi->lib = NULL;
397                                 bpi->name = seq->name+2;
398                                 bpi->len = sizeof(seq->strip->stripdata->name);
399                                 break;
400                         } else {
401                                 bpi_type_step__internal(bpi);
402                         }
403                 }
404         }
405 }
406
407 int BLI_bpathIterator_isDone( struct BPathIterator *bpi) {
408         return bpi->type==BPATH_DONE;
409 }
410
411 /* include the path argument */
412 static void bpathToText(Text *btxt, struct BPathIterator *bpi)
413 {
414         char *name;
415         char path_expanded[FILE_MAXDIR*2];
416         
417         switch(BLI_bpathIterator_getType(bpi)) {
418         case BPATH_IMAGE:
419                 txt_insert_buf( btxt, "Image \"" );
420                 break;
421         case BPATH_SOUND:
422                 txt_insert_buf( btxt, "Sound \"" );
423                 break;
424         case BPATH_FONT:
425                 txt_insert_buf( btxt, "Font \"" );
426                 break;
427         case BPATH_LIB:
428                 txt_insert_buf( btxt, "Library \"" );
429                 break;
430         case BPATH_SEQ:
431                 txt_insert_buf( btxt, "Sequence \"" );
432                 break;
433         default:
434                 txt_insert_buf( btxt, "Unknown \"" );
435                 break;
436         }
437         
438         name = BLI_bpathIterator_getName(bpi);
439         
440         if (name) {
441                 txt_insert_buf( btxt, name );
442         }
443         txt_insert_buf( btxt, "\" " );
444         
445         BLI_bpathIterator_getPathExpanded(bpi, path_expanded);
446         
447         txt_insert_buf( btxt, path_expanded );
448         txt_insert_buf( btxt, "\n" );
449         txt_move_eof( btxt, 0 );
450 }
451
452 /* high level function */
453 void checkMissingFiles( char *txtname ) {
454         Text *btxt = NULL;
455         struct BPathIterator bpi;
456         
457         /* be sure there is low chance of the path being too short */
458         char filepath_expanded[FILE_MAXDIR*2]; 
459         int files_missing = 0;
460         
461         BLI_bpathIterator_init(&bpi);
462         while (!BLI_bpathIterator_isDone(&bpi)) {
463                 BLI_bpathIterator_getPathExpanded( &bpi, filepath_expanded );
464                 
465                 if (!BLI_exists(filepath_expanded)) {
466                         if (!btxt) {
467                                 btxt = add_empty_text( "missing_files.log" );
468                                 if (txtname) {
469                                         BLI_strncpy(txtname, btxt->id.name+2, 24);
470                                 }
471                         }
472                         bpathToText(btxt, &bpi);
473                         files_missing = 1;
474                 }
475                 BLI_bpathIterator_step(&bpi);
476         }
477         BLI_bpathIterator_free(&bpi);
478 }
479
480 /* dont log any errors at the moment, should probably do this */
481 void makeFilesRelative(char *txtname, int *tot, int *changed, int *failed, int *linked) {
482         struct BPathIterator bpi;
483         char filepath[FILE_MAX], *libpath;
484         
485         /* be sure there is low chance of the path being too short */
486         char filepath_relative[(FILE_MAXDIR * 2) + FILE_MAXFILE];
487         
488         Text *btxt = NULL;
489         
490         *tot = *changed = *failed = *linked = 0;
491         
492         BLI_bpathIterator_init(&bpi);
493         while (!BLI_bpathIterator_isDone(&bpi)) {
494                 BLI_bpathIterator_getPath(&bpi, filepath);
495                 libpath = BLI_bpathIterator_getLib(&bpi);
496                 
497                 if(strncmp(filepath, "//", 2)) {
498                         if (libpath) { /* cant make relative if we are library - TODO, LOG THIS */
499                                 (*linked)++;
500                         } else { /* local data, use the blend files path */
501                                 BLI_strncpy(filepath_relative, filepath, sizeof(filepath_relative));
502                                 /* Important BLI_cleanup_dir runs before the path is made relative
503                                  * because it wont work for paths that start with "//../" */ 
504                                 BLI_cleanup_file(G.sce, filepath_relative); /* fix any /foo/../foo/ */
505                                 BLI_makestringcode(G.sce, filepath_relative);
506                                 /* be safe and check the length */
507                                 if (BLI_bpathIterator_getPathMaxLen(&bpi) <= strlen(filepath_relative)) {
508                                         if (!btxt) {
509                                                 btxt = add_empty_text( "missing_no_rel.log" );
510                                                 if (txtname) {
511                                                         BLI_strncpy(txtname, btxt->id.name+2, 24);
512                                                 }
513                                         }
514                                         bpathToText(btxt, &bpi);
515                                         (*failed)++;
516                                 } else {
517                                         if(strncmp(filepath_relative, "//", 2)==0) {
518                                                 BLI_bpathIterator_setPath(&bpi, filepath_relative);
519                                                 (*changed)++;
520                                         } else {
521                                                 if (!btxt) {
522                                                         btxt = add_empty_text( "missing_no_rel.log" );
523                                                         if (txtname) {
524                                                                 BLI_strncpy(txtname, btxt->id.name+2, 24);
525                                                         }
526                                                 }
527                                                 bpathToText(btxt, &bpi);
528                                                 (*failed)++;
529                                         }
530                                 }
531                         }
532                 }
533                 BLI_bpathIterator_step(&bpi);
534                 (*tot)++;
535         }
536         BLI_bpathIterator_free(&bpi);
537 }
538
539 /* dont log any errors at the moment, should probably do this -
540  * Verry similar to makeFilesRelative - keep in sync! */
541 void makeFilesAbsolute(char *txtname, int *tot, int *changed, int *failed, int *linked) {
542         struct BPathIterator bpi;
543         char filepath[FILE_MAX], *libpath;
544         
545         /* be sure there is low chance of the path being too short */
546         char filepath_absolute[(FILE_MAXDIR * 2) + FILE_MAXFILE];
547         
548         Text *btxt = NULL;
549         
550         *tot = *changed = *failed = *linked = 0;
551         
552         BLI_bpathIterator_init(&bpi);
553         while (!BLI_bpathIterator_isDone(&bpi)) {
554                 BLI_bpathIterator_getPath(&bpi, filepath);
555                 libpath = BLI_bpathIterator_getLib(&bpi);
556                 
557                 if(strncmp(filepath, "//", 2)==0) {
558                         if (libpath) { /* cant make absolute if we are library - TODO, LOG THIS */
559                                 (*linked)++;
560                         } else { /* get the expanded path and check it is relative or too long */
561                                 BLI_bpathIterator_getPathExpanded( &bpi, filepath_absolute );
562                                 BLI_cleanup_file(G.sce, filepath_absolute); /* fix any /foo/../foo/ */
563                                 /* to be safe, check the length */
564                                 if (BLI_bpathIterator_getPathMaxLen(&bpi) <= strlen(filepath_absolute)) {
565                                         if (!btxt) {
566                                                 btxt = add_empty_text( "missing_no_abs.log" );
567                                                 if (txtname) {
568                                                         BLI_strncpy(txtname, btxt->id.name+2, 24);
569                                                 }
570                                         }
571                                         bpathToText(btxt, &bpi);
572                                         (*failed)++;
573                                 } else {
574                                         if(strncmp(filepath_absolute, "//", 2)) {
575                                                 BLI_bpathIterator_setPath(&bpi, filepath_absolute);
576                                                 (*changed)++;
577                                         } else {
578                                                 if (!btxt) {
579                                                         btxt = add_empty_text( "missing_no_abs.log" );
580                                                         if (txtname) {
581                                                                 BLI_strncpy(txtname, btxt->id.name+2, 24);
582                                                         }
583                                                 }
584                                                 bpathToText(btxt, &bpi);
585                                                 (*failed)++;
586                                         }
587                                 }
588                         }
589                 }
590                 BLI_bpathIterator_step(&bpi);
591                 (*tot)++;
592         }
593         BLI_bpathIterator_free(&bpi);
594 }
595
596
597 /* find this file recursively, use the biggest file so thumbnails dont get used by mistake
598  - dir: subdir to search
599  - filename: set this filename
600  - filesize: filesize for the file
601 */
602 #define MAX_RECUR 16
603 static int findFileRecursive(char *filename_new, const char *dirname, const char *filename, int *filesize, int *recur_depth)
604 {
605         /* file searching stuff */
606         DIR *dir;
607         struct dirent *de;
608         struct stat status;
609         char path[FILE_MAX];
610         int size;
611         
612         dir = opendir(dirname);
613         
614         if (dir==0)
615                 return 0;
616         
617         if (*filesize == -1)
618                 *filesize = 0; /* dir opened fine */
619         
620         while ((de = readdir(dir)) != NULL) {
621                 
622                 if (strncmp(".", de->d_name, 2)==0 || strncmp("..", de->d_name, 3)==0)
623                         continue;
624                 
625                 BLI_join_dirfile(path, dirname, de->d_name);
626                 
627                 if (stat(path, &status) != 0)
628                         continue; /* cant stat, dont bother with this file, could print debug info here */
629                 
630                 if (S_ISREG(status.st_mode)) { /* is file */
631                         if (strncmp(filename, de->d_name, FILE_MAX)==0) { /* name matches */
632                                 /* open the file to read its size */
633                                 size = BLI_filepathsize(path);
634                                 if ((size > 0) && (size > *filesize)) { /* find the biggest file */
635                                         *filesize = size;
636                                         BLI_strncpy(filename_new, path, FILE_MAX);
637                                 }
638                         }
639                 } else if (S_ISDIR(status.st_mode)) { /* is subdir */
640                         if (*recur_depth <= MAX_RECUR) {
641                                 (*recur_depth)++;
642                                 findFileRecursive(filename_new, path, filename, filesize, recur_depth);
643                                 (*recur_depth)--;
644                         }
645                 }
646         }
647         closedir(dir);
648         return 1;
649 }
650
651 /* high level function - call from fileselector */
652 void findMissingFiles(char *str) {
653         struct BPathIterator bpi;
654         
655         /* be sure there is low chance of the path being too short */
656         char filepath_expanded[FILE_MAXDIR*2]; 
657         char filepath[FILE_MAX], *libpath;
658         int filesize, recur_depth;
659         
660         char dirname[FILE_MAX], filename[FILE_MAX], filename_new[FILE_MAX];
661         
662         waitcursor( 1 );
663         
664         BLI_split_dirfile_basic(str, dirname, NULL);
665         
666         BLI_bpathIterator_init(&bpi);
667         
668         while (!BLI_bpathIterator_isDone(&bpi)) {
669                 BLI_bpathIterator_getPath(&bpi, filepath);
670                 libpath = BLI_bpathIterator_getLib(&bpi);
671                 
672                 /* Check if esc was pressed because searching files can be slow */
673                 if (blender_test_break()) {
674                         break;
675                 }
676                 
677                 if (libpath==NULL) {
678                         
679                         BLI_bpathIterator_getPathExpanded( &bpi, filepath_expanded );
680                         
681                         if (!BLI_exists(filepath_expanded)) {
682                                 /* can the dir be opened? */
683                                 filesize = -1;
684                                 recur_depth = 0;
685                                 BLI_split_dirfile_basic(filepath, NULL, filename); /* the file to find */
686                                 
687                                 findFileRecursive(filename_new, dirname, filename, &filesize, &recur_depth);
688                                 if (filesize == -1) { /* could not open dir */
689                                         printf("Could not open dir \"%s\"\n", dirname);
690                                         return;
691                                 }
692                                 
693                                 if (filesize > 0) {
694                                         
695                                         if (BLI_bpathIterator_getPathMaxLen( &bpi ) < strlen(filename_new)) { 
696                                                 printf("cannot set path \"%s\" too long!", filename_new);
697                                         } else {
698                                                 /* copy the found path into the old one */
699                                                 if (G.relbase_valid)
700                                                         BLI_makestringcode(G.sce, filename_new);
701                                                 
702                                                 BLI_bpathIterator_setPath( &bpi, filename_new );
703                                         }
704                                 }
705                         }
706                 }
707                 BLI_bpathIterator_step(&bpi);
708         }
709         BLI_bpathIterator_free(&bpi);
710         
711         waitcursor( 0 );
712 }