Merged changes in the trunk up to revision 45308.
[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 don't support.
34  * - passing output paths to the visitor?, like render out.
35  * - passing sequence strips with many images.
36  * - passing directory paths - visitors don't 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 handling 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_movieclip_types.h"
60 #include "DNA_object_fluidsim.h"
61 #include "DNA_object_force.h"
62 #include "DNA_object_types.h"
63 #include "DNA_particle_types.h"
64 #include "DNA_sequence_types.h"
65 #include "DNA_sound_types.h"
66 #include "DNA_text_types.h"
67 #include "DNA_texture_types.h"
68 #include "DNA_vfont_types.h"
69 #include "DNA_scene_types.h"
70 #include "DNA_smoke_types.h"
71 #include "DNA_freestyle_types.h"
72
73 #include "BLI_blenlib.h"
74 #include "BLI_bpath.h"
75 #include "BLI_utildefines.h"
76
77 #include "BKE_library.h"
78 #include "BKE_main.h"
79 #include "BKE_report.h"
80 #include "BKE_sequencer.h"
81 #include "BKE_utildefines.h"
82 #include "BKE_image.h" /* so we can check the image's type */
83
84 static int checkMissingFiles_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
85 {
86         ReportList *reports = (ReportList *)userdata;
87
88         if (!BLI_exists(path_src)) {
89                 BKE_reportf(reports, RPT_WARNING, "Path Not Found \"%s\"", path_src);
90         }
91
92         return FALSE;
93 }
94
95 /* high level function */
96 void checkMissingFiles(Main *bmain, ReportList *reports)
97 {
98         bpath_traverse_main(bmain, checkMissingFiles_visit_cb, BPATH_TRAVERSE_ABS, reports);
99 }
100
101 typedef struct BPathRemap_Data {
102         const char *basedir;
103         ReportList *reports;
104
105         int count_tot;
106         int count_changed;
107         int count_failed;
108 } BPathRemap_Data;
109
110 static int makeFilesRelative_visit_cb(void *userdata, char *path_dst, const char *path_src)
111 {
112         BPathRemap_Data *data = (BPathRemap_Data *)userdata;
113
114         data->count_tot++;
115
116         if (strncmp(path_src, "//", 2) == 0) {
117                 return FALSE; /* already relative */
118         }
119         else {
120                 strcpy(path_dst, path_src);
121                 BLI_path_rel(path_dst, data->basedir);
122                 if (strncmp(path_dst, "//", 2) == 0) {
123                         data->count_changed++;
124                 }
125                 else {
126                         BKE_reportf(data->reports, RPT_WARNING, "Path cant be made relative \"%s\"", path_src);
127                         data->count_failed++;
128                 }
129                 return TRUE;
130         }
131 }
132
133 void makeFilesRelative(Main *bmain, const char *basedir, ReportList *reports)
134 {
135         BPathRemap_Data data = {NULL};
136
137         if (basedir[0] == '\0') {
138                 printf("%s: basedir='', this is a bug\n", __func__);
139                 return;
140         }
141
142         data.basedir = basedir;
143         data.reports = reports;
144
145         bpath_traverse_main(bmain, makeFilesRelative_visit_cb, 0, (void *)&data);
146
147         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
148                     "Total files %d|Changed %d|Failed %d",
149                     data.count_tot, data.count_changed, data.count_failed);
150 }
151
152 static int makeFilesAbsolute_visit_cb(void *userdata, char *path_dst, const char *path_src)
153 {
154         BPathRemap_Data *data = (BPathRemap_Data *)userdata;
155
156         data->count_tot++;
157
158         if (strncmp(path_src, "//", 2) != 0) {
159                 return FALSE; /* already absolute */
160         }
161         else {
162                 strcpy(path_dst, path_src);
163                 BLI_path_abs(path_dst, data->basedir);
164                 if (strncmp(path_dst, "//", 2) != 0) {
165                         data->count_changed++;
166                 }
167                 else {
168                         BKE_reportf(data->reports, RPT_WARNING, "Path cant be made absolute \"%s\"", path_src);
169                         data->count_failed++;
170                 }
171                 return TRUE;
172         }
173 }
174
175 /* similar to makeFilesRelative - keep in sync! */
176 void makeFilesAbsolute(Main *bmain, const char *basedir, ReportList *reports)
177 {
178         BPathRemap_Data data = {NULL};
179
180         if (basedir[0] == '\0') {
181                 printf("%s: basedir='', this is a bug\n", __func__);
182                 return;
183         }
184
185         data.basedir = basedir;
186         data.reports = reports;
187
188         bpath_traverse_main(bmain, makeFilesAbsolute_visit_cb, 0, (void *)&data);
189
190         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
191                     "Total files %d|Changed %d|Failed %d",
192                     data.count_tot, data.count_changed, data.count_failed);
193 }
194
195 /* find this file recursively, use the biggest file so thumbnails don't get used by mistake
196  * - dir: subdir to search
197  * - filename: set this filename
198  * - filesize: filesize for the file
199  *
200  * return found: 1/0.
201  */
202 #define MAX_RECUR 16
203 static int findFileRecursive(char *filename_new,
204                              const char *dirname,
205                              const char *filename,
206                              int *filesize,
207                              int *recur_depth)
208 {
209         /* file searching stuff */
210         DIR *dir;
211         struct dirent *de;
212         struct stat status;
213         char path[FILE_MAX];
214         int size;
215         int found = FALSE;
216
217         filename_new[0] = '\0';
218
219         dir = opendir(dirname);
220
221         if (dir == NULL)
222                 return found;
223
224         if (*filesize == -1)
225                 *filesize = 0; /* dir opened fine */
226
227         while ((de = readdir(dir)) != NULL) {
228
229                 if (strcmp(".", de->d_name) == 0 || strcmp("..", de->d_name) == 0)
230                         continue;
231
232                 BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
233
234                 if (stat(path, &status) != 0)
235                         continue; /* cant stat, don't bother with this file, could print debug info here */
236
237                 if (S_ISREG(status.st_mode)) { /* is file */
238                         if (strncmp(filename, de->d_name, FILE_MAX) == 0) { /* name matches */
239                                 /* open the file to read its size */
240                                 size = status.st_size;
241                                 if ((size > 0) && (size > *filesize)) { /* find the biggest file */
242                                         *filesize = size;
243                                         BLI_strncpy(filename_new, path, FILE_MAX);
244                                         found = TRUE;
245                                 }
246                         }
247                 }
248                 else if (S_ISDIR(status.st_mode)) { /* is subdir */
249                         if (*recur_depth <= MAX_RECUR) {
250                                 (*recur_depth)++;
251                                 found |= findFileRecursive(filename_new, path, filename, filesize, recur_depth);
252                                 (*recur_depth)--;
253                         }
254                 }
255         }
256         closedir(dir);
257         return found;
258 }
259
260 typedef struct BPathFind_Data {
261         const char *basedir;
262         char searchdir[FILE_MAX];
263         ReportList *reports;
264 } BPathFind_Data;
265
266 static int findMissingFiles_visit_cb(void *userdata, char *path_dst, const char *path_src)
267 {
268         BPathFind_Data *data = (BPathFind_Data *)userdata;
269         char filename_new[FILE_MAX];
270
271         int filesize = -1;
272         int recur_depth = 0;
273         int found;
274
275         found = findFileRecursive(filename_new,
276                                   data->searchdir, BLI_path_basename((char *)path_src),
277                                   &filesize, &recur_depth);
278
279         if (filesize == -1) { /* could not open dir */
280                 BKE_reportf(data->reports, RPT_WARNING,
281                             "Could open directory \"%s\"",
282                             BLI_path_basename(data->searchdir));
283                 return FALSE;
284         }
285         else if (found == FALSE) {
286                 BKE_reportf(data->reports, RPT_WARNING,
287                             "Could not find \"%s\" in \"%s\"",
288                             BLI_path_basename((char *)path_src), data->searchdir);
289                 return FALSE;
290         }
291         else {
292                 BLI_strncpy(path_dst, filename_new, FILE_MAX);
293                 return TRUE;
294         }
295 }
296
297 void findMissingFiles(Main *bmain, const char *searchpath, ReportList *reports)
298 {
299         struct BPathFind_Data data = {NULL};
300
301         data.reports = reports;
302         BLI_split_dir_part(searchpath, data.searchdir, sizeof(data.searchdir));
303
304         bpath_traverse_main(bmain, findMissingFiles_visit_cb, 0, (void *)&data);
305 }
306
307 /* Run a visitor on a string, replacing the contents of the string as needed. */
308 static int rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
309 {
310         char path_src_buf[FILE_MAX];
311         const char *path_src;
312         char path_dst[FILE_MAX];
313
314         if (absbase) {
315                 BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
316                 BLI_path_abs(path_src_buf, absbase);
317                 path_src = path_src_buf;
318         }
319         else {
320                 path_src = path;
321         }
322
323         if (visit_cb(userdata, path_dst, path_src)) {
324                 BLI_strncpy(path, path_dst, FILE_MAX);
325                 return TRUE;
326         }
327         else {
328                 return FALSE;
329         }
330 }
331
332 static int rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR],
333                                       char path_file[FILE_MAXFILE],
334                                       BPathVisitor visit_cb,
335                                       const char *absbase,
336                                       void *userdata)
337 {
338         char path_src[FILE_MAX];
339         char path_dst[FILE_MAX];
340
341         BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
342
343         if (absbase) {
344                 BLI_path_abs(path_src, absbase);
345         }
346
347         if (visit_cb(userdata, path_dst, (const char *)path_src)) {
348                 BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
349                 return TRUE;
350         }
351         else {
352                 return FALSE;
353         }
354 }
355
356 static int rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
357 {
358         char path_src_buf[FILE_MAX];
359         const char *path_src;
360         char path_dst[FILE_MAX];
361
362         if (absbase) {
363                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
364                 BLI_path_abs(path_src_buf, absbase);
365                 path_src = path_src_buf;
366         }
367         else {
368                 path_src = *path;
369         }
370
371         if (visit_cb(userdata, path_dst, path_src)) {
372                 MEM_freeN((*path));
373                 (*path) = BLI_strdup(path_dst);
374                 return TRUE;
375         }
376         else {
377                 return FALSE;
378         }
379 }
380
381 /* Run visitor function 'visit' on all paths contained in 'id'. */
382 void bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
383 {
384         Image *ima;
385         const char *absbase = (flag & BPATH_TRAVERSE_ABS) ? ID_BLEND_PATH(bmain, id) : NULL;
386
387         if ((flag & BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
388                 return;
389         }
390
391         switch(GS(id->name)) {
392         case ID_IM:
393                 ima= (Image *)id;
394                 if (ima->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
395                         if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
396                                 rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
397                         }
398                 }
399                 break;
400         case ID_BR:
401                 {
402                         Brush *brush= (Brush *)id;
403                         if (brush->icon_filepath[0]) {
404                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
405                         }
406                 }
407                 break;
408         case ID_OB:
409
410 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
411         {                                                                          \
412                 PointCache *cache;                                                     \
413                 for (cache= (ptcaches).first; cache; cache= cache->next) {              \
414                         if (cache->flag & PTCACHE_DISK_CACHE) {                             \
415                                 rewrite_path_fixed(cache->path,                                \
416                                                    visit_cb,                                   \
417                                                    absbase,                                    \
418                                                    bpath_user_data);                           \
419                         }                                                                  \
420                 }                                                                      \
421         }                                                                          \
422
423
424                 {
425                         Object *ob= (Object *)id;
426                         ModifierData *md;
427                         ParticleSystem *psys;
428
429                         /* do via modifiers instead */
430 #if 0
431                         if (ob->fluidsimSettings) {
432                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
433                         }
434 #endif
435
436                         for (md= ob->modifiers.first; md; md= md->next) {
437                                 if (md->type == eModifierType_Fluidsim) {
438                                         FluidsimModifierData *fluidmd= (FluidsimModifierData *)md;
439                                         if (fluidmd->fss) {
440                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
441                                         }
442                                 }
443                                 else if (md->type == eModifierType_Smoke) {
444                                         SmokeModifierData *smd= (SmokeModifierData *)md;
445                                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
446                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
447                                         }
448                                 }
449                                 else if (md->type==eModifierType_Cloth) {
450                                         ClothModifierData *clmd= (ClothModifierData*) md;
451                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
452                                 }
453                                 else if (md->type==eModifierType_Ocean) {
454                                         OceanModifierData *omd= (OceanModifierData*) md;
455                                         rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data);
456                                 }
457                         }
458
459                         if (ob->soft) {
460                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
461                         }
462
463                         for (psys= ob->particlesystem.first; psys; psys= psys->next) {
464                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
465                         }
466                 }
467
468 #undef BPATH_TRAVERSE_POINTCACHE
469
470                 break;
471         case ID_SO:
472                 {
473                         bSound *sound= (bSound *)id;
474                         if (sound->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
475                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
476                         }
477                 }
478                 break;
479         case ID_TXT:
480                 if (((Text*)id)->name) {
481                         rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
482                 }
483                 break;
484         case ID_VF:
485                 {
486                         VFont *vf= (VFont *)id;
487                         if (vf->packedfile == NULL || (flag & BPATH_TRAVERSE_SKIP_PACKED) == 0) {
488                                 if (strcmp(vf->name, FO_BUILTIN_NAME) != 0) {
489                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
490                                 }
491                         }
492                 }
493                 break;
494         case ID_TE:
495                 {
496                         Tex *tex = (Tex *)id;
497                         if (tex->plugin) {
498                                 /* FIXME: rewrite_path assumes path length of FILE_MAX, but
499                                  * tex->plugin->name is 160. ... is this field even a path? */
500                                 //rewrite_path(tex->plugin->name, visit_cb, bpath_user_data);
501                         }
502                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
503                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
504                         }
505                 }
506                 break;
507
508         case ID_SCE:
509                 {
510                         Scene *scene= (Scene *)id;
511                         SceneRenderLayer *srl= scene->r.layers.first;
512                         if (scene->ed) {
513                                 Sequence *seq;
514
515                                 SEQ_BEGIN(scene->ed, seq) {
516                                         if (SEQ_HAS_PATH(seq)) {
517                                                 if (ELEM(seq->type, SEQ_MOVIE, SEQ_SOUND)) {
518                                                         rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name,
519                                                                                    visit_cb, absbase, bpath_user_data);
520                                                 }
521                                                 else if (seq->type == SEQ_IMAGE) {
522                                                         /* might want an option not to loop over all strips */
523                                                         StripElem *se= seq->strip->stripdata;
524                                                         int len= MEM_allocN_len(se) / sizeof(*se);
525                                                         int i;
526
527                                                         if (flag & BPATH_TRAVERSE_SKIP_MULTIFILE) {
528                                                                 /* only operate on one path */
529                                                                 len= MIN2(1, len);
530                                                         }
531
532                                                         for (i= 0; i < len; i++, se++) {
533                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
534                                                                                            visit_cb, absbase, bpath_user_data);
535                                                         }
536                                                 }
537                                                 else {
538                                                         /* simple case */
539                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
540                                                 }
541                                         }
542                                         else if (seq->plugin) {
543                                                 rewrite_path_fixed(seq->plugin->name, visit_cb, absbase, bpath_user_data);
544                                         }
545
546                                 }
547                                 SEQ_END
548                         }
549                         for(; srl; srl= srl->next) {
550                                 FreestyleModuleConfig* module= srl->freestyleConfig.modules.first;
551                                 for (; module; module= module->next) {
552                                         rewrite_path_fixed(module->module_path, visit_cb, absbase, bpath_user_data);
553                                 }
554                         }
555                 }
556                 break;
557         case ID_ME:
558                 {
559                         Mesh *me= (Mesh *)id;
560                         if (me->fdata.external) {
561                                 rewrite_path_fixed(me->fdata.external->filename, visit_cb, absbase, bpath_user_data);
562                         }
563                 }
564                 break;
565         case ID_LI:
566                 {
567                         Library *lib= (Library *)id;
568                         if (rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
569                                 BKE_library_filepath_set(lib, lib->name);
570                         }
571                 }
572                 break;
573         case ID_MC:
574                 {
575                         MovieClip *clip= (MovieClip *)id;
576                         rewrite_path_fixed(clip->name, visit_cb, absbase, bpath_user_data);
577                 }
578                 break;
579         default:
580                 /* Nothing to do for other IDs that don't contain file paths. */
581                 break;
582         }
583 }
584
585 void bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
586 {
587         ID *id;
588         for (id = lb->first; id; id = id->next) {
589                 bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
590         }
591 }
592
593 void bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
594 {
595         ListBase *lbarray[MAX_LIBARRAY];
596         int a = set_listbasepointers(bmain, lbarray);
597         while (a--) {
598                 bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
599         }
600 }
601
602 /* Rewrites a relative path to be relative to the main file - unless the path is
603  * absolute, in which case it is not altered. */
604 int bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
605 {
606         /* be sure there is low chance of the path being too short */
607         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
608         const char *base_new = ((char **)pathbase_v)[0];
609         const char *base_old = ((char **)pathbase_v)[1];
610
611         if (strncmp(base_old, "//", 2) == 0) {
612                 printf("%s: error, old base path '%s' is not absolute.\n",
613                        __func__, base_old);
614                 return FALSE;
615         }
616
617         /* Make referenced file absolute. This would be a side-effect of
618          * BLI_cleanup_file, but we do it explicitly so we know if it changed. */
619         BLI_strncpy(filepath, path_src, FILE_MAX);
620         if (BLI_path_abs(filepath, base_old)) {
621                 /* Path was relative and is now absolute. Remap.
622                  * Important BLI_cleanup_dir runs before the path is made relative
623                  * because it wont work for paths that start with "//../" */
624                 BLI_cleanup_file(base_new, filepath);
625                 BLI_path_rel(filepath, base_new);
626                 BLI_strncpy(path_dst, filepath, FILE_MAX);
627                 return TRUE;
628         }
629         else {
630                 /* Path was not relative to begin with. */
631                 return FALSE;
632         }
633 }