svn merge -r41335:41371
[blender.git] / source / blender / blenlib / intern / bpath.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Campbell barton, Alex Fraser
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenlib/intern/bpath.c
29  *  \ingroup bli
30  */
31
32 /* TODO,
33  * currently there are some cases we dont support.
34  * - passing output paths to the visitor?, like render out.
35  * - passing sequence strips with many images.
36  * - passing directory paths - visitors dont know which path is a dir or a file.
37  * */
38
39 #include <sys/stat.h>
40
41 #include <string.h>
42 #include <assert.h>
43
44 /* path/file handeling stuff */
45 #ifndef WIN32
46   #include <dirent.h>
47   #include <unistd.h>
48 #else
49   #include <io.h>
50   #include "BLI_winstuff.h"
51 #endif
52
53 #include "MEM_guardedalloc.h"
54
55 #include "DNA_brush_types.h"
56 #include "DNA_image_types.h"
57 #include "DNA_mesh_types.h"
58 #include "DNA_modifier_types.h"
59 #include "DNA_object_fluidsim.h"
60 #include "DNA_object_force.h"
61 #include "DNA_object_types.h"
62 #include "DNA_particle_types.h"
63 #include "DNA_sequence_types.h"
64 #include "DNA_sound_types.h"
65 #include "DNA_text_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_vfont_types.h"
68 #include "DNA_scene_types.h"
69 #include "DNA_smoke_types.h"
70
71 #include "BLI_blenlib.h"
72 #include "BLI_bpath.h"
73 #include "BLI_utildefines.h"
74
75 #include "BKE_library.h"
76 #include "BKE_main.h"
77 #include "BKE_report.h"
78 #include "BKE_sequencer.h"
79 #include "BKE_utildefines.h"
80 #include "BKE_image.h" /* so we can check the image's type */
81
82 static int checkMissingFiles_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
83 {
84         ReportList *reports= (ReportList *)userdata;
85
86         if (!BLI_exists(path_src)) {
87                 BKE_reportf(reports, RPT_WARNING, "Path Not Found \"%s\"", path_src);
88         }
89
90         return FALSE;
91 }
92
93 /* high level function */
94 void checkMissingFiles(Main *bmain, ReportList *reports)
95 {
96         bpath_traverse_main(bmain, checkMissingFiles_visit_cb, BPATH_TRAVERSE_ABS, reports);
97 }
98
99 typedef struct BPathRemap_Data
100 {
101         const char *basedir;
102         ReportList *reports;
103
104         int count_tot;
105         int count_changed;
106         int count_failed;
107 } BPathRemap_Data;
108
109 static int makeFilesRelative_visit_cb(void *userdata, char *path_dst, const char *path_src)
110 {
111         BPathRemap_Data *data= (BPathRemap_Data *)userdata;
112
113         data->count_tot++;
114
115         if(strncmp(path_src, "//", 2)==0) {
116                 return FALSE; /* already relative */
117         }
118         else {
119                 strcpy(path_dst, path_src);
120                 BLI_path_rel(path_dst, data->basedir);
121                 if (strncmp(path_dst, "//", 2)==0) {
122                         data->count_changed++;
123                 }
124                 else {
125                         BKE_reportf(data->reports, RPT_WARNING, "Path cant be made relative \"%s\"", path_src);
126                         data->count_failed++;
127                 }
128                 return TRUE;
129         }
130 }
131
132 void makeFilesRelative(Main *bmain, const char *basedir, ReportList *reports)
133 {
134         BPathRemap_Data data= {0};
135
136         if(basedir[0] == '\0') {
137                 printf("%s: basedir='', this is a bug\n", __func__);
138                 return;
139         }
140
141         data.basedir= basedir;
142         data.reports= reports;
143
144         bpath_traverse_main(bmain, makeFilesRelative_visit_cb, 0, (void *)&data);
145
146         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
147                     "Total files %d|Changed %d|Failed %d",
148                     data.count_tot, data.count_changed, data.count_failed);
149 }
150
151 static int makeFilesAbsolute_visit_cb(void *userdata, char *path_dst, const char *path_src)
152 {
153         BPathRemap_Data *data= (BPathRemap_Data *)userdata;
154
155         data->count_tot++;
156
157         if(strncmp(path_src, "//", 2)!=0) {
158                 return FALSE; /* already absolute */
159         }
160         else {
161                 strcpy(path_dst, path_src);
162                 BLI_path_abs(path_dst, data->basedir);
163                 if (strncmp(path_dst, "//", 2)!=0) {
164                         data->count_changed++;
165                 }
166                 else {
167                         BKE_reportf(data->reports, RPT_WARNING, "Path cant be made absolute \"%s\"", path_src);
168                         data->count_failed++;
169                 }
170                 return TRUE;
171         }
172 }
173
174 /* similar to makeFilesRelative - keep in sync! */
175 void makeFilesAbsolute(Main *bmain, const char *basedir, ReportList *reports)
176 {
177         BPathRemap_Data data= {0};
178
179         if(basedir[0] == '\0') {
180                 printf("%s: basedir='', this is a bug\n", __func__);
181                 return;
182         }
183
184         data.basedir= basedir;
185         data.reports= reports;
186
187         bpath_traverse_main(bmain, makeFilesAbsolute_visit_cb, 0, (void *)&data);
188
189         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
190                     "Total files %d|Changed %d|Failed %d",
191                     data.count_tot, data.count_changed, data.count_failed);
192 }
193
194
195 /* find this file recursively, use the biggest file so thumbnails dont get used by mistake
196  - dir: subdir to search
197  - filename: set this filename
198  - filesize: filesize for the file
199 */
200 #define MAX_RECUR 16
201 static int findFileRecursive(char *filename_new, const char *dirname, const char *filename, int *filesize, int *recur_depth)
202 {
203         /* file searching stuff */
204         DIR *dir;
205         struct dirent *de;
206         struct stat status;
207         char path[FILE_MAX];
208         int size;
209
210         dir= opendir(dirname);
211
212         if (dir==NULL)
213                 return 0;
214
215         if (*filesize == -1)
216                 *filesize= 0; /* dir opened fine */
217
218         while ((de= readdir(dir)) != NULL) {
219
220                 if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0)
221                         continue;
222
223                 BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
224
225                 if (stat(path, &status) != 0)
226                         continue; /* cant stat, dont bother with this file, could print debug info here */
227
228                 if (S_ISREG(status.st_mode)) { /* is file */
229                         if (strncmp(filename, de->d_name, FILE_MAX)==0) { /* name matches */
230                                 /* open the file to read its size */
231                                 size= status.st_size;
232                                 if ((size > 0) && (size > *filesize)) { /* find the biggest file */
233                                         *filesize= size;
234                                         BLI_strncpy(filename_new, path, FILE_MAX);
235                                 }
236                         }
237                 }
238                 else if (S_ISDIR(status.st_mode)) { /* is subdir */
239                         if (*recur_depth <= MAX_RECUR) {
240                                 (*recur_depth)++;
241                                 findFileRecursive(filename_new, path, filename, filesize, recur_depth);
242                                 (*recur_depth)--;
243                         }
244                 }
245         }
246         closedir(dir);
247         return 1;
248 }
249
250 typedef struct BPathFind_Data
251 {
252         const char *basedir;
253         char searchdir[FILE_MAX];
254         ReportList *reports;
255 } BPathFind_Data;
256
257 static int findMissingFiles_visit_cb(void *userdata, char *path_dst, const char *path_src)
258 {
259         BPathFind_Data *data= (BPathFind_Data *)userdata;
260         char filename_new[FILE_MAX];
261
262         int filesize= -1;
263         int recur_depth= 0;
264
265         findFileRecursive(filename_new,
266                           data->searchdir, BLI_path_basename((char *)path_src),
267                           &filesize, &recur_depth);
268
269         if (filesize == -1) { /* could not open dir */
270                 BKE_reportf(data->reports, RPT_WARNING,
271                             "Could not find \"%s\" in \"%s\"",
272                             BLI_path_basename((char *)path_src), data->searchdir);
273                 return FALSE;
274         }
275         else {
276                 strcpy(path_dst, filename_new);
277                 return TRUE;
278         }
279 }
280
281 void findMissingFiles(Main *bmain, const char *searchpath, ReportList *reports)
282 {
283         struct BPathFind_Data data= {0};
284
285         data.reports= reports;
286         BLI_split_dir_part(searchpath, data.searchdir, sizeof(data.searchdir));
287
288         bpath_traverse_main(bmain, findMissingFiles_visit_cb, 0, (void *)&data);
289 }
290
291 /* Run a visitor on a string, replacing the contents of the string as needed. */
292 static int rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
293 {
294         char path_src_buf[FILE_MAX];
295         const char *path_src;
296         char path_dst[FILE_MAX];
297
298         if (absbase) {
299                 BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
300                 BLI_path_abs(path_src_buf, absbase);
301                 path_src= path_src_buf;
302         }
303         else {
304                 path_src= path;
305         }
306
307         if (visit_cb(userdata, path_dst, path_src)) {
308                 BLI_strncpy(path, path_dst, FILE_MAX);
309                 return TRUE;
310         }
311         else {
312                 return FALSE;
313         }
314 }
315
316 static int rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR], char path_file[FILE_MAXFILE], BPathVisitor visit_cb, const char *absbase, void *userdata)
317 {
318         char path_src[FILE_MAX];
319         char path_dst[FILE_MAX];
320
321         BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
322
323         if (absbase) {
324                 BLI_path_abs(path_src, absbase);
325         }
326
327         if (visit_cb(userdata, path_dst, (const char *)path_src)) {
328                 BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
329                 return TRUE;
330         }
331         else {
332                 return FALSE;
333         }
334 }
335
336 static int rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
337 {
338         char path_src_buf[FILE_MAX];
339         const char *path_src;
340         char path_dst[FILE_MAX];
341
342         if (absbase) {
343                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
344                 BLI_path_abs(path_src_buf, absbase);
345                 path_src= path_src_buf;
346         }
347         else {
348                 path_src= *path;
349         }
350
351         if (visit_cb(userdata, path_dst, path_src)) {
352                 MEM_freeN((*path));
353                 (*path)= BLI_strdup(path_dst);
354                 return TRUE;
355         }
356         else {
357                 return FALSE;
358         }
359 }
360
361 /* Run visitor function 'visit' on all paths contained in 'id'. */
362 void bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
363 {
364         Image *ima;
365         const char *absbase= (flag & BPATH_TRAVERSE_ABS) ? (id->lib ? id->lib->filepath : bmain->name) : NULL;
366
367         if ((flag & BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
368                 return;
369         }
370
371         switch(GS(id->name)) {
372         case ID_IM:
373                 ima= (Image *)id;
374                 if (ima->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
375                         if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
376                                 rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
377                         }
378                 }
379                 break;
380         case ID_BR:
381                 {
382                         Brush *brush= (Brush *)id;
383                         if (brush->icon_filepath[0]) {
384                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
385                         }
386                 }
387                 break;
388         case ID_OB:
389
390 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
391         {                                                                          \
392                 PointCache *cache;                                                     \
393                 for(cache= (ptcaches).first; cache; cache= cache->next) {              \
394                         if(cache->flag & PTCACHE_DISK_CACHE) {                             \
395                                 rewrite_path_fixed(cache->path,                                \
396                                                    visit_cb,                                   \
397                                                    absbase,                                    \
398                                                    bpath_user_data);                           \
399                         }                                                                  \
400                 }                                                                      \
401         }                                                                          \
402
403
404                 {
405                         Object *ob= (Object *)id;
406                         ModifierData *md;
407                         ParticleSystem *psys;
408
409
410                         /* do via modifiers instead */
411 #if 0
412                         if (ob->fluidsimSettings) {
413                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
414                         }
415 #endif
416
417                         for (md= ob->modifiers.first; md; md= md->next) {
418                                 if (md->type == eModifierType_Fluidsim) {
419                                         FluidsimModifierData *fluidmd= (FluidsimModifierData *)md;
420                                         if (fluidmd->fss) {
421                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
422                                         }
423                                 }
424                                 else if (md->type == eModifierType_Smoke) {
425                                         SmokeModifierData *smd= (SmokeModifierData *)md;
426                                         if(smd->type & MOD_SMOKE_TYPE_DOMAIN) {
427                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
428                                         }
429                                 }
430                                 else if (md->type==eModifierType_Cloth) {
431                                         ClothModifierData *clmd= (ClothModifierData*) md;
432                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
433                                 }
434                         }
435
436                         if (ob->soft) {
437                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
438                         }
439
440                         for (psys= ob->particlesystem.first; psys; psys= psys->next) {
441                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
442                         }
443                 }
444
445 #undef BPATH_TRAVERSE_POINTCACHE
446
447                 break;
448         case ID_SO:
449                 {
450                         bSound *sound= (bSound *)id;
451                         if (sound->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
452                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
453                         }
454                 }
455                 break;
456         case ID_TXT:
457                 if (((Text*)id)->name) {
458                         rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
459                 }
460                 break;
461         case ID_VF:
462                 {
463                         VFont *vf= (VFont *)id;
464                         if (vf->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
465                                 if (strcmp(vf->name, FO_BUILTIN_NAME) != 0) {
466                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
467                                 }
468                         }
469                 }
470                 break;
471         case ID_TE:
472                 {
473                         Tex *tex = (Tex *)id;
474                         if (tex->plugin) {
475                                 /* FIXME: rewrite_path assumes path length of FILE_MAX, but
476                                            tex->plugin->name is 160. ... is this field even a path? */
477                                 //rewrite_path(tex->plugin->name, visit_cb, bpath_user_data);
478                         }
479                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
480                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
481                         }
482                 }
483                 break;
484
485         case ID_SCE:
486                 {
487                         Scene *scene= (Scene *)id;
488                         if (scene->ed) {
489                                 Sequence *seq;
490
491                                 SEQ_BEGIN(scene->ed, seq) {
492                                         if (SEQ_HAS_PATH(seq)) {
493                                                 if (ELEM(seq->type, SEQ_MOVIE, SEQ_SOUND)) {
494                                                         rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name, visit_cb, absbase, bpath_user_data);
495                                                 }
496                                                 else if (seq->type == SEQ_IMAGE) {
497                                                         /* might want an option not to loop over all strips */
498                                                         StripElem *se= seq->strip->stripdata;
499                                                         int len= MEM_allocN_len(se) / sizeof(*se);
500                                                         int i;
501
502                                                         for(i= 0; i < len; i++, se++) {
503                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name, visit_cb, absbase, bpath_user_data);
504                                                         }
505                                                 }
506                                                 else {
507                                                         /* simple case */
508                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
509                                                 }
510                                         }
511                                         else if (seq->plugin) {
512                                                 rewrite_path_fixed(seq->plugin->name, visit_cb, absbase, bpath_user_data);
513                                         }
514
515                                 }
516                                 SEQ_END
517                         }
518                 }
519                 break;
520         case ID_ME:
521                 {
522                         Mesh *me= (Mesh *)id;
523                         if (me->fdata.external) {
524                                 rewrite_path_fixed(me->fdata.external->filename, visit_cb, absbase, bpath_user_data);
525                         }
526                 }
527                 break;
528         case ID_LI:
529                 {
530                         Library *lib= (Library *)id;
531                         if(rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
532                                 BKE_library_filepath_set(lib, lib->name);
533                         }
534                 }
535                 break;
536         default:
537                 /* Nothing to do for other IDs that don't contain file paths. */
538                 break;
539         }
540 }
541
542 void bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
543 {
544         ID *id;
545         for(id= lb->first; id; id= id->next) {
546                 bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
547         }
548 }
549
550 void bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
551 {
552         ListBase *lbarray[MAX_LIBARRAY];
553         int a= set_listbasepointers(bmain, lbarray);
554         while(a--) bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
555 }
556
557 /* Rewrites a relative path to be relative to the main file - unless the path is
558    absolute, in which case it is not altered. */
559 int bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
560 {
561         /* be sure there is low chance of the path being too short */
562         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
563         const char *base_new= ((char **)pathbase_v)[0];
564         const char *base_old= ((char **)pathbase_v)[1];
565
566         if (strncmp(base_old, "//", 2) == 0) {
567                 printf("%s: error, old base path '%s' is not absolute.\n",
568                        __func__, base_old);
569                 return FALSE;
570         }
571
572         /* Make referenced file absolute. This would be a side-effect of
573            BLI_cleanup_file, but we do it explicitely so we know if it changed. */
574         BLI_strncpy(filepath, path_src, FILE_MAX);
575         if (BLI_path_abs(filepath, base_old)) {
576                 /* Path was relative and is now absolute. Remap.
577                  * Important BLI_cleanup_dir runs before the path is made relative
578                  * because it wont work for paths that start with "//../" */
579                 BLI_cleanup_file(base_new, filepath);
580                 BLI_path_rel(filepath, base_new);
581                 BLI_strncpy(path_dst, filepath, FILE_MAX);
582                 return TRUE;
583         }
584         else {
585                 /* Path was not relative to begin with. */
586                 return FALSE;
587         }
588 }