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