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: a7ce1df5712f99bcd34f4738932a9096f8d8322b (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
/*
 * $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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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>

#ifndef WIN32
  #include <unistd.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 "BLI_bfile.h"

// This would provide config paths and their oldest viable version
// so if there is an uncompatible change, user's old versions are not loaded
//#include "bfile_tables.h"

/* Internal bfile type flags */
#define BTF_OPEN     (0)
#define BTF_FOPEN    (1<<0)
#define BTF_READ     (1<<1)
#define BTF_WRITE    (1<<2)
#define BTF_AT_END   (1<<3)
#define BTF_DISCARD  (1<<4)


void fill_paths(BFILE *bfile, const char *path) {
	char* source_path = NULL;
	int bflags = bfile->uflags;

	if(bflags & BFILE_NORMAL || bflags & BFILE_RAW) {
//		bfile->fpath is path with // replaced
	}
	if(bflags & BFILE_TEMP) {
//		bfile->fpath is tempdir+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->type & BTF_WRITE && !(bflags & BFILE_RAW)) {
		/* Generate temp path */
		// bfile->tpath is fpath+randstring
		if(!(bfile->type & BTF_DISCARD)) {
			/* Copy data to tpath */
			if(source_path) {
				// copy it from older version or sys version
			}
		}
	} else {
		bfile->tpath = bfile->fpath;
	}
}

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

	bfile = MEM_mallocN(sizeof(BFILE), "bfile-fopen");
	bfile->type = BTF_FOPEN;
	bfile->uflags = bflags;

	/* From fopen() doc, we can guess some logic:
	r  BTF_READ
	r+ BTF_READ | BTF_WRITE
	w  BTF_DISCARD | BTF_WRITE
	w+ BTF_DISCARD | BTF_WRITE | BTF_READ
	a  BTF_AT_END | BTF_WRITE
	a+ BTF_AT_END | BTF_WRITE | BTF_READ
	*/
	if(strchr(mode, 'r'))
		bfile->type |= BTF_READ;
	if(strchr(mode, 'w'))
		bfile->type |= (BTF_DISCARD | BTF_WRITE);
	if(strchr(mode, 'a'))
		bfile->type |= (BTF_AT_END | BTF_WRITE);
	if(strchr(mode, '+'))
		bfile->type |= (BTF_READ | BTF_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) {
	BFILE *bfile;

	bfile = MEM_mallocN(sizeof(BFILE), "bfile-open");
	bfile->type = BTF_OPEN;
	bfile->uflags = bflags;

	/* Easy mapping for open() */
	if(flags & O_RDONLY)
		bfile->type |= BTF_READ;
	if(flags & O_WRONLY)
		bfile->type |= BTF_WRITE;
	if(flags & O_RDWR)
		bfile->type |= (BTF_READ | BTF_WRITE);
	if(flags & O_APPEND)
		bfile->type |= BTF_AT_END;
	if(flags & O_TRUNC)
		bfile->type |= BTF_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->type | BTF_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;
	}
}