Fix T40157: Loading movies larger than 4GB in size fails
[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  * Contributor(s): Campbell barton, Alex Fraser
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/blenkernel/intern/bpath.c
24  *  \ingroup bli
25  */
26
27 /* TODO,
28  * currently there are some cases we don't support.
29  * - passing output paths to the visitor?, like render out.
30  * - passing sequence strips with many images.
31  * - passing directory paths - visitors don't know which path is a dir or a file.
32  * */
33
34 #include <sys/stat.h>
35
36 #include <string.h>
37 #include <assert.h>
38
39 /* path/file handling stuff */
40 #ifndef WIN32
41 #  include <dirent.h>
42 #  include <unistd.h>
43 #else
44 #  include <io.h>
45 #  include "BLI_winstuff.h"
46 #endif
47
48 #include "MEM_guardedalloc.h"
49
50 #include "DNA_brush_types.h"
51 #include "DNA_image_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_modifier_types.h"
54 #include "DNA_movieclip_types.h"
55 #include "DNA_object_fluidsim.h"
56 #include "DNA_object_force.h"
57 #include "DNA_object_types.h"
58 #include "DNA_particle_types.h"
59 #include "DNA_sequence_types.h"
60 #include "DNA_sound_types.h"
61 #include "DNA_text_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_node_types.h"
64 #include "DNA_texture_types.h"
65 #include "DNA_vfont_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_smoke_types.h"
68 #include "DNA_freestyle_types.h"
69
70 #include "BLI_blenlib.h"
71 #include "BLI_utildefines.h"
72
73 #include "BKE_font.h"
74 #include "BKE_library.h"
75 #include "BKE_main.h"
76 #include "BKE_node.h"
77 #include "BKE_report.h"
78 #include "BKE_sequencer.h"
79 #include "BKE_image.h"
80
81 #include "BKE_bpath.h"  /* own include */
82
83 static bool 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 '%s' not found", path_src);
89         }
90
91         return false;
92 }
93
94 /* high level function */
95 void BKE_bpath_missing_files_check(Main *bmain, ReportList *reports)
96 {
97         BKE_bpath_traverse_main(bmain, checkMissingFiles_visit_cb, BKE_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 bool 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 (BLI_path_is_rel(path_src)) {
116                 return false; /* already relative */
117         }
118         else {
119                 strcpy(path_dst, path_src);
120                 BLI_path_rel(path_dst, data->basedir);
121                 if (BLI_path_is_rel(path_dst)) {
122                         data->count_changed++;
123                 }
124                 else {
125                         BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made relative", path_src);
126                         data->count_failed++;
127                 }
128                 return true;
129         }
130 }
131
132 void BKE_bpath_relative_convert(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         BKE_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 bool 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 (BLI_path_is_rel(path_src) == false) {
158                 return false; /* already absolute */
159         }
160         else {
161                 strcpy(path_dst, path_src);
162                 BLI_path_abs(path_dst, data->basedir);
163                 if (BLI_path_is_rel(path_dst) == false) {
164                         data->count_changed++;
165                 }
166                 else {
167                         BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made absolute", path_src);
168                         data->count_failed++;
169                 }
170                 return true;
171         }
172 }
173
174 /* similar to BKE_bpath_relative_convert - keep in sync! */
175 void BKE_bpath_absolute_convert(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         BKE_bpath_traverse_main(bmain, makeFilesAbsolute_visit_cb, 0, (void *)&data);
188
189         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
190                     "Total files %d | Changed %d | Failed %d",
191                     data.count_tot, data.count_changed, data.count_failed);
192 }
193
194 /**
195  * find this file recursively, use the biggest file so thumbnails don't get used by mistake
196  * \param filename_new: the path will be copied here, caller must initialize as empty string.
197  * \param dirname: subdir to search
198  * \param filename: set this filename
199  * \param filesize: filesize for the file
200  *
201  * \returns found: 1/0.
202  */
203 #define MAX_RECUR 16
204 static int findFileRecursive(char *filename_new,
205                              const char *dirname,
206                              const char *filename,
207                              int *filesize,
208                              int *recur_depth)
209 {
210         /* file searching stuff */
211         DIR *dir;
212         struct dirent *de;
213         BLI_stat_t status;
214         char path[FILE_MAX];
215         int size;
216         bool found = false;
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 (STREQ(".", de->d_name) || STREQ("..", de->d_name))
229                         continue;
230
231                 BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
232
233                 if (BLI_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 (STREQLEN(filename, de->d_name, FILE_MAX)) { /* 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         const char *searchdir;
262         ReportList *reports;
263         bool find_all;
264 } BPathFind_Data;
265
266 static bool 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         if (data->find_all == false) {
276                 if (BLI_exists(path_src)) {
277                         return false;
278                 }
279         }
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                 bool was_relative = BLI_path_is_rel(path_dst);
301
302                 BLI_strncpy(path_dst, filename_new, FILE_MAX);
303
304                 /* keep path relative if the previous one was relative */
305                 if (was_relative)
306                         BLI_path_rel(path_dst, data->basedir);
307
308                 return true;
309         }
310 }
311
312 void BKE_bpath_missing_files_find(Main *bmain, const char *searchpath, ReportList *reports,
313                                   const bool find_all)
314 {
315         struct BPathFind_Data data = {NULL};
316
317         data.basedir = bmain->name;
318         data.reports = reports;
319         data.searchdir = searchpath;
320         data.find_all = find_all;
321
322         BKE_bpath_traverse_main(bmain, findMissingFiles_visit_cb, BKE_BPATH_TRAVERSE_ABS, (void *)&data);
323 }
324
325 /* Run a visitor on a string, replacing the contents of the string as needed. */
326 static bool rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
327 {
328         char path_src_buf[FILE_MAX];
329         const char *path_src;
330         char path_dst[FILE_MAX];
331
332         if (absbase) {
333                 BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
334                 BLI_path_abs(path_src_buf, absbase);
335                 path_src = path_src_buf;
336         }
337         else {
338                 path_src = path;
339         }
340
341         /* so functions can check old value */
342         BLI_strncpy(path_dst, path, FILE_MAX);
343
344         if (visit_cb(userdata, path_dst, path_src)) {
345                 BLI_strncpy(path, path_dst, FILE_MAX);
346                 return true;
347         }
348         else {
349                 return false;
350         }
351 }
352
353 static bool rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR],
354                                        char path_file[FILE_MAXFILE],
355                                        BPathVisitor visit_cb,
356                                        const char *absbase,
357                                        void *userdata)
358 {
359         char path_src[FILE_MAX];
360         char path_dst[FILE_MAX];
361
362         BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
363
364         if (absbase) {
365                 BLI_path_abs(path_src, absbase);
366         }
367
368         if (visit_cb(userdata, path_dst, (const char *)path_src)) {
369                 BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
370                 return true;
371         }
372         else {
373                 return false;
374         }
375 }
376
377 static bool rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
378 {
379         char path_src_buf[FILE_MAX];
380         const char *path_src;
381         char path_dst[FILE_MAX];
382
383         if (absbase) {
384                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
385                 BLI_path_abs(path_src_buf, absbase);
386                 path_src = path_src_buf;
387         }
388         else {
389                 path_src = *path;
390         }
391
392         if (visit_cb(userdata, path_dst, path_src)) {
393                 MEM_freeN((*path));
394                 (*path) = BLI_strdup(path_dst);
395                 return true;
396         }
397         else {
398                 return false;
399         }
400 }
401
402 /* fix the image user "ok" tag after updating paths, so ImBufs get loaded */
403 static void bpath_traverse_image_user_cb(Image *ima, ImageUser *iuser, void *customdata)
404 {
405         if (ima == customdata)
406                 iuser->ok = 1;
407 }
408
409 /* Run visitor function 'visit' on all paths contained in 'id'. */
410 void BKE_bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
411 {
412         const char *absbase = (flag & BKE_BPATH_TRAVERSE_ABS) ? ID_BLEND_PATH(bmain, id) : NULL;
413
414         if ((flag & BKE_BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
415                 return;
416         }
417
418         switch (GS(id->name)) {
419                 case ID_IM:
420                 {
421                         Image *ima;
422                         ima = (Image *)id;
423                         if (ima->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
424                                 if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
425                                         if (rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data)) {
426                                                 if (!ima->packedfile) {
427                                                         BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
428                                                         BKE_image_walk_all_users(bmain, ima, bpath_traverse_image_user_cb);
429                                                 }
430                                         }
431                                 }
432                         }
433                         break;
434                 }
435                 case ID_BR:
436                 {
437                         Brush *brush = (Brush *)id;
438                         if (brush->icon_filepath[0]) {
439                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
440                         }
441                         break;
442                 }
443                 case ID_OB:
444                 {
445                         Object *ob = (Object *)id;
446                         ModifierData *md;
447                         ParticleSystem *psys;
448
449 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
450         {                                                                          \
451                 PointCache *cache;                                                     \
452                 for (cache = (ptcaches).first; cache; cache = cache->next) {           \
453                         if (cache->flag & PTCACHE_DISK_CACHE) {                            \
454                                 rewrite_path_fixed(cache->path,                                \
455                                                    visit_cb,                                   \
456                                                    absbase,                                    \
457                                                    bpath_user_data);                           \
458                         }                                                                  \
459                 }                                                                      \
460         } (void)0
461
462                         /* do via modifiers instead */
463 #if 0
464                         if (ob->fluidsimSettings) {
465                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
466                         }
467 #endif
468
469                         for (md = ob->modifiers.first; md; md = md->next) {
470                                 if (md->type == eModifierType_Fluidsim) {
471                                         FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
472                                         if (fluidmd->fss) {
473                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
474                                         }
475                                 }
476                                 else if (md->type == eModifierType_Smoke) {
477                                         SmokeModifierData *smd = (SmokeModifierData *)md;
478                                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
479                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
480                                         }
481                                 }
482                                 else if (md->type == eModifierType_Cloth) {
483                                         ClothModifierData *clmd = (ClothModifierData *) md;
484                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
485                                 }
486                                 else if (md->type == eModifierType_Ocean) {
487                                         OceanModifierData *omd = (OceanModifierData *) md;
488                                         rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data);
489                                 }
490                                 else if (md->type == eModifierType_MeshCache) {
491                                         MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md;
492                                         rewrite_path_fixed(mcmd->filepath, visit_cb, absbase, bpath_user_data);
493                                 }
494                         }
495
496                         if (ob->soft) {
497                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
498                         }
499
500                         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
501                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
502                         }
503
504 #undef BPATH_TRAVERSE_POINTCACHE
505
506                         break;
507                 }
508                 case ID_SO:
509                 {
510                         bSound *sound = (bSound *)id;
511                         if (sound->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
512                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
513                         }
514                         break;
515                 }
516                 case ID_TXT:
517                         if (((Text *)id)->name) {
518                                 rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
519                         }
520                         break;
521                 case ID_VF:
522                 {
523                         VFont *vfont = (VFont *)id;
524                         if (vfont->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
525                                 if (BKE_vfont_is_builtin(vfont) == false) {
526                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
527                                 }
528                         }
529                         break;
530                 }
531                 case ID_MA:
532                 {
533                         Material *ma = (Material *)id;
534                         bNodeTree *ntree = ma->nodetree;
535
536                         if (ntree) {
537                                 bNode *node;
538
539                                 for (node = ntree->nodes.first; node; node = node->next) {
540                                         if (node->type == SH_NODE_SCRIPT) {
541                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
542                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
543                                         }
544                                 }
545                         }
546                         break;
547                 }
548                 case ID_NT:
549                 {
550                         bNodeTree *ntree = (bNodeTree *)id;
551                         bNode *node;
552
553                         if (ntree->type == NTREE_SHADER) {
554                                 /* same as lines above */
555                                 for (node = ntree->nodes.first; node; node = node->next) {
556                                         if (node->type == SH_NODE_SCRIPT) {
557                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
558                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
559                                         }
560                                 }
561                         }
562                         break;
563                 }
564                 case ID_TE:
565                 {
566                         Tex *tex = (Tex *)id;
567                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
568                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
569                         }
570                         break;
571                 }
572                 case ID_SCE:
573                 {
574                         Scene *scene = (Scene *)id;
575                         if (scene->ed) {
576                                 Sequence *seq;
577
578                                 SEQ_BEGIN(scene->ed, seq)
579                                 {
580                                         if (SEQ_HAS_PATH(seq)) {
581                                                 StripElem *se = seq->strip->stripdata;
582
583                                                 if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM) && se) {
584                                                         rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
585                                                                                    visit_cb, absbase, bpath_user_data);
586                                                 }
587                                                 else if ((seq->type == SEQ_TYPE_IMAGE) && se) {
588                                                         /* might want an option not to loop over all strips */
589                                                         int len = MEM_allocN_len(se) / sizeof(*se);
590                                                         int i;
591
592                                                         if (flag & BKE_BPATH_TRAVERSE_SKIP_MULTIFILE) {
593                                                                 /* only operate on one path */
594                                                                 len = MIN2(1, len);
595                                                         }
596
597                                                         for (i = 0; i < len; i++, se++) {
598                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
599                                                                                            visit_cb, absbase, bpath_user_data);
600                                                         }
601                                                 }
602                                                 else {
603                                                         /* simple case */
604                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
605                                                 }
606                                         }
607
608                                 }
609                                 SEQ_END
610                         }
611                         break;
612                 }
613                 case ID_ME:
614                 {
615                         Mesh *me = (Mesh *)id;
616                         if (me->ldata.external) {
617                                 rewrite_path_fixed(me->ldata.external->filename, visit_cb, absbase, bpath_user_data);
618                         }
619                         break;
620                 }
621                 case ID_LI:
622                 {
623                         Library *lib = (Library *)id;
624                         /* keep packedfile paths always relative to the blend */
625                         if (lib->packedfile == NULL) {
626                                 if (rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
627                                         BKE_library_filepath_set(lib, lib->name);
628                                 }
629                         }
630                         break;
631                 }
632                 case ID_MC:
633                 {
634                         MovieClip *clip = (MovieClip *)id;
635                         rewrite_path_fixed(clip->name, visit_cb, absbase, bpath_user_data);
636                         break;
637                 }
638                 default:
639                         /* Nothing to do for other IDs that don't contain file paths. */
640                         break;
641         }
642 }
643
644 void BKE_bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
645 {
646         ID *id;
647         for (id = lb->first; id; id = id->next) {
648                 BKE_bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
649         }
650 }
651
652 void BKE_bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
653 {
654         ListBase *lbarray[MAX_LIBARRAY];
655         int a = set_listbasepointers(bmain, lbarray);
656         while (a--) {
657                 BKE_bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
658         }
659 }
660
661 /* Rewrites a relative path to be relative to the main file - unless the path is
662  * absolute, in which case it is not altered. */
663 bool BKE_bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
664 {
665         /* be sure there is low chance of the path being too short */
666         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
667         const char *base_new = ((char **)pathbase_v)[0];
668         const char *base_old = ((char **)pathbase_v)[1];
669
670         if (BLI_path_is_rel(base_old)) {
671                 printf("%s: error, old base path '%s' is not absolute.\n",
672                        __func__, base_old);
673                 return false;
674         }
675
676         /* Make referenced file absolute. This would be a side-effect of
677          * BLI_cleanup_file, but we do it explicitly so we know if it changed. */
678         BLI_strncpy(filepath, path_src, FILE_MAX);
679         if (BLI_path_abs(filepath, base_old)) {
680                 /* Path was relative and is now absolute. Remap.
681                  * Important BLI_cleanup_dir runs before the path is made relative
682                  * because it wont work for paths that start with "//../" */
683                 BLI_cleanup_file(base_new, filepath);
684                 BLI_path_rel(filepath, base_new);
685                 BLI_strncpy(path_dst, filepath, FILE_MAX);
686                 return true;
687         }
688         else {
689                 /* Path was not relative to begin with. */
690                 return false;
691         }
692 }
693
694
695 /* -------------------------------------------------------------------- */
696 /**
697  * Backup/Restore/Free functions,
698  * \note These functions assume the data won't chane order.
699  */
700
701 struct PathStore {
702         struct PathStore *next, *prev;
703 };
704
705 static bool bpath_list_append(void *userdata, char *UNUSED(path_dst), const char *path_src)
706 {
707         /* store the path and string in a single alloc */
708         ListBase *ls = userdata;
709         size_t path_size = strlen(path_src) + 1;
710         struct PathStore *path_store = MEM_mallocN(sizeof(struct PathStore) + path_size, __func__);
711         char *filepath = (char *)(path_store + 1);
712
713         memcpy(filepath, path_src, path_size);
714         BLI_addtail(ls, path_store);
715         return false;
716 }
717
718 static bool bpath_list_restore(void *userdata, char *path_dst, const char *path_src)
719 {
720         /* assume ls->first wont be NULL because the number of paths can't change!
721          * (if they do caller is wrong) */
722         ListBase *ls = userdata;
723         struct PathStore *path_store = ls->first;
724         const char *filepath = (char *)(path_store + 1);
725         bool ret;
726
727         if (STREQ(path_src, filepath)) {
728                 ret = false;
729         }
730         else {
731                 BLI_strncpy(path_dst, filepath, FILE_MAX);
732                 ret = true;
733         }
734
735         BLI_freelinkN(ls, path_store);
736         return ret;
737 }
738
739 /* return ls_handle */
740 void *BKE_bpath_list_backup(Main *bmain, const int flag)
741 {
742         ListBase *ls = MEM_callocN(sizeof(ListBase), __func__);
743
744         BKE_bpath_traverse_main(bmain, bpath_list_append, flag, ls);
745
746         return ls;
747 }
748
749 void BKE_bpath_list_restore(Main *bmain, const int flag, void *ls_handle)
750 {
751         ListBase *ls = ls_handle;
752
753         BKE_bpath_traverse_main(bmain, bpath_list_restore, flag, ls);
754 }
755
756 void BKE_bpath_list_free(void *ls_handle)
757 {
758         ListBase *ls = ls_handle;
759         BLI_assert(BLI_listbase_is_empty(ls));  /* assumes we were used */
760         BLI_freelistN(ls);
761         MEM_freeN(ls);
762 }