Merged changes in the trunk up to revision 54421.
[blender.git] / source / blender / blenkernel / 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/blenkernel/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_material_types.h"
68 #include "DNA_node_types.h"
69 #include "DNA_texture_types.h"
70 #include "DNA_vfont_types.h"
71 #include "DNA_scene_types.h"
72 #include "DNA_smoke_types.h"
73 #ifdef WITH_FREESTYLE
74 #  include "DNA_freestyle_types.h"
75 #endif
76
77 #include "BLI_blenlib.h"
78 #include "BLI_utildefines.h"
79
80 #include "BKE_font.h"
81 #include "BKE_library.h"
82 #include "BKE_main.h"
83 #include "BKE_node.h"
84 #include "BKE_report.h"
85 #include "BKE_sequencer.h"
86 #include "BKE_image.h" /* so we can check the image's type */
87
88 #include "BKE_bpath.h"  /* own include */
89
90 static int checkMissingFiles_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
91 {
92         ReportList *reports = (ReportList *)userdata;
93
94         if (!BLI_exists(path_src)) {
95                 BKE_reportf(reports, RPT_WARNING, "Path '%s' not found", path_src);
96         }
97
98         return FALSE;
99 }
100
101 /* high level function */
102 void BKE_bpath_missing_files_check(Main *bmain, ReportList *reports)
103 {
104         BKE_bpath_traverse_main(bmain, checkMissingFiles_visit_cb, BKE_BPATH_TRAVERSE_ABS, reports);
105 }
106
107 typedef struct BPathRemap_Data {
108         const char *basedir;
109         ReportList *reports;
110
111         int count_tot;
112         int count_changed;
113         int count_failed;
114 } BPathRemap_Data;
115
116 static int makeFilesRelative_visit_cb(void *userdata, char *path_dst, const char *path_src)
117 {
118         BPathRemap_Data *data = (BPathRemap_Data *)userdata;
119
120         data->count_tot++;
121
122         if (BLI_path_is_rel(path_src)) {
123                 return FALSE; /* already relative */
124         }
125         else {
126                 strcpy(path_dst, path_src);
127                 BLI_path_rel(path_dst, data->basedir);
128                 if (BLI_path_is_rel(path_dst)) {
129                         data->count_changed++;
130                 }
131                 else {
132                         BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made relative", path_src);
133                         data->count_failed++;
134                 }
135                 return TRUE;
136         }
137 }
138
139 void BKE_bpath_relative_convert(Main *bmain, const char *basedir, ReportList *reports)
140 {
141         BPathRemap_Data data = {NULL};
142
143         if (basedir[0] == '\0') {
144                 printf("%s: basedir='', this is a bug\n", __func__);
145                 return;
146         }
147
148         data.basedir = basedir;
149         data.reports = reports;
150
151         BKE_bpath_traverse_main(bmain, makeFilesRelative_visit_cb, 0, (void *)&data);
152
153         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
154                     "Total files %d | Changed %d | Failed %d",
155                     data.count_tot, data.count_changed, data.count_failed);
156 }
157
158 static int makeFilesAbsolute_visit_cb(void *userdata, char *path_dst, const char *path_src)
159 {
160         BPathRemap_Data *data = (BPathRemap_Data *)userdata;
161
162         data->count_tot++;
163
164         if (BLI_path_is_rel(path_src) == FALSE) {
165                 return FALSE; /* already absolute */
166         }
167         else {
168                 strcpy(path_dst, path_src);
169                 BLI_path_abs(path_dst, data->basedir);
170                 if (BLI_path_is_rel(path_dst) == FALSE) {
171                         data->count_changed++;
172                 }
173                 else {
174                         BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made absolute", path_src);
175                         data->count_failed++;
176                 }
177                 return TRUE;
178         }
179 }
180
181 /* similar to BKE_bpath_relative_convert - keep in sync! */
182 void BKE_bpath_absolute_convert(Main *bmain, const char *basedir, ReportList *reports)
183 {
184         BPathRemap_Data data = {NULL};
185
186         if (basedir[0] == '\0') {
187                 printf("%s: basedir='', this is a bug\n", __func__);
188                 return;
189         }
190
191         data.basedir = basedir;
192         data.reports = reports;
193
194         BKE_bpath_traverse_main(bmain, makeFilesAbsolute_visit_cb, 0, (void *)&data);
195
196         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
197                     "Total files %d | Changed %d | Failed %d",
198                     data.count_tot, data.count_changed, data.count_failed);
199 }
200
201 /**
202  * find this file recursively, use the biggest file so thumbnails don't get used by mistake
203  * \param filename_new: the path will be copied here, caller must initialize as empty string.
204  * \param dirname: subdir to search
205  * \param filename: set this filename
206  * \param filesize: filesize for the file
207  *
208  * \returns found: 1/0.
209  */
210 #define MAX_RECUR 16
211 static int findFileRecursive(char *filename_new,
212                              const char *dirname,
213                              const char *filename,
214                              int *filesize,
215                              int *recur_depth)
216 {
217         /* file searching stuff */
218         DIR *dir;
219         struct dirent *de;
220         struct stat status;
221         char path[FILE_MAX];
222         int size;
223         int found = FALSE;
224
225         dir = opendir(dirname);
226
227         if (dir == NULL)
228                 return found;
229
230         if (*filesize == -1)
231                 *filesize = 0;  /* dir opened fine */
232
233         while ((de = readdir(dir)) != NULL) {
234
235                 if (strcmp(".", de->d_name) == 0 || strcmp("..", de->d_name) == 0)
236                         continue;
237
238                 BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
239
240                 if (stat(path, &status) != 0)
241                         continue;  /* cant stat, don't bother with this file, could print debug info here */
242
243                 if (S_ISREG(status.st_mode)) { /* is file */
244                         if (strncmp(filename, de->d_name, FILE_MAX) == 0) { /* name matches */
245                                 /* open the file to read its size */
246                                 size = status.st_size;
247                                 if ((size > 0) && (size > *filesize)) { /* find the biggest file */
248                                         *filesize = size;
249                                         BLI_strncpy(filename_new, path, FILE_MAX);
250                                         found = TRUE;
251                                 }
252                         }
253                 }
254                 else if (S_ISDIR(status.st_mode)) { /* is subdir */
255                         if (*recur_depth <= MAX_RECUR) {
256                                 (*recur_depth)++;
257                                 found |= findFileRecursive(filename_new, path, filename, filesize, recur_depth);
258                                 (*recur_depth)--;
259                         }
260                 }
261         }
262         closedir(dir);
263         return found;
264 }
265
266 typedef struct BPathFind_Data {
267         const char *basedir;
268         char searchdir[FILE_MAX];
269         ReportList *reports;
270 } BPathFind_Data;
271
272 static int findMissingFiles_visit_cb(void *userdata, char *path_dst, const char *path_src)
273 {
274         BPathFind_Data *data = (BPathFind_Data *)userdata;
275         char filename_new[FILE_MAX];
276
277         int filesize = -1;
278         int recur_depth = 0;
279         int found;
280
281         filename_new[0] = '\0';
282
283         found = findFileRecursive(filename_new,
284                                   data->searchdir, BLI_path_basename((char *)path_src),
285                                   &filesize, &recur_depth);
286
287         if (filesize == -1) { /* could not open dir */
288                 BKE_reportf(data->reports, RPT_WARNING,
289                             "Could not open directory '%s'",
290                             BLI_path_basename(data->searchdir));
291                 return FALSE;
292         }
293         else if (found == FALSE) {
294                 BKE_reportf(data->reports, RPT_WARNING,
295                             "Could not find '%s' in '%s'",
296                             BLI_path_basename((char *)path_src), data->searchdir);
297                 return FALSE;
298         }
299         else {
300                 BLI_strncpy(path_dst, filename_new, FILE_MAX);
301                 return TRUE;
302         }
303 }
304
305 void BKE_bpath_missing_files_find(Main *bmain, const char *searchpath, ReportList *reports)
306 {
307         struct BPathFind_Data data = {NULL};
308
309         data.reports = reports;
310         BLI_split_dir_part(searchpath, data.searchdir, sizeof(data.searchdir));
311
312         BKE_bpath_traverse_main(bmain, findMissingFiles_visit_cb, 0, (void *)&data);
313 }
314
315 /* Run a visitor on a string, replacing the contents of the string as needed. */
316 static int rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
317 {
318         char path_src_buf[FILE_MAX];
319         const char *path_src;
320         char path_dst[FILE_MAX];
321
322         if (absbase) {
323                 BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
324                 BLI_path_abs(path_src_buf, absbase);
325                 path_src = path_src_buf;
326         }
327         else {
328                 path_src = path;
329         }
330
331         if (visit_cb(userdata, path_dst, path_src)) {
332                 BLI_strncpy(path, path_dst, FILE_MAX);
333                 return TRUE;
334         }
335         else {
336                 return FALSE;
337         }
338 }
339
340 static int rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR],
341                                       char path_file[FILE_MAXFILE],
342                                       BPathVisitor visit_cb,
343                                       const char *absbase,
344                                       void *userdata)
345 {
346         char path_src[FILE_MAX];
347         char path_dst[FILE_MAX];
348
349         BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
350
351         if (absbase) {
352                 BLI_path_abs(path_src, absbase);
353         }
354
355         if (visit_cb(userdata, path_dst, (const char *)path_src)) {
356                 BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
357                 return TRUE;
358         }
359         else {
360                 return FALSE;
361         }
362 }
363
364 static int rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
365 {
366         char path_src_buf[FILE_MAX];
367         const char *path_src;
368         char path_dst[FILE_MAX];
369
370         if (absbase) {
371                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
372                 BLI_path_abs(path_src_buf, absbase);
373                 path_src = path_src_buf;
374         }
375         else {
376                 path_src = *path;
377         }
378
379         if (visit_cb(userdata, path_dst, path_src)) {
380                 MEM_freeN((*path));
381                 (*path) = BLI_strdup(path_dst);
382                 return TRUE;
383         }
384         else {
385                 return FALSE;
386         }
387 }
388
389 /* Run visitor function 'visit' on all paths contained in 'id'. */
390 void BKE_bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
391 {
392         const char *absbase = (flag & BKE_BPATH_TRAVERSE_ABS) ? ID_BLEND_PATH(bmain, id) : NULL;
393
394         if ((flag & BKE_BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
395                 return;
396         }
397
398         switch (GS(id->name)) {
399                 case ID_IM:
400                 {
401                         Image *ima;
402                         ima = (Image *)id;
403                         if (ima->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
404                                 if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
405                                         rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
406                                 }
407                         }
408                         break;
409                 }
410                 case ID_BR:
411                 {
412                         Brush *brush = (Brush *)id;
413                         if (brush->icon_filepath[0]) {
414                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
415                         }
416                         break;
417                 }
418                 case ID_OB:
419                 {
420                         Object *ob = (Object *)id;
421                         ModifierData *md;
422                         ParticleSystem *psys;
423
424 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
425         {                                                                          \
426                 PointCache *cache;                                                     \
427                 for (cache = (ptcaches).first; cache; cache = cache->next) {           \
428                         if (cache->flag & PTCACHE_DISK_CACHE) {                            \
429                                 rewrite_path_fixed(cache->path,                                \
430                                                    visit_cb,                                   \
431                                                    absbase,                                    \
432                                                    bpath_user_data);                           \
433                         }                                                                  \
434                 }                                                                      \
435         } (void)0
436
437                         /* do via modifiers instead */
438 #if 0
439                         if (ob->fluidsimSettings) {
440                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
441                         }
442 #endif
443
444                         for (md = ob->modifiers.first; md; md = md->next) {
445                                 if (md->type == eModifierType_Fluidsim) {
446                                         FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
447                                         if (fluidmd->fss) {
448                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
449                                         }
450                                 }
451                                 else if (md->type == eModifierType_Smoke) {
452                                         SmokeModifierData *smd = (SmokeModifierData *)md;
453                                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
454                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
455                                         }
456                                 }
457                                 else if (md->type == eModifierType_Cloth) {
458                                         ClothModifierData *clmd = (ClothModifierData *) md;
459                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
460                                 }
461                                 else if (md->type == eModifierType_Ocean) {
462                                         OceanModifierData *omd = (OceanModifierData *) md;
463                                         rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data);
464                                 }
465                                 else if (md->type == eModifierType_MeshCache) {
466                                         MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md;
467                                         rewrite_path_fixed(mcmd->filepath, visit_cb, absbase, bpath_user_data);
468                                 }
469                         }
470
471                         if (ob->soft) {
472                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
473                         }
474
475                         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
476                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
477                         }
478
479 #undef BPATH_TRAVERSE_POINTCACHE
480
481                         break;
482                 }
483                 case ID_SO:
484                 {
485                         bSound *sound = (bSound *)id;
486                         if (sound->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
487                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
488                         }
489                         break;
490                 }
491                 case ID_TXT:
492                         if (((Text *)id)->name) {
493                                 rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
494                         }
495                         break;
496                 case ID_VF:
497                 {
498                         VFont *vfont = (VFont *)id;
499                         if (vfont->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
500                                 if (BKE_vfont_is_builtin(vfont) == FALSE) {
501                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
502                                 }
503                         }
504                         break;
505                 }
506                 case ID_MA:
507                 {
508                         Material *ma = (Material *)id;
509                         bNodeTree *ntree = ma->nodetree;
510
511                         if (ntree) {
512                                 bNode *node;
513
514                                 for (node = ntree->nodes.first; node; node = node->next) {
515                                         if (node->type == SH_NODE_SCRIPT) {
516                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
517                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
518                                         }
519                                 }
520                         }
521                         break;
522                 }
523                 case ID_NT:
524                 {
525                         bNodeTree *ntree = (bNodeTree *)id;
526                         bNode *node;
527
528                         if (ntree->type == NTREE_SHADER) {
529                                 /* same as lines above */
530                                 for (node = ntree->nodes.first; node; node = node->next) {
531                                         if (node->type == SH_NODE_SCRIPT) {
532                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
533                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
534                                         }
535                                 }
536                         }
537                         break;
538                 }
539                 case ID_TE:
540                 {
541                         Tex *tex = (Tex *)id;
542                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
543                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
544                         }
545                         break;
546                 }
547                 case ID_SCE:
548                 {
549                         Scene *scene = (Scene *)id;
550                         if (scene->ed) {
551                                 Sequence *seq;
552
553                                 SEQ_BEGIN(scene->ed, seq)
554                                 {
555                                         if (SEQ_HAS_PATH(seq)) {
556                                                 if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM)) {
557                                                         rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name,
558                                                                                    visit_cb, absbase, bpath_user_data);
559                                                 }
560                                                 else if (seq->type == SEQ_TYPE_IMAGE) {
561                                                         /* might want an option not to loop over all strips */
562                                                         StripElem *se = seq->strip->stripdata;
563                                                         int len = MEM_allocN_len(se) / sizeof(*se);
564                                                         int i;
565
566                                                         if (flag & BKE_BPATH_TRAVERSE_SKIP_MULTIFILE) {
567                                                                 /* only operate on one path */
568                                                                 len = MIN2(1, len);
569                                                         }
570
571                                                         for (i = 0; i < len; i++, se++) {
572                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
573                                                                                            visit_cb, absbase, bpath_user_data);
574                                                         }
575                                                 }
576                                                 else {
577                                                         /* simple case */
578                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
579                                                 }
580                                         }
581
582                                 }
583                                 SEQ_END
584                         }
585 #ifdef WITH_FREESTYLE
586                         {
587                                 SceneRenderLayer *srl = scene->r.layers.first;
588
589                                 for(; srl; srl = srl->next) {
590                                         FreestyleModuleConfig* module = srl->freestyleConfig.modules.first;
591                                         for (; module; module = module->next) {
592                                                 rewrite_path_fixed(module->module_path, visit_cb, absbase, bpath_user_data);
593                                         }
594                                 }
595                         }
596 #endif
597                         break;
598                 }
599                 case ID_ME:
600                 {
601                         Mesh *me = (Mesh *)id;
602                         if (me->ldata.external) {
603                                 rewrite_path_fixed(me->ldata.external->filename, visit_cb, absbase, bpath_user_data);
604                         }
605                         break;
606                 }
607                 case ID_LI:
608                 {
609                         Library *lib = (Library *)id;
610                         /* keep packedfile paths always relative to the blend */
611                         if (lib->packedfile == NULL) {
612                                 if (rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
613                                         BKE_library_filepath_set(lib, lib->name);
614                                 }
615                         }
616                         break;
617                 }
618                 case ID_MC:
619                 {
620                         MovieClip *clip = (MovieClip *)id;
621                         rewrite_path_fixed(clip->name, visit_cb, absbase, bpath_user_data);
622                         break;
623                 }
624                 default:
625                         /* Nothing to do for other IDs that don't contain file paths. */
626                         break;
627         }
628 }
629
630 void BKE_bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
631 {
632         ID *id;
633         for (id = lb->first; id; id = id->next) {
634                 BKE_bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
635         }
636 }
637
638 void BKE_bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
639 {
640         ListBase *lbarray[MAX_LIBARRAY];
641         int a = set_listbasepointers(bmain, lbarray);
642         while (a--) {
643                 BKE_bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
644         }
645 }
646
647 /* Rewrites a relative path to be relative to the main file - unless the path is
648  * absolute, in which case it is not altered. */
649 int BKE_bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
650 {
651         /* be sure there is low chance of the path being too short */
652         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
653         const char *base_new = ((char **)pathbase_v)[0];
654         const char *base_old = ((char **)pathbase_v)[1];
655
656         if (BLI_path_is_rel(base_old)) {
657                 printf("%s: error, old base path '%s' is not absolute.\n",
658                        __func__, base_old);
659                 return FALSE;
660         }
661
662         /* Make referenced file absolute. This would be a side-effect of
663          * BLI_cleanup_file, but we do it explicitly so we know if it changed. */
664         BLI_strncpy(filepath, path_src, FILE_MAX);
665         if (BLI_path_abs(filepath, base_old)) {
666                 /* Path was relative and is now absolute. Remap.
667                  * Important BLI_cleanup_dir runs before the path is made relative
668                  * because it wont work for paths that start with "//../" */
669                 BLI_cleanup_file(base_new, filepath);
670                 BLI_path_rel(filepath, base_new);
671                 BLI_strncpy(path_dst, filepath, FILE_MAX);
672                 return TRUE;
673         }
674         else {
675                 /* Path was not relative to begin with. */
676                 return FALSE;
677         }
678 }
679
680
681 /* -------------------------------------------------------------------- */
682 /**
683  * Backup/Restore/Free functions,
684  * \note These functions assume the data won't chane order.
685  */
686
687 struct PathStore {
688         struct PathStore *next, *prev;
689 } PathStore;
690
691 static int bpath_list_append(void *userdata, char *UNUSED(path_dst), const char *path_src)
692 {
693         /* store the path and string in a single alloc */
694         ListBase *ls = userdata;
695         size_t path_size = strlen(path_src) + 1;
696         struct PathStore *path_store = MEM_mallocN(sizeof(PathStore) + path_size, __func__);
697         char *filepath = (char *)(path_store + 1);
698
699         memcpy(filepath, path_src, path_size);
700         BLI_addtail(ls, path_store);
701         return FALSE;
702 }
703
704 static int bpath_list_restore(void *userdata, char *path_dst, const char *path_src)
705 {
706         /* assume ls->first wont be NULL because the number of paths can't change!
707          * (if they do caller is wrong) */
708         ListBase *ls = userdata;
709         struct PathStore *path_store = ls->first;
710         const char *filepath = (char *)(path_store + 1);
711         int ret;
712
713         if (strcmp(path_src, filepath) == 0) {
714                 ret = FALSE;
715         }
716         else {
717                 BLI_strncpy(path_dst, filepath, FILE_MAX);
718                 ret = TRUE;
719         }
720
721         BLI_freelinkN(ls, path_store);
722         return ret;
723 }
724
725 /* return ls_handle */
726 void *BKE_bpath_list_backup(Main *bmain, const int flag)
727 {
728         ListBase *ls = MEM_callocN(sizeof(ListBase), __func__);
729
730         BKE_bpath_traverse_main(bmain, bpath_list_append, flag, ls);
731
732         return ls;
733 }
734
735 void BKE_bpath_list_restore(Main *bmain, const int flag, void *ls_handle)
736 {
737         ListBase *ls = ls_handle;
738
739         BKE_bpath_traverse_main(bmain, bpath_list_restore, flag, ls);
740 }
741
742 void BKE_bpath_list_free(void *ls_handle)
743 {
744         ListBase *ls = ls_handle;
745         BLI_assert(ls->first == NULL);  /* assumes we were used */
746         BLI_freelistN(ls);
747         MEM_freeN(ls);
748 }