2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19
* All rights reserved.
21
* The Original Code is: all of this file.
23
* Contributor(s): Campbell barton, Alex Fraser
25
* ***** END GPL LICENSE BLOCK *****
28
/** \file blender/blenkernel/intern/bpath.c
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.
44
/* path/file handling stuff */
50
# include "BLI_winstuff.h"
53
#include "MEM_guardedalloc.h"
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"
74
#include "BLI_blenlib.h"
75
#include "BLI_utildefines.h"
78
#include "BKE_library.h"
81
#include "BKE_report.h"
82
#include "BKE_sequencer.h"
83
#include "BKE_image.h" /* so we can check the image's type */
85
#include "BKE_bpath.h" /* own include */
87
static int checkMissingFiles_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
89
ReportList *reports = (ReportList *)userdata;
91
if (!BLI_exists(path_src)) {
92
BKE_reportf(reports, RPT_WARNING, "Path '%s' not found", path_src);
98
/* high level function */
99
void BKE_bpath_missing_files_check(Main *bmain, ReportList *reports)
101
BKE_bpath_traverse_main(bmain, checkMissingFiles_visit_cb, BKE_BPATH_TRAVERSE_ABS, reports);
104
typedef struct BPathRemap_Data {
113
static int makeFilesRelative_visit_cb(void *userdata, char *path_dst, const char *path_src)
115
BPathRemap_Data *data = (BPathRemap_Data *)userdata;
119
if (BLI_path_is_rel(path_src)) {
120
return FALSE; /* already relative */
123
strcpy(path_dst, path_src);
124
BLI_path_rel(path_dst, data->basedir);
125
if (BLI_path_is_rel(path_dst)) {
126
data->count_changed++;
129
BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made relative", path_src);
130
data->count_failed++;
136
void BKE_bpath_relative_convert(Main *bmain, const char *basedir, ReportList *reports)
138
BPathRemap_Data data = {NULL};
140
if (basedir[0] == '\0') {
141
printf("%s: basedir='', this is a bug\n", __func__);
145
data.basedir = basedir;
146
data.reports = reports;
148
BKE_bpath_traverse_main(bmain, makeFilesRelative_visit_cb, 0, (void *)&data);
150
BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
151
"Total files %d | Changed %d | Failed %d",
152
data.count_tot, data.count_changed, data.count_failed);
155
static int makeFilesAbsolute_visit_cb(void *userdata, char *path_dst, const char *path_src)
157
BPathRemap_Data *data = (BPathRemap_Data *)userdata;
161
if (BLI_path_is_rel(path_src) == FALSE) {
162
return FALSE; /* already absolute */
165
strcpy(path_dst, path_src);
166
BLI_path_abs(path_dst, data->basedir);
167
if (BLI_path_is_rel(path_dst) == FALSE) {
168
data->count_changed++;
171
BKE_reportf(data->reports, RPT_WARNING, "Path '%s' cannot be made absolute", path_src);
172
data->count_failed++;
178
/* similar to BKE_bpath_relative_convert - keep in sync! */
179
void BKE_bpath_absolute_convert(Main *bmain, const char *basedir, ReportList *reports)
181
BPathRemap_Data data = {NULL};
183
if (basedir[0] == '\0') {
184
printf("%s: basedir='', this is a bug\n", __func__);
188
data.basedir = basedir;
189
data.reports = reports;
191
BKE_bpath_traverse_main(bmain, makeFilesAbsolute_visit_cb, 0, (void *)&data);
193
BKE_reportf(reports, data.count_failed ? RPT_WARNING : RPT_INFO,
194
"Total files %d | Changed %d | Failed %d",
195
data.count_tot, data.count_changed, data.count_failed);
199
* find this file recursively, use the biggest file so thumbnails don't get used by mistake
200
* \param filename_new: the path will be copied here, caller must initialize as empty string.
201
* \param dirname: subdir to search
202
* \param filename: set this filename
203
* \param filesize: filesize for the file
205
* \returns found: 1/0.
208
static int findFileRecursive(char *filename_new,
210
const char *filename,
214
/* file searching stuff */
222
dir = opendir(dirname);
228
*filesize = 0; /* dir opened fine */
230
while ((de = readdir(dir)) != NULL) {
232
if (strcmp(".", de->d_name) == 0 || strcmp("..", de->d_name) == 0)
235
BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
237
if (stat(path, &status) != 0)
238
continue; /* cant stat, don't bother with this file, could print debug info here */
240
if (S_ISREG(status.st_mode)) { /* is file */
241
if (strncmp(filename, de->d_name, FILE_MAX) == 0) { /* name matches */
242
/* open the file to read its size */
243
size = status.st_size;
244
if ((size > 0) && (size > *filesize)) { /* find the biggest file */
246
BLI_strncpy(filename_new, path, FILE_MAX);
251
else if (S_ISDIR(status.st_mode)) { /* is subdir */
252
if (*recur_depth <= MAX_RECUR) {
254
found |= findFileRecursive(filename_new, path, filename, filesize, recur_depth);
263
typedef struct BPathFind_Data {
265
char searchdir[FILE_MAX];
269
static int findMissingFiles_visit_cb(void *userdata, char *path_dst, const char *path_src)
271
BPathFind_Data *data = (BPathFind_Data *)userdata;
272
char filename_new[FILE_MAX];
278
filename_new[0] = '\0';
280
found = findFileRecursive(filename_new,
281
data->searchdir, BLI_path_basename((char *)path_src),
282
&filesize, &recur_depth);
284
if (filesize == -1) { /* could not open dir */
285
BKE_reportf(data->reports, RPT_WARNING,
286
"Could not open directory '%s'",
287
BLI_path_basename(data->searchdir));
290
else if (found == FALSE) {
291
BKE_reportf(data->reports, RPT_WARNING,
292
"Could not find '%s' in '%s'",
293
BLI_path_basename((char *)path_src), data->searchdir);
297
BLI_strncpy(path_dst, filename_new, FILE_MAX);
302
void BKE_bpath_missing_files_find(Main *bmain, const char *searchpath, ReportList *reports)
304
struct BPathFind_Data data = {NULL};
306
data.reports = reports;
307
BLI_split_dir_part(searchpath, data.searchdir, sizeof(data.searchdir));
309
BKE_bpath_traverse_main(bmain, findMissingFiles_visit_cb, 0, (void *)&data);
312
/* Run a visitor on a string, replacing the contents of the string as needed. */
313
static int rewrite_path_fixed(char *path, BPathVisitor visit_cb, const char *absbase, void *userdata)
315
char path_src_buf[FILE_MAX];
316
const char *path_src;
317
char path_dst[FILE_MAX];
320
BLI_strncpy(path_src_buf, path, sizeof(path_src_buf));
321
BLI_path_abs(path_src_buf, absbase);
322
path_src = path_src_buf;
328
if (visit_cb(userdata, path_dst, path_src)) {
329
BLI_strncpy(path, path_dst, FILE_MAX);
337
static int rewrite_path_fixed_dirfile(char path_dir[FILE_MAXDIR],
338
char path_file[FILE_MAXFILE],
339
BPathVisitor visit_cb,
343
char path_src[FILE_MAX];
344
char path_dst[FILE_MAX];
346
BLI_join_dirfile(path_src, sizeof(path_src), path_dir, path_file);
349
BLI_path_abs(path_src, absbase);
352
if (visit_cb(userdata, path_dst, (const char *)path_src)) {
353
BLI_split_dirfile(path_dst, path_dir, path_file, FILE_MAXDIR, FILE_MAXFILE);
361
static int rewrite_path_alloc(char **path, BPathVisitor visit_cb, const char *absbase, void *userdata)
363
char path_src_buf[FILE_MAX];
364
const char *path_src;
365
char path_dst[FILE_MAX];
368
BLI_strncpy(path_src_buf, *path, sizeof(path_src_buf));
369
BLI_path_abs(path_src_buf, absbase);
370
path_src = path_src_buf;
376
if (visit_cb(userdata, path_dst, path_src)) {
378
(*path) = BLI_strdup(path_dst);
386
/* Run visitor function 'visit' on all paths contained in 'id'. */
387
void BKE_bpath_traverse_id(Main *bmain, ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
389
const char *absbase = (flag & BKE_BPATH_TRAVERSE_ABS) ? ID_BLEND_PATH(bmain, id) : NULL;
391
if ((flag & BKE_BPATH_TRAVERSE_SKIP_LIBRARY) && id->lib) {
395
switch (GS(id->name)) {
400
if (ima->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
401
if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
402
rewrite_path_fixed(ima->name, visit_cb, absbase, bpath_user_data);
409
Brush *brush = (Brush *)id;
410
if (brush->icon_filepath[0]) {
411
rewrite_path_fixed(brush->icon_filepath, visit_cb, absbase, bpath_user_data);
417
Object *ob = (Object *)id;
419
ParticleSystem *psys;
421
#define BPATH_TRAVERSE_POINTCACHE(ptcaches) \
424
for (cache = (ptcaches).first; cache; cache = cache->next) { \
425
if (cache->flag & PTCACHE_DISK_CACHE) { \
426
rewrite_path_fixed(cache->path, \
434
/* do via modifiers instead */
436
if (ob->fluidsimSettings) {
437
rewrite_path_fixed(ob->fluidsimSettings->surfdataPath, visit_cb, absbase, bpath_user_data);
441
for (md = ob->modifiers.first; md; md = md->next) {
442
if (md->type == eModifierType_Fluidsim) {
443
FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
445
rewrite_path_fixed(fluidmd->fss->surfdataPath, visit_cb, absbase, bpath_user_data);
448
else if (md->type == eModifierType_Smoke) {
449
SmokeModifierData *smd = (SmokeModifierData *)md;
450
if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
451
BPATH_TRAVERSE_POINTCACHE(smd->domain->ptcaches[0]);
454
else if (md->type == eModifierType_Cloth) {
455
ClothModifierData *clmd = (ClothModifierData *) md;
456
BPATH_TRAVERSE_POINTCACHE(clmd->ptcaches);
458
else if (md->type == eModifierType_Ocean) {
459
OceanModifierData *omd = (OceanModifierData *) md;
460
rewrite_path_fixed(omd->cachepath, visit_cb, absbase, bpath_user_data);
462
else if (md->type == eModifierType_MeshCache) {
463
MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md;
464
rewrite_path_fixed(mcmd->filepath, visit_cb, absbase, bpath_user_data);
469
BPATH_TRAVERSE_POINTCACHE(ob->soft->ptcaches);
472
for (psys = ob->particlesystem.first; psys; psys = psys->next) {
473
BPATH_TRAVERSE_POINTCACHE(psys->ptcaches);
476
#undef BPATH_TRAVERSE_POINTCACHE
482
bSound *sound = (bSound *)id;
483
if (sound->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
484
rewrite_path_fixed(sound->name, visit_cb, absbase, bpath_user_data);
489
if (((Text *)id)->name) {
490
rewrite_path_alloc(&((Text *)id)->name, visit_cb, absbase, bpath_user_data);
495
VFont *vfont = (VFont *)id;
496
if (vfont->packedfile == NULL || (flag & BKE_BPATH_TRAVERSE_SKIP_PACKED) == 0) {
497
if (BKE_vfont_is_builtin(vfont) == FALSE) {
498
rewrite_path_fixed(((VFont *)id)->name, visit_cb, absbase, bpath_user_data);
505
Material *ma = (Material *)id;
506
bNodeTree *ntree = ma->nodetree;
511
for (node = ntree->nodes.first; node; node = node->next) {
512
if (node->type == SH_NODE_SCRIPT) {
513
NodeShaderScript *nss = (NodeShaderScript *)node->storage;
514
rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
522
bNodeTree *ntree = (bNodeTree *)id;
525
if (ntree->type == NTREE_SHADER) {
526
/* same as lines above */
527
for (node = ntree->nodes.first; node; node = node->next) {
528
if (node->type == SH_NODE_SCRIPT) {
529
NodeShaderScript *nss = (NodeShaderScript *)node->storage;
530
rewrite_path_fixed(nss->filepath, visit_cb, absbase, bpath_user_data);
538
Tex *tex = (Tex *)id;
539
if (tex->type == TEX_VOXELDATA && TEX_VD_IS_SOURCE_PATH(tex->vd->file_format)) {
540
rewrite_path_fixed(tex->vd->source_path, visit_cb, absbase, bpath_user_data);
546
Scene *scene = (Scene *)id;
550
SEQ_BEGIN(scene->ed, seq)
552
if (SEQ_HAS_PATH(seq)) {
553
if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM)) {
554
rewrite_path_fixed_dirfile(seq->strip->dir, seq->strip->stripdata->name,
555
visit_cb, absbase, bpath_user_data);
557
else if (seq->type == SEQ_TYPE_IMAGE) {
558
/* might want an option not to loop over all strips */
559
StripElem *se = seq->strip->stripdata;
560
int len = MEM_allocN_len(se) / sizeof(*se);
563
if (flag & BKE_BPATH_TRAVERSE_SKIP_MULTIFILE) {
564
/* only operate on one path */
568
for (i = 0; i < len; i++, se++) {
569
rewrite_path_fixed_dirfile(seq->strip->dir, se->name,
570
visit_cb, absbase, bpath_user_data);
575
rewrite_path_fixed(seq->strip->dir, visit_cb, absbase, bpath_user_data);
586
Mesh *me = (Mesh *)id;
587
if (me->ldata.external) {
588
rewrite_path_fixed(me->ldata.external->filename, visit_cb, absbase, bpath_user_data);
594
Library *lib = (Library *)id;
595
/* keep packedfile paths always relative to the blend */
596
if (lib->packedfile == NULL) {
597
if (rewrite_path_fixed(lib->name, visit_cb, absbase, bpath_user_data)) {
598
BKE_library_filepath_set(lib, lib->name);
605
MovieClip *clip = (MovieClip *)id;
606
rewrite_path_fixed(clip->name, visit_cb, absbase, bpath_user_data);
610
/* Nothing to do for other IDs that don't contain file paths. */
615
void BKE_bpath_traverse_id_list(Main *bmain, ListBase *lb, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
618
for (id = lb->first; id; id = id->next) {
619
BKE_bpath_traverse_id(bmain, id, visit_cb, flag, bpath_user_data);
623
void BKE_bpath_traverse_main(Main *bmain, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
625
ListBase *lbarray[MAX_LIBARRAY];
626
int a = set_listbasepointers(bmain, lbarray);
628
BKE_bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data);
632
/* Rewrites a relative path to be relative to the main file - unless the path is
633
* absolute, in which case it is not altered. */
634
int BKE_bpath_relocate_visitor(void *pathbase_v, char *path_dst, const char *path_src)
636
/* be sure there is low chance of the path being too short */
637
char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
638
const char *base_new = ((char **)pathbase_v)[0];
639
const char *base_old = ((char **)pathbase_v)[1];
641
if (BLI_path_is_rel(base_old)) {
642
printf("%s: error, old base path '%s' is not absolute.\n",
647
/* Make referenced file absolute. This would be a side-effect of
648
* BLI_cleanup_file, but we do it explicitly so we know if it changed. */
649
BLI_strncpy(filepath, path_src, FILE_MAX);
650
if (BLI_path_abs(filepath, base_old)) {
651
/* Path was relative and is now absolute. Remap.
652
* Important BLI_cleanup_dir runs before the path is made relative
653
* because it wont work for paths that start with "//../" */
654
BLI_cleanup_file(base_new, filepath);
655
BLI_path_rel(filepath, base_new);
656
BLI_strncpy(path_dst, filepath, FILE_MAX);
660
/* Path was not relative to begin with. */
666
/* -------------------------------------------------------------------- */
668
* Backup/Restore/Free functions,
669
* \note These functions assume the data won't chane order.
673
struct PathStore *next, *prev;
676
static int bpath_list_append(void *userdata, char *UNUSED(path_dst), const char *path_src)
678
/* store the path and string in a single alloc */
679
ListBase *ls = userdata;
680
size_t path_size = strlen(path_src) + 1;
681
struct PathStore *path_store = MEM_mallocN(sizeof(PathStore) + path_size, __func__);
682
char *filepath = (char *)(path_store + 1);
684
memcpy(filepath, path_src, path_size);
685
BLI_addtail(ls, path_store);
689
static int bpath_list_restore(void *userdata, char *path_dst, const char *path_src)
691
/* assume ls->first wont be NULL because the number of paths can't change!
692
* (if they do caller is wrong) */
693
ListBase *ls = userdata;
694
struct PathStore *path_store = ls->first;
695
const char *filepath = (char *)(path_store + 1);
698
if (strcmp(path_src, filepath) == 0) {
702
BLI_strncpy(path_dst, filepath, FILE_MAX);
706
BLI_freelinkN(ls, path_store);
710
/* return ls_handle */
711
void *BKE_bpath_list_backup(Main *bmain, const int flag)
713
ListBase *ls = MEM_callocN(sizeof(ListBase), __func__);
715
BKE_bpath_traverse_main(bmain, bpath_list_append, flag, ls);
720
void BKE_bpath_list_restore(Main *bmain, const int flag, void *ls_handle)
722
ListBase *ls = ls_handle;
724
BKE_bpath_traverse_main(bmain, bpath_list_restore, flag, ls);
727
void BKE_bpath_list_free(void *ls_handle)
729
ListBase *ls = ls_handle;
730
BLI_assert(ls->first == NULL); /* assumes we were used */