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