]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/enumerate.c
more AFP 3.0 changes
[netatalk.git] / etc / afpd / enumerate.c
1 /*
2  * $Id: enumerate.c,v 1.25 2002-10-15 19:34:34 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 char *check_dirent(const struct vol *vol, char *name)
154 {
155     char *m_name = NULL;
156
157     if (!strcmp(name, "..") || !strcmp(name, "."))
158         return NULL;
159
160     if (!(validupath(vol, name)))
161         return NULL;
162
163     /* check for vetoed filenames */
164     if (veto_file(vol->v_veto, name))
165         return NULL;
166
167     /* now check against too big a file */
168     if (strlen(m_name = utompath(vol, name)) > MACFILELEN)
169         return NULL;
170
171     return m_name;
172 }
173
174 /* ----------------------------- */
175 int 
176 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
177 {
178     DIR             *dp;
179     struct dirent       *de;
180     char            *m_name;
181     int             ret;
182     
183     if (( dp = opendir( name)) == NULL ) {
184         return -1;
185     }
186     ret = 0;
187     for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
188         if (!(m_name = check_dirent(vol, de->d_name)))
189             continue;
190
191         ret++;
192         if (fn && fn(de,m_name, data) < 0) {
193            closedir(dp);
194            return -1;
195         }
196     }
197     closedir(dp);
198     return ret;
199 }
200
201 /* ----------------------------- */
202 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
203 AFPObj       *obj;
204 char         *ibuf, *rbuf;
205 unsigned int ibuflen, *rbuflen;
206 int     ext;
207 {
208     static struct savedir       sd = { 0, 0, 0, NULL, NULL, 0 };
209     struct vol                  *vol;
210     struct dir                  *dir;
211     int                         did, ret, esz, len, first = 1;
212     char                        *data, *start;
213     u_int16_t                   vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
214     u_int16_t                   temp16;
215     u_int32_t                   sindex, maxsz, sz = 0;
216     struct path                 *o_path;
217     struct path                 s_path;
218     int                         header;
219         
220     if ( sd.sd_buflen == 0 ) {
221         if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
222             LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
223             *rbuflen = 0;
224             return AFPERR_MISC;
225         }
226         sd.sd_buflen = SDBUFBRK;
227     }
228
229     ibuf += 2;
230
231     memcpy( &vid, ibuf, sizeof( vid ));
232     ibuf += sizeof( vid );
233
234     if (( vol = getvolbyvid( vid )) == NULL ) {
235         *rbuflen = 0;
236         return( AFPERR_PARAM );
237     }
238
239     memcpy( &did, ibuf, sizeof( did ));
240     ibuf += sizeof( did );
241
242     if (( dir = dirlookup( vol, did )) == NULL ) {
243         *rbuflen = 0;
244         return( AFPERR_NODIR );
245     }
246
247     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
248     fbitmap = ntohs( fbitmap );
249     ibuf += sizeof( fbitmap );
250
251     memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
252     dbitmap = ntohs( dbitmap );
253     ibuf += sizeof( dbitmap );
254
255     /* check for proper bitmaps -- the stuff in comments is for
256      * variable directory ids. */
257     if (!(fbitmap || dbitmap)
258             /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
259               (dbitmap & (1 << DIRPBIT_PDID))*/) {
260         *rbuflen = 0;
261         return AFPERR_BITMAP;
262     }
263
264     memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
265     reqcnt = ntohs( reqcnt );
266     ibuf += sizeof( reqcnt );
267
268     if (ext == 2) {
269         memcpy( &sindex, ibuf, sizeof( sindex ));
270         sindex = ntohs( sindex );
271         ibuf += sizeof( sindex );
272     }
273     else {
274         memcpy( &temp16, ibuf, sizeof( temp16 ));
275         sindex = ntohs( temp16 );
276         ibuf += sizeof( temp16 );
277     }
278     
279     if (ext == 2) {
280         memcpy( &maxsz, ibuf, sizeof( maxsz ));
281         maxsz = ntohs( maxsz );
282         ibuf += sizeof( maxsz );
283     }
284     else {
285         memcpy( &temp16, ibuf, sizeof( temp16 ));
286         maxsz = ntohs( temp16 );
287         ibuf += sizeof( temp16 );
288     }
289     
290     header = (ext)?4:2;
291     header *=sizeof( u_char );
292     
293     maxsz = min(maxsz, *rbuflen);
294
295     if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
296         *rbuflen = 0;
297         return( AFPERR_NODIR );
298     }
299
300     if ( *o_path->m_name != '\0') {
301         *rbuflen = 0;
302         return( AFPERR_BADTYPE );
303     }
304
305     data = rbuf + 3 * sizeof( u_int16_t );
306     sz = 3 * sizeof( u_int16_t );       /* fbitmap, dbitmap, reqcount */
307
308     /*
309      * Read the directory into a pre-malloced buffer, stored
310      *          len <name> \0
311      * The end is indicated by a len of 0.
312      */
313     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
314         sd.sd_last = sd.sd_buf;
315         if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
316             switch (errno) {
317             case EACCES:
318                 return AFPERR_ACCESS;
319             case ENOTDIR:
320                 return AFPERR_BADTYPE;
321             case ENOMEM:
322                 return AFPERR_MISC;
323             default:
324                 return AFPERR_NODIR;
325             }
326         }
327         curdir->ctime  = o_path->st.st_ctime; /* play safe */
328         if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
329             *rbuflen = 0;
330             switch (errno) {
331             case EACCES:
332                 return AFPERR_ACCESS;
333             case ENOTDIR:
334                 return AFPERR_BADTYPE;
335             case ENOMEM:
336                 return AFPERR_MISC;
337             default:
338                 return AFPERR_NODIR;
339             }
340         }
341         curdir->offcnt = ret;
342         *sd.sd_last = 0;
343
344         sd.sd_last = sd.sd_buf;
345         sd.sd_sindex = 1;
346
347         sd.sd_vid = vid;
348         sd.sd_did = did;
349     }
350
351     /*
352      * Position sd_last as dictated by sindex.
353      */
354     if ( sindex < sd.sd_sindex ) {
355         sd.sd_sindex = 1;
356         sd.sd_last = sd.sd_buf;
357     }
358     while ( sd.sd_sindex < sindex ) {
359         len = *(sd.sd_last)++;
360         if ( len == 0 ) {
361             sd.sd_did = 0;      /* invalidate sd struct to force re-read */
362             *rbuflen = 0;
363             return( AFPERR_NOOBJ );
364         }
365         sd.sd_last += len + 1;
366         sd.sd_sindex++;
367     }
368
369     while (( len = *(sd.sd_last)) != 0 ) {
370         /*
371          * If we've got all we need, send it.
372          */
373         if ( actcnt == reqcnt ) {
374             break;
375         }
376
377         /*
378          * Save the start position, in case we exceed the buffer
379          * limitation, and have to back up one.
380          */
381         start = sd.sd_last;
382         sd.sd_last++;
383
384         if (*sd.sd_last == 0) {
385             /* stat() already failed on this one */
386             sd.sd_last += len + 1;
387             continue;
388         }
389         s_path.u_name = sd.sd_last;
390         if (of_stat( &s_path) < 0 ) {
391             /*
392              * Somebody else plays with the dir, well it can be us with 
393             * "Empty Trash..."
394             */
395
396             /* so the next time it won't try to stat it again
397              * another solution would be to invalidate the cache with 
398              * sd.sd_did = 0 but if it's not ENOENT error it will start again
399              */
400             *sd.sd_last = 0;
401             sd.sd_last += len + 1;
402             continue;
403         }
404
405         /*
406          * If a fil/dir is not a dir, it's a file. This is slightly
407          * inaccurate, since that means /dev/null is a file, /dev/printer
408          * is a file, etc.
409          */
410         if ( S_ISDIR(s_path.st.st_mode)) {
411             if ( dbitmap == 0 ) {
412                 sd.sd_last += len + 1;
413                 continue;
414             }
415             dir = curdir->d_child;
416             s_path.m_name = NULL;
417             while (dir) {
418                 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
419                     break;
420                 }
421                 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
422             }
423             if (!dir) {
424                 s_path.m_name = utompath(vol, s_path.u_name);
425                 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
426                     *rbuflen = 0;
427                     return AFPERR_MISC;
428                 }
429             }
430
431             if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
432                                      data + header , &esz )) != AFP_OK ) {
433                 *rbuflen = 0;
434                 return( ret );
435             }
436
437         } else {
438             if ( fbitmap == 0 ) {
439                 sd.sd_last += len + 1;
440                 continue;
441             }
442             s_path.m_name = utompath(vol, s_path.u_name);
443             if (( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
444                                      data + header , &esz )) != AFP_OK ) {
445                 *rbuflen = 0;
446                 return( ret );
447             }
448         }
449
450         /*
451          * Make sure entry is an even length, possibly with a null
452          * byte on the end.
453          */
454         if ( (esz + header) & 1 ) {
455             *(data + header + esz ) = '\0';
456             esz++;
457         }
458
459         /*
460          * Check if we've exceeded the size limit.
461          */
462         if ( maxsz < sz + esz + header) {
463             if (first) { /* maxsz can't hold a single reply */
464                 *rbuflen = 0;
465                 return AFPERR_PARAM;
466             }
467             sd.sd_last = start;
468             break;
469         }
470
471         if (first)
472             first = 0;
473
474         sz += esz + header;
475         if (ext) {
476             temp16 = htons( esz + header );
477             memcpy( data, &temp16, sizeof( temp16 ));
478             data += sizeof(temp16);
479         }
480         else {
481             *data++ = esz + header;
482         }
483
484         *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
485         if (ext) {
486              *data++ = 0;
487         }
488         data += esz;
489         actcnt++;
490         sd.sd_last += len + 1;
491     }
492
493     if ( actcnt == 0 ) {
494         *rbuflen = 0;
495         sd.sd_did = 0;          /* invalidate sd struct to force re-read */
496         return( AFPERR_NOOBJ );
497     }
498     sd.sd_sindex = sindex + actcnt;
499
500     /*
501      * All done, fill in misc junk in rbuf
502      */
503     fbitmap = htons( fbitmap );
504     memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
505     rbuf += sizeof( fbitmap );
506     dbitmap = htons( dbitmap );
507     memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
508     rbuf += sizeof( dbitmap );
509     actcnt = htons( actcnt );
510     memcpy( rbuf, &actcnt, sizeof( actcnt ));
511     rbuf += sizeof( actcnt );
512     *rbuflen = sz;
513     return( AFP_OK );
514 }
515
516 /* ----------------------------- */
517 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
518 AFPObj       *obj;
519 char         *ibuf, *rbuf;
520 unsigned int ibuflen, *rbuflen;
521 {
522     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
523 }
524
525 /* ----------------------------- */
526 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
527 AFPObj       *obj;
528 char         *ibuf, *rbuf;
529 unsigned int ibuflen, *rbuflen;
530 {
531     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
532 }
533
534 /* ----------------------------- */
535 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
536 AFPObj       *obj;
537 char         *ibuf, *rbuf;
538 unsigned int ibuflen, *rbuflen;
539 {
540     return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);
541 }
542