Update.
[glibc.git] / db2 / os / os_fid.c
blobcf48c01bd8bb261b7861415cd6c98a5bc2ccf5c1
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)os_fid.c 10.11 (Sleepycat) 4/26/98";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 #include <sys/stat.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <time.h>
21 #endif
23 #include "db_int.h"
24 #include "common_ext.h"
27 * __db_fileid --
28 * Return a unique identifier for a file.
30 * PUBLIC: int __db_fileid __P((DB_ENV *, const char *, int, u_int8_t *));
32 int
33 __db_fileid(dbenv, fname, timestamp, fidp)
34 DB_ENV *dbenv;
35 const char *fname;
36 int timestamp;
37 u_int8_t *fidp;
39 struct stat sb;
40 size_t i;
41 time_t now;
42 u_int8_t *p;
44 /* Clear the buffer. */
45 memset(fidp, 0, DB_FILE_ID_LEN);
47 /* Check for the unthinkable. */
48 if (sizeof(sb.st_ino) +
49 sizeof(sb.st_dev) + sizeof(time_t) > DB_FILE_ID_LEN)
50 return (EINVAL);
52 /* On UNIX, use a dev/inode pair. */
53 if (stat(fname, &sb)) {
54 __db_err(dbenv, "%s: %s", fname, strerror(errno));
55 return (errno);
59 * Use the inode first and in reverse order, hopefully putting the
60 * distinguishing information early in the string.
62 for (p = (u_int8_t *)&sb.st_ino +
63 sizeof(sb.st_ino), i = 0; i < sizeof(sb.st_ino); ++i)
64 *fidp++ = *--p;
65 for (p = (u_int8_t *)&sb.st_dev +
66 sizeof(sb.st_dev), i = 0; i < sizeof(sb.st_dev); ++i)
67 *fidp++ = *--p;
69 if (timestamp) {
70 (void)time(&now);
71 for (p = (u_int8_t *)&now +
72 sizeof(now), i = 0; i < sizeof(now); ++i)
73 *fidp++ = *--p;
75 return (0);