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