summaryrefslogtreecommitdiff
path: root/src/blob/blob_fileops.c
blob: 713e7e83d28c449416bf1d6d216ac4d0e85d05b7 (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2013, 2015 Oracle and/or its affiliates.  All rights reserved.
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/db_am.h"
#include "dbinc/fop.h"

/*
 * __blob_file_create --
 *	Blobs are orginaized in a directory sturcture consisting of
 *	<DB_HOME>/__db_bl/<blob_sub_dir>/.  Below that, the blob_id
 *	is used to construct a path to the blob file, and to name
 *	the blob file.  blob_id=1 would result in __db.bl001.
 *	blob_id=12002 would result in 012/__db.bl012002.
 *
 * PUBLIC: int __blob_file_create __P
 * PUBLIC:  ((DBC *, DB_FH **, db_seq_t *));
 */
int
__blob_file_create(dbc, fhpp, blob_id)
	DBC *dbc;
	DB_FH **fhpp;
	db_seq_t *blob_id;
{
	DB  *dbp;
	DB_FH *fhp;
	ENV *env;
	int ret;
	char *ppath;
	const char *dir;

	dbp = dbc->dbp;
	env = dbp->env;
	fhp = *fhpp = NULL;
	ppath = NULL;
	dir = NULL;
	DB_ASSERT(env, !DB_IS_READONLY(dbc->dbp));

	if ((ret = __blob_generate_id(dbp, dbc->txn, blob_id)) != 0)
		goto err;

	if ((ret = __blob_id_to_path(
	    env, dbp->blob_sub_dir, *blob_id, &ppath)) != 0)
		goto err;

	if ((ret = __fop_create(env, dbc->txn,
	    &fhp, ppath, &dir, DB_APP_BLOB, env->db_mode,
	    (F_ISSET(dbc->dbp, DB_AM_NOT_DURABLE) ? DB_LOG_NOT_DURABLE : 0)))
	    != 0) {
		__db_errx(env, DB_STR_A("0228",
		    "Error creating blob file: %llu.", "%llu"),
		    (unsigned long long)*blob_id);
		goto err;
	}

err:	if (ppath != NULL)
		__os_free(env, ppath);
	if (ret == 0)
		*fhpp = fhp;
	return (ret);
}

/*
 * __blob_file_close --
 *
 * PUBLIC: int  __blob_file_close __P ((DBC *, DB_FH *, u_int32_t));
 */
int
__blob_file_close(dbc, fhp, flags)
	DBC *dbc;
	DB_FH *fhp;
	u_int32_t flags;
{
	ENV *env;
	int ret, t_ret;

	env = dbc->env;
	ret = t_ret = 0;
	if (fhp != NULL) {
		/* Only sync if the file was open for writing. */
		if (LF_ISSET(DB_FOP_WRITE))
			t_ret = __os_fsync(env, fhp);
		ret = __os_closehandle(env, fhp);
		if (t_ret != 0)
			ret = t_ret;
	}

	return (ret);
}

/*
 * __blob_file_delete --
 *	Delete a blob file.
 *
 * PUBLIC: int __blob_file_delete __P((DBC *, db_seq_t));
 */
int
__blob_file_delete(dbc, blob_id)
	DBC *dbc;
	db_seq_t blob_id;
{
	ENV *env;
	char *blob_name, *full_path;
	int ret;

	env = dbc->dbp->env;
	blob_name = full_path = NULL;

	if ((ret = __blob_id_to_path(
	    env, dbc->dbp->blob_sub_dir, blob_id, &blob_name)) != 0) {
		__db_errx(env, DB_STR_A("0229",
		   "Failed to construct path for blob file %llu.",
		   "%llu"), (unsigned long long)blob_id);
		goto err;
	}

	/* Log the file remove event. */
	if (!IS_REAL_TXN(dbc->txn)) {
		if ((ret = __db_appname(
		    env, DB_APP_BLOB, blob_name, NULL, &full_path)) != 0)
			goto err;
		ret = __os_unlink(env, full_path, 0);
	} else {
		ret = __fop_remove(
		    env, dbc->txn, NULL, blob_name, NULL, DB_APP_BLOB, 0);
	}

	if (ret != 0) {
		__db_errx(env, DB_STR_A("0230",
		    "Failed to remove blob file while deleting: %s.",
		    "%s"), blob_name);
		goto err;
	}

err:	if (blob_name != NULL)
		__os_free(env, blob_name);
	if (full_path != NULL)
		__os_free(env, full_path);
	return (ret);
}

/*
 * __blob_file_open --
 *
 * PUBLIC: int __blob_file_open
 * PUBLIC:	__P((DB *, DB_FH **, db_seq_t, u_int32_t, int));
 */
int
__blob_file_open(dbp, fhpp, blob_id, flags, printerr)
	DB *dbp;
	DB_FH **fhpp;
	db_seq_t blob_id;
	u_int32_t flags;
	int printerr;
{
	ENV *env;
	int ret;
	u_int32_t oflags;
	char *path, *ppath;

	env = dbp->env;
	*fhpp = NULL;
	ppath = path = NULL;
	oflags = 0;

	if ((ret = __blob_id_to_path(
	    env, dbp->blob_sub_dir, blob_id, &ppath)) != 0)
		goto err;

	if ((ret = __db_appname(
	    env, DB_APP_BLOB, ppath, NULL, &path)) != 0) {
		__db_errx(env, DB_STR_A("0231",
		    "Failed to get path to blob file: %llu.", "%llu"),
		    (unsigned long long)blob_id);
		goto err;
	}

	if (LF_ISSET(DB_FOP_READONLY) || DB_IS_READONLY(dbp))
		oflags |= DB_OSO_RDONLY;
	if ((ret = __os_open(env, path, 0, oflags, 0, fhpp)) != 0) {
		/*
		 * In replication it is possible to try to read a blob file
		 * that has been deleted.  In that case do not print an error.
		 */
		if (printerr == 1) {
			__db_errx(env, DB_STR_A("0232",
			    "Error opening blob file: %s.", "%s"), path);
		}
		goto err;
	}

err:	if (path != NULL)
		__os_free(env, path);
	if (ppath != NULL)
		__os_free(env, ppath);
	return (ret);
}

/*
 * __blob_file_read --
 *
 * PUBLIC: int __blob_file_read
 * PUBLIC:	__P((ENV *, DB_FH *, DBT *, off_t, u_int32_t));
 */
int
__blob_file_read(env, fhp, dbt, offset, size)
	ENV *env;
	DB_FH *fhp;
	DBT *dbt;
	off_t offset;
	u_int32_t size;
{
	int ret;
	size_t bytes;
	void *buf;

	bytes = 0;
	buf = NULL;

	if ((ret = __os_seek(env, fhp, 0, 0, offset)) != 0)
		goto err;

	if (F_ISSET(dbt, DB_DBT_USERCOPY)) {
		if ((ret = __os_malloc(env, size, &buf)) != 0)
			goto err;
	} else
		buf = dbt->data;

	if ((ret = __os_read(env, fhp, buf, size, &bytes)) != 0) {
		__db_errx(env, DB_STR("0233", "Error reading blob file."));
		goto err;
	}
	/*
	 * It is okay to read off the end of the file, in which case less bytes
	 * will be returned than requested.  This is also how the code behaves
	 * in the DB_DBT_PARTIAL API.
	 */
	dbt->size = (u_int32_t)bytes;

	if (F_ISSET(dbt, DB_DBT_USERCOPY)  && dbt->size != 0) {
		ret = env->dbt_usercopy(
		    dbt, 0, buf, dbt->size, DB_USERCOPY_SETDATA);
	}

err:	if (buf != NULL && buf != dbt->data)
		__os_free(env, buf);
	return (ret);
}

/*
 * __blob_file_write --
 *
 * PUBLIC: int __blob_file_write
 * PUBLIC: __P((DBC *, DB_FH *, DBT *,
 * PUBLIC:    off_t, db_seq_t, off_t *, u_int32_t));
 */
int
__blob_file_write(dbc, fhp, buf, offset, blob_id, file_size, flags)
	DBC *dbc;
	DB_FH *fhp;
	DBT *buf;
	off_t offset;
	db_seq_t blob_id;
	off_t *file_size;
	u_int32_t flags;
{
	ENV *env;
	off_t size, write_offset;
	char *dirname, *name;
	int ret, blob_lg;
	size_t data_size;
	void *ptr;

	env = dbc->env;
	dirname = name = NULL;
	size = 0;
	write_offset = offset;
	DB_ASSERT(env, !DB_IS_READONLY(dbc->dbp));
	DB_ASSERT(env, fhp != NULL);

	/* File size is used to tell if the write is extending the file. */
	size = *file_size;

	if (DBENV_LOGGING(env)) {
		if ((ret = __log_get_config(
		    env->dbenv, DB_LOG_BLOB, &blob_lg)) != 0)
			goto err;
		if (blob_lg == 0 && !REP_ON(env))
			LF_SET(DB_FOP_PARTIAL_LOG);
		if (!LF_ISSET(DB_FOP_CREATE) && (size <= offset))
			LF_SET(DB_FOP_APPEND);
	}

	if ((ret = __blob_id_to_path(
	    env, dbc->dbp->blob_sub_dir, blob_id, &name)) != 0)
		goto err;

	if ((ret = __dbt_usercopy(env, buf)) != 0)
		goto err;

	/*
	 * If the write overwrites some of the file, and writes off the end
	 * of the file, break the write into two writes, one that overwrites
	 * data, and an append.  Otherwise if the write is aborted, the
	 * data written past the end of the file will not be erased.
	 */
	if (offset < size && (offset + buf->size) > size) {
		ptr = buf->data;
		data_size = (size_t)(size - offset);
		if ((ret = __fop_write_file(env, dbc->txn, name, dirname,
		    DB_APP_BLOB, fhp, offset, ptr, data_size, flags)) != 0) {
			__db_errx(env, DB_STR_A("0235",
			    "Error writing blob file: %s.", "%s"), name);
				goto err;
		}
		LF_SET(DB_FOP_APPEND);
		ptr = (u_int8_t *)ptr + data_size;
		data_size = buf->size - data_size;
		write_offset = size;
	} else {
		if (!LF_ISSET(DB_FOP_CREATE) && (offset >= size))
			LF_SET(DB_FOP_APPEND);
		ptr = buf->data;
		data_size = buf->size;
	}

	if ((ret = __fop_write_file(env, dbc->txn, name, dirname,
	    DB_APP_BLOB, fhp, write_offset, ptr, data_size, flags)) != 0) {
		__db_errx(env, DB_STR_A("0236",
		    "Error writing blob file: %s.", "%s"), name);
		goto err;
	}

	if (LF_ISSET(DB_FOP_SYNC_WRITE))
		if ((ret = __os_fsync(env, fhp)) != 0)
			goto err;

	/* Update the size of the file. */
	if ((offset + (off_t)buf->size) > size)
		*file_size = offset + (off_t)buf->size;

err:	if (name != NULL)
		__os_free(env, name);

	return (ret);
}