Merged revision(s) 57908-57978 from trunk/blender into soc-2013-dingto.
[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" /* so we can check the image's type */
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         struct stat 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                 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                                   const bool find_all)
307 {
308         struct BPathFind_Data data = {NULL};
309
310         data.reports = reports;
311         data.searchdir = searchpath;
312         data.find_all = find_all;
313
314         BKE_bpath_traverse_main(bmain, findMissingFiles_visit_cb, BKE_BPATH_TRAVERSE_ABS, (void *)&data);
315 }
316
317 /* Run a visitor on a string, replacing the contents of the string as needed. */
318 static bool rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
319 {
320         char path_src_buf[FILE_MAX];
321         const char *path_src;
322         char path_dst[FILE_MAX];
323
324         if (absbase) {
325                 BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
326                 BLI_path_abs(path_src_buf, absbase);
327                 path_src = path_src_buf;
328         }
329         else {
330                 path_src = path;
331         }
332
333         if (visit_cb(userdata, path_dst, path_src)) {
334                 BLI_strncpy(path, path_dst, FILE_MAX);
335                 return true;
336         }
337         else {
338                 return false;
339         }
340 }
341
342 static bool rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR],
343                                        char path_file[FILE_MAXFILE],
344                                        BPathVisitor visit_cb,
345                                        const char *absbase,
346                                        void *userdata)
347 {
348         char path_src[FILE_MAX];
349         char path_dst[FILE_MAX];
350
351         BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
352
353         if (absbase) {
354                 BLI_path_abs(path_src, absbase);
355         }
356
357         if (visit_cb(userdata, path_dst, (const char *)path_src)) {
358                 BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
359                 return true;
360         }
361         else {
362                 return false;
363         }
364 }
365
366 static bool rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
367 {
368         char path_src_buf[FILE_MAX];
369         const char *path_src;
370         char path_dst[FILE_MAX];
371
372         if (absbase) {
373                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
374                 BLI_path_abs(path_src_buf, absbase);
375                 path_src = path_src_buf;
376         }
377         else {
378                 path_src = *path;
379         }
380
381         if (visit_cb(userdata, path_dst, path_src)) {
382                 MEM_freeN((*path));
383                 (*path) = BLI_strdup(path_dst);
384                 return true;
385         }
386         else {
387                 return false;
388         }
389 }
390
391 /* Run visitor function 'visit' on all paths contained in 'id'. */
392 void BKE_bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
393 {
394         const char *absbase = (flag & BKE_BPATH_TRAVERSE_ABS) ? ID_BLEND_PATH(bmain, id) : NULL;
395
396         if ((flag & BKE_BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
397                 return;
398         }
399
400         switch (GS(id->name)) {
401                 case ID_IM:
402                 {
403                         Image *ima;
404                         ima = (Image *)id;
405                         if (ima->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
406                                 if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
407                                         rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
408                                 }
409                         }
410                         break;
411                 }
412                 case ID_BR:
413                 {
414                         Brush *brush = (Brush *)id;
415                         if (brush->icon_filepath[0]) {
416                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
417                         }
418                         break;
419                 }
420                 case ID_OB:
421                 {
422                         Object *ob = (Object *)id;
423                         ModifierData *md;
424                         ParticleSystem *psys;
425
426 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
427         {                                                                          \
428                 PointCache *cache;                                                     \
429                 for (cache = (ptcaches).first; cache; cache = cache->next) {           \
430                         if (cache->flag & PTCACHE_DISK_CACHE) {                            \
431                                 rewrite_path_fixed(cache->path,                                \
432                                                    visit_cb,                                   \
433                                                    absbase,                                    \
434                                                    bpath_user_data);                           \
435                         }                                                                  \
436                 }                                                                      \
437         } (void)0
438
439                         /* do via modifiers instead */
440 #if 0
441                         if (ob->fluidsimSettings) {
442                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
443                         }
444 #endif
445
446                         for (md = ob->modifiers.first; md; md = md->next) {
447                                 if (md->type == eModifierType_Fluidsim) {
448                                         FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
449                                         if (fluidmd->fss) {
450                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
451                                         }
452                                 }
453                                 else if (md->type == eModifierType_Smoke) {
454                                         SmokeModifierData *smd = (SmokeModifierData *)md;
455                                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
456                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
457                                         }
458                                 }
459                                 else if (md->type == eModifierType_Cloth) {
460                                         ClothModifierData *clmd = (ClothModifierData *) md;
461                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
462                                 }
463                                 else if (md->type == eModifierType_Ocean) {
464                                         OceanModifierData *omd = (OceanModifierData *) md;
465                                         rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data);
466                                 }
467                                 else if (md->type == eModifierType_MeshCache) {
468                                         MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md;
469                                         rewrite_path_fixed(mcmd->filepath, visit_cb, absbase, bpath_user_data);
470                                 }
471                         }
472
473                         if (ob->soft) {
474                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
475                         }
476
477                         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
478                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
479                         }
480
481 #undef BPATH_TRAVERSE_POINTCACHE
482
483                         break;
484                 }
485                 case ID_SO:
486                 {
487                         bSound *sound = (bSound *)id;
488                         if (sound->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
489                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
490                         }
491                         break;
492                 }
493                 case ID_TXT:
494                         if (((Text *)id)->name) {
495                                 rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
496                         }
497                         break;
498                 case ID_VF:
499                 {
500                         VFont *vfont = (VFont *)id;
501                         if (vfont->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
502                                 if (BKE_vfont_is_builtin(vfont) == false) {
503                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
504                                 }
505                         }
506                         break;
507                 }
508                 case ID_MA:
509                 {
510                         Material *ma = (Material *)id;
511                         bNodeTree *ntree = ma->nodetree;
512
513                         if (ntree) {
514                                 bNode *node;
515
516                                 for (node = ntree->nodes.first; node; node = node->next) {
517                                         if (node->type == SH_NODE_SCRIPT) {
518                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
519                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
520                                         }
521                                 }
522                         }
523                         break;
524                 }
525                 case ID_NT:
526                 {
527                         bNodeTree *ntree = (bNodeTree *)id;
528                         bNode *node;
529
530                         if (ntree->type == NTREE_SHADER) {
531                                 /* same as lines above */
532                                 for (node = ntree->nodes.first; node; node = node->next) {
533                                         if (node->type == SH_NODE_SCRIPT) {
534                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
535                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
536                                         }
537                                 }
538                         }
539                         break;
540                 }
541                 case ID_TE:
542                 {
543                         Tex *tex = (Tex *)id;
544                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
545                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
546                         }
547                         break;
548                 }
549                 case ID_SCE:
550                 {
551                         Scene *scene = (Scene *)id;
552                         if (scene->ed) {
553                                 Sequence *seq;
554
555                                 SEQ_BEGIN(scene->ed, seq)
556                                 {
557                                         if (SEQ_HAS_PATH(seq)) {
558                                                 if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM)) {
559                                                         rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name,
560                                                                                    visit_cb, absbase, bpath_user_data);
561                                                 }
562                                                 else if (seq->type == SEQ_TYPE_IMAGE) {
563                                                         /* might want an option not to loop over all strips */
564                                                         StripElem *se = seq->strip->stripdata;
565                                                         int len = MEM_allocN_len(se) / sizeof(*se);
566                                                         int i;
567
568                                                         if (flag & BKE_BPATH_TRAVERSE_SKIP_MULTIFILE) {
569                                                                 /* only operate on one path */
570                                                                 len = MIN2(1, len);
571                                                         }
572
573                                                         for (i = 0; i < len; i++, se++) {
574                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
575                                                                                            visit_cb, absbase, bpath_user_data);
576                                                         }
577                                                 }
578                                                 else {
579                                                         /* simple case */
580                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
581                                                 }
582                                         }
583
584                                 }
585                                 SEQ_END
586                         }
587                         break;
588                 }
589                 case ID_ME:
590                 {
591                         Mesh *me = (Mesh *)id;
592                         if (me->ldata.external) {
593                                 rewrite_path_fixed(me->ldata.external->filename, visit_cb, absbase, bpath_user_data);
594                         }
595                         break;
596                 }
597                 case ID_LI:
598                 {
599                         Library *lib = (Library *)id;
600                         /* keep packedfile paths always relative to the blend */
601                         if (lib->packedfile == NULL) {
602                                 if (rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
603                                         BKE_library_filepath_set(lib, lib->name);
604                                 }
605                         }
606                         break;
607                 }
608                 case ID_MC:
609                 {
610                         MovieClip *clip = (MovieClip *)id;
611                         rewrite_path_fixed(clip->name, visit_cb, absbase, bpath_user_data);
612                         break;
613                 }
614                 default:
615                         /* Nothing to do for other IDs that don't contain file paths. */
616                         break;
617         }
618 }
619
620 void BKE_bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
621 {
622         ID *id;
623         for (id = lb->first; id; id = id->next) {
624                 BKE_bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
625         }
626 }
627
628 void BKE_bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
629 {
630         ListBase *lbarray[MAX_LIBARRAY];
631         int a = set_listbasepointers(bmain, lbarray);
632         while (a--) {
633                 BKE_bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
634         }
635 }
636
637 /* Rewrites a relative path to be relative to the main file - unless the path is
638  * absolute, in which case it is not altered. */
639 bool BKE_bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
640 {
641         /* be sure there is low chance of the path being too short */
642         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
643         const char *base_new = ((char **)pathbase_v)[0];
644         const char *base_old = ((char **)pathbase_v)[1];
645
646         if (BLI_path_is_rel(base_old)) {
647                 printf("%s: error, old base path '%s' is not absolute.\n",
648                        __func__, base_old);
649                 return false;
650         }
651
652         /* Make referenced file absolute. This would be a side-effect of
653          * BLI_cleanup_file, but we do it explicitly so we know if it changed. */
654         BLI_strncpy(filepath, path_src, FILE_MAX);
655         if (BLI_path_abs(filepath, base_old)) {
656                 /* Path was relative and is now absolute. Remap.
657                  * Important BLI_cleanup_dir runs before the path is made relative
658                  * because it wont work for paths that start with "//../" */
659                 BLI_cleanup_file(base_new, filepath);
660                 BLI_path_rel(filepath, base_new);
661                 BLI_strncpy(path_dst, filepath, FILE_MAX);
662                 return true;
663         }
664         else {
665                 /* Path was not relative to begin with. */
666                 return false;
667         }
668 }
669
670
671 /* -------------------------------------------------------------------- */
672 /**
673  * Backup/Restore/Free functions,
674  * \note These functions assume the data won't chane order.
675  */
676
677 struct PathStore {
678         struct PathStore *next, *prev;
679 };
680
681 static bool bpath_list_append(void *userdata, char *UNUSED(path_dst), const char *path_src)
682 {
683         /* store the path and string in a single alloc */
684         ListBase *ls = userdata;
685         size_t path_size = strlen(path_src) + 1;
686         struct PathStore *path_store = MEM_mallocN(sizeof(struct PathStore) + path_size, __func__);
687         char *filepath = (char *)(path_store + 1);
688
689         memcpy(filepath, path_src, path_size);
690         BLI_addtail(ls, path_store);
691         return false;
692 }
693
694 static bool bpath_list_restore(void *userdata, char *path_dst, const char *path_src)
695 {
696         /* assume ls->first wont be NULL because the number of paths can't change!
697          * (if they do caller is wrong) */
698         ListBase *ls = userdata;
699         struct PathStore *path_store = ls->first;
700         const char *filepath = (char *)(path_store + 1);
701         bool ret;
702
703         if (STREQ(path_src, filepath)) {
704                 ret = false;
705         }
706         else {
707                 BLI_strncpy(path_dst, filepath, FILE_MAX);
708                 ret = true;
709         }
710
711         BLI_freelinkN(ls, path_store);
712         return ret;
713 }
714
715 /* return ls_handle */
716 void *BKE_bpath_list_backup(Main *bmain, const int flag)
717 {
718         ListBase *ls = MEM_callocN(sizeof(ListBase), __func__);
719
720         BKE_bpath_traverse_main(bmain, bpath_list_append, flag, ls);
721
722         return ls;
723 }
724
725 void BKE_bpath_list_restore(Main *bmain, const int flag, void *ls_handle)
726 {
727         ListBase *ls = ls_handle;
728
729         BKE_bpath_traverse_main(bmain, bpath_list_restore, flag, ls);
730 }
731
732 void BKE_bpath_list_free(void *ls_handle)
733 {
734         ListBase *ls = ls_handle;
735         BLI_assert(ls->first == NULL);  /* assumes we were used */
736         BLI_freelistN(ls);
737         MEM_freeN(ls);
738 }