2 Unix SMB/CIFS implementation.
3 client directory list routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /****************************************************************************
26 Interpret a long filename structure - this is mostly guesses at the moment.
27 The length of the structure is returned
28 The structure of a long filename depends on the info level. 260 is used
29 by NT and 2 is used by OS/2
30 ****************************************************************************/
32 static int interpret_long_filename(struct cli_state
*cli
,
33 int level
,char *p
,file_info
*finfo
)
35 extern file_info def_finfo
;
40 if (!finfo
) finfo
= &finfo2
;
42 memcpy(finfo
,&def_finfo
,sizeof(*finfo
));
45 case 1: /* OS/2 understands this */
46 /* these dates are converted to GMT by
48 finfo
->ctime
= make_unix_date2(p
+4);
49 finfo
->atime
= make_unix_date2(p
+8);
50 finfo
->mtime
= make_unix_date2(p
+12);
51 finfo
->size
= IVAL(p
,16);
52 finfo
->mode
= CVAL(p
,24);
55 p
+= clistr_align_in(cli
, p
, 0);
56 /* the len+2 below looks strange but it is
57 important to cope with the differences
58 between win2000 and win9x for this call
60 p
+= clistr_pull(cli
, finfo
->name
, p
,
64 return PTR_DIFF(p
, base
);
66 case 2: /* this is what OS/2 uses mostly */
67 /* these dates are converted to GMT by
69 finfo
->ctime
= make_unix_date2(p
+4);
70 finfo
->atime
= make_unix_date2(p
+8);
71 finfo
->mtime
= make_unix_date2(p
+12);
72 finfo
->size
= IVAL(p
,16);
73 finfo
->mode
= CVAL(p
,24);
76 /* check for unisys! */
77 p
+= clistr_pull(cli
, finfo
->name
, p
,
81 return PTR_DIFF(p
, base
) + 1;
83 case 260: /* NT uses this, but also accepts 2 */
86 p
+= 4; /* next entry offset */
87 p
+= 4; /* fileindex */
89 /* these dates appear to arrive in a
90 weird way. It seems to be localtime
91 plus the serverzone given in the
92 initial connect. This is GMT when
93 DST is not in effect and one hour
94 from GMT otherwise. Can this really
97 I suppose this could be called
98 kludge-GMT. Is is the GMT you get
99 by using the current DST setting on
100 a different localtime. It will be
101 cheap to calculate, I suppose, as
102 no DST tables will be needed */
104 finfo
->ctime
= interpret_long_date(p
);
106 finfo
->atime
= interpret_long_date(p
);
108 finfo
->mtime
= interpret_long_date(p
);
111 finfo
->size
= IVAL2_TO_SMB_BIG_UINT(p
,0);
113 p
+= 8; /* alloc size */
114 finfo
->mode
= CVAL(p
,0);
118 p
+= 4; /* EA size */
122 /* stupid NT bugs. grr */
124 if (p
[1] == 0 && namelen
> 1) flags
|= STR_UNICODE
;
125 clistr_pull(cli
, finfo
->short_name
, p
,
126 sizeof(finfo
->short_name
),
129 p
+= 24; /* short name? */
130 clistr_pull(cli
, finfo
->name
, p
,
133 return SVAL(base
, 0);
137 DEBUG(1,("Unknown long filename format %d\n",level
));
141 /****************************************************************************
142 Do a directory listing, calling fn on each file found.
143 ****************************************************************************/
145 int cli_list_new(struct cli_state
*cli
,const char *Mask
,uint16 attribute
,
146 void (*fn
)(file_info
*, const char *, void *), void *state
)
149 int max_matches
= 1366; /* Match W2k - was 512. */
151 int max_matches
= 512;
158 char *tdl
, *dirlist
= NULL
;
160 int total_received
= -1;
162 int ff_searchcount
=0;
167 char *rparam
=NULL
, *rdata
=NULL
;
168 unsigned int param_len
, data_len
;
172 /* NT uses 260, OS/2 uses 2. Both accept 1. */
173 info_level
= (cli
->capabilities
&CAP_NT_SMBS
)?260:1;
175 /* when getting a directory listing from a 2k dfs root share,
176 we have to include the full path (\server\share\mask) here */
179 pstr_sprintf( mask
, "\\%s\\%s\\%s", cli
->desthost
, cli
->share
, Mask
);
183 while (ff_eos
== 0) {
185 if (loop_count
> 200) {
186 DEBUG(0,("Error: Looping in FIND_NEXT??\n"));
191 setup
= TRANSACT2_FINDFIRST
;
192 SSVAL(param
,0,attribute
); /* attribute */
193 SSVAL(param
,2,max_matches
); /* max count */
194 SSVAL(param
,4,4+2); /* resume required + close on end */
195 SSVAL(param
,6,info_level
);
198 p
+= clistr_push(cli
, param
+12, mask
, sizeof(param
)-12,
201 setup
= TRANSACT2_FINDNEXT
;
202 SSVAL(param
,0,ff_dir_handle
);
203 SSVAL(param
,2,max_matches
); /* max count */
204 SSVAL(param
,4,info_level
);
205 SIVAL(param
,6,0); /* ff_resume_key */
206 SSVAL(param
,10,8+4+2); /* continue + resume required + close on end */
208 p
+= clistr_push(cli
, param
+12, mask
, sizeof(param
)-12,
212 param_len
= PTR_DIFF(p
, param
);
214 if (!cli_send_trans(cli
, SMBtrans2
,
216 -1, 0, /* fid, flags */
217 &setup
, 1, 0, /* setup, length, max */
218 param
, param_len
, 10, /* param, length, max */
222 MIN(16384,cli
->max_xmit
) /* data, length, max. */
224 cli
->max_xmit
/* data, length, max. */
230 if (!cli_receive_trans(cli
, SMBtrans2
,
232 &rdata
, &data_len
) &&
233 cli_is_dos_error(cli
)) {
234 /* we need to work around a Win95 bug - sometimes
235 it gives ERRSRV/ERRerror temprarily */
238 cli_dos_error(cli
, &eclass
, &ecode
);
239 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
)
245 if (cli_is_error(cli
) || !rdata
|| !rparam
)
248 if (total_received
== -1)
251 /* parse out some important return info */
254 ff_dir_handle
= SVAL(p
,0);
255 ff_searchcount
= SVAL(p
,2);
257 ff_lastname
= SVAL(p
,8);
259 ff_searchcount
= SVAL(p
,0);
261 ff_lastname
= SVAL(p
,6);
264 if (ff_searchcount
== 0)
267 /* point to the data bytes */
270 /* we might need the lastname for continuations */
271 if (ff_lastname
> 0) {
274 clistr_pull(cli
, mask
, p
+ff_lastname
,
276 data_len
-ff_lastname
,
280 clistr_pull(cli
, mask
, p
+ff_lastname
+1,
290 /* and add them to the dirlist pool */
291 tdl
= SMB_REALLOC(dirlist
,dirlist_len
+ data_len
);
294 DEBUG(0,("cli_list_new: Failed to expand dirlist\n"));
300 /* put in a length for the last entry, to ensure we can chain entries
301 into the next packet */
302 for (p2
=p
,i
=0;i
<(ff_searchcount
-1);i
++)
303 p2
+= interpret_long_filename(cli
,info_level
,p2
,NULL
);
304 SSVAL(p2
,0,data_len
- PTR_DIFF(p2
,p
));
306 /* grab the data for later use */
307 memcpy(dirlist
+dirlist_len
,p
,data_len
);
308 dirlist_len
+= data_len
;
310 total_received
+= ff_searchcount
;
315 DEBUG(3,("received %d entries (eos=%d)\n",
316 ff_searchcount
,ff_eos
));
318 if (ff_searchcount
> 0)
324 for (p
=dirlist
,i
=0;i
<total_received
;i
++) {
325 p
+= interpret_long_filename(cli
,info_level
,p
,&finfo
);
326 fn(&finfo
, Mask
, state
);
329 /* free up the dirlist buffer */
331 return(total_received
);
334 /****************************************************************************
335 Interpret a short filename structure.
336 The length of the structure is returned.
337 ****************************************************************************/
339 static int interpret_short_filename(struct cli_state
*cli
, char *p
,file_info
*finfo
)
341 extern file_info def_finfo
;
345 finfo
->mode
= CVAL(p
,21);
347 /* this date is converted to GMT by make_unix_date */
348 finfo
->ctime
= make_unix_date(p
+22);
349 finfo
->mtime
= finfo
->atime
= finfo
->ctime
;
350 finfo
->size
= IVAL(p
,26);
351 clistr_pull(cli
, finfo
->name
, p
+30, sizeof(finfo
->name
), 12, STR_ASCII
);
352 if (strcmp(finfo
->name
, "..") && strcmp(finfo
->name
, ".")) {
353 strncpy(finfo
->short_name
,finfo
->name
, sizeof(finfo
->short_name
)-1);
354 finfo
->short_name
[sizeof(finfo
->short_name
)-1] = '\0';
357 return(DIR_STRUCT_SIZE
);
361 /****************************************************************************
362 Do a directory listing, calling fn on each file found.
363 this uses the old SMBsearch interface. It is needed for testing Samba,
364 but should otherwise not be used.
365 ****************************************************************************/
367 int cli_list_old(struct cli_state
*cli
,const char *Mask
,uint16 attribute
,
368 void (*fn
)(file_info
*, const char *, void *), void *state
)
374 int num_asked
= (cli
->max_xmit
- 100)/DIR_STRUCT_SIZE
;
375 int num_received
= 0;
377 char *tdl
, *dirlist
= NULL
;
385 memset(cli
->outbuf
,'\0',smb_size
);
386 memset(cli
->inbuf
,'\0',smb_size
);
388 set_message(cli
->outbuf
,2,0,True
);
390 SCVAL(cli
->outbuf
,smb_com
,SMBsearch
);
392 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
393 cli_setup_packet(cli
);
395 SSVAL(cli
->outbuf
,smb_vwv0
,num_asked
);
396 SSVAL(cli
->outbuf
,smb_vwv1
,attribute
);
398 p
= smb_buf(cli
->outbuf
);
401 p
+= clistr_push(cli
, p
, first
?mask
:"", -1, STR_TERMINATE
);
413 cli_setup_bcc(cli
, p
);
415 if (!cli_receive_smb(cli
)) break;
417 received
= SVAL(cli
->inbuf
,smb_vwv0
);
418 if (received
<= 0) break;
422 tdl
= SMB_REALLOC(dirlist
,(num_received
+ received
)*DIR_STRUCT_SIZE
);
425 DEBUG(0,("cli_list_old: failed to expand dirlist"));
431 p
= smb_buf(cli
->inbuf
) + 3;
433 memcpy(dirlist
+num_received
*DIR_STRUCT_SIZE
,
434 p
,received
*DIR_STRUCT_SIZE
);
436 memcpy(status
,p
+ ((received
-1)*DIR_STRUCT_SIZE
),21);
438 num_received
+= received
;
440 if (cli_is_error(cli
)) break;
444 memset(cli
->outbuf
,'\0',smb_size
);
445 memset(cli
->inbuf
,'\0',smb_size
);
447 set_message(cli
->outbuf
,2,0,True
);
448 SCVAL(cli
->outbuf
,smb_com
,SMBfclose
);
449 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
450 cli_setup_packet(cli
);
452 SSVAL(cli
->outbuf
, smb_vwv0
, 0); /* find count? */
453 SSVAL(cli
->outbuf
, smb_vwv1
, attribute
);
455 p
= smb_buf(cli
->outbuf
);
465 cli_setup_bcc(cli
, p
);
467 if (!cli_receive_smb(cli
)) {
468 DEBUG(0,("Error closing search: %s\n",cli_errstr(cli
)));
472 for (p
=dirlist
,i
=0;i
<num_received
;i
++) {
474 p
+= interpret_short_filename(cli
, p
,&finfo
);
475 fn(&finfo
, Mask
, state
);
479 return(num_received
);
482 /****************************************************************************
483 Do a directory listing, calling fn on each file found.
484 This auto-switches between old and new style.
485 ****************************************************************************/
487 int cli_list(struct cli_state
*cli
,const char *Mask
,uint16 attribute
,
488 void (*fn
)(file_info
*, const char *, void *), void *state
)
490 if (cli
->protocol
<= PROTOCOL_LANMAN1
)
491 return cli_list_old(cli
, Mask
, attribute
, fn
, state
);
492 return cli_list_new(cli
, Mask
, attribute
, fn
, state
);