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