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