Merge with trunk r41342
[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,
329                                   sizeof(path_dir), sizeof(path_file));
330                 return TRUE;
331         }
332         else {
333                 return FALSE;
334         }
335 }
336
337 static int rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
338 {
339         char path_src_buf[FILE_MAX];
340         const char *path_src;
341         char path_dst[FILE_MAX];
342
343         if (absbase) {
344                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
345                 BLI_path_abs(path_src_buf, absbase);
346                 path_src= path_src_buf;
347         }
348         else {
349                 path_src= *path;
350         }
351
352         if (visit_cb(userdata, path_dst, path_src)) {
353                 MEM_freeN((*path));
354                 (*path)= BLI_strdup(path_dst);
355                 return TRUE;
356         }
357         else {
358                 return FALSE;
359         }
360 }
361
362 /* Run visitor function 'visit' on all paths contained in 'id'. */
363 void bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
364 {
365         Image *ima;
366         const char *absbase= (flag & BPATH_TRAVERSE_ABS) ? (id->lib ? id->lib->filepath : bmain->name) : NULL;
367
368         if ((flag & BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
369                 return;
370         }
371
372         switch(GS(id->name)) {
373         case ID_IM:
374                 ima= (Image *)id;
375                 if (ima->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
376                         if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
377                                 rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
378                         }
379                 }
380                 break;
381         case ID_BR:
382                 {
383                         Brush *brush= (Brush *)id;
384                         if (brush->icon_filepath[0]) {
385                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
386                         }
387                 }
388                 break;
389         case ID_OB:
390
391 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
392         {                                                                          \
393                 PointCache *cache;                                                     \
394                 for(cache= (ptcaches).first; cache; cache= cache->next) {              \
395                         if(cache->flag & PTCACHE_DISK_CACHE) {                             \
396                                 rewrite_path_fixed(cache->path,                                \
397                                                    visit_cb,                                   \
398                                                    absbase,                                    \
399                                                    bpath_user_data);                           \
400                         }                                                                  \
401                 }                                                                      \
402         }                                                                          \
403
404
405                 {
406                         Object *ob= (Object *)id;
407                         ModifierData *md;
408                         ParticleSystem *psys;
409
410
411                         /* do via modifiers instead */
412 #if 0
413                         if (ob->fluidsimSettings) {
414                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
415                         }
416 #endif
417
418                         for (md= ob->modifiers.first; md; md= md->next) {
419                                 if (md->type == eModifierType_Fluidsim) {
420                                         FluidsimModifierData *fluidmd= (FluidsimModifierData *)md;
421                                         if (fluidmd->fss) {
422                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
423                                         }
424                                 }
425                                 else if (md->type == eModifierType_Smoke) {
426                                         SmokeModifierData *smd= (SmokeModifierData *)md;
427                                         if(smd->type & MOD_SMOKE_TYPE_DOMAIN) {
428                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
429                                         }
430                                 }
431                                 else if (md->type==eModifierType_Cloth) {
432                                         ClothModifierData *clmd= (ClothModifierData*) md;
433                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
434                                 }
435                         }
436
437                         if (ob->soft) {
438                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
439                         }
440
441                         for (psys= ob->particlesystem.first; psys; psys= psys->next) {
442                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
443                         }
444                 }
445
446 #undef BPATH_TRAVERSE_POINTCACHE
447
448                 break;
449         case ID_SO:
450                 {
451                         bSound *sound= (bSound *)id;
452                         if (sound->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
453                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
454                         }
455                 }
456                 break;
457         case ID_TXT:
458                 if (((Text*)id)->name) {
459                         rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
460                 }
461                 break;
462         case ID_VF:
463                 {
464                         VFont *vf= (VFont *)id;
465                         if (vf->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
466                                 if (strcmp(vf->name, FO_BUILTIN_NAME) != 0) {
467                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
468                                 }
469                         }
470                 }
471                 break;
472         case ID_TE:
473                 {
474                         Tex *tex = (Tex *)id;
475                         if (tex->plugin) {
476                                 /* FIXME: rewrite_path assumes path length of FILE_MAX, but
477                                            tex->plugin->name is 160. ... is this field even a path? */
478                                 //rewrite_path(tex->plugin->name, visit_cb, bpath_user_data);
479                         }
480                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
481                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
482                         }
483                 }
484                 break;
485
486         case ID_SCE:
487                 {
488                         Scene *scene= (Scene *)id;
489                         if (scene->ed) {
490                                 Sequence *seq;
491
492                                 SEQ_BEGIN(scene->ed, seq) {
493                                         if (SEQ_HAS_PATH(seq)) {
494                                                 if (ELEM(seq->type, SEQ_MOVIE, SEQ_SOUND)) {
495                                                         rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name, visit_cb, absbase, bpath_user_data);
496                                                 }
497                                                 else if (seq->type == SEQ_IMAGE) {
498                                                         /* might want an option not to loop over all strips */
499                                                         StripElem *se= seq->strip->stripdata;
500                                                         int len= MEM_allocN_len(se) / sizeof(*se);
501                                                         int i;
502
503                                                         for(i= 0; i < len; i++, se++) {
504                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name, visit_cb, absbase, bpath_user_data);
505                                                         }
506                                                 }
507                                                 else {
508                                                         /* simple case */
509                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
510                                                 }
511                                         }
512                                         else if (seq->plugin) {
513                                                 rewrite_path_fixed(seq->plugin->name, visit_cb, absbase, bpath_user_data);
514                                         }
515
516                                 }
517                                 SEQ_END
518                         }
519                 }
520                 break;
521         case ID_ME:
522                 {
523                         Mesh *me= (Mesh *)id;
524                         if (me->fdata.external) {
525                                 rewrite_path_fixed(me->fdata.external->filename, visit_cb, absbase, bpath_user_data);
526                         }
527                 }
528                 break;
529         case ID_LI:
530                 {
531                         Library *lib= (Library *)id;
532                         if(rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
533                                 BKE_library_filepath_set(lib, lib->name);
534                         }
535                 }
536                 break;
537         default:
538                 /* Nothing to do for other IDs that don't contain file paths. */
539                 break;
540         }
541 }
542
543 void bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
544 {
545         ID *id;
546         for(id= lb->first; id; id= id->next) {
547                 bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
548         }
549 }
550
551 void bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
552 {
553         ListBase *lbarray[MAX_LIBARRAY];
554         int a= set_listbasepointers(bmain, lbarray);
555         while(a--) bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
556 }
557
558 /* Rewrites a relative path to be relative to the main file - unless the path is
559    absolute, in which case it is not altered. */
560 int bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
561 {
562         /* be sure there is low chance of the path being too short */
563         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
564         const char *base_new= ((char **)pathbase_v)[0];
565         const char *base_old= ((char **)pathbase_v)[1];
566
567         if (strncmp(base_old, "//", 2) == 0) {
568                 printf("%s: error, old base path '%s' is not absolute.\n",
569                        __func__, base_old);
570                 return FALSE;
571         }
572
573         /* Make referenced file absolute. This would be a side-effect of
574            BLI_cleanup_file, but we do it explicitely so we know if it changed. */
575         BLI_strncpy(filepath, path_src, FILE_MAX);
576         if (BLI_path_abs(filepath, base_old)) {
577                 /* Path was relative and is now absolute. Remap.
578                  * Important BLI_cleanup_dir runs before the path is made relative
579                  * because it wont work for paths that start with "//../" */
580                 BLI_cleanup_file(base_new, filepath);
581                 BLI_path_rel(filepath, base_new);
582                 BLI_strncpy(path_dst, filepath, FILE_MAX);
583                 return TRUE;
584         }
585         else {
586                 /* Path was not relative to begin with. */
587                 return FALSE;
588         }
589 }