Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

env_file.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: env_file.c,v 12.3 2005/06/16 20:21:56 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <string.h>
00016 #endif
00017 
00018 #include "db_int.h"
00019 
00020 /*
00021  * __db_file_extend --
00022  *      Initialize a regular file by writing the last page of the file.
00023  *
00024  * PUBLIC: int __db_file_extend __P((DB_ENV *, DB_FH *, size_t));
00025  */
00026 int
00027 __db_file_extend(dbenv, fhp, size)
00028         DB_ENV *dbenv;
00029         DB_FH *fhp;
00030         size_t size;
00031 {
00032         db_pgno_t pages;
00033         size_t nw;
00034         u_int32_t relative;
00035         int ret;
00036         char buf[8 * 1024];
00037 
00038         /*
00039          * Extend the file by writing the last page.  If the region is >4Gb,
00040          * increment may be larger than the maximum possible seek "relative"
00041          * argument, as it's an unsigned 32-bit value.  Break the offset into
00042          * pages of 1MB each so we don't overflow -- (2^20 + 2^32 is bigger
00043          * than any memory I expect to see for awhile).
00044          */
00045         memset(buf, 0, sizeof(buf));
00046 
00047         if ((ret = __os_seek(dbenv, fhp, 0, 0, 0, 0, DB_OS_SEEK_END)) != 0)
00048                 return (ret);
00049         pages = (db_pgno_t)((size - sizeof(buf)) / MEGABYTE);
00050         relative = (u_int32_t)((size - sizeof(buf)) % MEGABYTE);
00051         if ((ret = __os_seek(dbenv,
00052             fhp, MEGABYTE, pages, relative, 0, DB_OS_SEEK_CUR)) != 0)
00053                 return (ret);
00054         if ((ret = __os_write(dbenv, fhp, buf, sizeof(buf), &nw)) != 0)
00055                 return (ret);
00056 
00057         return (0);
00058 }
00059 
00060 /*
00061  * __db_file_multi_write  --
00062  *      Overwrite a file with multiple passes to corrupt the data.
00063  *
00064  * PUBLIC: int __db_file_multi_write __P((DB_ENV *, const char *));
00065  */
00066 int
00067 __db_file_multi_write(dbenv, path)
00068         DB_ENV *dbenv;
00069         const char *path;
00070 {
00071         DB_FH *fhp;
00072         u_int32_t mbytes, bytes;
00073         int ret;
00074 
00075         if ((ret = __os_open(dbenv, path, DB_OSO_REGION, 0, &fhp)) == 0 &&
00076             (ret = __os_ioinfo(dbenv, path, fhp, &mbytes, &bytes, NULL)) == 0) {
00077                 /*
00078                  * !!!
00079                  * Overwrite a regular file with alternating 0xff, 0x00 and 0xff
00080                  * byte patterns.  Implies a fixed-block filesystem, journaling
00081                  * or logging filesystems will require operating system support.
00082                  */
00083                 if ((ret = __db_file_write(
00084                     dbenv, path, fhp, mbytes, bytes, 255)) != 0)
00085                         goto err;
00086                 if ((ret = __db_file_write(
00087                     dbenv, path, fhp, mbytes, bytes, 0)) != 0)
00088                         goto err;
00089                 if ((ret = __db_file_write(
00090                     dbenv, path, fhp, mbytes, bytes, 255)) != 0)
00091                         goto err;
00092         } else
00093                 __db_err(dbenv, "%s: %s", path, db_strerror(ret));
00094 
00095 err:    if (fhp != NULL)
00096                 (void)__os_closehandle(dbenv, fhp);
00097         return (ret);
00098 }
00099 
00100 /*
00101  * __db_file_write --
00102  *      A single pass over the file, writing the specified byte pattern.
00103  *
00104  * PUBLIC: int __db_file_write __P((DB_ENV *,
00105  * PUBLIC:     const char *, DB_FH *, u_int32_t, u_int32_t, int));
00106  */
00107 int
00108 __db_file_write(dbenv, path, fhp, mbytes, bytes, pattern)
00109         DB_ENV *dbenv;
00110         const char *path;
00111         DB_FH *fhp;
00112         int pattern;
00113         u_int32_t mbytes, bytes;
00114 {
00115         size_t len, nw;
00116         int i, ret;
00117         char buf[32 * 1024];
00118 
00119         if ((ret = __os_seek(dbenv, fhp, 0, 0, 0, 0, DB_OS_SEEK_SET)) != 0)
00120                 goto err;
00121 
00122         memset(buf, pattern, sizeof(buf));
00123 
00124         for (; mbytes > 0; --mbytes)
00125                 for (i = MEGABYTE / sizeof(buf); i > 0; --i)
00126                         if ((ret =
00127                             __os_write(dbenv, fhp, buf, sizeof(buf), &nw)) != 0)
00128                                 goto err;
00129         for (; bytes > 0; bytes -= (u_int32_t)len) {
00130                 len = bytes < sizeof(buf) ? bytes : sizeof(buf);
00131                 if ((ret = __os_write(dbenv, fhp, buf, len, &nw)) != 0)
00132                         goto err;
00133         }
00134 
00135         if ((ret = __os_fsync(dbenv, fhp)) != 0)
00136 err:            __db_err(dbenv, "%s: %s", path, db_strerror(ret));
00137 
00138         return (ret);
00139 }

Generated on Sun Dec 25 12:14:24 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2