Fix #33364: cycles tile rendering artifacts.
[blender.git] / source / blender / blenlib / intern / bpath.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Campbell barton, Alex Fraser
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenlib/intern/bpath.c
29  *  \ingroup bli
30  */
31
32 /* TODO,
33  * currently there are some cases we don't support.
34  * - passing output paths to the visitor?, like render out.
35  * - passing sequence strips with many images.
36  * - passing directory paths - visitors don't know which path is a dir or a file.
37  * */
38
39 #include <sys/stat.h>
40
41 #include <string.h>
42 #include <assert.h>
43
44 /* path/file handling stuff */
45 #ifndef WIN32
46 #  include <dirent.h>
47 #  include <unistd.h>
48 #else
49 #  include <io.h>
50 #  include "BLI_winstuff.h"
51 #endif
52
53 #include "MEM_guardedalloc.h"
54
55 #include "DNA_brush_types.h"
56 #include "DNA_image_types.h"
57 #include "DNA_mesh_types.h"
58 #include "DNA_modifier_types.h"
59 #include "DNA_movieclip_types.h"
60 #include "DNA_object_fluidsim.h"
61 #include "DNA_object_force.h"
62 #include "DNA_object_types.h"
63 #include "DNA_particle_types.h"
64 #include "DNA_sequence_types.h"
65 #include "DNA_sound_types.h"
66 #include "DNA_text_types.h"
67 #include "DNA_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
74 #include "BLI_blenlib.h"
75 #include "BLI_bpath.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 static int checkMissingFiles_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
87 {
88         ReportList *reports = (ReportList *)userdata;
89
90         if (!BLI_exists(path_src)) {
91                 BKE_reportf(reports, RPT_WARNING, "Path '%s' not found", path_src);
92         }
93
94         return FALSE;
95 }
96
97 /* high level function */
98 void BLI_bpath_missing_files_check(Main *bmain, ReportList *reports)
99 {
100         BLI_bpath_traverse_main(bmain, checkMissingFiles_visit_cb, BLI_BPATH_TRAVERSE_ABS, reports);
101 }
102
103 typedef struct BPathRemap_Data {
104         const char *basedir;
105         ReportList *reports;
106
107         int count_tot;
108         int count_changed;
109         int count_failed;
110 } BPathRemap_Data;
111
112 static int makeFilesRelative_visit_cb(void *userdata, char *path_dst, const char *path_src)
113 {
114         BPathRemap_Data *data = (BPathRemap_Data *)userdata;
115
116         data->count_tot++;
117
118         if (BLI_path_is_rel(path_src)) {
119                 return FALSE; /* already relative */
120         }
121         else {
122                 strcpy(path_dst, path_src);
123                 BLI_path_rel(path_dst, data->basedir);
124                 if (BLI_path_is_rel(path_dst)) {
125                         data->count_changed++;
126                 }
127                 else {
128                         BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made relative", path_src);
129                         data->count_failed++;
130                 }
131                 return TRUE;
132         }
133 }
134
135 void BLI_bpath_relative_convert(Main *bmain, const char *basedir, ReportList *reports)
136 {
137         BPathRemap_Data data = {NULL};
138
139         if (basedir[0] == '\0') {
140                 printf("%s: basedir='', this is a bug\n", __func__);
141                 return;
142         }
143
144         data.basedir = basedir;
145         data.reports = reports;
146
147         BLI_bpath_traverse_main(bmain, makeFilesRelative_visit_cb, 0, (void *)&data);
148
149         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
150                     "Total files %d | Changed %d | Failed %d",
151                     data.count_tot, data.count_changed, data.count_failed);
152 }
153
154 static int makeFilesAbsolute_visit_cb(void *userdata, char *path_dst, const char *path_src)
155 {
156         BPathRemap_Data *data = (BPathRemap_Data *)userdata;
157
158         data->count_tot++;
159
160         if (BLI_path_is_rel(path_src) == FALSE) {
161                 return FALSE; /* already absolute */
162         }
163         else {
164                 strcpy(path_dst, path_src);
165                 BLI_path_abs(path_dst, data->basedir);
166                 if (BLI_path_is_rel(path_dst) == FALSE) {
167                         data->count_changed++;
168                 }
169                 else {
170                         BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made absolute", path_src);
171                         data->count_failed++;
172                 }
173                 return TRUE;
174         }
175 }
176
177 /* similar to BLI_bpath_relative_convert - keep in sync! */
178 void BLI_bpath_absolute_convert(Main *bmain, const char *basedir, ReportList *reports)
179 {
180         BPathRemap_Data data = {NULL};
181
182         if (basedir[0] == '\0') {
183                 printf("%s: basedir='', this is a bug\n", __func__);
184                 return;
185         }
186
187         data.basedir = basedir;
188         data.reports = reports;
189
190         BLI_bpath_traverse_main(bmain, makeFilesAbsolute_visit_cb, 0, (void *)&data);
191
192         BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
193                     "Total files %d | Changed %d | Failed %d",
194                     data.count_tot, data.count_changed, data.count_failed);
195 }
196
197 /**
198  * find this file recursively, use the biggest file so thumbnails don't get used by mistake
199  * \param filename_new: the path will be copied here, caller must initialize as empty string.
200  * \param dirname: subdir to search
201  * \param filename: set this filename
202  * \param filesize: filesize for the file
203  *
204  * \returns found: 1/0.
205  */
206 #define MAX_RECUR 16
207 static int findFileRecursive(char *filename_new,
208                              const char *dirname,
209                              const char *filename,
210                              int *filesize,
211                              int *recur_depth)
212 {
213         /* file searching stuff */
214         DIR *dir;
215         struct dirent *de;
216         struct stat status;
217         char path[FILE_MAX];
218         int size;
219         int found = FALSE;
220
221         dir = opendir(dirname);
222
223         if (dir == NULL)
224                 return found;
225
226         if (*filesize == -1)
227                 *filesize = 0;  /* dir opened fine */
228
229         while ((de = readdir(dir)) != NULL) {
230
231                 if (strcmp(".", de->d_name) == 0 || strcmp("..", de->d_name) == 0)
232                         continue;
233
234                 BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
235
236                 if (stat(path, &status) != 0)
237                         continue;  /* cant stat, don't bother with this file, could print debug info here */
238
239                 if (S_ISREG(status.st_mode)) { /* is file */
240                         if (strncmp(filename, de->d_name, FILE_MAX) == 0) { /* name matches */
241                                 /* open the file to read its size */
242                                 size = status.st_size;
243                                 if ((size > 0) && (size > *filesize)) { /* find the biggest file */
244                                         *filesize = size;
245                                         BLI_strncpy(filename_new, path, FILE_MAX);
246                                         found = TRUE;
247                                 }
248                         }
249                 }
250                 else if (S_ISDIR(status.st_mode)) { /* is subdir */
251                         if (*recur_depth <= MAX_RECUR) {
252                                 (*recur_depth)++;
253                                 found |= findFileRecursive(filename_new, path, filename, filesize, recur_depth);
254                                 (*recur_depth)--;
255                         }
256                 }
257         }
258         closedir(dir);
259         return found;
260 }
261
262 typedef struct BPathFind_Data {
263         const char *basedir;
264         char searchdir[FILE_MAX];
265         ReportList *reports;
266 } BPathFind_Data;
267
268 static int findMissingFiles_visit_cb(void *userdata, char *path_dst, const char *path_src)
269 {
270         BPathFind_Data *data = (BPathFind_Data *)userdata;
271         char filename_new[FILE_MAX];
272
273         int filesize = -1;
274         int recur_depth = 0;
275         int found;
276
277         filename_new[0] = '\0';
278
279         found = findFileRecursive(filename_new,
280                                   data->searchdir, BLI_path_basename((char *)path_src),
281                                   &filesize, &recur_depth);
282
283         if (filesize == -1) { /* could not open dir */
284                 BKE_reportf(data->reports, RPT_WARNING,
285                             "Could not open directory '%s'",
286                             BLI_path_basename(data->searchdir));
287                 return FALSE;
288         }
289         else if (found == FALSE) {
290                 BKE_reportf(data->reports, RPT_WARNING,
291                             "Could not find '%s' in '%s'",
292                             BLI_path_basename((char *)path_src), data->searchdir);
293                 return FALSE;
294         }
295         else {
296                 BLI_strncpy(path_dst, filename_new, FILE_MAX);
297                 return TRUE;
298         }
299 }
300
301 void BLI_bpath_missing_files_find(Main *bmain, const char *searchpath, ReportList *reports)
302 {
303         struct BPathFind_Data data = {NULL};
304
305         data.reports = reports;
306         BLI_split_dir_part(searchpath, data.searchdir, sizeof(data.searchdir));
307
308         BLI_bpath_traverse_main(bmain, findMissingFiles_visit_cb, 0, (void *)&data);
309 }
310
311 /* Run a visitor on a string, replacing the contents of the string as needed. */
312 static int rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
313 {
314         char path_src_buf[FILE_MAX];
315         const char *path_src;
316         char path_dst[FILE_MAX];
317
318         if (absbase) {
319                 BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
320                 BLI_path_abs(path_src_buf, absbase);
321                 path_src = path_src_buf;
322         }
323         else {
324                 path_src = path;
325         }
326
327         if (visit_cb(userdata, path_dst, path_src)) {
328                 BLI_strncpy(path, path_dst, FILE_MAX);
329                 return TRUE;
330         }
331         else {
332                 return FALSE;
333         }
334 }
335
336 static int rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR],
337                                       char path_file[FILE_MAXFILE],
338                                       BPathVisitor visit_cb,
339                                       const char *absbase,
340                                       void *userdata)
341 {
342         char path_src[FILE_MAX];
343         char path_dst[FILE_MAX];
344
345         BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
346
347         if (absbase) {
348                 BLI_path_abs(path_src, absbase);
349         }
350
351         if (visit_cb(userdata, path_dst, (const char *)path_src)) {
352                 BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
353                 return TRUE;
354         }
355         else {
356                 return FALSE;
357         }
358 }
359
360 static int rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
361 {
362         char path_src_buf[FILE_MAX];
363         const char *path_src;
364         char path_dst[FILE_MAX];
365
366         if (absbase) {
367                 BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
368                 BLI_path_abs(path_src_buf, absbase);
369                 path_src = path_src_buf;
370         }
371         else {
372                 path_src = *path;
373         }
374
375         if (visit_cb(userdata, path_dst, path_src)) {
376                 MEM_freeN((*path));
377                 (*path) = BLI_strdup(path_dst);
378                 return TRUE;
379         }
380         else {
381                 return FALSE;
382         }
383 }
384
385 /* Run visitor function 'visit' on all paths contained in 'id'. */
386 void BLI_bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
387 {
388         const char *absbase = (flag & BLI_BPATH_TRAVERSE_ABS) ? ID_BLEND_PATH(bmain, id) : NULL;
389
390         if ((flag & BLI_BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
391                 return;
392         }
393
394         switch (GS(id->name)) {
395                 case ID_IM:
396                 {
397                         Image *ima;
398                         ima = (Image *)id;
399                         if (ima->packedfile == NULL || (flag & BLI_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
400                                 if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
401                                         rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
402                                 }
403                         }
404                         break;
405                 }
406                 case ID_BR:
407                 {
408                         Brush *brush = (Brush *)id;
409                         if (brush->icon_filepath[0]) {
410                                 rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
411                         }
412                         break;
413                 }
414                 case ID_OB:
415                 {
416                         Object *ob = (Object *)id;
417                         ModifierData *md;
418                         ParticleSystem *psys;
419
420 #define BPATH_TRAVERSE_POINTCACHE(ptcaches)                                    \
421         {                                                                          \
422                 PointCache *cache;                                                     \
423                 for (cache = (ptcaches).first; cache; cache = cache->next) {           \
424                         if (cache->flag & PTCACHE_DISK_CACHE) {                            \
425                                 rewrite_path_fixed(cache->path,                                \
426                                                    visit_cb,                                   \
427                                                    absbase,                                    \
428                                                    bpath_user_data);                           \
429                         }                                                                  \
430                 }                                                                      \
431         } (void)0
432
433                         /* do via modifiers instead */
434 #if 0
435                         if (ob->fluidsimSettings) {
436                                 rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
437                         }
438 #endif
439
440                         for (md = ob->modifiers.first; md; md = md->next) {
441                                 if (md->type == eModifierType_Fluidsim) {
442                                         FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
443                                         if (fluidmd->fss) {
444                                                 rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
445                                         }
446                                 }
447                                 else if (md->type == eModifierType_Smoke) {
448                                         SmokeModifierData *smd = (SmokeModifierData *)md;
449                                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
450                                                 BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
451                                         }
452                                 }
453                                 else if (md->type == eModifierType_Cloth) {
454                                         ClothModifierData *clmd = (ClothModifierData *) md;
455                                         BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
456                                 }
457                                 else if (md->type == eModifierType_Ocean) {
458                                         OceanModifierData *omd = (OceanModifierData *) md;
459                                         rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data);
460                                 }
461                         }
462
463                         if (ob->soft) {
464                                 BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
465                         }
466
467                         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
468                                 BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
469                         }
470
471 #undef BPATH_TRAVERSE_POINTCACHE
472
473                         break;
474                 }
475                 case ID_SO:
476                 {
477                         bSound *sound = (bSound *)id;
478                         if (sound->packedfile == NULL || (flag & BLI_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
479                                 rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
480                         }
481                         break;
482                 }
483                 case ID_TXT:
484                         if (((Text *)id)->name) {
485                                 rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
486                         }
487                         break;
488                 case ID_VF:
489                 {
490                         VFont *vfont = (VFont *)id;
491                         if (vfont->packedfile == NULL || (flag & BLI_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
492                                 if (BKE_vfont_is_builtin(vfont) == FALSE) {
493                                         rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
494                                 }
495                         }
496                         break;
497                 }
498                 case ID_MA:
499                 {
500                         Material *ma = (Material *)id;
501                         bNodeTree *ntree = ma->nodetree;
502
503                         if(ntree) {
504                                 bNode *node;
505
506                                 for (node = ntree->nodes.first; node; node = node->next) {
507                                         if (node->type == SH_NODE_SCRIPT) {
508                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
509                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
510                                         }
511                                 }
512                         }
513                         break;
514                 }
515                 case ID_NT:
516                 {
517                         bNodeTree *ntree = (bNodeTree *)id;
518                         bNode *node;
519
520                         if (ntree->type == NTREE_SHADER) {
521                                 /* same as lines above */
522                                 for (node = ntree->nodes.first; node; node = node->next) {
523                                         if (node->type == SH_NODE_SCRIPT) {
524                                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
525                                                 rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
526                                         }
527                                 }
528                         }
529                         break;
530                 }
531                 case ID_TE:
532                 {
533                         Tex *tex = (Tex *)id;
534                         if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
535                                 rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
536                         }
537                         break;
538                 }
539                 case ID_SCE:
540                 {
541                         Scene *scene = (Scene *)id;
542                         if (scene->ed) {
543                                 Sequence *seq;
544
545                                 SEQ_BEGIN(scene->ed, seq)
546                                 {
547                                         if (SEQ_HAS_PATH(seq)) {
548                                                 if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM)) {
549                                                         rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name,
550                                                                                    visit_cb, absbase, bpath_user_data);
551                                                 }
552                                                 else if (seq->type == SEQ_TYPE_IMAGE) {
553                                                         /* might want an option not to loop over all strips */
554                                                         StripElem *se = seq->strip->stripdata;
555                                                         int len = MEM_allocN_len(se) / sizeof(*se);
556                                                         int i;
557
558                                                         if (flag & BLI_BPATH_TRAVERSE_SKIP_MULTIFILE) {
559                                                                 /* only operate on one path */
560                                                                 len = MIN2(1, len);
561                                                         }
562
563                                                         for (i = 0; i < len; i++, se++) {
564                                                                 rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
565                                                                                            visit_cb, absbase, bpath_user_data);
566                                                         }
567                                                 }
568                                                 else {
569                                                         /* simple case */
570                                                         rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
571                                                 }
572                                         }
573
574                                 }
575                                 SEQ_END
576                         }
577                         break;
578                 }
579                 case ID_ME:
580                 {
581                         Mesh *me = (Mesh *)id;
582                         if (me->ldata.external) {
583                                 rewrite_path_fixed(me->ldata.external->filename, visit_cb, absbase, bpath_user_data);
584                         }
585                         break;
586                 }
587                 case ID_LI:
588                 {
589                         Library *lib = (Library *)id;
590                         if (rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
591                                 BKE_library_filepath_set(lib, lib->name);
592                         }
593                         break;
594                 }
595                 case ID_MC:
596                 {
597                         MovieClip *clip = (MovieClip *)id;
598                         rewrite_path_fixed(clip->name, visit_cb, absbase, bpath_user_data);
599                         break;
600                 }
601                 default:
602                         /* Nothing to do for other IDs that don't contain file paths. */
603                         break;
604         }
605 }
606
607 void BLI_bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
608 {
609         ID *id;
610         for (id = lb->first; id; id = id->next) {
611                 BLI_bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
612         }
613 }
614
615 void BLI_bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
616 {
617         ListBase *lbarray[MAX_LIBARRAY];
618         int a = set_listbasepointers(bmain, lbarray);
619         while (a--) {
620                 BLI_bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
621         }
622 }
623
624 /* Rewrites a relative path to be relative to the main file - unless the path is
625  * absolute, in which case it is not altered. */
626 int BLI_bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
627 {
628         /* be sure there is low chance of the path being too short */
629         char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
630         const char *base_new = ((char **)pathbase_v)[0];
631         const char *base_old = ((char **)pathbase_v)[1];
632
633         if (BLI_path_is_rel(base_old)) {
634                 printf("%s: error, old base path '%s' is not absolute.\n",
635                        __func__, base_old);
636                 return FALSE;
637         }
638
639         /* Make referenced file absolute. This would be a side-effect of
640          * BLI_cleanup_file, but we do it explicitly so we know if it changed. */
641         BLI_strncpy(filepath, path_src, FILE_MAX);
642         if (BLI_path_abs(filepath, base_old)) {
643                 /* Path was relative and is now absolute. Remap.
644                  * Important BLI_cleanup_dir runs before the path is made relative
645                  * because it wont work for paths that start with "//../" */
646                 BLI_cleanup_file(base_new, filepath);
647                 BLI_path_rel(filepath, base_new);
648                 BLI_strncpy(path_dst, filepath, FILE_MAX);
649                 return TRUE;
650         }
651         else {
652                 /* Path was not relative to begin with. */
653                 return FALSE;
654         }
655 }
656
657
658 /* -------------------------------------------------------------------- */
659 /**
660  * Backup/Restore/Free functions,
661  * \note These functions assume the data won't chane order.
662  */
663
664 struct PathStore {
665         struct PathStore *next, *prev;
666 } PathStore;
667
668 static int bpath_list_append(void *userdata, char *UNUSED(path_dst), const char *path_src)
669 {
670         /* store the path and string in a single alloc */
671         ListBase *ls = userdata;
672         size_t path_size = strlen(path_src) + 1;
673         struct PathStore *path_store = MEM_mallocN(sizeof(PathStore) + path_size, __func__);
674         char *filepath = (char *)(path_store + 1);
675
676         memcpy(filepath, path_src, path_size);
677         BLI_addtail(ls, path_store);
678         return FALSE;
679 }
680
681 static int bpath_list_restore(void *userdata, char *path_dst, const char *path_src)
682 {
683         /* assume ls->first wont be NULL because the number of paths can't change!
684          * (if they do caller is wrong) */
685         ListBase *ls = userdata;
686         struct PathStore *path_store = ls->first;
687         const char *filepath = (char *)(path_store + 1);
688         int ret;
689
690         if (strcmp(path_src, filepath) == 0) {
691                 ret = FALSE;
692         }
693         else {
694                 BLI_strncpy(path_dst, filepath, FILE_MAX);
695                 ret = TRUE;
696         }
697
698         BLI_freelinkN(ls, path_store);
699         return ret;
700 }
701
702 /* return ls_handle */
703 void *BLI_bpath_list_backup(Main *bmain, const int flag)
704 {
705         ListBase *ls = MEM_callocN(sizeof(ListBase), __func__);
706
707         BLI_bpath_traverse_main(bmain, bpath_list_append, flag, ls);
708
709         return ls;
710 }
711
712 void BLI_bpath_list_restore(Main *bmain, const int flag, void *ls_handle)
713 {
714         ListBase *ls = ls_handle;
715
716         BLI_bpath_traverse_main(bmain, bpath_list_restore, flag, ls);
717 }
718
719 void BLI_bpath_list_free(void *ls_handle)
720 {
721         ListBase *ls = ls_handle;
722         BLI_assert(ls->first == NULL);  /* assumes we were used */
723         BLI_freelistN(ls);
724         MEM_freeN(ls);
725 }