]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/vfs.c
738e3b837851abc278296f05ebe6d038559dcbc6
[netatalk.git] / libatalk / vfs / vfs.c
1 /*
2     Copyright (c) 2004 Didier Gautheron
3     Copyright (c) 2009 Frank Lahm
4  
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9  
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <libgen.h>
31
32 #include <atalk/afp.h>    
33 #include <atalk/adouble.h>
34 #include <atalk/ea.h>
35 #include <atalk/acl.h>
36 #include <atalk/logger.h>
37 #include <atalk/util.h>
38 #include <atalk/volume.h>
39 #include <atalk/vfs.h>
40 #include <atalk/directory.h>
41 #include <atalk/unix.h>
42 #include <atalk/errchk.h>
43 #include <atalk/bstrlib.h>
44 #include <atalk/bstradd.h>
45 #include <atalk/compat.h>
46
47 struct perm {
48     uid_t uid;
49     gid_t gid;
50 };
51
52 typedef int (*rf_loop)(struct dirent *, char *, void *, int , mode_t );
53
54 /* ----------------------------- */
55 static int 
56 for_each_adouble(const char *from, const char *name, rf_loop fn, void *data, int flag, mode_t v_umask)
57 {
58     char            buf[ MAXPATHLEN + 1];
59     char            *m;
60     DIR             *dp;
61     struct dirent   *de;
62     int             ret;
63     
64
65     if (NULL == ( dp = opendir( name)) ) {
66         if (!flag) {
67             LOG(log_error, logtype_afpd, "%s: opendir %s: %s", from, fullpathname(name),strerror(errno) );
68             return -1;
69         }
70         return 0;
71     }
72     strlcpy( buf, name, sizeof(buf));
73     strlcat( buf, "/", sizeof(buf) );
74     m = strchr( buf, '\0' );
75     ret = 0;
76     while ((de = readdir(dp))) {
77         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
78                 continue;
79         }
80         
81         strlcat(buf, de->d_name, sizeof(buf));
82         if (fn && (ret = fn(de, buf, data, flag, v_umask))) {
83            closedir(dp);
84            return ret;
85         }
86         *m = 0;
87     }
88     closedir(dp);
89     return ret;
90 }
91
92 static int netatalk_name(const char *name)
93 {
94     return strcmp(name,".AppleDB") && strcmp(name,".AppleDesktop");        
95 }
96
97 /*******************************************************************************
98  * classic adouble format 
99  *******************************************************************************/
100
101 static int validupath_adouble(VFS_FUNC_ARGS_VALIDUPATH)
102 {
103     if (name[0] != '.')
104         return 1;
105     
106     if (!(vol->v_flags & AFPVOL_USEDOTS))
107         return 0;
108         
109     return netatalk_name(name) && strcmp(name,".AppleDouble") && strcasecmp(name,".Parent");
110 }                                           
111
112 /* ----------------- */
113 static int RF_chown_adouble(VFS_FUNC_ARGS_CHOWN)
114 {
115     struct stat st;
116     const char *ad_p;
117
118     ad_p = vol->ad_path(path, ADFLAGS_HF );
119
120     if ( stat( ad_p, &st ) < 0 )
121         return 0; /* ignore */
122
123     return chown( ad_p, uid, gid );
124 }
125
126 /* ----------------- */
127 static int RF_renamedir_adouble(VFS_FUNC_ARGS_RENAMEDIR)
128 {
129     return 0;
130 }
131
132 /* ----------------- */
133 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask)
134 {
135     struct stat st;
136     int         err;
137     
138     /* bail if the file exists in the current directory.
139      * note: this will not fail with dangling symlinks */
140     
141     if (lstat(de->d_name, &st) == 0)
142         return AFPERR_DIRNEMPT;
143
144     if ((err = netatalk_unlink(name)))
145         return err;
146
147     return 0;
148 }
149
150 static int RF_deletecurdir_adouble(VFS_FUNC_ARGS_DELETECURDIR)
151 {
152     int err;
153
154     /* delete stray .AppleDouble files. this happens to get .Parent files
155        as well. */
156     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1, vol->v_umask))) 
157         return err;
158     return netatalk_rmdir(-1, ".AppleDouble" );
159 }
160
161 /* ----------------- */
162 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
163 {
164     return setfilmode(name, ad_hf_mode(mode), st, v_umask);
165 }
166
167 static int RF_setfilmode_adouble(VFS_FUNC_ARGS_SETFILEMODE)
168 {
169     return adouble_setfilmode(vol->ad_path(name, ADFLAGS_HF ), mode, st, vol->v_umask);
170 }
171
172 /* ----------------- */
173 static int RF_setdirunixmode_adouble(VFS_FUNC_ARGS_SETDIRUNIXMODE)
174 {
175     const char *adouble = vol->ad_path(name, ADFLAGS_DIR );
176     int  dropbox = vol->v_flags;
177
178     if (dir_rx_set(mode)) {
179         if (chmod_acl(ad_dir(adouble), (DIRBITS | mode) & ~vol->v_umask) < 0 ) 
180             return -1;
181     }
182
183     if (adouble_setfilmode(vol->ad_path(name, ADFLAGS_DIR ), mode, st, vol->v_umask) < 0) 
184         return -1;
185
186     if (!dir_rx_set(mode)) {
187         if (chmod_acl(ad_dir(adouble), (DIRBITS | mode) & ~vol->v_umask) < 0 ) 
188             return  -1 ;
189     }
190     return 0;
191 }
192
193 /* ----------------- */
194 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag, mode_t v_umask)
195 {
196     mode_t hf_mode = *(mode_t *)data;
197     struct stat st;
198
199     if ( stat( name, &st ) < 0 ) {
200         if (flag)
201             return 0;
202         LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
203     }
204     else if (!S_ISDIR(st.st_mode)) {
205         if (setfilmode(name, hf_mode , &st, v_umask) < 0) {
206                /* FIXME what do we do then? */
207         }
208     }
209     return 0;
210 }
211
212 static int RF_setdirmode_adouble(VFS_FUNC_ARGS_SETDIRMODE)
213 {
214     int   dropbox = vol->v_flags;
215     mode_t hf_mode = ad_hf_mode(mode);
216     const char  *adouble = vol->ad_path(name, ADFLAGS_DIR );
217     const char  *adouble_p = ad_dir(adouble);
218
219     if (dir_rx_set(mode)) {
220         if (chmod_acl(ad_dir(adouble), (DIRBITS | mode) & ~vol->v_umask) < 0) 
221             return -1;
222     }
223
224     if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, 0, vol->v_umask))
225         return -1;
226
227     if (!dir_rx_set(mode)) {
228         if (chmod_acl(ad_dir(adouble), (DIRBITS | mode) & ~vol->v_umask) < 0) 
229             return  -1 ;
230     }
231     return 0;
232 }
233
234 static int RF_setdirowner_adouble(VFS_FUNC_ARGS_SETDIROWNER)
235 {
236     if (lchown(".AppleDouble", uid, gid) < 0 && errno != EPERM ) {
237         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
238             uid, gid,fullpathname(".AppleDouble"), strerror(errno));
239     }
240     return 0;
241 }
242
243 /* ----------------- */
244 static int RF_deletefile_adouble(VFS_FUNC_ARGS_DELETEFILE)
245 {
246         return netatalk_unlinkat(dirfd, vol->ad_path(file, ADFLAGS_HF));
247 }
248
249 /* ----------------- */
250 static int RF_renamefile_adouble(VFS_FUNC_ARGS_RENAMEFILE)
251 {
252     char  adsrc[ MAXPATHLEN + 1];
253     int   err = 0;
254
255     strcpy( adsrc, vol->ad_path(src, 0 ));
256     if (unix_rename(dirfd, adsrc, -1, vol->ad_path(dst, 0 )) < 0) {
257         struct stat st;
258
259         err = errno;
260         if (errno == ENOENT) {
261                 struct adouble    ad;
262
263             if (lstatat(dirfd, adsrc, &st)) /* source has no ressource fork, */
264                 return 0;
265
266             /* We are here  because :
267              * -there's no dest folder. 
268              * -there's no .AppleDouble in the dest folder.
269              * if we use the struct adouble passed in parameter it will not
270              * create .AppleDouble if the file is already opened, so we
271              * use a diff one, it's not a pb,ie it's not the same file, yet.
272              */
273             ad_init(&ad, vol); 
274             if (ad_open(&ad, dst, ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) == 0) {
275                 ad_close(&ad, ADFLAGS_HF);
276                 if (!unix_rename(dirfd, adsrc, -1, vol->ad_path(dst, 0 )) ) 
277                    err = 0;
278                 else 
279                    err = errno;
280             }
281             else { /* it's something else, bail out */
282                     err = errno;
283                 }
284             }
285         }
286         if (err) {
287                 errno = err;
288                 return -1;
289         }
290         return 0;
291 }
292
293 static int RF_copyfile_adouble(VFS_FUNC_ARGS_COPYFILE)
294 /* const struct vol *vol, int sfd, const char *src, const char *dst */
295 {
296     EC_INIT;
297     bstring s = NULL, d = NULL;
298     char *dup1 = NULL;
299     char *dup2 = NULL;
300     char *dup3 = NULL;
301     char *dup4 = NULL;
302     const char *name = NULL;
303     const char *dir = NULL;
304
305     struct stat st;
306     EC_ZERO(stat(dst, &st));
307
308     if (S_ISDIR(st.st_mode)) {
309         /* build src path to AppleDouble file*/
310         EC_NULL(s = bfromcstr(src));
311         EC_ZERO(bcatcstr(s, "/.AppleDouble/.Parent"));
312
313         /* build dst path to AppleDouble file*/
314         EC_NULL(d = bfromcstr(dst));
315         EC_ZERO(bcatcstr(d, "/.AppleDouble/.Parent"));
316     } else {
317         /* get basename */
318
319         /* build src path to AppleDouble file*/
320         EC_NULL(dup1 = strdup(src));
321         EC_NULL(name = basename(strdup(dup1)));
322
323         EC_NULL(dup2 = strdup(src));
324         EC_NULL(dir = dirname(dup2));
325         EC_NULL(s = bfromcstr(dir));
326         EC_ZERO(bcatcstr(s, "/.AppleDouble/"));
327         EC_ZERO(bcatcstr(s, name));
328
329         /* build dst path to AppleDouble file*/
330         EC_NULL(dup4 = strdup(dst));
331         EC_NULL(name = basename(strdup(dup4)));
332
333         EC_NULL(dup3 = strdup(dst));
334         EC_NULL(dir = dirname(dup3));
335         EC_NULL(d = bfromcstr(dir));
336         EC_ZERO(bcatcstr(d, "/.AppleDouble/"));
337         EC_ZERO(bcatcstr(d, name));
338     }
339
340     /* ignore errors */
341     if (copy_file(sfd, cfrombstr(s), cfrombstr(d), 0666) != 0)
342         if (errno != ENOENT)
343             EC_FAIL;
344
345 EC_CLEANUP:
346     bdestroy(s);
347     bdestroy(d);
348     if (dup1) free(dup1);
349     if (dup2) free(dup2);
350     if (dup3) free(dup3);
351     if (dup4) free(dup4);
352
353     EC_EXIT;
354 }
355
356 #ifdef HAVE_SOLARIS_ACLS
357 static int RF_solaris_acl(VFS_FUNC_ARGS_ACL)
358 {
359     static char buf[ MAXPATHLEN + 1];
360     struct stat st;
361     int len;
362
363     if ((stat(path, &st)) != 0)
364         return -1;
365     if (S_ISDIR(st.st_mode)) {
366         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
367         if (len < 0 || len >=  MAXPATHLEN)
368             return -1;
369         /* set acl on .AppleDouble dir first */
370         if ((acl(buf, cmd, count, aces)) != 0)
371             return -1;
372         /* now set ACL on ressource fork */
373         if ((acl(vol->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
374             return -1;
375     } else
376         /* set ACL on ressource fork */
377         if ((acl(vol->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
378             return -1;
379
380     return 0;
381 }
382
383 static int RF_solaris_remove_acl(VFS_FUNC_ARGS_REMOVE_ACL)
384 {
385     int ret;
386     static char buf[ MAXPATHLEN + 1];
387     int len;
388
389     if (dir) {
390         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
391         if (len < 0 || len >=  MAXPATHLEN)
392             return AFPERR_MISC;
393         /* remove ACL from .AppleDouble/.Parent first */
394         if ((ret = remove_acl_vfs(vol->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
395             return ret;
396         /* now remove from .AppleDouble dir */
397         if ((ret = remove_acl_vfs(buf)) != AFP_OK)
398             return ret;
399     } else
400         /* remove ACL from ressource fork */
401         if ((ret = remove_acl_vfs(vol->ad_path(path, ADFLAGS_HF))) != AFP_OK)
402             return ret;
403
404     return AFP_OK;
405 }
406 #endif
407
408 #ifdef HAVE_POSIX_ACLS
409 static int RF_posix_acl(VFS_FUNC_ARGS_ACL)
410 {
411     EC_INIT;
412     static char buf[ MAXPATHLEN + 1];
413     struct stat st;
414     int len;
415
416     if (S_ISDIR(st.st_mode)) {
417         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
418         if (len < 0 || len >=  MAXPATHLEN)
419             EC_FAIL;
420         /* set acl on .AppleDouble dir first */
421         EC_ZERO_LOG(acl_set_file(buf, type, acl));
422
423         if (type == ACL_TYPE_ACCESS)
424             /* set ACL on ressource fork (".Parent") too */
425             EC_ZERO_LOG(acl_set_file(vol->ad_path(path, ADFLAGS_DIR), type, acl));
426     } else {
427         /* set ACL on ressource fork */
428         EC_ZERO_LOG(acl_set_file(vol->ad_path(path, ADFLAGS_HF), type, acl));
429     }
430     
431 EC_CLEANUP:
432     if (ret != 0)
433         return AFPERR_MISC;
434     return AFP_OK;
435 }
436
437 static int RF_posix_remove_acl(VFS_FUNC_ARGS_REMOVE_ACL)
438 {
439     EC_INIT;
440     static char buf[ MAXPATHLEN + 1];
441     int len;
442
443     if (dir) {
444         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
445         if (len < 0 || len >=  MAXPATHLEN)
446             return AFPERR_MISC;
447         /* remove ACL from .AppleDouble/.Parent first */
448         EC_ZERO_LOG_ERR(remove_acl_vfs(vol->ad_path(path, ADFLAGS_DIR)), AFPERR_MISC);
449
450         /* now remove from .AppleDouble dir */
451         EC_ZERO_LOG_ERR(remove_acl_vfs(buf), AFPERR_MISC);
452     } else {
453         /* remove ACL from ressource fork */
454         EC_ZERO_LOG_ERR(remove_acl_vfs(vol->ad_path(path, ADFLAGS_HF)), AFPERR_MISC);
455     }
456
457 EC_CLEANUP:
458     EC_EXIT;
459 }
460 #endif
461
462 /*************************************************************************
463  * EA adouble format 
464  ************************************************************************/
465 static int validupath_ea(VFS_FUNC_ARGS_VALIDUPATH)
466 {
467     if (name[0] != '.')
468         return 1;
469     
470     if (!(vol->v_flags & AFPVOL_USEDOTS))
471         return 0;
472
473 #ifndef HAVE_EAFD
474     if (name[1] == '_')
475         return ad_valid_header_osx(name);
476 #endif
477     return netatalk_name(name);
478 }
479
480 /* ----------------- */
481 static int RF_chown_ea(VFS_FUNC_ARGS_CHOWN)
482 {
483 #ifndef HAVE_EAFD
484     return chown(vol->ad_path(path, ADFLAGS_HF ), uid, gid);
485 #endif
486     return 0;
487 }
488
489 /* ---------------- */
490 static int RF_renamedir_ea(VFS_FUNC_ARGS_RENAMEDIR)
491 {
492     return 0;
493 }
494
495 /* Returns 1 if the entry is NOT an ._ file */
496 static int deletecurdir_ea_osx_chkifempty_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask _U_)
497 {
498     if (de->d_name[0] != '.' || de->d_name[0] == '_')
499         return 1;
500
501     return 0;
502 }
503
504 static int deletecurdir_ea_osx_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask _U_)
505 {
506     int ret;
507     
508     if ((ret = netatalk_unlink(name)) != 0)
509         return ret;
510
511     return 0;
512 }
513
514 /* ---------------- */
515 static int RF_deletecurdir_ea(VFS_FUNC_ARGS_DELETECURDIR)
516 {
517 #ifndef HAVE_EAFD
518     int err;
519     /* delete stray ._AppleDouble files */
520
521     /* first check if there's really no other file besides files starting with ._ */
522     if ((err = for_each_adouble("deletecurdir_ea_osx", ".",
523                                 deletecurdir_ea_osx_chkifempty_loop,
524                                 NULL, 0, 0)) != 0) {
525         if (err == 1)
526             return AFPERR_DIRNEMPT;
527         return AFPERR_MISC;
528     }
529
530     /* Now delete orphaned ._ files */
531     if ((err = for_each_adouble("deletecurdir_ea_osx", ".",
532                                 deletecurdir_ea_osx_loop,
533                                 NULL, 0, 0)) != 0)
534         return err;
535
536 #endif
537     return 0;
538 }
539
540 /* ---------------- */
541 static int RF_setdirunixmode_ea(VFS_FUNC_ARGS_SETDIRUNIXMODE)
542 {
543 #ifndef HAVE_EAFD
544 #endif
545     return 0;
546 }
547
548 static int RF_setfilmode_ea(VFS_FUNC_ARGS_SETFILEMODE)
549 {
550 #ifndef HAVE_EAFD
551     return adouble_setfilmode(vol->ad_path(name, ADFLAGS_HF ), mode, st, vol->v_umask);
552 #endif
553     return 0;
554 }
555
556 /* ---------------- */
557 static int RF_setdirmode_ea(VFS_FUNC_ARGS_SETDIRMODE)
558 {
559 #ifndef HAVE_EAFD
560 #endif
561     return 0;
562 }
563
564 /* ---------------- */
565 static int RF_setdirowner_ea(VFS_FUNC_ARGS_SETDIROWNER)
566 {
567 #ifndef HAVE_EAFD
568 #endif
569         return 0;
570 }
571
572 static int RF_deletefile_ea(VFS_FUNC_ARGS_DELETEFILE)
573 {
574 #ifndef HAVE_EAFD
575         return netatalk_unlinkat(dirfd, vol->ad_path(file, ADFLAGS_HF));
576 #endif
577     return 0;
578 }
579 static int RF_copyfile_ea(VFS_FUNC_ARGS_COPYFILE)
580 {
581 #ifdef HAVE_EAFD
582     /* the EA VFS module does this all for us */
583     return 0;
584 #endif
585
586     EC_INIT;
587     bstring s = NULL, d = NULL;
588     char *dup1 = NULL;
589     char *dup2 = NULL;
590     char *dup3 = NULL;
591     char *dup4 = NULL;
592     const char *name = NULL;
593     const char *dir = NULL;
594
595     /* get basename */
596
597     /* build src path to ._ file*/
598     EC_NULL(dup1 = strdup(src));
599     EC_NULL(name = basename(strdup(dup1)));
600
601     EC_NULL(dup2 = strdup(src));
602     EC_NULL(dir = dirname(dup2));
603     EC_NULL(s = bfromcstr(dir));
604     EC_ZERO(bcatcstr(s, "/._"));
605     EC_ZERO(bcatcstr(s, name));
606
607     /* build dst path to ._file*/
608     EC_NULL(dup4 = strdup(dst));
609     EC_NULL(name = basename(strdup(dup4)));
610
611     EC_NULL(dup3 = strdup(dst));
612     EC_NULL(dir = dirname(dup3));
613     EC_NULL(d = bfromcstr(dir));
614     EC_ZERO(bcatcstr(d, "/._"));
615     EC_ZERO(bcatcstr(d, name));
616
617     if (copy_file(sfd, cfrombstr(s), cfrombstr(d), 0666) != 0) {
618         switch (errno) {
619         case ENOENT:
620             break;
621         default:
622             LOG(log_error, logtype_afpd, "[VFS] copyfile(\"%s\" -> \"%s\"): %s",
623                 cfrombstr(s), cfrombstr(d), strerror(errno));
624             EC_FAIL;
625                     
626         }
627     }
628
629 EC_CLEANUP:
630     bdestroy(s);
631     bdestroy(d);
632     free(dup1);
633     free(dup2);
634     free(dup3);
635     free(dup4);
636     EC_EXIT;
637 }
638
639 /* ---------------- */
640 static int RF_renamefile_ea(VFS_FUNC_ARGS_RENAMEFILE)
641 {
642 #ifndef HAVE_EAFD
643     char  adsrc[ MAXPATHLEN + 1];
644     int   err = 0;
645
646     strcpy( adsrc, vol->ad_path(src, 0 ));
647
648     if (unix_rename(dirfd, adsrc, -1, vol->ad_path(dst, 0 )) < 0) {
649         struct stat st;
650
651         err = errno;
652         if (errno == ENOENT && lstatat(dirfd, adsrc, &st)) /* source has no ressource fork, */
653             return 0;
654         errno = err;
655         return -1;
656     }
657     return 0;
658 #endif
659     return 0;
660 }
661
662 /********************************************************************************************
663  * VFS chaining
664  ********************************************************************************************/
665
666 /* 
667  * Up until we really start stacking many VFS modules on top of one another or use
668  * dynamic module loading like we do for UAMs, up until then we just stack VFS modules
669  * via an fixed size array.
670  * All VFS funcs must return AFP_ERR codes. When a func in the chain returns an error
671  * this error code will be returned to the caller, BUT the chain in followed and all
672  * following funcs are called in order to give them a chance.
673  */
674
675 /* 
676  * Define most VFS funcs with macros as they all do the same.
677  * Only "ad_path" and "validupath" will NOT do stacking and only
678  * call the func from the first module.
679  */
680
681 #define VFS_MFUNC(name, args, vars) \
682     static int vfs_ ## name(args) \
683     { \
684         int i = 0, ret = AFP_OK, err; \
685         while (vol->vfs_modules[i]) { \
686             if (vol->vfs_modules[i]->vfs_ ## name) { \
687                 err = vol->vfs_modules[i]->vfs_ ## name (vars); \
688                 if ((ret == AFP_OK) && (err != AFP_OK)) \
689                     ret = err; \
690             } \
691             i ++; \
692         } \
693         return ret; \
694     }
695
696 VFS_MFUNC(chown, VFS_FUNC_ARGS_CHOWN, VFS_FUNC_VARS_CHOWN)
697 VFS_MFUNC(renamedir, VFS_FUNC_ARGS_RENAMEDIR, VFS_FUNC_VARS_RENAMEDIR) 
698 VFS_MFUNC(deletecurdir, VFS_FUNC_ARGS_DELETECURDIR, VFS_FUNC_VARS_DELETECURDIR)
699 VFS_MFUNC(setfilmode, VFS_FUNC_ARGS_SETFILEMODE, VFS_FUNC_VARS_SETFILEMODE)
700 VFS_MFUNC(setdirmode, VFS_FUNC_ARGS_SETDIRMODE, VFS_FUNC_VARS_SETDIRMODE)
701 VFS_MFUNC(setdirunixmode, VFS_FUNC_ARGS_SETDIRUNIXMODE, VFS_FUNC_VARS_SETDIRUNIXMODE)
702 VFS_MFUNC(setdirowner, VFS_FUNC_ARGS_SETDIROWNER, VFS_FUNC_VARS_SETDIROWNER)
703 VFS_MFUNC(deletefile, VFS_FUNC_ARGS_DELETEFILE, VFS_FUNC_VARS_DELETEFILE)
704 VFS_MFUNC(renamefile, VFS_FUNC_ARGS_RENAMEFILE, VFS_FUNC_VARS_RENAMEFILE)
705 VFS_MFUNC(copyfile, VFS_FUNC_ARGS_COPYFILE, VFS_FUNC_VARS_COPYFILE)
706 #ifdef HAVE_ACLS
707 VFS_MFUNC(acl, VFS_FUNC_ARGS_ACL, VFS_FUNC_VARS_ACL)
708 VFS_MFUNC(remove_acl, VFS_FUNC_ARGS_REMOVE_ACL, VFS_FUNC_VARS_REMOVE_ACL)
709 #endif
710 VFS_MFUNC(ea_getsize, VFS_FUNC_ARGS_EA_GETSIZE, VFS_FUNC_VARS_EA_GETSIZE)
711 VFS_MFUNC(ea_getcontent, VFS_FUNC_ARGS_EA_GETCONTENT, VFS_FUNC_VARS_EA_GETCONTENT)
712 VFS_MFUNC(ea_list, VFS_FUNC_ARGS_EA_LIST, VFS_FUNC_VARS_EA_LIST)
713 VFS_MFUNC(ea_set, VFS_FUNC_ARGS_EA_SET, VFS_FUNC_VARS_EA_SET)
714 VFS_MFUNC(ea_remove, VFS_FUNC_ARGS_EA_REMOVE, VFS_FUNC_VARS_EA_REMOVE)
715
716 static int vfs_validupath(VFS_FUNC_ARGS_VALIDUPATH)
717 {
718     return vol->vfs_modules[0]->vfs_validupath(VFS_FUNC_VARS_VALIDUPATH);
719 }
720
721 /*
722  * These function pointers get called from the lib users via vol->vfs->func.
723  * These funcs are defined via the macros above.
724  */
725 static struct vfs_ops vfs_master_funcs = {
726     vfs_validupath,
727     vfs_chown,
728     vfs_renamedir,
729     vfs_deletecurdir,
730     vfs_setfilmode,
731     vfs_setdirmode,
732     vfs_setdirunixmode,
733     vfs_setdirowner,
734     vfs_deletefile,
735     vfs_renamefile,
736     vfs_copyfile,
737 #ifdef HAVE_ACLS
738     vfs_acl,
739     vfs_remove_acl,
740 #endif
741     vfs_ea_getsize,
742     vfs_ea_getcontent,
743     vfs_ea_list,
744     vfs_ea_set,
745     vfs_ea_remove
746 };
747
748 /* 
749  * Primary adouble modules: v2, ea
750  */
751
752 static struct vfs_ops netatalk_adouble_v2 = {
753     /* vfs_validupath:    */ validupath_adouble,
754     /* vfs_chown:         */ RF_chown_adouble,
755     /* vfs_renamedir:     */ RF_renamedir_adouble,
756     /* vfs_deletecurdir:  */ RF_deletecurdir_adouble,
757     /* vfs_setfilmode:    */ RF_setfilmode_adouble,
758     /* vfs_setdirmode:    */ RF_setdirmode_adouble,
759     /* vfs_setdirunixmode:*/ RF_setdirunixmode_adouble,
760     /* vfs_setdirowner:   */ RF_setdirowner_adouble,
761     /* vfs_deletefile:    */ RF_deletefile_adouble,
762     /* vfs_renamefile:    */ RF_renamefile_adouble,
763     /* vfs_copyfile:      */ RF_copyfile_adouble,
764     NULL
765 };
766
767 static struct vfs_ops netatalk_adouble_ea = {
768     /* vfs_validupath:    */ validupath_ea,
769     /* vfs_chown:         */ RF_chown_ea,
770     /* vfs_renamedir:     */ RF_renamedir_ea,
771     /* vfs_deletecurdir:  */ RF_deletecurdir_ea,
772     /* vfs_setfilmode:    */ RF_setfilmode_ea,
773     /* vfs_setdirmode:    */ RF_setdirmode_ea,
774     /* vfs_setdirunixmode:*/ RF_setdirunixmode_ea,
775     /* vfs_setdirowner:   */ RF_setdirowner_ea,
776     /* vfs_deletefile:    */ RF_deletefile_ea,
777     /* vfs_renamefile:    */ RF_renamefile_ea,
778     /* vfs_copyfile:      */ RF_copyfile_ea,
779     NULL
780 };
781
782 /* 
783  * Secondary vfs modules for Extended Attributes
784  */
785
786 static struct vfs_ops netatalk_ea_adouble = {
787     /* vfs_validupath:    */ NULL,
788     /* vfs_chown:         */ ea_chown,
789     /* vfs_renamedir:     */ NULL, /* ok */
790     /* vfs_deletecurdir:  */ NULL, /* ok */
791     /* vfs_setfilmode:    */ ea_chmod_file,
792     /* vfs_setdirmode:    */ NULL, /* ok */
793     /* vfs_setdirunixmode:*/ ea_chmod_dir,
794     /* vfs_setdirowner:   */ NULL, /* ok */
795     /* vfs_deletefile:    */ ea_deletefile,
796     /* vfs_renamefile:    */ ea_renamefile,
797     /* vfs_copyfile       */ ea_copyfile,
798 #ifdef HAVE_ACLS
799     /* vfs_acl:           */ NULL,
800     /* vfs_remove_acl     */ NULL,
801 #endif
802     /* vfs_getsize        */ get_easize,
803     /* vfs_getcontent     */ get_eacontent,
804     /* vfs_list           */ list_eas,
805     /* vfs_set            */ set_ea,
806     /* vfs_remove         */ remove_ea
807 };
808
809 static struct vfs_ops netatalk_ea_sys = {
810     /* validupath:        */ NULL,
811     /* rf_chown:          */ NULL,
812     /* rf_renamedir:      */ NULL,
813     /* rf_deletecurdir:   */ NULL,
814     /* rf_setfilmode:     */ NULL,
815     /* rf_setdirmode:     */ NULL,
816     /* rf_setdirunixmode: */ NULL,
817     /* rf_setdirowner:    */ NULL,
818     /* rf_deletefile:     */ NULL,
819     /* rf_renamefile:     */ NULL,
820     /* vfs_copyfile:      */ sys_ea_copyfile,
821 #ifdef HAVE_ACLS
822     /* rf_acl:            */ NULL,
823     /* rf_remove_acl      */ NULL,
824 #endif
825     /* ea_getsize         */ sys_get_easize,
826     /* ea_getcontent      */ sys_get_eacontent,
827     /* ea_list            */ sys_list_eas,
828     /* ea_set             */ sys_set_ea,
829     /* ea_remove          */ sys_remove_ea
830 };
831
832 /* 
833  * Tertiary VFS modules for ACLs
834  */
835
836 #ifdef HAVE_SOLARIS_ACLS
837 static struct vfs_ops netatalk_solaris_acl_adouble = {
838     /* validupath:        */ NULL,
839     /* rf_chown:          */ NULL,
840     /* rf_renamedir:      */ NULL,
841     /* rf_deletecurdir:   */ NULL,
842     /* rf_setfilmode:     */ NULL,
843     /* rf_setdirmode:     */ NULL,
844     /* rf_setdirunixmode: */ NULL,
845     /* rf_setdirowner:    */ NULL,
846     /* rf_deletefile:     */ NULL,
847     /* rf_renamefile:     */ NULL,
848     /* vfs_copyfile       */ NULL,
849     /* rf_acl:            */ RF_solaris_acl,
850     /* rf_remove_acl      */ RF_solaris_remove_acl,
851     NULL
852 };
853 #endif
854
855 #ifdef HAVE_POSIX_ACLS
856 static struct vfs_ops netatalk_posix_acl_adouble = {
857     /* validupath:        */ NULL,
858     /* rf_chown:          */ NULL,
859     /* rf_renamedir:      */ NULL,
860     /* rf_deletecurdir:   */ NULL,
861     /* rf_setfilmode:     */ NULL,
862     /* rf_setdirmode:     */ NULL,
863     /* rf_setdirunixmode: */ NULL,
864     /* rf_setdirowner:    */ NULL,
865     /* rf_deletefile:     */ NULL,
866     /* rf_renamefile:     */ NULL,
867     /* vfs_copyfile       */ NULL,
868     /* rf_acl:            */ RF_posix_acl,
869     /* rf_remove_acl      */ RF_posix_remove_acl,
870     NULL
871 };
872 #endif
873
874 /* ---------------- */
875 void initvol_vfs(struct vol *vol)
876 {
877     vol->vfs = &vfs_master_funcs;
878
879     /* Default adouble stuff */
880     if (vol->v_adouble == AD_VERSION2) {
881         vol->vfs_modules[0] = &netatalk_adouble_v2;
882         vol->ad_path = ad_path;
883     } else {
884         vol->vfs_modules[0] = &netatalk_adouble_ea;
885 #ifdef HAVE_EAFD
886         vol->ad_path = ad_path_ea;
887 #else
888         vol->ad_path = ad_path_osx;
889 #endif
890     }
891
892     /* Extended Attributes */
893     if (vol->v_vfs_ea == AFPVOL_EA_SYS) {
894         LOG(log_debug, logtype_afpd, "initvol_vfs: enabling EA support with native EAs");
895         vol->vfs_modules[1] = &netatalk_ea_sys;
896     } else if (vol->v_vfs_ea == AFPVOL_EA_AD) {
897         LOG(log_debug, logtype_afpd, "initvol_vfs: enabling EA support with adouble files");
898         vol->vfs_modules[1] = &netatalk_ea_adouble;
899     } else {
900         LOG(log_debug, logtype_afpd, "initvol_vfs: volume without EA support");
901     }
902
903     /* ACLs */
904 #ifdef HAVE_SOLARIS_ACLS
905     vol->vfs_modules[2] = &netatalk_solaris_acl_adouble;
906 #endif
907 #ifdef HAVE_POSIX_ACLS
908     vol->vfs_modules[2] = &netatalk_posix_acl_adouble;
909 #endif
910
911 }