Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[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 "MEM_guardedalloc.h"
30
31 #include "DNA_ID.h" /* Library */
32 #include "DNA_vfont_types.h"
33 #include "DNA_image_types.h"
34 #include "DNA_sound_types.h"
35 #include "DNA_scene_types.h" /* to get the current frame */
36 #include "DNA_sequence_types.h"
37 #include "DNA_text_types.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_bpath.h"
41
42 #include "BKE_global.h"
43 #include "BKE_image.h" /* so we can check the image's type */
44 #include "BKE_main.h" /* so we can access G.main->*.first */
45 #include "BKE_sequence.h"
46 #include "BKE_text.h" /* for writing to a textblock */
47 #include "BKE_utildefines.h"
48
49 //XXX #include "BIF_screen.h" /* only for wait cursor */
50 //
51 /* for sequence */
52 //XXX #include "BSE_sequence.h"
53 //XXX define below from BSE_sequence.h - otherwise potentially odd behaviour
54 #define SEQ_HAS_PATH(seq) (seq->type==SEQ_MOVIE || seq->type==SEQ_IMAGE)
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 #include <string.h>
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 {
206         Editing *ed;
207         Sequence *seq;
208         
209         /* Initializing */
210         if (bpi->seqdata.scene==NULL) {
211                 bpi->seqdata.scene= G.main->scene.first;
212         }
213         
214         if (step_next) {
215                 bpi->seqdata.seq++;
216         }
217         
218         while (bpi->seqdata.scene) {
219                 ed= seq_give_editing(bpi->seqdata.scene, 0);
220                 if (ed) {
221                         if (bpi->seqdata.seqar == NULL) {
222                                 /* allocate the sequencer array */
223                                 seq_array(ed, &bpi->seqdata.seqar, &bpi->seqdata.totseq, 0);
224                                 bpi->seqdata.seq = 0;
225                         }
226                         
227                         if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
228                                 seq = NULL;
229                         } else {
230                                 seq = bpi->seqdata.seqar[bpi->seqdata.seq];
231                                 while (!SEQ_HAS_PATH(seq)) {
232                                         bpi->seqdata.seq++;
233                                         if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
234                                                 seq = NULL;
235                                                 break;
236                                         }
237                                         seq = bpi->seqdata.seqar[bpi->seqdata.seq];
238                                 }
239                         }
240                         if (seq) {
241                                 return seq;
242                         } else {
243                                 /* keep looking through the next scene, reallocate seq array */
244                                 if (bpi->seqdata.seqar) {
245                                         MEM_freeN((void *)bpi->seqdata.seqar);
246                                         bpi->seqdata.seqar = NULL;
247                                 }
248                                 bpi->seqdata.scene = bpi->seqdata.scene->id.next;
249                         }
250                 } else {
251                         /* no seq data in this scene, next */
252                         bpi->seqdata.scene = bpi->seqdata.scene->id.next;
253                 }
254         }
255         
256         return NULL;
257 }
258
259 static void seq_getpath(struct BPathIterator *bpi, char *path) {
260         Sequence *seq = (Sequence *)bpi->data;
261
262         
263         path[0] = '\0'; /* incase we cant get the path */
264         if (seq==NULL) return;
265         if (SEQ_HAS_PATH(seq)) {
266                 if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
267                         BLI_strncpy(path, seq->strip->dir, FILE_MAX);
268                         BLI_add_slash(path); /* incase its missing */
269                         if (seq->strip->stripdata) { /* should always be true! */
270                                 /* Using the first image is weak for image sequences */
271                                 strcat(path, seq->strip->stripdata->name);
272                         } 
273                 } else {
274                         /* simple case */
275                         BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
276                 }
277         }
278 }
279
280 static void seq_setpath(struct BPathIterator *bpi, char *path) {
281         Sequence *seq = (Sequence *)bpi->data;
282         if (seq==NULL) return; 
283         
284         if (SEQ_HAS_PATH(seq)) {
285                 if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
286                         BLI_split_dirfile_basic(path, seq->strip->dir, seq->strip->stripdata->name);
287                 } else {
288                         /* simple case */
289                         BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
290                 }
291         }
292 }
293
294 static void bpi_type_step__internal( struct BPathIterator *bpi) {
295         bpi->type++; /* advance to the next type */
296         bpi->data = NULL;
297         
298         switch (bpi->type) {
299         case BPATH_SEQ:
300                 bpi->getpath_callback = seq_getpath;
301                 bpi->setpath_callback = seq_setpath;
302                 break;
303         default:
304                 bpi->getpath_callback = NULL;
305                 bpi->setpath_callback = NULL;
306                 break;
307         }
308 }
309
310 void BLI_bpathIterator_step( struct BPathIterator *bpi) {
311         while (bpi->type != BPATH_DONE) {
312                 
313                 if  ((bpi->type) == BPATH_IMAGE) {
314                         /*if (bpi->data)        bpi->data = ((ID *)bpi->data)->next;*/
315                         if (bpi->data)  bpi->data = ima_stepdata__internal( (Image *)bpi->data, 1 ); /* must skip images that have no path */
316                         else                    bpi->data = ima_stepdata__internal(G.main->image.first, 0);
317                         
318                         if (bpi->data) {
319                                 /* get the path info from this datatype */
320                                 Image *ima = (Image *)bpi->data;
321                                 
322                                 bpi->lib = ima->id.lib ? ima->id.lib->filename : NULL;
323                                 bpi->path = ima->name;
324                                 bpi->name = ima->id.name+2;
325                                 bpi->len = sizeof(ima->name);
326                                 
327                                 /* we are done, advancing to the next item, this type worked fine */
328                                 break;
329                                 
330                         } else {
331                                 bpi_type_step__internal(bpi);
332                         }
333                         
334                         
335                 } else if  ((bpi->type) == BPATH_SOUND) {
336                         if (bpi->data)  bpi->data = snd_stepdata__internal( (bSound *)bpi->data, 1 ); /* must skip images that have no path */
337                         else                    bpi->data = snd_stepdata__internal(G.main->sound.first, 0);
338                         
339                         if (bpi->data) {
340                                 /* get the path info from this datatype */
341                                 bSound *snd = (bSound *)bpi->data;
342                                 
343                                 bpi->lib = snd->id.lib ? snd->id.lib->filename : NULL;
344                                 bpi->path = snd->name;
345                                 bpi->name = snd->id.name+2;
346                                 bpi->len = sizeof(snd->name);
347                                 
348                                 /* we are done, advancing to the next item, this type worked fine */
349                                 break;
350                         } else {
351                                 bpi_type_step__internal(bpi);
352                         }
353                         
354                         
355                 } else if  ((bpi->type) == BPATH_FONT) {
356                         
357                         if (bpi->data)  bpi->data = vf_stepdata__internal( (VFont *)bpi->data, 1 );
358                         else                    bpi->data = vf_stepdata__internal( G.main->vfont.first, 0 );
359                         
360                         if (bpi->data) {
361                                 /* get the path info from this datatype */
362                                 VFont *vf = (VFont *)bpi->data;
363                                 
364                                 bpi->lib = vf->id.lib ? vf->id.lib->filename : NULL;
365                                 bpi->path = vf->name;
366                                 bpi->name = vf->id.name+2;
367                                 bpi->len = sizeof(vf->name);
368                                 
369                                 /* we are done, advancing to the next item, this type worked fine */
370                                 break;
371                         } else {
372                                 bpi_type_step__internal(bpi);
373                         }
374                         
375                 } else if  ((bpi->type) == BPATH_LIB) {
376                         if (bpi->data)  bpi->data = ((ID *)bpi->data)->next;
377                         else                    bpi->data = G.main->library.first;
378                         
379                         if (bpi->data) {
380                                 /* get the path info from this datatype */
381                                 Library *lib = (Library *)bpi->data;
382                                 
383                                 bpi->lib = NULL;
384                                 bpi->path = lib->name;
385                                 bpi->name = NULL;
386                                 bpi->len = sizeof(lib->name);
387                                 
388                                 /* we are done, advancing to the next item, this type worked fine */
389                                 break;
390                         } else {
391                                 bpi_type_step__internal(bpi);
392                         }
393                 } else if  ((bpi->type) == BPATH_SEQ) {
394                         if (bpi->data)  bpi->data = seq_stepdata__internal( bpi, 1 );
395                         else                    bpi->data = seq_stepdata__internal( bpi, 0 );
396                         if (bpi->data) {
397                                 Sequence *seq = (Sequence *)bpi->data;
398                                 bpi->lib = NULL;
399                                 bpi->name = seq->name+2;
400                                 bpi->len = sizeof(seq->strip->stripdata->name);
401                                 break;
402                         } else {
403                                 bpi_type_step__internal(bpi);
404                         }
405                 }
406         }
407 }
408
409 int BLI_bpathIterator_isDone( struct BPathIterator *bpi) {
410         return bpi->type==BPATH_DONE;
411 }
412
413 /* include the path argument */
414 static void bpathToText(Text *btxt, struct BPathIterator *bpi)
415 {
416         char *name;
417         char path_expanded[FILE_MAXDIR*2];
418         
419         switch(BLI_bpathIterator_getType(bpi)) {
420         case BPATH_IMAGE:
421                 txt_insert_buf( btxt, "Image \"" );
422                 break;
423         case BPATH_SOUND:
424                 txt_insert_buf( btxt, "Sound \"" );
425                 break;
426         case BPATH_FONT:
427                 txt_insert_buf( btxt, "Font \"" );
428                 break;
429         case BPATH_LIB:
430                 txt_insert_buf( btxt, "Library \"" );
431                 break;
432         case BPATH_SEQ:
433                 txt_insert_buf( btxt, "Sequence \"" );
434                 break;
435         default:
436                 txt_insert_buf( btxt, "Unknown \"" );
437                 break;
438         }
439         
440         name = BLI_bpathIterator_getName(bpi);
441         
442         if (name) {
443                 txt_insert_buf( btxt, name );
444         }
445         txt_insert_buf( btxt, "\" " );
446         
447         BLI_bpathIterator_getPathExpanded(bpi, path_expanded);
448         
449         txt_insert_buf( btxt, path_expanded );
450         txt_insert_buf( btxt, "\n" );
451         txt_move_eof( btxt, 0 );
452 }
453
454 /* high level function */
455 void checkMissingFiles( char *txtname ) {
456         Text *btxt = NULL;
457         struct BPathIterator bpi;
458         
459         /* be sure there is low chance of the path being too short */
460         char filepath_expanded[FILE_MAXDIR*2]; 
461         
462         BLI_bpathIterator_init(&bpi);
463         while (!BLI_bpathIterator_isDone(&bpi)) {
464                 BLI_bpathIterator_getPathExpanded( &bpi, filepath_expanded );
465                 
466                 if (!BLI_exists(filepath_expanded)) {
467                         if (!btxt) {
468                                 btxt = add_empty_text( "missing_files.log" );
469                                 if (txtname) {
470                                         BLI_strncpy(txtname, btxt->id.name+2, 24);
471                                 }
472                         }
473                         bpathToText(btxt, &bpi);
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         //XXX 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                 /*XXX 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         //XXX waitcursor( 0 );
712 }