xref: /onnv-gate/usr/src/cmd/sendmail/db/os/os_fid.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*0Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #include "config.h"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #ifndef lint
11*0Sstevel@tonic-gate static const char sccsid[] = "@(#)os_fid.c	10.12 (Sleepycat) 7/21/98";
12*0Sstevel@tonic-gate #endif /* not lint */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
15*0Sstevel@tonic-gate #include <sys/types.h>
16*0Sstevel@tonic-gate #include <sys/stat.h>
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate #include <errno.h>
19*0Sstevel@tonic-gate #include <string.h>
20*0Sstevel@tonic-gate #include <time.h>
21*0Sstevel@tonic-gate #endif
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #include "db_int.h"
24*0Sstevel@tonic-gate #include "common_ext.h"
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * __os_fileid --
28*0Sstevel@tonic-gate  *	Return a unique identifier for a file.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * PUBLIC: int __os_fileid __P((DB_ENV *, const char *, int, u_int8_t *));
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate int
__os_fileid(dbenv,fname,timestamp,fidp)33*0Sstevel@tonic-gate __os_fileid(dbenv, fname, timestamp, fidp)
34*0Sstevel@tonic-gate 	DB_ENV *dbenv;
35*0Sstevel@tonic-gate 	const char *fname;
36*0Sstevel@tonic-gate 	int timestamp;
37*0Sstevel@tonic-gate 	u_int8_t *fidp;
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate 	struct stat sb;
40*0Sstevel@tonic-gate 	size_t i;
41*0Sstevel@tonic-gate 	time_t now;
42*0Sstevel@tonic-gate 	u_int8_t *p;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	/* Clear the buffer. */
45*0Sstevel@tonic-gate 	memset(fidp, 0, DB_FILE_ID_LEN);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	/* Check for the unthinkable. */
48*0Sstevel@tonic-gate 	if (sizeof(sb.st_ino) +
49*0Sstevel@tonic-gate 	    sizeof(sb.st_dev) + sizeof(time_t) > DB_FILE_ID_LEN)
50*0Sstevel@tonic-gate 		return (EINVAL);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	/* On UNIX, use a dev/inode pair. */
53*0Sstevel@tonic-gate 	if (stat(fname, &sb)) {
54*0Sstevel@tonic-gate 		__db_err(dbenv, "%s: %s", fname, strerror(errno));
55*0Sstevel@tonic-gate 		return (errno);
56*0Sstevel@tonic-gate 	}
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	/*
59*0Sstevel@tonic-gate 	 * Use the inode first and in reverse order, hopefully putting the
60*0Sstevel@tonic-gate 	 * distinguishing information early in the string.
61*0Sstevel@tonic-gate 	 */
62*0Sstevel@tonic-gate 	for (p = (u_int8_t *)&sb.st_ino +
63*0Sstevel@tonic-gate 	    sizeof(sb.st_ino), i = 0; i < sizeof(sb.st_ino); ++i)
64*0Sstevel@tonic-gate 		*fidp++ = *--p;
65*0Sstevel@tonic-gate 	for (p = (u_int8_t *)&sb.st_dev +
66*0Sstevel@tonic-gate 	    sizeof(sb.st_dev), i = 0; i < sizeof(sb.st_dev); ++i)
67*0Sstevel@tonic-gate 		*fidp++ = *--p;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (timestamp) {
70*0Sstevel@tonic-gate 		(void)time(&now);
71*0Sstevel@tonic-gate 		for (p = (u_int8_t *)&now +
72*0Sstevel@tonic-gate 		    sizeof(now), i = 0; i < sizeof(now); ++i)
73*0Sstevel@tonic-gate 			*fidp++ = *--p;
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	return (0);
76*0Sstevel@tonic-gate }
77