Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_bfile.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5dd764d6c64454a96c5d183da0a917221c84376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2009 by Stichting Blender Foundation.
 * All rights reserved.
 *
 * ***** END GPL LICENSE BLOCK *****
 * BFILE* based abstraction for file access.
 */

#include <string.h>
#include <stdlib.h>
#ifndef WIN32
 #include <libgen.h>
 #include <unistd.h>
 #include <sys/param.h>
#else
 #include <io.h>
 #include "BLI_winstuff.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "MEM_guardedalloc.h"
#include "BKE_utildefines.h"
#include "BKE_blender.h"
#include "BLI_path_util.h"
#include "BLI_fileops.h"
#include "BLI_storage.h"
#include "BLI_bfile.h"

#include "GHOST_C-api.h"

/* Internal bfile classification flags */
#define BCF_OPEN     (0)
#define BCF_FOPEN    (1<<0)
#define BCF_READ     (1<<1)
#define BCF_WRITE    (1<<2)
#define BCF_AT_END   (1<<3)
#define BCF_DISCARD  (1<<4)

/* Standard files names */
#define LAST_SESSION_FILE "last-session"
#define ENVIRONMENT_FILE "environment"


/* Declaration of internal functions */
void chomp(char* line);
void expand_envvars(char* src, char* dst);
void fill_paths(BFILE *bfile, const char *path);
char* find_in_pathlist(char* filename, char* pathlist);
void init_vars_from_file(const char* path);
void setup_temp();

/*** Exported functions ***/

BFILE *BLI_bfile_fopen(const char *path, const char *mode, int bflags,
					   BEnvVarFam envvars)
{
	BFILE *bfile;

	bfile = MEM_mallocN(sizeof(BFILE), "bfile-fopen");
	bfile->classf = BCF_FOPEN;
	bfile->uflags = bflags;

	/* From fopen() doc, we can guess some logic:
	r  BCF_READ
	r+ BCF_READ | BCF_WRITE
	w  BCF_DISCARD | BCF_WRITE
	w+ BCF_DISCARD | BCF_WRITE | BCF_READ
	a  BCF_AT_END | BCF_WRITE
	a+ BCF_AT_END | BCF_WRITE | BCF_READ
	*/
	if(strchr(mode, 'r'))
		bfile->classf |= BCF_READ;
	if(strchr(mode, 'w'))
		bfile->classf |= (BCF_DISCARD | BCF_WRITE);
	if(strchr(mode, 'a'))
		bfile->classf |= (BCF_AT_END | BCF_WRITE);
	if(strchr(mode, '+'))
		bfile->classf |= (BCF_READ | BCF_WRITE);

	fill_paths(bfile, path);

	bfile->stream = fopen(bfile->tpath, mode);
	// detect failed fopen
	bfile->fd = fileno(bfile->stream);
	return bfile;
}


BFILE *BLI_bfile_open(const char *pathname, int flags, int bflags,
					  BEnvVarFam envvars)
{
	BFILE *bfile;

	bfile = MEM_mallocN(sizeof(BFILE), "bfile-open");
	bfile->classf = BCF_OPEN;
	bfile->uflags = bflags;

	/* Easy mapping for open() */
	if(flags & O_RDONLY)
		bfile->classf |= BCF_READ;
	if(flags & O_WRONLY)
		bfile->classf |= BCF_WRITE;
	if(flags & O_RDWR)
		bfile->classf |= (BCF_READ | BCF_WRITE);
	if(flags & O_APPEND)
		bfile->classf |= BCF_AT_END;
	if(flags & O_TRUNC)
		bfile->classf |= BCF_DISCARD;

	fill_paths(bfile, pathname);

	bfile->fd = open(bfile->tpath, flags);
	// detect failed open
//	bfile->stream = fdopen(bfile->fd, XXX); /* MSWindows _fdopen? */
	return bfile;
}


FILE *BLI_bfile_file_from_bfile(BFILE *bfile) {
	return bfile->stream;
}


int BLI_bfile_fd_from_bfile(BFILE *bfile) {
	return bfile->fd;
}


ssize_t BLI_bfile_write(BFILE *f, const void *buf, size_t count) {
	ssize_t ret;

	ret = write((f->fd), buf, count);
	if (ret == -1) {
		f->error = 1;
	}

	return ret;
}


ssize_t BLI_bfile_read(BFILE *f, void *buf, size_t count) {
	ssize_t ret;

	ret = read((f->fd), buf, count);
	if (ret == -1) {
		f->error = 1;
	}

	return ret;
}


size_t BLI_bfile_fwrite(const void *ptr, size_t size, size_t nmemb,
						BFILE *f)
{
	size_t ret;

	ret = fwrite(ptr, size, nmemb, f->stream);
	if (ret < 0) {
		f->error = 1;
	}

	return ret;
}


size_t BLI_bfile_fread(void *ptr, size_t size, size_t nmemb, BFILE *f) {
	size_t ret;

	ret = fread(ptr, size, nmemb, f->stream);
	if ((ret < 0) && ferror(f->stream)) {
		f->error = 1;
	}

	return ret;
}


