]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/enumerate.c
afp_enumerate() decremente offspring count if a file is not in the reply packet....
[netatalk.git] / etc / afpd / enumerate.c
1 /*
2  * $Id: enumerate.c,v 1.28 2002-12-23 00:21:35 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
17 #include <atalk/logger.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/file.h>
21 #include <sys/param.h>
22
23 #include <netatalk/endian.h>
24 #include <atalk/afp.h>
25 #include <atalk/adouble.h>
26 #ifdef CNID_DB
27 #include <atalk/cnid.h>
28 #endif /* CNID_DB */
29 #include "desktop.h"
30 #include "directory.h"
31 #include "volume.h"
32 #include "globals.h"
33 #include "file.h"
34 #include "fork.h"
35 #include "filedir.h"
36
37 #define min(a,b)        ((a)<(b)?(a):(b))
38
39 /* ---------------------------- */
40 struct dir *
41             adddir( vol, dir, path)
42 struct vol      *vol;
43 struct dir      *dir;
44 struct path     *path;
45 {
46     struct dir  *cdir, *edir;
47     int         upathlen;
48     char        *name;
49     char        *upath;
50 #ifndef USE_LASTDID
51     struct stat lst, *lstp;
52 #endif /* USE_LASTDID */
53     struct stat *st;
54
55     upath = path->u_name;
56     name  = path->m_name;    
57     st    = &path->st;
58     upathlen = strlen(upath);
59     if ((cdir = dirnew(name, upath)) == NULL) {
60         LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
61         return NULL;
62     }
63
64     cdir->d_did = 0;
65
66 #ifdef CNID_DB
67     /* add to cnid db */
68     cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
69                            upathlen, cdir->d_did);
70     /* Fail out if things go bad with CNID. */
71     if (cdir->d_did == CNID_INVALID) {
72         switch (errno) {
73         case CNID_ERR_PARAM:
74             LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add");
75             return NULL;
76         case CNID_ERR_PATH:
77         case CNID_ERR_DB:
78         case CNID_ERR_MAX:
79             return NULL;
80         }
81     }
82 #endif /* CNID_DB */
83
84     if (cdir->d_did == 0) {
85 #ifdef USE_LASTDID
86         /* last way of doing DIDs */
87         cdir->d_did = htonl( vol->v_lastdid++ );
88 #else /* USE_LASTDID */
89         lstp = lstat(upath, &lst) < 0 ? st : &lst;
90         /* the old way of doing DIDs (default) */
91         cdir->d_did = htonl( CNID(lstp, 0) );
92 #endif /* USE_LASTDID */
93     }
94
95     if ((edir = dirinsert( vol, cdir ))) {
96         edir->d_m_name = cdir->d_m_name;
97         edir->d_u_name = cdir->d_u_name;
98         free(cdir);
99         cdir = edir;
100     }
101
102     /* parent/child directories */
103     cdir->d_parent = dir;
104     dirchildadd(dir, cdir);
105     return( cdir );
106 }
107 /*
108  * Struct to save directory reading context in. Used to prevent
109  * O(n^2) searches on a directory.
110  */
111 struct savedir {
112     u_short      sd_vid;
113     u_int32_t    sd_did;
114     int          sd_buflen;
115     char         *sd_buf;
116     char         *sd_last;
117     unsigned int sd_sindex;
118 };
119 #define SDBUFBRK        1024
120
121 static int enumerate_loop(struct dirent *de, char *mname, void *data)
122 {
123     struct savedir *sd = data; 
124     char *start, *end;
125     int  len;
126     
127     end = sd->sd_buf + sd->sd_buflen;
128     len = strlen(de->d_name);
129     *(sd->sd_last)++ = len;
130
131     if ( sd->sd_last + len + 2 > end ) {
132         char *buf;
133
134         start = sd->sd_buf;
135         if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
136             LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
137                         strerror(errno) );
138             errno = ENOMEM;
139             return -1;
140         }
141         sd->sd_buf = buf;
142         sd->sd_buflen += SDBUFBRK;
143         sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
144         end = sd->sd_buf + sd->sd_buflen;
145     }
146
147     memcpy( sd->sd_last, de->d_name, len + 1 );
148     sd->sd_last += len + 1;
149     return 0;
150 }
151
152 /* ----------------------------- 
153  * FIXME: 
154  * Doesn't work with dangling symlink
155  * ie: 
156  * - Move a folder with a dangling symlink in the trash
157  * - empty the trash
158  * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams 
159  * and the Mac doesn't try to call afp_delete!
160  *
161  * Another option for symlink
162  * cf:
163  * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
164  * 
165 */
166 char *check_dirent(const struct vol *vol, char *name)
167 {
168     char *m_name = NULL;
169
170     if (!strcmp(name, "..") || !strcmp(name, "."))
171         return NULL;
172
173     if (!(validupath(vol, name)))
174         return NULL;
175
176     /* check for vetoed filenames */
177     if (veto_file(vol->v_veto, name))
178         return NULL;
179
180     /* now check against too big a file */
181     if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
182         return NULL;
183
184     return m_name;
185 }
186
187 /* ----------------------------- */
188 int 
189 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
190 {
191     DIR             *dp;
192     struct dirent       *de;
193     char            *m_name;
194     int             ret;
195     
196     if (( dp = opendir( name)) == NULL ) {
197         return -1;
198     }
199     ret = 0;
200     for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
201         if (!(m_name = check_dirent(vol, de->d_name)))
202             continue;
203
204         ret++;
205         if (fn && fn(de,m_name, data) < 0) {
206            closedir(dp);
207            return -1;
208         }
209     }
210     closedir(dp);
211     return ret;
212 }
213
214 /* ----------------------------- */
215 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
216 AFPObj       *obj;
217 char         *ibuf, *rbuf;
218 unsigned int ibuflen, *rbuflen;
219 int     ext;
220 {
221     static struct savedir       sd = { 0, 0, 0, NULL, NULL, 0 };
222     struct vol                  *vol;
223     struct dir                  *dir;
224     int                         did, ret, esz, len, first = 1;
225     char                        *data, *start;
226     u_int16_t                   vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
227     u_int16_t                   temp16;
228     u_int32_t                   sindex, maxsz, sz = 0;
229     struct path                 *o_path;
230     struct path                 s_path;
231     int                         header;
232         
233     if ( sd.sd_buflen == 0 ) {
234         if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
235             LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
236             *rbuflen = 0;
237             return AFPERR_MISC;
238         }
239         sd.sd_buflen = SDBUFBRK;
240     }
241
242     ibuf += 2;
243
244     memcpy( &vid, ibuf, sizeof( vid ));
245     ibuf += sizeof( vid );
246
247     if (( vol = getvolbyvid( vid )) == NULL ) {
248         *rbuflen = 0;
249         return( AFPERR_PARAM );
250     }
251
252     memcpy( &did, ibuf, sizeof( did ));
253     ibuf += sizeof( did );
254
255     if (( dir = dirlookup( vol, did )) == NULL ) {
256         *rbuflen = 0;
257         return( AFPERR_NODIR );
258     }
259
260     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
261     fbitmap = ntohs( fbitmap );
262     ibuf += sizeof( fbitmap );
263
264     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
265     dbitmap = ntohs( dbitmap );
266     ibuf += sizeof( dbitmap );
267
268     /* check for proper bitmaps -- the stuff in comments is for
269      * variable directory ids. */
270     if (!(fbitmap || dbitmap)
271             /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
272               (dbitmap & (1 << DIRPBIT_PDID))*/) {
273         *rbuflen = 0;
274         return AFPERR_BITMAP;
275     }
276
277     memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
278     reqcnt = ntohs( reqcnt );
279     ibuf += sizeof( reqcnt );
280
281     if (ext == 2) {
282         memcpy( &sindex, ibuf, sizeof( sindex ));
283         sindex = ntohl( sindex );
284         ibuf += sizeof( sindex );
285     }
286     else {
287         memcpy( &temp16, ibuf, sizeof( temp16 ));
288         sindex = ntohs( temp16 );
289         ibuf += sizeof( temp16 );
290     }
291     
292     if (ext == 2) {
293         memcpy( &maxsz, ibuf, sizeof( maxsz ));
294         maxsz = ntohl( maxsz );
295         ibuf += sizeof( maxsz );
296     }
297     else {
298         memcpy( &temp16, ibuf, sizeof( temp16 ));
299         maxsz = ntohs( temp16 );
300         ibuf += sizeof( temp16 );
301     }
302     
303     header = (ext)?4:2;
304     header *=sizeof( u_char );
305     
306     maxsz = min(maxsz, *rbuflen);
307
308     if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
309         *rbuflen = 0;
310         return( AFPERR_NODIR );
311     }
312
313     if ( *o_path->m_name != '\0') {
314         *rbuflen = 0;
315         return( AFPERR_BADTYPE );
316     }
317
318     data = rbuf + 3 * sizeof( u_int16_t );
319     sz = 3 * sizeof( u_int16_t );       /* fbitmap, dbitmap, reqcount */
320
321     /*
322      * Read the directory into a pre-malloced buffer, stored
323      *          len <name> \0
324      * The end is indicated by a len of 0.
325      */
326     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
327         sd.sd_last = sd.sd_buf;
328         if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
329             switch (errno) {
330             case EACCES:
331                 return AFPERR_ACCESS;
332             case ENOTDIR:
333                 return AFPERR_BADTYPE;
334             case ENOMEM:
335                 return AFPERR_MISC;
336             default:
337                 return AFPERR_NODIR;
338             }
339         }
340         curdir->ctime  = o_path->st.st_ctime; /* play safe */
341         if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
342             *rbuflen = 0;
343             switch (errno) {
344             case EACCES:
345                 return AFPERR_ACCESS;
346             case ENOTDIR:
347                 return AFPERR_BADTYPE;
348             case ENOMEM:
349                 return AFPERR_MISC;
350             default:
351                 return AFPERR_NODIR;
352             }
353         }
354         curdir->offcnt = ret;
355         *sd.sd_last = 0;
356
357         sd.sd_last = sd.sd_buf;
358         sd.sd_sindex = 1;
359
360         sd.sd_vid = vid;
361         sd.sd_did = did;
362     }
363
364     /*
365      * Position sd_last as dictated by sindex.
366      */
367     if ( sindex < sd.sd_sindex ) {
368         sd.sd_sindex = 1;
369         sd.sd_last = sd.sd_buf;
370     }
371     while ( sd.sd_sindex < sindex ) {
372         len = *(sd.sd_last)++;
373         if ( len == 0 ) {
374             sd.sd_did = 0;      /* invalidate sd struct to force re-read */
375             *rbuflen = 0;
376             return( AFPERR_NOOBJ );
377         }
378         sd.sd_last += len + 1;
379         sd.sd_sindex++;
380     }
381
382     while (( len = *(sd.sd_last)) != 0 ) {
383         /*
384          * If we've got all we need, send it.
385          */
386         if ( actcnt == reqcnt ) {
387             break;
388         }
389
390         /*
391          * Save the start position, in case we exceed the buffer
392          * limitation, and have to back up one.
393          */
394         start = sd.sd_last;
395         sd.sd_last++;
396
397         if (*sd.sd_last == 0) {
398             /* stat() already failed on this one */
399             sd.sd_last += len + 1;
400             continue;
401         }
402         s_path.u_name = sd.sd_last;
403         if (of_stat( &s_path) < 0 ) {
404             /*
405              * Somebody else plays with the dir, well it can be us with 
406             * "Empty Trash..."
407             */
408
409             /* so the next time it won't try to stat it again
410              * another solution would be to invalidate the cache with 
411              * sd.sd_did = 0 but if it's not ENOENT error it will start again
412              */
413             *sd.sd_last = 0;
414             sd.sd_last += len + 1;
415             curdir->offcnt--;           /* a little lie */
416             continue;
417         }
418
419         /*
420          * If a fil/dir is not a dir, it's a file. This is slightly
421          * inaccurate, since that means /dev/null is a file, /dev/printer
422          * is a file, etc.
423          */
424         if ( S_ISDIR(s_path.st.st_mode)) {
425             if ( dbitmap == 0 ) {
426                 sd.sd_last += len + 1;
427                 continue;
428             }
429             dir = curdir->d_child;
430             s_path.m_name = NULL;
431             while (dir) {
432                 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
433                     break;
434                 }
435                 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
436             }
437             if (!dir) {
438                 s_path.m_name = utompath(vol, s_path.u_name);
439                 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
440                     *rbuflen = 0;
441                     return AFPERR_MISC;
442                 }
443             }
444
445             if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
446                                      data + header , &esz )) != AFP_OK ) {
447                 *rbuflen = 0;
448                 return( ret );
449             }
450
451         } else {
452             if ( fbitmap == 0 ) {
453                 sd.sd_last += len + 1;
454                 continue;
455             }
456             s_path.m_name = utompath(vol, s_path.u_name);
457             if (( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
458                                      data + header , &esz )) != AFP_OK ) {
459                 *rbuflen = 0;
460                 return( ret );
461             }
462         }
463
464         /*
465          * Make sure entry is an even length, possibly with a null
466          * byte on the end.
467          */
468         if ( (esz + header) & 1 ) {
469             *(data + header + esz ) = '\0';
470             esz++;
471         }
472
473         /*
474          * Check if we've exceeded the size limit.
475          */
476         if ( maxsz < sz + esz + header) {
477             if (first) { /* maxsz can't hold a single reply */
478                 *rbuflen = 0;
479                 return AFPERR_PARAM;
480             }
481             sd.sd_last = start;
482             break;
483         }
484
485         if (first)
486             first = 0;
487
488         sz += esz + header;
489         if (ext) {
490             temp16 = htons( esz + header );
491             memcpy( data, &temp16, sizeof( temp16 ));
492             data += sizeof(temp16);
493         }
494         else {
495             *data++ = esz + header;
496         }
497
498         *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
499         if (ext) {
500              *data++ = 0;
501         }
502         data += esz;
503         actcnt++;
504         sd.sd_last += len + 1;
505     }
506
507     if ( actcnt == 0 ) {
508         *rbuflen = 0;
509         sd.sd_did = 0;          /* invalidate sd struct to force re-read */
510         return( AFPERR_NOOBJ );
511     }
512     sd.sd_sindex = sindex + actcnt;
513
514     /*
515      * All done, fill in misc junk in rbuf
516      */
517     fbitmap = htons( fbitmap );
518     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
519     rbuf += sizeof( fbitmap );
520     dbitmap = htons( dbitmap );
521     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
522     rbuf += sizeof( dbitmap );
523     actcnt = htons( actcnt );
524     memcpy( rbuf, &actcnt, sizeof( actcnt ));
525     rbuf += sizeof( actcnt );
526     *rbuflen = sz;
527     return( AFP_OK );
528 }
529
530 /* ----------------------------- */
531 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
532 AFPObj       *obj;
533 char         *ibuf, *rbuf;
534 unsigned int ibuflen, *rbuflen;
535 {
536     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
537 }
538
539 /* ----------------------------- */
540 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
541 AFPObj       *obj;
542 char         *ibuf, *rbuf;
543 unsigned int ibuflen, *rbuflen;
544 {
545     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
546 }
547
548 /* ----------------------------- */
549 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
550 AFPObj       *obj;
551 char         *ibuf, *rbuf;
552 unsigned int ibuflen, *rbuflen;
553 {
554     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);
555 }
556