first public release of samba4 code
[jelmer/samba4-debian.git] / source / libcli / clilist.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client directory list routines
4    Copyright (C) Andrew Tridgell 1994-2003
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 struct search_private {
25         file_info *dirlist;
26         TALLOC_CTX *mem_ctx;
27         int dirlist_len;
28         int ff_searchcount;  /* total received in 1 server trip */
29         int total_received;  /* total received all together */
30         int info_level;
31         DATA_BLOB status;       /* used for old-style search */
32 };
33
34
35 /****************************************************************************
36  Interpret a long filename structure.
37 ****************************************************************************/
38 static BOOL interpret_long_filename(int level,
39                                     union smb_search_data *info,
40                                     file_info *finfo)
41 {
42         file_info finfo2;
43
44         if (!finfo) finfo = &finfo2;
45         ZERO_STRUCTP(finfo);
46         
47         finfo->size = info->both_directory_info.size;
48         finfo->ctime = nt_time_to_unix(&info->both_directory_info.create_time);
49         finfo->atime = nt_time_to_unix(&info->both_directory_info.access_time);
50         finfo->mtime = nt_time_to_unix(&info->both_directory_info.write_time);
51         finfo->mode = info->both_directory_info.attrib; /* 32 bit->16 bit attrib */
52         if (info->both_directory_info.short_name.s) {
53                 strncpy(finfo->short_name, info->both_directory_info.short_name.s, 
54                         sizeof(finfo->short_name)-1);
55         }
56         finfo->name = info->both_directory_info.name.s;
57         return True;
58 }
59
60 /* callback function used for trans2 search */
61 static BOOL cli_list_new_callback(void *private, union smb_search_data *file)
62 {
63         struct search_private *state = (struct search_private*) private;
64         file_info *tdl;
65  
66         /* add file info to the dirlist pool */
67         tdl = talloc_realloc(state->mem_ctx, state->dirlist,
68                         state->dirlist_len + sizeof(struct file_info));
69
70         if (!tdl) {
71                 return False;
72         }
73         state->dirlist = tdl;
74         state->dirlist_len += sizeof(struct file_info);
75
76         interpret_long_filename(state->info_level, file, &state->dirlist[state->total_received]);
77
78         state->total_received++;
79         state->ff_searchcount++;
80         
81         return True;
82 }
83
84 int cli_list_new(struct cli_state *cli, const char *Mask, uint16 attribute, 
85                  void (*fn)(file_info *, const char *, void *), void *caller_state)
86 {
87         union smb_search_first first_parms;
88         union smb_search_next next_parms;
89         struct search_private state;  /* for callbacks */
90         int received = 0;
91         BOOL first = True;
92         int num_received = 0;
93         int max_matches = 512;
94         char *mask;
95         int ff_eos = 0, i, ff_searchcount;
96         int ff_dir_handle=0;
97         int level;
98
99         /* initialize state for search */
100         state.dirlist = NULL;
101         state.mem_ctx = talloc_init("cli_list_new");
102         state.dirlist_len = 0;
103         state.total_received = 0;
104         
105         mask = talloc_strdup(state.mem_ctx, Mask);
106
107         if (cli->transport->negotiate.capabilities & CAP_NT_SMBS) {
108                 level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
109         } else {
110                 level = RAW_SEARCH_STANDARD;
111         }
112
113         while (1) {
114                 state.ff_searchcount = 0;
115                 if (first) {
116                         NTSTATUS status;
117
118                         first_parms.t2ffirst.level = level;
119                         first_parms.t2ffirst.in.max_count = max_matches;
120                         first_parms.t2ffirst.in.search_attrib = attribute;
121                         first_parms.t2ffirst.in.pattern = mask;
122                         first_parms.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
123                         first_parms.t2ffirst.in.storage_type = 0;
124                         
125                         status = smb_raw_search_first(cli->tree, 
126                                                       state.mem_ctx, &first_parms,
127                                                       (void*)&state, cli_list_new_callback);
128                         if (!NT_STATUS_IS_OK(status)) {
129                                 talloc_destroy(state.mem_ctx);
130                                 return -1;
131                         }
132                 
133                         ff_dir_handle = first_parms.t2ffirst.out.handle;
134                         ff_searchcount = first_parms.t2ffirst.out.count;
135                         ff_eos = first_parms.t2ffirst.out.end_of_search;
136                         
137                         received = first_parms.t2ffirst.out.count;
138                         if (received <= 0) break;
139                         if (ff_eos) break;
140                         first = False;
141                 } else {
142                         NTSTATUS status;
143
144                         next_parms.t2fnext.level = level;
145                         next_parms.t2fnext.in.max_count = max_matches;
146                         next_parms.t2fnext.in.last_name = mask;
147                         next_parms.t2fnext.in.handle = ff_dir_handle;
148                         next_parms.t2fnext.in.resume_key = 0;
149                         next_parms.t2fnext.in.flags = FLAG_TRANS2_FIND_CONTINUE | FLAG_TRANS2_FIND_CLOSE_IF_END;
150                         
151                         status = smb_raw_search_next(cli->tree, 
152                                                      state.mem_ctx,
153                                                      &next_parms,
154                                                      (void*)&state, 
155                                                      cli_list_new_callback);
156                         
157                         if (!NT_STATUS_IS_OK(status)) {
158                                 return -1;
159                         }
160                         ff_searchcount = next_parms.t2fnext.out.count;
161                         ff_eos = next_parms.t2fnext.out.end_of_search;
162                         received = next_parms.t2fnext.out.count;
163                         if (received <= 0) break;
164                         if (ff_eos) break;
165                 }
166                 
167                 num_received += received;
168         }
169
170         for (i=0;i<state.total_received;i++) {
171                 fn(&state.dirlist[i], Mask, caller_state);
172         }
173
174         talloc_destroy(state.mem_ctx);
175
176         return state.total_received;
177 }
178
179 /****************************************************************************
180  Interpret a short filename structure.
181  The length of the structure is returned.
182 ****************************************************************************/
183 static BOOL interpret_short_filename(int level,
184                                 union smb_search_data *info,
185                                 file_info *finfo)
186 {
187         file_info finfo2;
188
189         if (!finfo) finfo = &finfo2;
190         ZERO_STRUCTP(finfo);
191         
192         finfo->ctime = info->search.write_time;
193         finfo->atime = info->search.write_time;
194         finfo->mtime = info->search.write_time;
195         finfo->size = info->search.size;
196         finfo->mode = info->search.attrib;
197         finfo->name = info->search.name;
198         return True;
199 }
200
201 /* callback function used for smb_search */
202 static BOOL cli_list_old_callback(void *private, union smb_search_data *file)
203 {
204         struct search_private *state = (struct search_private*) private;
205         file_info *tdl;
206         
207         /* add file info to the dirlist pool */
208         tdl = talloc_realloc(state->mem_ctx, state->dirlist,
209                         state->dirlist_len + sizeof(struct file_info));
210
211         if (!tdl) {
212                 return False;
213         }
214         state->dirlist = tdl;
215         state->dirlist_len += sizeof(struct file_info);
216
217         interpret_short_filename(state->info_level, file, &state->dirlist[state->total_received]);
218
219         state->total_received++;
220         state->ff_searchcount++;
221         state->status = file->search.search_id; /* return resume info */
222         
223         return True;
224 }
225
226 int cli_list_old(struct cli_state *cli, const char *Mask, uint16 attribute, 
227                  void (*fn)(file_info *, const char *, void *), void *caller_state)
228 {
229         union smb_search_first first_parms;
230         union smb_search_next next_parms;
231         struct search_private state;  /* for callbacks */
232         const int num_asked = 500;
233         int received = 0;
234         BOOL first = True;
235         int num_received = 0;
236         char *mask;
237         int i;
238
239         /* initialize state for search */
240         state.dirlist = NULL;
241         state.mem_ctx = talloc_init("cli_list_old");
242         state.dirlist_len = 0;
243         state.total_received = 0;
244         
245         mask = talloc_strdup(state.mem_ctx, Mask);
246   
247         while (1) {
248                 state.ff_searchcount = 0;
249                 if (first) {
250                         NTSTATUS status;
251
252                         first_parms.search_first.level = RAW_SEARCH_SEARCH;
253                         first_parms.search_first.in.max_count = num_asked;
254                         first_parms.search_first.in.search_attrib = attribute;
255                         first_parms.search_first.in.pattern = mask;
256                         
257                         status = smb_raw_search_first(cli->tree, state.mem_ctx, 
258                                                            &first_parms,
259                                                            (void*)&state, 
260                                                            cli_list_old_callback);
261                         if (!NT_STATUS_IS_OK(status)) {
262                                 talloc_destroy(state.mem_ctx);
263                                 return -1;
264                         }
265                 
266                         received = first_parms.search_first.out.count;
267                         if (received <= 0) break;
268                         first = False;
269                 } else {
270                         NTSTATUS status;
271
272                         next_parms.search_next.level = RAW_SEARCH_SEARCH;
273                         next_parms.search_next.in.max_count = num_asked;
274                         next_parms.search_next.in.search_attrib = attribute;
275                         next_parms.search_next.in.search_id = state.status;
276                         
277                         status = smb_raw_search_next(cli->tree, state.mem_ctx,
278                                                           &next_parms,
279                                                           (void*)&state, 
280                                                           cli_list_old_callback);
281                         
282                         if (!NT_STATUS_IS_OK(status)) {
283                                 talloc_destroy(state.mem_ctx);
284                                 return -1;
285                         }
286                         received = next_parms.search_next.out.count;
287                         if (received <= 0) break;
288                 }
289                 
290                 num_received += received;
291         }
292
293         for (i=0;i<state.total_received;i++) {
294                 fn(&state.dirlist[i], Mask, caller_state);
295         }
296
297         talloc_destroy(state.mem_ctx);
298
299         return state.total_received;
300 }
301
302 /****************************************************************************
303  Do a directory listing, calling fn on each file found.
304  This auto-switches between old and new style.
305 ****************************************************************************/
306
307 int cli_list(struct cli_state *cli,const char *Mask,uint16 attribute, 
308              void (*fn)(file_info *, const char *, void *), void *state)
309 {
310         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1)
311                 return cli_list_old(cli, Mask, attribute, fn, state);
312         return cli_list_new(cli, Mask, attribute, fn, state);
313 }