1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2004,2008 Oracle.  All rights reserved.
5 *
6 * $Id: os_truncate.c,v 12.13 2008/02/18 19:34:21 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * __os_truncate --
15 *	Truncate the file.
16 *
17 * PUBLIC: int __os_truncate __P((ENV *, DB_FH *, db_pgno_t, u_int32_t));
18 */
19int
20__os_truncate(env, fhp, pgno, pgsize)
21	ENV *env;
22	DB_FH *fhp;
23	db_pgno_t pgno;
24	u_int32_t pgsize;
25{
26	DB_ENV *dbenv;
27	off_t offset;
28	int ret;
29
30	dbenv = env == NULL ? NULL : env->dbenv;
31
32	/*
33	 * Truncate a file so that "pgno" is discarded from the end of the
34	 * file.
35	 */
36	offset = (off_t)pgsize * pgno;
37
38	if (dbenv != NULL &&
39	    FLD_ISSET(dbenv->verbose, DB_VERB_FILEOPS | DB_VERB_FILEOPS_ALL))
40		__db_msg(env,
41		    "fileops: truncate %s to %lu", fhp->name, (u_long)offset);
42
43	LAST_PANIC_CHECK_BEFORE_IO(env);
44
45	if (DB_GLOBAL(j_ftruncate) != NULL)
46		ret = DB_GLOBAL(j_ftruncate)(fhp->fd, offset);
47	else {
48#ifdef HAVE_FTRUNCATE
49		RETRY_CHK((ftruncate(fhp->fd, offset)), ret);
50#else
51		ret = DB_OPNOTSUP;
52#endif
53	}
54
55	if (ret != 0) {
56		__db_syserr(env, ret, "ftruncate: %lu", (u_long)offset);
57		ret = __os_posix_err(ret);
58	}
59
60	return (ret);
61}
62