2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
17
static u_int32_t fid_serial = SERIAL_INIT;
21
* Return a unique identifier for a file.
24
__os_fileid(dbenv, fname, unique_okay, fidp)
36
* The documentation for GetFileInformationByHandle() states that the
37
* inode-type numbers are not constant between processes. Actually,
38
* they are, they're the NTFS MFT indexes. So, this works on NTFS,
39
* but perhaps not on other platforms, and perhaps not over a network.
40
* Can't think of a better solution right now.
43
BY_HANDLE_FILE_INFORMATION fi;
46
DB_ASSERT(fname != NULL);
48
/* Clear the buffer. */
49
memset(fidp, 0, DB_FILE_ID_LEN);
52
* Initialize/increment the serial number we use to help avoid
53
* fileid collisions. Note that we don't bother with locking;
54
* it's unpleasant to do from down in here, and if we race on
55
* this no real harm will be done, since the finished fileid
56
* has so many other components.
58
* We increment by 100000 on each call as a simple way of
59
* randomizing; simply incrementing seems potentially less useful
60
* if pids are also simply incremented, since this is process-local
61
* and we may be one of a set of processes starting up. 100000
62
* pushes us out of pid space on most platforms, and has few
63
* interesting properties in base 2.
65
if (fid_serial == SERIAL_INIT)
71
* First we open the file, because we're not given a handle to it.
72
* If we can't open it, we're in trouble.
74
if ((ret = __os_open(dbenv, fname, DB_OSO_RDONLY, _S_IREAD, &fh)) != 0)
77
/* File open, get its info */
78
if ((retval = GetFileInformationByHandle(fh.handle, &fi)) == FALSE)
79
ret = __os_win32_errno();
80
__os_closehandle(dbenv, &fh);
86
* We want the three 32-bit words which tell us the volume ID and
87
* the file ID. We make a crude attempt to copy the bytes over to
90
* We don't worry about byte sexing or the actual variable sizes.
92
* When this routine is called from the DB access methods, it's only
93
* called once -- whatever ID is generated when a database is created
94
* is stored in the database file's metadata, and that is what is
95
* saved in the mpool region's information to uniquely identify the
98
* When called from the mpool layer this routine will be called each
99
* time a new thread of control wants to share the file, which makes
100
* things tougher. As far as byte sexing goes, since the mpool region
101
* lives on a single host, there's no issue of that -- the entire
102
* region is byte sex dependent. As far as variable sizes go, we make
103
* the simplifying assumption that 32-bit and 64-bit processes will
104
* get the same 32-bit values if we truncate any returned 64-bit value
107
tmp = (u_int32_t)fi.nFileIndexLow;
108
for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
110
tmp = (u_int32_t)fi.nFileIndexHigh;
111
for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
116
* Use the system time to try to get a unique value
117
* within this process. A millisecond counter
118
* overflows 32 bits in about 49 days. So we use 8
119
* bytes, and don't bother with the volume ID, which
120
* is not very useful for our purposes.
125
tmp = (st.wYear - 1900) * 12 + (st.wMonth - 1);
126
for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
128
tmp = ((((st.wDay - 1) * 24 + st.wHour) * 60 +
129
st.wMinute) * 60 + st.wSecond) * 1000 +
131
for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
133
for (p = (u_int8_t *)&fid_serial, i = sizeof(u_int32_t);
137
tmp = (u_int32_t)fi.dwVolumeSerialNumber;
138
for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)