void BLI_bfile_close(BFILE *bfile) {
	if((bfile->classf | BCF_WRITE) &&
	   !(bfile->uflags | BFILE_RAW)) {
		/* Make sure data is on disk */
		/* Move to final name if no errors */
	}

	/* Normal close */

	/* Cleanup */
	if(bfile->fpath) {
		MEM_freeN(bfile->fpath);
	}
	if(bfile->tpath) {
		MEM_freeN(bfile->tpath);
	}
}


void BLI_bfile_clear_error(BFILE *bfile) {
	bfile->error = 0;
}


void BLI_bfile_set_error(BFILE *bfile, int error) {
	/* No cheating, use clear_error() for 0 */
	if (error) {
		bfile->error = error;
	}
}


void BLI_bfile_init_vars() {
	char file[MAXPATHLEN];
	char temp[MAXPATHLEN];
	extern char bprogname[];
	FILE* fp;

	/* This one is unconditional */
	sprintf(temp, "%d", BLENDER_VERSION);
	BLI_setenv("BLENDER_VERSION", temp);

	/* Is this unpack&run? */
	sprintf(temp, "%s/%d/environment", dirname(bprogname), BLENDER_VERSION);
	if(BLI_exist(temp)) {
		BLI_setenv_if_new("BLENDER_SHARE", dirname(bprogname));
	} else {
		BLI_setenv_if_new("BLENDER_SHARE", (const char*)GHOST_getSystemDir());
	}

	strcpy(file, (const char*)GHOST_getUserDir());
	BLI_add_slash(file);
	strcat(file, LAST_SESSION_FILE);
	fp = fopen(file, "r");
	/* 1st line, read previous version */
	if (fp && (fscanf(fp, "%3c\n", temp) == 1)) {
		temp[3] = '\0';
		BLI_setenv("BLENDER_VERSION_PREV", temp);
		/* 2nd line, read previous session path if needed */
		if(!getenv("BLENDER_TEMP")) {
			if ((fgets(temp, MAXPATHLEN, fp) != NULL)) {
				/* Clean any \n */
				chomp(temp);
				/* Check the dir is still there or generate new one */
				if(!BLI_exist(temp)) {
					setup_temp();
				}
			} else {
				/* We have to generate it for sure */
				setup_temp();
			}
		}
	} else {
		/* Probably new user, or only <=249 before */
		BLI_setenv("BLENDER_VERSION_PREV", "0");
		setup_temp();
	}

	if(fp) {
		fclose(fp);
	}

	/* Load vars from user and system files */
	strcpy(file, (const char *)GHOST_getUserDir());
	BLI_add_slash(file);
	strcat(file, ENVIRONMENT_FILE);
	init_vars_from_file(file);
	sprintf(temp, "/%d/environment", BLENDER_VERSION);
	BLI_make_file_string("/", file, getenv("BLENDER_SHARE"), temp);
	init_vars_from_file(file);
}


/*** Internal functions ***/

/**
 Eliminate trailing EOL by writing a \0 over it.
 Name taken from Perl.
 */
void chomp(char* line) {
	int len = strlen(line);
#ifndef WIN32
	if (line[len - 1] == '\n') {
		line[len - 1] = '\0';
	}
#else
	if ((line[len - 2] == '\r' ) && ((line[len - 1] == '\n'))) {
		line[len - 2] = '\0';
	}
#endif /* WIN32 */
}


/**
 Parse a file with lines like FOO=bar (comment lines have # as first
 character) assigning to envvar FOO the value bar if FOO does not
 exist yet.
 Any white space before FOO, around the = or trailing will be used,
 so beware.
 */
#define MAX_LINE 4096
#define ENV_VAR 256
#define VAR_LEN 8192
void init_vars_from_file(const char* path) {
	char line[MAX_LINE];
	char name[ENV_VAR];
	FILE *fp;
	char* separator;
	char expanded[VAR_LEN];

	fp = fopen(path, "r");
	if (!fp) return;

	while (fgets(line, MAX_LINE, fp) != NULL) {
		/* Ignore comment lines */
		if (line[0] == '#')
			continue;

		/* Split into envvar name and contents */
		separator = strchr(line, '=');
		if(separator && ((separator - line) < ENV_VAR)) {
			/* First remove EOL */
			chomp(line);
			strncpy(name, line, separator - line);
			name[separator - line] = '\0';
			expand_envvars(separator + 1, expanded);
			BLI_setenv_if_new(name, expanded);
		}
	}
	fclose(fp);
}


/**
 Look for ${} (or %%) env vars in src and expand if the var
 exists (even if empty value). If not exist, the name is left as is.
 The process is done all over src, and nested ${${}} is not supported.
 src must be \0 terminated, and dst must be big enough.
*/
#ifndef WIN32
 #define ENVVAR_PREFFIX "${"
 #define ENVVAR_P_SIZE 2
 #define ENVVAR_SUFFIX "}"
 #define ENVVAR_S_SIZE 1
