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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
Unix SMB/CIFS implementation.
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
directory listing functions for posix backend
*/
#include "includes.h"
#include "vfs_posix.h"
struct pvfs_dir {
struct pvfs_state *pvfs;
BOOL no_wildcard;
char *last_name;
const char *pattern;
off_t offset;
DIR *dir;
const char *unix_path;
BOOL end_of_search;
};
/*
a special directory listing case where the pattern has no wildcard. We can just do a single stat()
thus avoiding the more expensive directory scan
*/
static NTSTATUS pvfs_list_no_wildcard(struct pvfs_state *pvfs, struct pvfs_filename *name,
const char *pattern, struct pvfs_dir *dir)
{
if (!name->exists) {
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
dir->pvfs = pvfs;
dir->no_wildcard = True;
dir->end_of_search = False;
dir->unix_path = talloc_strdup(dir, name->full_name);
if (!dir->unix_path) {
return NT_STATUS_NO_MEMORY;
}
dir->last_name = talloc_strdup(dir, pattern);
if (!dir->last_name) {
return NT_STATUS_NO_MEMORY;
}
dir->dir = NULL;
dir->offset = 0;
dir->pattern = NULL;
return NT_STATUS_OK;
}
/*
destroy an open search
*/
static int pvfs_dirlist_destructor(void *ptr)
{
struct pvfs_dir *dir = ptr;
if (dir->dir) closedir(dir->dir);
return 0;
}
/*
start to read a directory
if the pattern matches no files then we return NT_STATUS_OK, with dir->count = 0
*/
NTSTATUS pvfs_list_start(struct pvfs_state *pvfs, struct pvfs_filename *name,
TALLOC_CTX *mem_ctx, struct pvfs_dir **dirp)
{
char *pattern;
struct pvfs_dir *dir;
(*dirp) = talloc_p(mem_ctx, struct pvfs_dir);
if (*dirp == NULL) {
return NT_STATUS_NO_MEMORY;
}
dir = *dirp;
/* split the unix path into a directory + pattern */
pattern = strrchr(name->full_name, '/');
if (!pattern) {
/* this should not happen, as pvfs_unix_path is supposed to
return an absolute path */
return NT_STATUS_UNSUCCESSFUL;
}
*pattern++ = 0;
if (!name->has_wildcard) {
return pvfs_list_no_wildcard(pvfs, name, pattern, dir);
}
dir->unix_path = talloc_strdup(dir, name->full_name);
if (!dir->unix_path) {
return NT_STATUS_NO_MEMORY;
}
dir->pattern = talloc_strdup(dir, pattern);
if (dir->pattern == NULL) {
return NT_STATUS_NO_MEMORY;
}
dir->dir = opendir(name->full_name);
if (!dir->dir) {
return pvfs_map_errno(pvfs, errno);
}
dir->pvfs = pvfs;
dir->no_wildcard = False;
dir->last_name = NULL;
dir->end_of_search = False;
dir->offset = 0;
talloc_set_destructor(dir, pvfs_dirlist_destructor);
return NT_STATUS_OK;
}
/*
return the next entry
*/
const char *pvfs_list_next(struct pvfs_dir *dir, uint_t *ofs)
{
struct dirent *de;
/* non-wildcard searches are easy */
if (dir->no_wildcard) {
dir->end_of_search = True;
if (*ofs != 0) return NULL;
(*ofs)++;
return dir->last_name;
}
if (*ofs != dir->offset) {
seekdir(dir->dir, *ofs);
dir->offset = *ofs;
}
while ((de = readdir(dir->dir))) {
const char *dname = de->d_name;
if (ms_fnmatch(dir->pattern, dname,
dir->pvfs->tcon->smb_conn->negotiate.protocol) != 0) {
char *short_name = pvfs_short_name_component(dir->pvfs, dname);
if (short_name == NULL ||
ms_fnmatch(dir->pattern, short_name,
dir->pvfs->tcon->smb_conn->negotiate.protocol) != 0) {
talloc_free(short_name);
continue;
}
talloc_free(short_name);
}
dir->offset = telldir(dir->dir);
(*ofs) = dir->offset;
if (dir->last_name) talloc_free(dir->last_name);
dir->last_name = talloc_strdup(dir, de->d_name);
return dir->last_name;
}
if (dir->last_name) talloc_free(dir->last_name);
dir->last_name = NULL;
dir->end_of_search = True;
pvfs_list_hibernate(dir);
return NULL;
}
/*
put the directory to sleep. Used between search calls to give the
right directory change semantics
*/
void pvfs_list_hibernate(struct pvfs_dir *dir)
{
if (dir->dir) {
closedir(dir->dir);
dir->dir = NULL;
}
if (!dir->no_wildcard && dir->last_name) {
talloc_free(dir->last_name);
dir->last_name = NULL;
}
}
/*
wake up the directory search
*/
NTSTATUS pvfs_list_wakeup(struct pvfs_dir *dir, uint_t *ofs)
{
if (dir->no_wildcard ||
dir->dir != NULL) {
return NT_STATUS_OK;
}
dir->dir = opendir(dir->unix_path);
if (dir->dir == NULL) {
dir->end_of_search = True;
return pvfs_map_errno(dir->pvfs, errno);
}
seekdir(dir->dir, *ofs);
dir->offset = telldir(dir->dir);
if (dir->offset != *ofs) {
DEBUG(0,("pvfs_list_wakeup: search offset changed %u -> %u\n",
*ofs, (unsigned)dir->offset));
}
return NT_STATUS_OK;
}
/*
return unix directory of an open search
*/
const char *pvfs_list_unix_path(struct pvfs_dir *dir)
{
return dir->unix_path;
}
/*
return True if end of search has been reached
*/
BOOL pvfs_list_eos(struct pvfs_dir *dir, uint_t ofs)
{
return dir->end_of_search;
}
/*
seek to the given name
*/
NTSTATUS pvfs_list_seek(struct pvfs_dir *dir, const char *name, uint_t *ofs)
{
struct dirent *de;
NTSTATUS status;
status = pvfs_list_wakeup(dir, ofs);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
if (dir->last_name &&
StrCaseCmp(name, dir->last_name) == 0) {
*ofs = dir->offset;
return NT_STATUS_OK;
}
rewinddir(dir->dir);
while ((de = readdir(dir->dir))) {
if (StrCaseCmp(name, de->d_name) == 0) {
dir->offset = telldir(dir->dir);
*ofs = dir->offset;
if (dir->last_name) talloc_free(dir->last_name);
dir->last_name = talloc_strdup(dir, de->d_name);
return NT_STATUS_OK;
}
}
dir->end_of_search = True;
pvfs_list_hibernate(dir);
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
|