-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlocate.c
171 lines (142 loc) · 3.3 KB
/
locate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* UNFS3 brute force file search
* (C) 2004, Pascal Schmidt
* see file LICENSE for license details
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <rpc/rpc.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#ifdef HAVE_SYS_MNTTAB_H
#include <sys/mnttab.h>
#endif
#include "nfs.h"
#include "fh.h"
#include "daemon.h"
/*
* these are the brute-force file searching routines that are used
* when both the filehandle cache and the hashed path inside the
* filehandle are unable to locate the file
*
* this can only happen if a file was rename(3)'d across directories
*
* these routines are slow, but better than returning ESTALE to
* clients
*/
#if HAVE_MNTENT_H == 1 || HAVE_SYS_MNTTAB_H == 1
/*
* locate file given prefix, device, and inode number
*/
static int locate_pfx(const char *pfx, uint32 dev, uint64 ino, char *result)
{
char path[NFS_MAXPATHLEN];
backend_dirstream *search;
struct dirent *ent;
backend_statstruct buf;
int res;
search = opendir(pfx);
if (!search)
return FALSE;
while ((ent = readdir(search))) {
if (strlen(pfx) + strlen(ent->d_name) + 2 >= NFS_MAXPATHLEN)
continue;
sprintf(path, "%s/%s", pfx, ent->d_name);
res = backend_lstat(path, &buf);
if (res != 0)
continue;
/* check for matching object */
if (buf.st_dev == dev && buf.st_ino == ino) {
strcpy(result, path);
st_cache = buf;
st_cache_valid = TRUE;
closedir(search);
return TRUE;
}
/* descend into directories with same dev */
if (buf.st_dev == dev && S_ISDIR(buf.st_mode) &&
strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
res = locate_pfx(path, dev, ino, result);
if (res == TRUE) {
closedir(search);
return TRUE;
}
}
}
closedir(search);
return FALSE;
}
#endif
/*
* locate file given device and inode number
*
* slow fallback in case other filehandle resolution functions fail
*/
char *locate_file(U(uint32 dev), U(uint64 ino))
{
#if HAVE_MNTENT_H == 1 || HAVE_SYS_MNTTAB_H == 1
static char path[NFS_MAXPATHLEN];
FILE *mtab;
struct stat buf;
int res;
#endif
#ifdef HAVE_MNTENT_H
struct mntent *ent;
#endif
#ifdef HAVE_SYS_MNTTAB_H
struct mnttab ent;
int found = FALSE;
#endif
if (!opt_brute_force)
return NULL;
#ifdef HAVE_MNTENT_H
mtab = setmntent("/etc/mtab", "r");
if (!mtab)
return NULL;
/*
* look for mtab entry with matching device
*/
while ((ent = getmntent(mtab))) {
res = lstat(ent->mnt_dir, &buf);
if (res == 0 && buf.st_dev == dev)
break;
}
endmntent(mtab);
/* found matching entry? */
if (ent) {
res = locate_pfx(ent->mnt_dir, dev, ino, path);
if (res == TRUE)
return path;
}
#endif
#ifdef HAVE_SYS_MNTTAB_H
mtab = fopen("/etc/mnttab", "r");
if (!mtab)
return NULL;
/*
* look for mnttab entry with matching device
*/
while (getmntent(mtab, &ent) == 0) {
res = lstat(ent.mnt_mountp, &buf);
if (res == 0 && buf.st_dev == dev) {
found = TRUE;
break;
}
}
fclose(mtab);
/* found matching entry? */
if (found) {
res = locate_pfx(ent.mnt_mountp, dev, ino, path);
if (res == TRUE)
return path;
}
#endif
return NULL;
}