]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/vfs.c
Fix NFSv4 ACL VFS integration
[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 <string.h>
25 #include <stdio.h>
26
27 #include <atalk/afp.h>    
28 #include <atalk/adouble.h>
29 #include <atalk/ea.h>
30 #include <atalk/acl.h>
31 #include <atalk/logger.h>
32 #include <atalk/util.h>
33 #include <atalk/volume.h>
34 #include <atalk/vfs.h>
35 #include <atalk/directory.h>
36 #include <atalk/unix.h>
37
38 struct perm {
39     uid_t uid;
40     gid_t gid;
41 };
42
43 typedef int (*rf_loop)(struct dirent *, char *, void *, int , mode_t );
44
45 /* ----------------------------- */
46 static int 
47 for_each_adouble(const char *from, const char *name, rf_loop fn, void *data, int flag, mode_t v_umask)
48 {
49     char            buf[ MAXPATHLEN + 1];
50     char            *m;
51     DIR             *dp;
52     struct dirent   *de;
53     int             ret;
54     
55
56     if (NULL == ( dp = opendir( name)) ) {
57         if (!flag) {
58             LOG(log_error, logtype_afpd, "%s: opendir %s: %s", from, fullpathname(name),strerror(errno) );
59             return -1;
60         }
61         return 0;
62     }
63     strlcpy( buf, name, sizeof(buf));
64     strlcat( buf, "/", sizeof(buf) );
65     m = strchr( buf, '\0' );
66     ret = 0;
67     while ((de = readdir(dp))) {
68         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
69                 continue;
70         }
71         
72         strlcat(buf, de->d_name, sizeof(buf));
73         if (fn && (ret = fn(de, buf, data, flag, v_umask))) {
74            closedir(dp);
75            return ret;
76         }
77         *m = 0;
78     }
79     closedir(dp);
80     return ret;
81 }
82
83 /*******************************************************************************
84  * classic adouble format 
85  *******************************************************************************/
86
87 static int netatalk_name(const char *name)
88 {
89     return strcasecmp(name,".AppleDouble") &&
90         strcasecmp(name,".AppleDB") &&
91         strcasecmp(name,".AppleDesktop");
92 }
93
94 static int validupath_adouble(VFS_FUNC_ARGS_VALIDUPATH)
95 {
96     if (name[0] != '.')
97         return 1;
98     
99     if (!(vol->v_flags & AFPVOL_USEDOTS))
100         return 0;
101         
102     return netatalk_name(name) && strcasecmp(name,".Parent");
103 }                                           
104
105 /* ----------------- */
106 static int RF_chown_adouble(VFS_FUNC_ARGS_CHOWN)
107 {
108     struct stat st;
109     char        *ad_p;
110
111     ad_p = vol->ad_path(path, ADFLAGS_HF );
112
113     if ( stat( ad_p, &st ) < 0 )
114         return 0; /* ignore */
115
116     return chown( ad_p, uid, gid );
117 }
118
119 /* ----------------- */
120 static int RF_renamedir_adouble(VFS_FUNC_ARGS_RENAMEDIR)
121 {
122     return 0;
123 }
124
125 /* ----------------- */
126 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask)
127 {
128     struct stat st;
129     int         err;
130     
131     /* bail if the file exists in the current directory.
132      * note: this will not fail with dangling symlinks */
133     
134     if (stat(de->d_name, &st) == 0)
135         return AFPERR_DIRNEMPT;
136
137     if ((err = netatalk_unlink(name)))
138         return err;
139
140     return 0;
141 }
142
143 static int RF_deletecurdir_adouble(VFS_FUNC_ARGS_DELETECURDIR)
144 {
145     int err;
146
147     /* delete stray .AppleDouble files. this happens to get .Parent files
148        as well. */
149     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1, vol->v_umask))) 
150         return err;
151     return netatalk_rmdir( ".AppleDouble" );
152 }
153
154 /* ----------------- */
155 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
156 {
157     return setfilmode(name, ad_hf_mode(mode), st, v_umask);
158 }
159
160 static int RF_setfilmode_adouble(VFS_FUNC_ARGS_SETFILEMODE)
161 {
162     return adouble_setfilmode(vol->ad_path(name, ADFLAGS_HF ), mode, st, vol->v_umask);
163 }
164
165 /* ----------------- */
166 static int RF_setdirunixmode_adouble(VFS_FUNC_ARGS_SETDIRUNIXMODE)
167 {
168     char *adouble = vol->ad_path(name, ADFLAGS_DIR );
169     int  dropbox = vol->v_flags;
170
171     if (dir_rx_set(mode)) {
172         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0 ) 
173             return -1;
174     }
175
176     if (adouble_setfilmode(vol->ad_path(name, ADFLAGS_DIR ), mode, st, vol->v_umask) < 0) 
177         return -1;
178
179     if (!dir_rx_set(mode)) {
180         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0 ) 
181             return  -1 ;
182     }
183     return 0;
184 }
185
186 /* ----------------- */
187 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag, mode_t v_umask)
188 {
189     mode_t hf_mode = *(mode_t *)data;
190     struct stat st;
191
192     if ( stat( name, &st ) < 0 ) {
193         if (flag)
194             return 0;
195         LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
196     }
197     else if (!S_ISDIR(st.st_mode)) {
198         if (setfilmode(name, hf_mode , &st, v_umask) < 0) {
199                /* FIXME what do we do then? */
200         }
201     }
202     return 0;
203 }
204
205 static int RF_setdirmode_adouble(VFS_FUNC_ARGS_SETDIRMODE)
206 {
207     int   dropbox = vol->v_flags;
208     mode_t hf_mode = ad_hf_mode(mode);
209     char  *adouble = vol->ad_path(name, ADFLAGS_DIR );
210     char  *adouble_p = ad_dir(adouble);
211
212     if (dir_rx_set(mode)) {
213         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0) 
214             return -1;
215     }
216
217     if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, vol_noadouble(vol), vol->v_umask))
218         return -1;
219
220     if (!dir_rx_set(mode)) {
221         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox, vol->v_umask) < 0) 
222             return  -1 ;
223     }
224     return 0;
225 }
226
227 /* ----------------- */
228 static int setdirowner_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag _U_, mode_t v_umask _U_)
229 {
230     struct perm   *owner  = data;
231
232     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
233          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
234                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
235          /* return ( -1 ); Sometimes this is okay */
236     }
237     return 0;
238 }
239
240 static int RF_setdirowner_adouble(VFS_FUNC_ARGS_SETDIROWNER)
241 {
242     int           noadouble = vol_noadouble(vol);
243     char          *adouble_p;
244     struct stat   st;
245     struct perm   owner;
246     
247     owner.uid = uid;
248     owner.gid = gid;
249
250     adouble_p = ad_dir(vol->ad_path(name, ADFLAGS_DIR ));
251
252     if (for_each_adouble("setdirowner", adouble_p, setdirowner_adouble_loop, &owner, noadouble, vol->v_umask)) 
253         return -1;
254
255     /*
256      * We cheat: we know that chown doesn't do anything.
257      */
258     if ( stat( ".AppleDouble", &st ) < 0) {
259         if (errno == ENOENT && noadouble)
260             return 0;
261         LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
262         return -1;
263     }
264     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
265         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
266             uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
267         /* return ( -1 ); Sometimes this is okay */
268     }
269     return 0;
270 }
271
272 /* ----------------- */
273 static int RF_deletefile_adouble(VFS_FUNC_ARGS_DELETEFILE)
274 {
275         return netatalk_unlink(vol->ad_path(file, ADFLAGS_HF));
276 }
277
278 /* ----------------- */
279 static int RF_renamefile_adouble(VFS_FUNC_ARGS_RENAMEFILE)
280 {
281     char  adsrc[ MAXPATHLEN + 1];
282     int   err = 0;
283
284     strcpy( adsrc, vol->ad_path(src, 0 ));
285     if (unix_rename( adsrc, vol->ad_path(dst, 0 )) < 0) {
286         struct stat st;
287
288         err = errno;
289         if (errno == ENOENT) {
290                 struct adouble    ad;
291
292             if (stat(adsrc, &st)) /* source has no ressource fork, */
293                 return 0;
294             
295             /* We are here  because :
296              * -there's no dest folder. 
297              * -there's no .AppleDouble in the dest folder.
298              * if we use the struct adouble passed in parameter it will not
299              * create .AppleDouble if the file is already opened, so we
300              * use a diff one, it's not a pb,ie it's not the same file, yet.
301              */
302             ad_init(&ad, vol->v_adouble, vol->v_ad_options); 
303             if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
304                 ad_close(&ad, ADFLAGS_HF);
305                 if (!unix_rename( adsrc, vol->ad_path(dst, 0 )) ) 
306                    err = 0;
307                 else 
308                    err = errno;
309             }
310             else { /* it's something else, bail out */
311                     err = errno;
312                 }
313             }
314         }
315         if (err) {
316                 errno = err;
317                 return -1;
318         }
319         return 0;
320 }
321
322 #ifdef HAVE_NFSv4_ACLS
323 static int RF_solaris_acl(VFS_FUNC_ARGS_ACL)
324 {
325     static char buf[ MAXPATHLEN + 1];
326     struct stat st;
327     int len;
328
329     if ((stat(path, &st)) != 0)
330         return -1;
331     if (S_ISDIR(st.st_mode)) {
332         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
333         if (len < 0 || len >=  MAXPATHLEN)
334             return -1;
335         /* set acl on .AppleDouble dir first */
336         if ((acl(buf, cmd, count, aces)) != 0)
337             return -1;
338         /* now set ACL on ressource fork */
339         if ((acl(vol->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
340             return -1;
341     } else
342         /* set ACL on ressource fork */
343         if ((acl(vol->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
344             return -1;
345
346     return 0;
347 }
348
349 static int RF_solaris_remove_acl(VFS_FUNC_ARGS_REMOVE_ACL)
350 {
351     int ret;
352     static char buf[ MAXPATHLEN + 1];
353     int len;
354
355     if (dir) {
356         len = snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path);
357         if (len < 0 || len >=  MAXPATHLEN)
358             return AFPERR_MISC;
359         /* remove ACL from .AppleDouble/.Parent first */
360         if ((ret = remove_acl(vol->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
361             return ret;
362         /* now remove from .AppleDouble dir */
363         if ((ret = remove_acl(buf)) != AFP_OK)
364             return ret;
365     } else
366         /* remove ACL from ressource fork */
367         if ((ret = remove_acl(vol->ad_path(path, ADFLAGS_HF))) != AFP_OK)
368             return ret;
369
370     return AFP_OK;
371 }
372 #endif
373
374 /*********************************************************************************
375  * sfm adouble format
376  *********************************************************************************/
377 static int ads_chown_loop(struct dirent *de _U_, char *name, void *data, int flag _U_, mode_t v_umask _U_)
378 {
379     struct perm   *owner  = data;
380     
381     if (chown( name , owner->uid, owner->gid ) < 0) {
382         return -1;
383     }
384     return 0;
385 }
386
387 static int RF_chown_ads(VFS_FUNC_ARGS_CHOWN)
388 {
389     struct        stat st;
390     char          *ad_p;
391     struct perm   owner;
392     
393     owner.uid = uid;
394     owner.gid = gid;
395
396
397     ad_p = ad_dir(vol->ad_path(path, ADFLAGS_HF ));
398
399     if ( stat( ad_p, &st ) < 0 ) {
400         /* ignore */
401         return 0;
402     }
403     
404     if (chown( ad_p, uid, gid ) < 0) {
405         return -1;
406     }
407     return for_each_adouble("chown_ads", ad_p, ads_chown_loop, &owner, 1, vol->v_umask);
408 }
409
410 /* --------------------------------- */
411 static int deletecurdir_ads1_loop(struct dirent *de _U_, char *name, void *data _U_, int flag _U_, mode_t v_umask _U_)
412 {
413     return netatalk_unlink(name);
414 }
415
416 static int ads_delete_rf(char *name)
417 {
418     int err;
419
420     if ((err = for_each_adouble("deletecurdir", name, deletecurdir_ads1_loop, NULL, 1, 0))) 
421         return err;
422     /* FIXME 
423      * it's a problem for a nfs mounted folder, there's .nfsxxx around
424      * for linux the following line solve it.
425      * but it could fail if rm .nfsxxx  create a new .nfsyyy :(
426     */
427     if ((err = for_each_adouble("deletecurdir", name, deletecurdir_ads1_loop, NULL, 1, 0))) 
428         return err;
429     return netatalk_rmdir(name);
430 }
431
432 static int deletecurdir_ads_loop(struct dirent *de, char *name, void *data _U_, int flag _U_, mode_t v_umask _U_)
433 {
434     struct stat st;
435     
436     /* bail if the file exists in the current directory.
437      * note: this will not fail with dangling symlinks */
438     
439     if (stat(de->d_name, &st) == 0) {
440         return AFPERR_DIRNEMPT;
441     }
442     return ads_delete_rf(name);
443 }
444
445 static int RF_deletecurdir_ads(VFS_FUNC_ARGS_DELETECURDIR)
446 {
447     int err;
448     
449     /* delete stray .AppleDouble files. this happens to get .Parent files as well. */
450     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_ads_loop, NULL, 1, 0))) 
451         return err;
452     return netatalk_rmdir( ".AppleDouble" );
453 }
454
455 /* ------------------- */
456 struct set_mode {
457     mode_t mode;
458     struct stat *st;
459 };
460
461 static int ads_setfilmode_loop(struct dirent *de _U_, char *name, void *data, int flag _U_, mode_t v_umask)
462 {
463     struct set_mode *param = data;
464
465     return setfilmode(name, param->mode, param->st, v_umask);
466 }
467
468 static int ads_setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
469 {
470     mode_t file_mode = ad_hf_mode(mode);
471     mode_t dir_mode = file_mode;
472     struct set_mode param;
473
474     if ((dir_mode & (S_IRUSR | S_IWUSR )))
475         dir_mode |= S_IXUSR;
476     if ((dir_mode & (S_IRGRP | S_IWGRP )))
477         dir_mode |= S_IXGRP;
478     if ((dir_mode & (S_IROTH | S_IWOTH )))
479         dir_mode |= S_IXOTH;    
480     
481         /* change folder */
482         dir_mode |= DIRBITS;
483     if (dir_rx_set(dir_mode)) {
484         if (chmod( name,  dir_mode ) < 0)
485             return -1;
486     }
487     param.st = st;
488     param.mode = file_mode;
489     if (for_each_adouble("setfilmode_ads", name, ads_setfilmode_loop, &param, 0, v_umask) < 0)
490         return -1;
491
492     if (!dir_rx_set(dir_mode)) {
493         if (chmod( name,  dir_mode ) < 0)
494             return -1;
495     }
496
497     return 0;
498 }
499
500 static int RF_setfilmode_ads(VFS_FUNC_ARGS_SETFILEMODE)
501 {
502     return ads_setfilmode(ad_dir(vol->ad_path(name, ADFLAGS_HF )), mode, st, vol->v_umask);
503 }
504
505 /* ------------------- */
506 static int RF_setdirunixmode_ads(VFS_FUNC_ARGS_SETDIRUNIXMODE)
507 {
508     char *adouble = vol->ad_path(name, ADFLAGS_DIR );
509     char   ad_p[ MAXPATHLEN + 1];
510     int dropbox = vol->v_flags;
511
512     strlcpy(ad_p,ad_dir(adouble), MAXPATHLEN + 1);
513
514     if (dir_rx_set(mode)) {
515
516         /* .AppleDouble */
517         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, dropbox, vol->v_umask) < 0) 
518             return -1;
519
520         /* .AppleDouble/.Parent */
521         if (stickydirmode(ad_p, DIRBITS | mode, dropbox, vol->v_umask) < 0) 
522             return -1;
523     }
524
525     if (ads_setfilmode(ad_dir(vol->ad_path(name, ADFLAGS_DIR)), mode, st, vol->v_umask) < 0)
526         return -1;
527
528     if (!dir_rx_set(mode)) {
529         if (stickydirmode(ad_p, DIRBITS | mode, dropbox, vol->v_umask) < 0) 
530             return  -1 ;
531         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, dropbox, vol->v_umask) < 0) 
532             return -1;
533     }
534     return 0;
535 }
536
537 /* ------------------- */
538 struct dir_mode {
539     mode_t mode;
540     int    dropbox;
541 };
542
543 static int setdirmode_ads_loop(struct dirent *de _U_, char *name, void *data, int flag, mode_t v_umask)
544 {
545
546     struct dir_mode *param = data;
547     int    ret = 0; /* 0 ignore error, -1 */
548
549     if (dir_rx_set(param->mode)) {
550         if (stickydirmode(name, DIRBITS | param->mode, param->dropbox, v_umask) < 0) {
551             if (flag) {
552                 return 0;
553             }
554             return ret;
555         }
556     }
557     if (ads_setfilmode(name, param->mode, NULL, v_umask) < 0)
558         return ret;
559
560     if (!dir_rx_set(param->mode)) {
561         if (stickydirmode(name, DIRBITS | param->mode, param->dropbox, v_umask) < 0) {
562             if (flag) {
563                 return 0;
564             }
565             return ret;
566         }
567     }
568     return 0;
569 }
570
571 static int RF_setdirmode_ads(VFS_FUNC_ARGS_SETDIRMODE)
572 {
573     char *adouble = vol->ad_path(name, ADFLAGS_DIR );
574     char   ad_p[ MAXPATHLEN + 1];
575     struct dir_mode param;
576
577     param.mode = mode;
578     param.dropbox = vol->v_flags;
579
580     strlcpy(ad_p,ad_dir(adouble), sizeof(ad_p));
581
582     if (dir_rx_set(mode)) {
583         /* .AppleDouble */
584         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, param.dropbox, vol->v_umask) < 0) 
585             return -1;
586     }
587
588     if (for_each_adouble("setdirmode_ads", ad_dir(ad_p), setdirmode_ads_loop, &param, vol_noadouble(vol), vol->v_umask))
589         return -1;
590
591     if (!dir_rx_set(mode)) {
592         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, param.dropbox, vol->v_umask) < 0 ) 
593             return -1;
594     }
595     return 0;
596 }
597
598 /* ------------------- */
599 static int setdirowner_ads1_loop(struct dirent *de _U_, char *name, void *data, int flag _U_, mode_t v_umask _U_)
600 {
601     struct perm   *owner  = data;
602
603     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
604          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
605                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
606          /* return ( -1 ); Sometimes this is okay */
607     }
608     return 0;
609 }
610
611 static int setdirowner_ads_loop(struct dirent *de _U_, char *name, void *data, int flag, mode_t v_umask _U_)
612 {
613     struct perm   *owner  = data;
614
615     if (for_each_adouble("setdirowner", name, setdirowner_ads1_loop, data, flag, 0) < 0)
616         return -1;
617
618     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
619          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
620                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
621          /* return ( -1 ); Sometimes this is okay */
622     }
623     return 0;
624 }
625
626 static int RF_setdirowner_ads(VFS_FUNC_ARGS_SETDIROWNER)
627 {
628     int           noadouble = vol_noadouble(vol);
629     char          adouble_p[ MAXPATHLEN + 1];
630     struct stat   st;
631     struct perm   owner;
632     
633     owner.uid = uid;
634     owner.gid = gid;
635
636     strlcpy(adouble_p, ad_dir(vol->ad_path(name, ADFLAGS_DIR )), sizeof(adouble_p));
637
638     if (for_each_adouble("setdirowner", ad_dir(adouble_p), setdirowner_ads_loop, &owner, noadouble, 0)) 
639         return -1;
640
641     /*
642      * We cheat: we know that chown doesn't do anything.
643      */
644     if ( stat( ".AppleDouble", &st ) < 0) {
645         if (errno == ENOENT && noadouble)
646             return 0;
647         LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
648         return -1;
649     }
650     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
651         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
652             uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
653         /* return ( -1 ); Sometimes this is okay */
654     }
655     return 0;
656 }
657
658 /* ------------------- */
659 static int RF_deletefile_ads(VFS_FUNC_ARGS_DELETEFILE)
660 {
661     char *ad_p = ad_dir(vol->ad_path(file, ADFLAGS_HF ));
662
663     return ads_delete_rf(ad_p);
664 }
665
666 /* --------------------------- */
667 static int RF_renamefile_ads(VFS_FUNC_ARGS_RENAMEFILE)
668 {
669     char  adsrc[ MAXPATHLEN + 1];
670     int   err = 0;
671
672     strcpy( adsrc, ad_dir(vol->ad_path(src, 0 )));
673     if (unix_rename( adsrc, ad_dir(vol->ad_path(dst, 0 ))) < 0) {
674         struct stat st;
675
676         err = errno;
677         if (errno == ENOENT) {
678                 struct adouble    ad;
679
680             if (stat(adsrc, &st)) /* source has no ressource fork, */
681                 return 0;
682             
683             /* We are here  because :
684              * -there's no dest folder. 
685              * -there's no .AppleDouble in the dest folder.
686              * if we use the struct adouble passed in parameter it will not
687              * create .AppleDouble if the file is already opened, so we
688              * use a diff one, it's not a pb,ie it's not the same file, yet.
689              */
690             ad_init(&ad, vol->v_adouble, vol->v_ad_options); 
691             if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
692                 ad_close(&ad, ADFLAGS_HF);
693
694                 /* We must delete it */
695                 RF_deletefile_ads(vol, dst );
696                 if (!unix_rename( adsrc, ad_dir(vol->ad_path(dst, 0 ))) ) 
697                    err = 0;
698                 else 
699                    err = errno;
700             }
701             else { /* it's something else, bail out */
702                     err = errno;
703                 }
704             }
705         }
706         if (err) {
707                 errno = err;
708                 return -1;
709         }
710         return 0;
711 }
712
713 /*************************************************************************
714  * osx adouble format 
715  ************************************************************************/
716 static int validupath_osx(VFS_FUNC_ARGS_VALIDUPATH)
717 {
718     return strncmp(name,"._", 2) && (
719       (vol->v_flags & AFPVOL_USEDOTS) ? netatalk_name(name): name[0] != '.');
720 }             
721
722 /* ---------------- */
723 static int RF_renamedir_osx(VFS_FUNC_ARGS_RENAMEDIR)
724 {
725     /* We simply move the corresponding ad file as well */
726     char   tempbuf[258]="._";
727     return rename(vol->ad_path(oldpath,0),strcat(tempbuf,newpath));
728 }
729
730 /* ---------------- */
731 static int RF_deletecurdir_osx(VFS_FUNC_ARGS_DELETECURDIR)
732 {
733     return netatalk_unlink( vol->ad_path(".",0) );
734 }
735
736 /* ---------------- */
737 static int RF_setdirunixmode_osx(VFS_FUNC_ARGS_SETDIRUNIXMODE)
738 {
739     return adouble_setfilmode(vol->ad_path(name, ADFLAGS_DIR ), mode, st, vol->v_umask);
740 }
741
742 /* ---------------- */
743 static int RF_setdirmode_osx(VFS_FUNC_ARGS_SETDIRMODE)
744 {
745     return 0;
746 }
747
748 /* ---------------- */
749 static int RF_setdirowner_osx(VFS_FUNC_ARGS_SETDIROWNER)
750 {
751         return 0;
752 }
753
754 /* ---------------- */
755 static int RF_renamefile_osx(VFS_FUNC_ARGS_RENAMEFILE)
756 {
757     char  adsrc[ MAXPATHLEN + 1];
758     int   err = 0;
759
760     strcpy( adsrc, vol->ad_path(src, 0 ));
761
762     if (unix_rename( adsrc, vol->ad_path(dst, 0 )) < 0) {
763         struct stat st;
764
765         err = errno;
766         if (errno == ENOENT && stat(adsrc, &st)) /* source has no ressource fork, */
767             return 0;
768         errno = err;
769         return -1;
770     }
771     return 0;
772 }
773
774 /********************************************************************************************
775  * VFS chaining
776  ********************************************************************************************/
777
778 /* 
779  * Up until we really start stacking many VFS modules on top of one another or use
780  * dynamic module loading like we do for UAMs, up until then we just stack VFS modules
781  * via an fixed size array.
782  * All VFS funcs must return AFP_ERR codes. When a func in the chain returns an error
783  * this error code will be returned to the caller, BUT the chain in followed and all
784  * following funcs are called in order to give them a chance.
785  */
786
787 /* 
788  * Define most VFS funcs with macros as they all do the same.
789  * Only "ad_path" and "validupath" will NOT do stacking and only
790  * call the func from the first module.
791  */
792
793 #define VFS_MFUNC(name, args, vars) \
794     static int vfs_ ## name(args) \
795     { \
796         int i = 0, ret = AFP_OK, err; \
797         while (vol->vfs_modules[i]) { \
798             if (vol->vfs_modules[i]->vfs_ ## name) { \
799                 err = vol->vfs_modules[i]->vfs_ ## name (vars); \
800                 if ((ret == AFP_OK) && (err != AFP_OK)) \
801                     ret = err; \
802             } \
803             i ++; \
804         } \
805         return ret; \
806     }
807
808 VFS_MFUNC(chown, VFS_FUNC_ARGS_CHOWN, VFS_FUNC_VARS_CHOWN)
809 VFS_MFUNC(renamedir, VFS_FUNC_ARGS_RENAMEDIR, VFS_FUNC_VARS_RENAMEDIR) 
810 VFS_MFUNC(deletecurdir, VFS_FUNC_ARGS_DELETECURDIR, VFS_FUNC_VARS_DELETECURDIR)
811 VFS_MFUNC(setfilmode, VFS_FUNC_ARGS_SETFILEMODE, VFS_FUNC_VARS_SETFILEMODE)
812 VFS_MFUNC(setdirmode, VFS_FUNC_ARGS_SETDIRMODE, VFS_FUNC_VARS_SETDIRMODE)
813 VFS_MFUNC(setdirunixmode, VFS_FUNC_ARGS_SETDIRUNIXMODE, VFS_FUNC_VARS_SETDIRUNIXMODE)
814 VFS_MFUNC(setdirowner, VFS_FUNC_ARGS_SETDIROWNER, VFS_FUNC_VARS_SETDIROWNER)
815 VFS_MFUNC(deletefile, VFS_FUNC_ARGS_DELETEFILE, VFS_FUNC_VARS_DELETEFILE)
816 VFS_MFUNC(renamefile, VFS_FUNC_ARGS_RENAMEFILE, VFS_FUNC_VARS_RENAMEFILE)
817 VFS_MFUNC(copyfile, VFS_FUNC_ARGS_COPYFILE, VFS_FUNC_VARS_COPYFILE)
818 VFS_MFUNC(acl, VFS_FUNC_ARGS_ACL, VFS_FUNC_VARS_ACL)
819 VFS_MFUNC(remove_acl, VFS_FUNC_ARGS_REMOVE_ACL, VFS_FUNC_VARS_REMOVE_ACL)
820 VFS_MFUNC(ea_getsize, VFS_FUNC_ARGS_EA_GETSIZE, VFS_FUNC_VARS_EA_GETSIZE)
821 VFS_MFUNC(ea_getcontent, VFS_FUNC_ARGS_EA_GETCONTENT, VFS_FUNC_VARS_EA_GETCONTENT)
822 VFS_MFUNC(ea_list, VFS_FUNC_ARGS_EA_LIST, VFS_FUNC_VARS_EA_LIST)
823 VFS_MFUNC(ea_set, VFS_FUNC_ARGS_EA_SET, VFS_FUNC_VARS_EA_SET)
824 VFS_MFUNC(ea_remove, VFS_FUNC_ARGS_EA_REMOVE, VFS_FUNC_VARS_EA_REMOVE)
825
826 static int vfs_validupath(VFS_FUNC_ARGS_VALIDUPATH)
827 {
828     return vol->vfs_modules[0]->vfs_validupath(VFS_FUNC_VARS_VALIDUPATH);
829 }
830
831 /*
832  * These function pointers get called from the lib users via vol->vfs->func.
833  * These funcs are defined via the macros above.
834  */
835 static struct vfs_ops vfs_master_funcs = {
836     vfs_validupath,
837     vfs_chown,
838     vfs_renamedir,
839     vfs_deletecurdir,
840     vfs_setfilmode,
841     vfs_setdirmode,
842     vfs_setdirunixmode,
843     vfs_setdirowner,
844     vfs_deletefile,
845     vfs_renamefile,
846     vfs_copyfile,
847     vfs_acl,
848     vfs_remove_acl,
849     vfs_ea_getsize,
850     vfs_ea_getcontent,
851     vfs_ea_list,
852     vfs_ea_set,
853     vfs_ea_remove
854 };
855
856 /* 
857  * Primary adouble modules: default, osx, sfm
858  */
859
860 static struct vfs_ops netatalk_adouble = {
861     /* vfs_validupath:    */ validupath_adouble,
862     /* vfs_chown:         */ RF_chown_adouble,
863     /* vfs_renamedir:     */ RF_renamedir_adouble,
864     /* vfs_deletecurdir:  */ RF_deletecurdir_adouble,
865     /* vfs_setfilmode:    */ RF_setfilmode_adouble,
866     /* vfs_setdirmode:    */ RF_setdirmode_adouble,
867     /* vfs_setdirunixmode:*/ RF_setdirunixmode_adouble,
868     /* vfs_setdirowner:   */ RF_setdirowner_adouble,
869     /* vfs_deletefile:    */ RF_deletefile_adouble,
870     /* vfs_renamefile:    */ RF_renamefile_adouble,
871     NULL
872 };
873
874 static struct vfs_ops netatalk_adouble_osx = {
875     /* vfs_validupath:    */ validupath_osx,
876     /* vfs_chown:         */ RF_chown_adouble,
877     /* vfs_renamedir:     */ RF_renamedir_osx,
878     /* vfs_deletecurdir:  */ RF_deletecurdir_osx,
879     /* vfs_setfilmode:    */ RF_setfilmode_adouble,
880     /* vfs_setdirmode:    */ RF_setdirmode_osx,
881     /* vfs_setdirunixmode:*/ RF_setdirunixmode_osx,
882     /* vfs_setdirowner:   */ RF_setdirowner_osx,
883     /* vfs_deletefile:    */ RF_deletefile_adouble,
884     /* vfs_renamefile:    */ RF_renamefile_osx,
885     NULL
886 };
887
888 /* samba sfm format. ad_path shouldn't be set her */
889 static struct vfs_ops netatalk_adouble_sfm = {
890     /* vfs_validupath:    */ validupath_adouble,
891     /* vfs_chown:         */ RF_chown_ads,
892     /* vfs_renamedir:     */ RF_renamedir_adouble,
893     /* vfs_deletecurdir:  */ RF_deletecurdir_ads,
894     /* vfs_setfilmode:    */ RF_setfilmode_ads,
895     /* vfs_setdirmode:    */ RF_setdirmode_ads,
896     /* vfs_setdirunixmode:*/ RF_setdirunixmode_ads,
897     /* vfs_setdirowner:   */ RF_setdirowner_ads,
898     /* vfs_deletefile:    */ RF_deletefile_ads,
899     /* vfs_renamefile:    */ RF_renamefile_ads,
900     NULL
901 };
902
903 /* 
904  * Secondary vfs modules for Extended Attributes
905  */
906
907 static struct vfs_ops netatalk_ea_adouble = {
908     /* vfs_validupath:    */ NULL,
909     /* vfs_chown:         */ ea_chown,
910     /* vfs_renamedir:     */ NULL, /* ok */
911     /* vfs_deletecurdir:  */ NULL, /* ok */
912     /* vfs_setfilmode:    */ ea_chmod_file,
913     /* vfs_setdirmode:    */ NULL, /* ok */
914     /* vfs_setdirunixmode:*/ ea_chmod_dir,
915     /* vfs_setdirowner:   */ NULL, /* ok */
916     /* vfs_deletefile:    */ ea_deletefile,
917     /* vfs_renamefile:    */ ea_renamefile,
918     /* vfs_copyfile       */ ea_copyfile,
919     /* vfs_acl:           */ NULL,
920     /* vfs_remove_acl     */ NULL,
921     /* vfs_getsize        */ get_easize,
922     /* vfs_getcontent     */ get_eacontent,
923     /* vfs_list           */ list_eas,
924     /* vfs_set            */ set_ea,
925     /* vfs_remove         */ remove_ea
926 };
927
928 static struct vfs_ops netatalk_ea_sys = {
929     /* validupath:        */ NULL,
930     /* rf_chown:          */ NULL,
931     /* rf_renamedir:      */ NULL,
932     /* rf_deletecurdir:   */ NULL,
933     /* rf_setfilmode:     */ NULL,
934     /* rf_setdirmode:     */ NULL,
935     /* rf_setdirunixmode: */ NULL,
936     /* rf_setdirowner:    */ NULL,
937     /* rf_deletefile:     */ NULL,
938     /* rf_renamefile:     */ NULL,
939     /* vfs_copyfile:      */ sys_ea_copyfile,
940     /* rf_acl:            */ NULL,
941     /* rf_remove_acl      */ NULL,
942     /* ea_getsize         */ sys_get_easize,
943     /* ea_getcontent      */ sys_get_eacontent,
944     /* ea_list            */ sys_list_eas,
945     /* ea_set             */ sys_set_ea,
946     /* ea_remove          */ sys_remove_ea
947 };
948
949 /* 
950  * Tertiary VFS modules for ACLs
951  */
952
953 #ifdef HAVE_NFSv4_ACLS
954 static struct vfs_ops netatalk_solaris_acl_adouble = {
955     /* validupath:        */ NULL,
956     /* rf_chown:          */ NULL,
957     /* rf_renamedir:      */ NULL,
958     /* rf_deletecurdir:   */ NULL,
959     /* rf_setfilmode:     */ NULL,
960     /* rf_setdirmode:     */ NULL,
961     /* rf_setdirunixmode: */ NULL,
962     /* rf_setdirowner:    */ NULL,
963     /* rf_deletefile:     */ NULL,
964     /* rf_renamefile:     */ NULL,
965     /* vfs_copyfile       */ NULL,
966     /* rf_acl:            */ RF_solaris_acl,
967     /* rf_remove_acl      */ RF_solaris_remove_acl,
968     NULL
969 };
970 #endif
971
972 /* ---------------- */
973 void initvol_vfs(struct vol *vol)
974 {
975     vol->vfs = &vfs_master_funcs;
976
977     /* Default adouble stuff */
978     if (vol->v_adouble == AD_VERSION2_OSX) {
979         vol->vfs_modules[0] = &netatalk_adouble_osx;
980         vol->ad_path = ad_path_osx;
981     }
982     else if (vol->v_adouble == AD_VERSION1_SFM) {
983         vol->vfs_modules[0] = &netatalk_adouble_sfm;
984         vol->ad_path = ad_path_sfm;
985     }
986     else {
987         vol->vfs_modules[0] = &netatalk_adouble;
988         vol->ad_path = ad_path;
989     }
990
991     /* Extended Attributes */
992     if (vol->v_vfs_ea == AFPVOL_EA_SYS) {
993         LOG(log_debug, logtype_afpd, "initvol_vfs: Enabling EA support with native EAs.");
994         vol->vfs_modules[1] = &netatalk_ea_sys;
995     } else {
996         /* default: AFPVOL_EA_AD */
997         LOG(log_debug, logtype_afpd, "initvol_vfs: Enabling EA support with adouble files.");
998         vol->vfs_modules[1] = &netatalk_ea_adouble;
999     }
1000
1001     /* ACLs */
1002 #ifdef HAVE_NFSv4_ACLS
1003     vol->vfs_modules[2] = &netatalk_solaris_acl_adouble;
1004 #endif
1005 }