File: os_finit.c

package info (click to toggle)
htdig 1%3A3.2.0b6-16
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 15,624 kB
  • ctags: 9,630
  • sloc: ansic: 49,630; cpp: 46,470; sh: 17,400; xml: 4,180; perl: 2,543; makefile: 886; php: 79; asm: 14
file content (109 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download | duplicates (9)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1998, 1999
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
static const char sccsid[] = "@(#)os_finit.c	11.3 (Sleepycat) 9/22/99";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <errno.h>
#include <string.h>
#endif

#include "db_int.h"

/*
 * CDB___os_finit --
 *	Initialize a regular file, optionally zero-filling it as well.
 *
 * PUBLIC: int CDB___os_finit __P((DB_FH *, size_t, int));
 */
int
CDB___os_finit(fhp, size, zerofill)
	DB_FH *fhp;
	size_t size;
	int zerofill;
{
	db_pgno_t pages;
	size_t i;
	ssize_t nw;
	u_int32_t relative;
	int ret;
	char buf[OS_VMPAGESIZE];

	/* Write nuls to the new bytes. */
	memset(buf, 0, sizeof(buf));

	/*
	 * Extend the region by writing the last page.  If the region is >4Gb,
	 * increment may be larger than the maximum possible seek "relative"
	 * argument, as it's an unsigned 32-bit value.  Break the offset into
	 * pages of 1MB each so that we don't overflow (2^20 + 2^32 is bigger
	 * than any memory I expect to see for awhile).
	 */
	if ((ret = CDB___os_seek(fhp, 0, 0, 0, 0, DB_OS_SEEK_END)) != 0)
		return (ret);
	pages = (size - OS_VMPAGESIZE) / MEGABYTE;
	relative = (size - OS_VMPAGESIZE) % MEGABYTE;
	if ((ret =
	    CDB___os_seek(fhp, MEGABYTE, pages, relative, 0, DB_OS_SEEK_CUR)) != 0)
		return (ret);
	if ((ret = CDB___os_write(fhp, buf, sizeof(buf), &nw)) != 0)
		return (ret);
	if (nw != sizeof(buf))
		return (EIO);

	/*
	 * We may want to guarantee that there is enough disk space for the
	 * file, so we also write a byte to each page.  We write the byte
	 * because reading it is insufficient on systems smart enough not to
	 * instantiate disk pages to satisfy a read (e.g., Solaris).
	 */
	if (zerofill) {
		pages = size / MEGABYTE;
		relative = size % MEGABYTE;
		if ((ret = CDB___os_seek(fhp,
		    MEGABYTE, pages, relative, 1, DB_OS_SEEK_END)) != 0)
			return (ret);

		/* Write a byte to each page. */
		for (i = 0; i < size; i += OS_VMPAGESIZE) {
			if ((ret = CDB___os_write(fhp, buf, 1, &nw)) != 0)
				return (ret);
			if (nw != 1)
				return (EIO);
			if ((ret = CDB___os_seek(fhp,
			    0, 0, OS_VMPAGESIZE - 1, 0, DB_OS_SEEK_CUR)) != 0)
				return (ret);
		}
	}
	return (0);
}

/*
 * CDB___os_fpinit --
 *	Initialize a page in a regular file.
 *
 * PUBLIC: int CDB___os_fpinit __P((DB_FH *, db_pgno_t, int, int));
 */
int
CDB___os_fpinit(fhp, pgno, pagecount, pagesize)
	DB_FH *fhp;
	db_pgno_t pgno;
	int pagecount, pagesize;
{
	COMPQUIET(fhp, NULL);
	COMPQUIET(pgno, 0);
	COMPQUIET(pagecount, 0);
	COMPQUIET(pagesize, 0);

	return (0);
}