1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 1992, 1993, 1994, 2000 by Sun Microsystems, Inc.
25 * All rights reserved.
26 */
27
28 #pragma ident "%Z%%M% %I% %E% SMI"
29
30 /*
31 * Caching stat function
32 */
33
34 #include <meta.h>
35
36 #define MD_NUM_STAT_HEAD 16
37
38 struct statcache {
39 struct statcache *sc_next;
40 struct stat sc_stat;
41 char *sc_filename;
42 };
43
44 static struct statcache *statcache_head[MD_NUM_STAT_HEAD] =
45 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
46
47 int
meta_stat(const char * filename,struct stat * sbp)48 meta_stat(const char *filename, struct stat *sbp)
49 {
50 struct statcache *scp;
51 int hash;
52 char *cp;
53
54 hash = 0;
55 for (cp = (char *)filename; *cp != 0; cp++)
56 hash += *cp;
57
58 hash &= 0xf;
59
60 for (scp = statcache_head[hash]; scp != NULL; scp = scp->sc_next)
61 if (strcmp(filename, scp->sc_filename) == 0)
62 break;
63 if (scp) {
64 (void) memcpy((caddr_t)sbp, (caddr_t)&scp->sc_stat,
65 sizeof (*sbp));
66 return (0);
67 }
68 if (stat(filename, sbp) != 0)
69 return (-1);
70
71 if (!S_ISBLK(sbp->st_mode) && !S_ISCHR(sbp->st_mode))
72 return (-1);
73
74 scp = (struct statcache *)malloc(sizeof (*scp));
75 if (scp != NULL) {
76 (void) memcpy((caddr_t)&scp->sc_stat, (caddr_t)sbp,
77 sizeof (*sbp));
78 scp->sc_filename = strdup(filename);
79 if (scp->sc_filename == NULL) {
80 free((char *)scp);
81 return (0);
82 }
83 scp->sc_next = statcache_head[hash];
84 statcache_head[hash] = scp;
85 }
86 return (0);
87 }
88
89 void
metaflushstatcache(void)90 metaflushstatcache(void)
91 {
92 struct statcache *p, *n;
93 int i;
94
95 for (i = 0; i < MD_NUM_STAT_HEAD; i++) {
96 for (p = statcache_head[i], n = NULL; p != NULL; p = n) {
97 n = p->sc_next;
98 Free(p->sc_filename);
99 Free(p);
100 }
101 statcache_head[i] = NULL;
102 }
103 }
104