#else
 #define ENVVAR_PREFFIX "%"
 #define ENVVAR_P_SIZE 1
 #define ENVVAR_SUFFIX "%"
 #define ENVVAR_S_SIZE 1
#endif /* WIN32 */
void expand_envvars(char* src, char* dst) {
	char* hit1;
	char* hit2;
	char name[ENV_VAR];
	char* value;
	int prevlen;
	int done = 0;
	char* source = src;

	dst[0] = '\0';
	while (!done) {
		hit1 = strstr(source, ENVVAR_PREFFIX);
		if (hit1) {
			hit2 = strstr(hit1 + ENVVAR_P_SIZE, ENVVAR_SUFFIX);
			if (hit2) {
				/* "Copy" the leading part, if any */
				if (hit1 != source) {
					prevlen = strlen(dst);
					strncat(dst, source, hit1 - source);
					dst[prevlen + (hit1 - source)] = '\0';
				}
				/* Figure the name of the env var we just found  */
				strncpy(name, hit1 + ENVVAR_P_SIZE,
						hit2 - (hit1 + ENVVAR_P_SIZE));
				name[hit2 - (hit1 + ENVVAR_P_SIZE)] = '\0';
				/* See if we can get something with that name */
				value = getenv(name);
				if (value) {
					/* Push the var value */
					strcat(dst, value);
				} else {
					/* Leave the var name, so it is clear that it failed */
					strcat(dst, ENVVAR_PREFFIX);
					strcat(dst, name);
					strcat(dst, ENVVAR_SUFFIX);
				}
				/* Continue after closing mark, like a new string */
				source = hit2 + ENVVAR_S_SIZE;
			} else {
				/* Non terminated var so "copy as is" and finish */
				strcat(dst, source);
				done = 1;
			}
		} else {
			/* "Copy" whatever is left */
			strcat(dst, source);
			done = 1;
		}
	}
}


/**
 Return a full path if the filename exists when combined
 with any item from pathlist. Or NULL otherwise.
 */
#ifdef WIN32
 #define SEPARATOR ';'
#else
 #define SEPARATOR ':'
#endif
char* find_in_pathlist(char* filename, char* pathlist) {
	char first[FILE_MAX + 10];
	char* rest = NULL;

	/* Separate first path from rest, use typical separator for current OS */
	rest = strchr(pathlist, SEPARATOR);
	if (rest) {
		strncpy(first, pathlist, rest - pathlist);
		first[rest - pathlist] = '\0';
		/* Skip the separator so it becomes a valid new pathlist */
		rest++;
	} else {
		strcpy(first, pathlist);
	}

	/* Check if combination exists */
	BLI_add_slash(first);
	strcat(first, filename);
	if(BLI_exist(first)) {
		return strdup(first);
	}

	/* First path failed, try with rest of paths if possible */
	if(rest) {
		return find_in_pathlist(filename, rest);
	} else {
		return NULL;
	}
}


/**
 Setup fpath and tpath based in the needs of the bfile.
 */
void fill_paths(BFILE *bfile, const char *path) {
	char* source_path = NULL;
	char* temp_path = NULL;
	int bflags = bfile->uflags;

	if(bflags & BFILE_NORMAL || bflags & BFILE_RAW) {
//		bfile->fpath is path with // replaced
	}
	if(bflags & BFILE_TEMP) {
		temp_path = MEM_mallocN(MAXPATHLEN, "bfile-fpath-1");
		snprintf(temp_path, MAXPATHLEN, "%s/%s", getenv("BLENDER_TEMP"), path);
		bfile->fpath = temp_path;
	}
	if(bflags & BFILE_CONFIG) {
//		bfile->fpath is userdir+version+path
//		source_path is first hit in (if using fallback to older versions)
//		    userdir+curversion+path (... userdir+limitversion+path) sysdir+path
//		(limitversion is based in path, using some kind of regex or "tables")
	}

	if(bfile->classf & BCF_WRITE && !(bflags & BFILE_RAW)) {
		/* Generate temp path */
		temp_path = MEM_mallocN(MAXPATHLEN, "bfile-fpath-2");
		snprintf(temp_path, MAXPATHLEN, "%s.XXXXXX", path);
		bfile->tpath = mkdtemp(temp_path);
		if(!(bfile->classf & BCF_DISCARD)) {
			/* Copy data to tpath */
			if(source_path) {
				// copy it from older version or sys version
			}
		}
	} else {
		bfile->tpath = bfile->fpath;
	}
}


/**
 Create a temp directory in safe and multiuser way.
 */
void setup_temp() {
	char template[MAXPATHLEN];
	char* tempdir;

	if(getenv("TMPDIR")) {
		sprintf(template, "%s/blender-XXXXXX", getenv("TMPDIR"));
	} else {
		sprintf(template, "/tmp/blender-XXXXXX");
// MacOSX NSTemporaryDirectory and WIN32 ???
	}
	tempdir = mkdtemp(template);
	BLI_setenv("BLENDER_TEMP", tempdir);
}