xref: /titanic_51/usr/src/cmd/sendmail/db/os/os_fid.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997, 1998
5  *	Sleepycat Software.  All rights reserved.
6  */
7 
8 #include "config.h"
9 
10 #ifndef lint
11 static const char sccsid[] = "@(#)os_fid.c	10.12 (Sleepycat) 7/21/98";
12 #endif /* not lint */
13 
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include <errno.h>
19 #include <string.h>
20 #include <time.h>
21 #endif
22 
23 #include "db_int.h"
24 #include "common_ext.h"
25 
26 /*
27  * __os_fileid --
28  *	Return a unique identifier for a file.
29  *
30  * PUBLIC: int __os_fileid __P((DB_ENV *, const char *, int, u_int8_t *));
31  */
32 int
33 __os_fileid(dbenv, fname, timestamp, fidp)
34 	DB_ENV *dbenv;
35 	const char *fname;
36 	int timestamp;
37 	u_int8_t *fidp;
38 {
39 	struct stat sb;
40 	size_t i;
41 	time_t now;
42 	u_int8_t *p;
43 
44 	/* Clear the buffer. */
45 	memset(fidp, 0, DB_FILE_ID_LEN);
46 
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);
51 
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);
56 	}
57 
58 	/*
59 	 * Use the inode first and in reverse order, hopefully putting the
60 	 * distinguishing information early in the string.
61 	 */
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;
68 
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;
74 	}
75 	return (0);
76 }
77