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

os_fid.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_fid.c,v 12.4 2005/10/11 18:17:00 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #include "db_int.h"
00013 
00014 #define SERIAL_INIT     0
00015 static u_int32_t fid_serial = SERIAL_INIT;
00016 
00017 /*
00018  * __os_fileid --
00019  *      Return a unique identifier for a file.
00020  */
00021 int
00022 __os_fileid(dbenv, fname, unique_okay, fidp)
00023         DB_ENV *dbenv;
00024         const char *fname;
00025         int unique_okay;
00026         u_int8_t *fidp;
00027 {
00028         db_threadid_t tid;
00029         pid_t pid;
00030         size_t i;
00031         u_int32_t tmp;
00032         u_int8_t *p;
00033         int ret;
00034 
00035         /*
00036          * The documentation for GetFileInformationByHandle() states that the
00037          * inode-type numbers are not constant between processes.  Actually,
00038          * they are, they're the NTFS MFT indexes.  So, this works on NTFS,
00039          * but perhaps not on other platforms, and perhaps not over a network.
00040          * Can't think of a better solution right now.
00041          */
00042         DB_FH *fhp;
00043         BY_HANDLE_FILE_INFORMATION fi;
00044         BOOL retval = FALSE;
00045 
00046         DB_ASSERT(fname != NULL);
00047 
00048         /* Clear the buffer. */
00049         memset(fidp, 0, DB_FILE_ID_LEN);
00050 
00051         /*
00052          * Initialize/increment the serial number we use to help avoid
00053          * fileid collisions.  Note that we don't bother with locking;
00054          * it's unpleasant to do from down in here, and if we race on
00055          * this no real harm will be done, since the finished fileid
00056          * has so many other components.
00057          *
00058          * We use the bottom 32-bits of the process ID, hoping they
00059          * are more random than the top 32-bits (should we be on a
00060          * machine with 64-bit process IDs).
00061          *
00062          * We increment by 100000 on each call as a simple way of
00063          * randomizing;  simply incrementing seems potentially less useful
00064          * if pids are also simply incremented, since this is process-local
00065          * and we may be one of a set of processes starting up.  100000
00066          * pushes us out of pid space on most platforms, and has few
00067          * interesting properties in base 2.
00068          */
00069         if (fid_serial == SERIAL_INIT) {
00070                 __os_id(dbenv, &pid, &tid);
00071                 fid_serial = pid;
00072         } else
00073                 fid_serial += 100000;
00074 
00075         /*
00076          * First we open the file, because we're not given a handle to it.
00077          * If we can't open it, we're in trouble.
00078          */
00079         if ((ret = __os_open(dbenv, fname, DB_OSO_RDONLY, _S_IREAD, &fhp)) != 0)
00080                 return (ret);
00081 
00082         /* File open, get its info */
00083         if ((retval = GetFileInformationByHandle(fhp->handle, &fi)) == FALSE)
00084                 ret = __os_get_errno();
00085         (void)__os_closehandle(dbenv, fhp);
00086 
00087         if (retval == FALSE)
00088                 return (ret);
00089 
00090         /*
00091          * We want the three 32-bit words which tell us the volume ID and
00092          * the file ID.  We make a crude attempt to copy the bytes over to
00093          * the callers buffer.
00094          *
00095          * We don't worry about byte sexing or the actual variable sizes.
00096          *
00097          * When this routine is called from the DB access methods, it's only
00098          * called once -- whatever ID is generated when a database is created
00099          * is stored in the database file's metadata, and that is what is
00100          * saved in the mpool region's information to uniquely identify the
00101          * file.
00102          *
00103          * When called from the mpool layer this routine will be called each
00104          * time a new thread of control wants to share the file, which makes
00105          * things tougher.  As far as byte sexing goes, since the mpool region
00106          * lives on a single host, there's no issue of that -- the entire
00107          * region is byte sex dependent.  As far as variable sizes go, we make
00108          * the simplifying assumption that 32-bit and 64-bit processes will
00109          * get the same 32-bit values if we truncate any returned 64-bit value
00110          * to a 32-bit value.
00111          */
00112         tmp = (u_int32_t)fi.nFileIndexLow;
00113         for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
00114                 *fidp++ = *p++;
00115         tmp = (u_int32_t)fi.nFileIndexHigh;
00116         for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
00117                 *fidp++ = *p++;
00118 
00119         if (unique_okay) {
00120                 /*
00121                  * Use the system time to try to get a unique value
00122                  * within this process.  A millisecond counter
00123                  * overflows 32 bits in about 49 days.  So we use 8
00124                  * bytes, and don't bother with the volume ID, which
00125                  * is not very useful for our purposes.
00126                  */
00127                 SYSTEMTIME st;
00128 
00129                 GetSystemTime(&st);
00130                 tmp = (st.wYear - 1900) * 12 + (st.wMonth - 1);
00131                 for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
00132                         *fidp++ = *p++;
00133                 tmp = ((((st.wDay - 1) * 24 + st.wHour) * 60 +
00134                         st.wMinute) * 60 + st.wSecond) * 1000 +
00135                         st.wMilliseconds;
00136                 for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
00137                         *fidp++ = *p++;
00138                 for (p = (u_int8_t *)&fid_serial, i = sizeof(u_int32_t);
00139                     i > 0; --i)
00140                         *fidp++ = *p++;
00141         } else {
00142                 tmp = (u_int32_t)fi.dwVolumeSerialNumber;
00143                 for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
00144                         *fidp++ = *p++;
00145         }
00146 
00147         return (0);
00148 }

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