]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/enumerate.c
remove pre ansi declarations
[netatalk.git] / etc / afpd / enumerate.c
1 /*
2  * $Id: enumerate.c,v 1.45 2009-10-13 22:55:36 didg Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <sys/file.h>
17 #include <sys/param.h>
18
19 #include <atalk/logger.h>
20 #include <atalk/afp.h>
21 #include <atalk/adouble.h>
22 #include <atalk/vfs.h>
23 #include <atalk/cnid.h>
24 #include "desktop.h"
25 #include "directory.h"
26 #include "volume.h"
27 #include "globals.h"
28 #include "file.h"
29 #include "fork.h"
30 #include "filedir.h"
31
32 #define min(a,b)        ((a)<(b)?(a):(b))
33
34 /*
35  * Struct to save directory reading context in. Used to prevent
36  * O(n^2) searches on a directory.
37  */
38 struct savedir {
39     u_short      sd_vid;
40     u_int32_t    sd_did;
41     int          sd_buflen;
42     char         *sd_buf;
43     char         *sd_last;
44     unsigned int sd_sindex;
45 };
46 #define SDBUFBRK        2048
47
48 static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
49 {
50     struct savedir *sd = data; 
51     char *start, *end;
52     int  len,lenm;
53     
54     end = sd->sd_buf + sd->sd_buflen;
55     len = strlen(de->d_name);
56     *(sd->sd_last)++ = len;
57     lenm = 0; /* strlen(mname);*/
58     if ( sd->sd_last + len +lenm + 4 > end ) {
59         char *buf;
60
61         start = sd->sd_buf;
62         if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
63             LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
64                         strerror(errno) );
65             errno = ENOMEM;
66             return -1;
67         }
68         sd->sd_buf = buf;
69         sd->sd_buflen += SDBUFBRK;
70         sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
71         end = sd->sd_buf + sd->sd_buflen;
72     }
73
74     memcpy( sd->sd_last, de->d_name, len + 1 );
75     sd->sd_last += len + 1;
76 #if 0
77     *(sd->sd_last)++ = lenm;
78     memcpy( sd->sd_last, mname, lenm + 1 );
79     sd->sd_last += lenm + 1;
80 #endif    
81     return 0;
82 }
83
84 /* ----------------------------- 
85  * FIXME: 
86  * Doesn't work with dangling symlink
87  * ie: 
88  * - Move a folder with a dangling symlink in the trash
89  * - empty the trash
90  * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams 
91  * and the Mac doesn't try to call afp_delete!
92  *
93  * Another option for symlink
94  * cf:
95  * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
96  * 
97 */
98 char *check_dirent(const struct vol *vol, char *name)
99 {
100
101     if (!strcmp(name, "..") || !strcmp(name, "."))
102         return NULL;
103
104     if (!vol->vfs->validupath(vol, name))
105         return NULL;
106
107     /* check for vetoed filenames */
108     if (veto_file(vol->v_veto, name))
109         return NULL;
110 #if 0
111     char *m_name = NULL;
112
113     if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding()))) 
114         return NULL;    
115
116     /* now check against too big a file */
117     if (strlen(m_name) > vol->max_filename)
118         return NULL;
119 #endif
120     return name;
121 }
122
123 /* ----------------------------- */
124 int 
125 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
126 {
127     DIR             *dp;
128     struct dirent       *de;
129     char            *m_name;
130     int             ret;
131     
132     if (NULL == ( dp = opendir( name)) ) {
133         return -1;
134     }
135     ret = 0;
136     for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
137         if (!(m_name = check_dirent(vol, de->d_name)))
138             continue;
139
140         ret++;
141         if (fn && fn(de,m_name, data) < 0) {
142            closedir(dp);
143            return -1;
144         }
145     }
146     closedir(dp);
147     return ret;
148 }
149
150 /* This is the maximal length of a single entry for a file/dir in the reply
151    block if all bits in the file/dir bitmap are set: header(4) + params(104) +
152    macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) +
153    oddpadding(1) */
154
155 #define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + 255 + 1)
156
157 /* ----------------------------- */
158 static int enumerate(AFPObj *obj _U_, char *ibuf, unsigned int ibuflen _U_, 
159     char *rbuf, 
160     unsigned int *rbuflen, 
161     int ext)
162 {
163     static struct savedir       sd = { 0, 0, 0, NULL, NULL, 0 };
164     struct vol                  *vol;
165     struct dir                  *dir;
166     int                         did, ret, esz, len, first = 1;
167     char                        *data, *start;
168     u_int16_t                   vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
169     u_int16_t                   temp16;
170     u_int32_t                   sindex, maxsz, sz = 0;
171     struct path                 *o_path;
172     struct path                 s_path;
173     int                         header;
174         
175     if ( sd.sd_buflen == 0 ) {
176         if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
177             LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
178             *rbuflen = 0;
179             return AFPERR_MISC;
180         }
181         sd.sd_buflen = SDBUFBRK;
182     }
183
184     ibuf += 2;
185
186     memcpy( &vid, ibuf, sizeof( vid ));
187     ibuf += sizeof( vid );
188
189     if (NULL == ( vol = getvolbyvid( vid )) ) {
190         *rbuflen = 0;
191         return( AFPERR_PARAM );
192     }
193
194     memcpy( &did, ibuf, sizeof( did ));
195     ibuf += sizeof( did );
196
197     if (NULL == ( dir = dirlookup( vol, did )) ) {
198         *rbuflen = 0;
199         return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
200     }
201
202     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
203     fbitmap = ntohs( fbitmap );
204     ibuf += sizeof( fbitmap );
205
206     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
207     dbitmap = ntohs( dbitmap );
208     ibuf += sizeof( dbitmap );
209
210     /* check for proper bitmaps -- the stuff in comments is for
211      * variable directory ids. */
212     if (!(fbitmap || dbitmap)
213             /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
214               (dbitmap & (1 << DIRPBIT_PDID))*/) {
215         *rbuflen = 0;
216         return AFPERR_BITMAP;
217     }
218
219     memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
220     reqcnt = ntohs( reqcnt );
221     ibuf += sizeof( reqcnt );
222
223     if (ext == 2) {
224         memcpy( &sindex, ibuf, sizeof( sindex ));
225         sindex = ntohl( sindex );
226         ibuf += sizeof( sindex );
227     }
228     else {
229         memcpy( &temp16, ibuf, sizeof( temp16 ));
230         sindex = ntohs( temp16 );
231         ibuf += sizeof( temp16 );
232     }
233
234     if (!sindex) {
235         *rbuflen = 0;
236         return AFPERR_PARAM ;
237     }
238
239     if (ext == 2) {
240         memcpy( &maxsz, ibuf, sizeof( maxsz ));
241         maxsz = ntohl( maxsz );
242         ibuf += sizeof( maxsz );
243     }
244     else {
245         memcpy( &temp16, ibuf, sizeof( temp16 ));
246         maxsz = ntohs( temp16 );
247         ibuf += sizeof( temp16 );
248     }
249     
250     header = (ext)?4:2;
251     header *=sizeof( u_char );
252     
253     maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
254     o_path = cname( vol, dir, &ibuf );
255
256     if (afp_errno == AFPERR_NOOBJ) 
257         afp_errno = AFPERR_NODIR;
258
259     *rbuflen = 0;
260     if (NULL == o_path ) {
261         return get_afp_errno(AFPERR_NOOBJ); 
262     }
263     if ( *o_path->m_name != '\0') {
264         /* it's a file or it's a dir and extendir() was unable to chdir in it */
265         return path_error(o_path, AFPERR_NODIR );
266     }
267
268     data = rbuf + 3 * sizeof( u_int16_t );
269     sz = 3 * sizeof( u_int16_t );       /* fbitmap, dbitmap, reqcount */
270
271     /*
272      * Read the directory into a pre-malloced buffer, stored
273      *          len <name> \0
274      * The end is indicated by a len of 0.
275      */
276     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
277         sd.sd_last = sd.sd_buf;
278         /* if dir was in the cache we don't have the inode */
279         if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
280               (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) 
281         {
282             switch (errno) {
283             case EACCES:
284                 return AFPERR_ACCESS;
285             case ENOTDIR:
286                 return AFPERR_BADTYPE;
287             case ENOMEM:
288                 return AFPERR_MISC;
289             default:
290                 return AFPERR_NODIR;
291             }
292         }
293         setdiroffcnt(curdir, &o_path->st,  ret);
294         *sd.sd_last = 0;
295
296         sd.sd_last = sd.sd_buf;
297         sd.sd_sindex = 1;
298
299         sd.sd_vid = vid;
300         sd.sd_did = curdir->d_did;
301     }
302
303     /*
304      * Position sd_last as dictated by sindex.
305      */
306     if ( sindex < sd.sd_sindex ) {
307         sd.sd_sindex = 1;
308         sd.sd_last = sd.sd_buf;
309     }
310     while ( sd.sd_sindex < sindex ) {
311         len = (unsigned char)*(sd.sd_last)++;
312         if ( len == 0 ) {
313             sd.sd_did = 0;      /* invalidate sd struct to force re-read */
314             return( AFPERR_NOOBJ );
315         }
316         sd.sd_last += len + 1;
317         sd.sd_sindex++;
318     }
319
320     while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
321         /*
322          * If we've got all we need, send it.
323          */
324         if ( actcnt == reqcnt ) {
325             break;
326         }
327
328         /*
329          * Save the start position, in case we exceed the buffer
330          * limitation, and have to back up one.
331          */
332         start = sd.sd_last;
333         sd.sd_last++;
334
335         if (*sd.sd_last == 0) {
336             /* stat() already failed on this one */
337             sd.sd_last += len + 1;
338             continue;
339         }
340         memset(&s_path, 0, sizeof(s_path));
341         s_path.u_name = sd.sd_last;
342         if (of_stat( &s_path) < 0 ) {
343             /*
344              * Somebody else plays with the dir, well it can be us with 
345             * "Empty Trash..."
346             */
347
348             /* so the next time it won't try to stat it again
349              * another solution would be to invalidate the cache with 
350              * sd.sd_did = 0 but if it's not ENOENT error it will start again
351              */
352             *sd.sd_last = 0;
353             sd.sd_last += len + 1;
354             curdir->offcnt--;           /* a little lie */
355             continue;
356         }
357
358         sd.sd_last += len + 1;
359         s_path.m_name = NULL;
360         /*
361          * If a fil/dir is not a dir, it's a file. This is slightly
362          * inaccurate, since that means /dev/null is a file, /dev/printer
363          * is a file, etc.
364          */
365         if ( S_ISDIR(s_path.st.st_mode)) {
366             if ( dbitmap == 0 ) {
367                 continue;
368             }
369             dir = dirsearch_byname(vol, curdir, s_path.u_name);
370             if (!dir && NULL == (dir = adddir( vol, curdir, &s_path) ) ) {
371                     return AFPERR_MISC;
372                 }
373             if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
374                                      data + header , &esz ))) {
375                 return( ret );
376             }
377
378         } else {
379             if ( fbitmap == 0 ) {
380                 continue;
381             }
382             if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
383                                      data + header , &esz )) ) {
384                 return( ret );
385             }
386         }
387
388         /*
389          * Make sure entry is an even length, possibly with a null
390          * byte on the end.
391          */
392         if ( (esz + header) & 1 ) {
393             *(data + header + esz ) = '\0';
394             esz++;
395         }
396
397         /*
398          * Check if we've exceeded the size limit.
399          */
400         if ( maxsz < sz + esz + header) {
401             if (first) { /* maxsz can't hold a single reply */
402                 return AFPERR_PARAM;
403             }
404             sd.sd_last = start;
405             break;
406         }
407
408         if (first)
409             first = 0;
410
411         sz += esz + header;
412         if (ext) {
413             temp16 = htons( esz + header );
414             memcpy( data, &temp16, sizeof( temp16 ));
415             data += sizeof(temp16);
416         }
417         else {
418             *data++ = esz + header;
419         }
420
421         *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
422         if (ext) {
423              *data++ = 0;
424         }
425         data += esz;
426         actcnt++;
427         /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
428     }
429
430     if ( actcnt == 0 ) {
431         sd.sd_did = 0;          /* invalidate sd struct to force re-read */
432         return( AFPERR_NOOBJ );
433     }
434     sd.sd_sindex = sindex + actcnt;
435
436     /*
437      * All done, fill in misc junk in rbuf
438      */
439     fbitmap = htons( fbitmap );
440     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
441     rbuf += sizeof( fbitmap );
442     dbitmap = htons( dbitmap );
443     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
444     rbuf += sizeof( dbitmap );
445     actcnt = htons( actcnt );
446     memcpy( rbuf, &actcnt, sizeof( actcnt ));
447     rbuf += sizeof( actcnt );
448     *rbuflen = sz;
449     return( AFP_OK );
450 }
451
452 /* ----------------------------- */
453 int afp_enumerate(AFPObj *obj, char *ibuf, unsigned int ibuflen, 
454     char *rbuf, 
455     unsigned int *rbuflen)
456 {
457     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
458 }
459
460 /* ----------------------------- */
461 int afp_enumerate_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen, 
462     char *rbuf, 
463     unsigned int *rbuflen)
464 {
465     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
466 }
467
468 /* ----------------------------- */
469 int afp_enumerate_ext2(AFPObj *obj, char *ibuf, unsigned int ibuflen, 
470     char *rbuf, 
471     unsigned int *rbuflen)
472 {
473     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);
474 }
475