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
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.
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]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * Subdirectory detection: needed by exportfs and rpc.mountd.
44 * The above programs call issubdir() frequently, so we make
45 * it fast by caching the results of stat().
47 #include <sys/types.h>
48 #include <sys/param.h>
52 #define MAXSTATS MAXPATHLEN/2 /* maximum number of stat()'s to save */
54 #define inoeq(ino1, ino2) ((ino1) == (ino2))
55 #define deveq(dev1, dev2) ((dev1) == (dev2))
58 * dir1 is a subdirectory of dir2 within the same filesystem if
59 * (a) dir1 is identical to dir2
60 * (b) dir1's parent is dir2
68 struct stat parent_st
;
72 static dev_t child_dev
;
73 static dev_t child_rdev
;
74 static ino_t child_ino
[MAXSTATS
];
76 static char childdir
[MAXPATHLEN
];
77 static char child_fstype
[_ST_FSTYPSZ
];
80 * Get parent directory info
82 if (stat(dir2
, &parent_st
) < 0) {
86 if (strcmp(childdir
, dir1
) != 0) {
88 * Not in cache: get child directory info
90 p
= strcpy(childdir
, dir1
) + strlen(dir1
);
94 if (stat(childdir
, &st
) < 0) {
95 childdir
[0] = 0; /* invalidate cache */
99 child_dev
= st
.st_dev
;
100 child_rdev
= st
.st_rdev
;
101 (void) strncpy(child_fstype
, st
.st_fstype
,
102 sizeof (child_fstype
));
105 (child_dev
!= st
.st_dev
||
106 inoeq(child_ino
[index
- 1], st
.st_ino
))) {
112 child_ino
[index
++] = st
.st_ino
;
113 if (S_ISDIR(st
.st_mode
)) {
114 p
= strcpy(p
, "/..") + 3;
116 p
= strrchr(childdir
, '/');
118 p
= strcpy(childdir
, ".") + 1;
120 while (((p
- 1) > childdir
) &&
129 (void) strcpy(childdir
, dir1
);
135 if (!deveq(parent_st
.st_dev
, child_dev
)) {
140 * Check rdev also in case of lofs
142 if (((strcmp(parent_st
.st_fstype
, "lofs") == 0)) &&
143 (strcmp(child_fstype
, "lofs") == 0)) {
144 if (!deveq(parent_st
.st_rdev
, child_rdev
)) {
149 for (index
= 0; index
< valid
; index
++) {
150 if (inoeq(child_ino
[index
], parent_st
.st_ino
)) {