forked from unfs3/unfs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaddir.c
197 lines (166 loc) · 4.88 KB
/
readdir.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* UNFS3 readdir routine
* (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 <time.h>
#include <unistd.h>
#include "nfs.h"
#include "mount.h"
#include "fh.h"
#include "readdir.h"
#include "backend.h"
#include "Config/exports.h"
#include "daemon.h"
#include "error.h"
/*
* maximum number of entries in readdir results
*
* this is 4096 / 28 (the minimum size of an entry3)
*/
#define MAX_ENTRIES 143
/*
* static READDIR3resok size with XDR overhead
*
* 88 bytes attributes, 8 bytes verifier, 4 bytes value_follows for
* first entry, 4 bytes eof flag
*/
#define RESOK_SIZE 104
/*
* static entry3 size with XDR overhead
*
* 8 bytes fileid, 4 bytes name length, 8 bytes cookie, 4 byte value_follows
*/
#define ENTRY_SIZE 24
/*
* size of a name with XDR overhead
*
* XDR pads to multiple of 4 bytes
*/
#define NAME_SIZE(x) (((strlen((x))+3)/4)*4)
/*
* perform a READDIR operation
*
* fh_decomp must be called directly before to fill the stat cache
*/
READDIR3res read_dir(const char *path, cookie3 cookie, cookieverf3 verf,
count3 count)
{
READDIR3res result;
READDIR3resok resok;
cookie3 upper;
static entry3 entry[MAX_ENTRIES];
backend_statstruct buf;
int res;
backend_dirstream *search;
struct dirent *this;
count3 i, real_count;
static char obj[NFS_MAXPATHLEN * MAX_ENTRIES];
char scratch[NFS_MAXPATHLEN];
/* check upper part of cookie */
upper = cookie & 0xFFFFFFFF00000000ULL;
if (cookie != 0 && upper != rcookie) {
/* ignore cookie if unexpected so we restart from the beginning */
cookie = 0;
}
cookie &= 0xFFFFFFFFULL;
/* we refuse to return more than 4k from READDIR */
if (count > 4096)
count = 4096;
/* account for size of information heading resok structure */
real_count = RESOK_SIZE;
/* We are always returning zero as a cookie verifier. One reason for this
is that stat() on Windows seems to return cached st_mtime values,
which gives spurious NFS3ERR_BAD_COOKIEs. Btw, here's what Peter
Staubach has to say about cookie verifiers:
"From my viewpoint, the cookieverifier was a failed experiment in NFS
Version 3. The semantics were never well understood nor supported by
many local file systems. The Solaris NFS server always returns zeros
in the cookieverifier field." */
memset(verf, 0, NFS3_COOKIEVERFSIZE);
search = backend_opendir(path);
if (!search) {
if ((exports_opts & OPT_REMOVABLE) && (export_point(path))) {
/* Removable media export point; probably no media inserted.
Return empty directory. */
memset(resok.cookieverf, 0, NFS3_COOKIEVERFSIZE);
resok.reply.entries = NULL;
resok.reply.eof = TRUE;
result.status = NFS3_OK;
result.READDIR3res_u.resok = resok;
return result;
} else {
result.status = readdir_err();
return result;
}
}
this = backend_readdir(search);
/* We cannot use telldir()/seekdir(), since the value from telldir() is
not valid after closedir(). */
for (i = 0; i < cookie; i++)
if (this)
this = backend_readdir(search);
i = 0;
entry[0].name = NULL;
while (this && real_count < count && i < MAX_ENTRIES) {
if (i > 0)
entry[i - 1].nextentry = &entry[i];
if (strlen(path) + strlen(this->d_name) + 1 < NFS_MAXPATHLEN) {
if (strcmp(path, "/") == 0)
sprintf(scratch, "/%s", this->d_name);
else
sprintf(scratch, "%s/%s", path, this->d_name);
res = backend_lstat(scratch, &buf);
if (res == -1) {
result.status = readdir_err();
backend_closedir(search);
return result;
}
strcpy(&obj[i * NFS_MAXPATHLEN], this->d_name);
#if defined(WIN32) || defined(AFS_SUPPORT)
/* See comment in attr.c:get_post_buf */
entry[i].fileid = (buf.st_ino >> 32) ^ (buf.st_ino & 0xffffffff);
#else
entry[i].fileid = buf.st_ino;
#endif
entry[i].name = &obj[i * NFS_MAXPATHLEN];
entry[i].cookie = (cookie + 1 + i) | rcookie;
entry[i].nextentry = NULL;
/* account for entry size */
real_count += ENTRY_SIZE + NAME_SIZE(this->d_name);
/* whoops, overflowed the maximum size */
if (real_count > count && i > 0)
entry[i - 1].nextentry = NULL;
else {
/* advance to next entry */
this = backend_readdir(search);
}
i++;
} else {
result.status = NFS3ERR_IO;
backend_closedir(search);
return result;
}
}
backend_closedir(search);
if (entry[0].name)
resok.reply.entries = &entry[0];
else
resok.reply.entries = NULL;
if (this)
resok.reply.eof = FALSE;
else
resok.reply.eof = TRUE;
memcpy(resok.cookieverf, verf, NFS3_COOKIEVERFSIZE);
result.status = NFS3_OK;
result.READDIR3res_u.resok = resok;
return result;
}