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