]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/vfs_adouble.c
Initial checkin of ACLs patch
[netatalk.git] / etc / afpd / vfs_adouble.c
1 /*
2     Copyright (c) 2004 Didier Gautheron
3  
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13  
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif /* HAVE_CONFIG_H */
22
23 #ifdef STDC_HEADERS
24 #include <string.h>
25 #endif
26
27 #include <stdio.h>
28     
29 #include <atalk/adouble.h>
30 #include <atalk/logger.h>
31 #include <atalk/util.h>
32
33 #include "directory.h"
34 #include "volume.h"
35 #include "unix.h"
36
37 #ifdef HAVE_NFSv4_ACLS
38 extern int remove_acl(const char *name);
39 #endif
40
41 struct perm {
42     uid_t uid;
43     gid_t gid;
44 };
45
46 typedef int (*rf_loop)(struct dirent *, char *, void *, int );
47
48 /* ----------------------------- */
49 static int 
50 for_each_adouble(const char *from, const char *name, rf_loop fn, void *data, int flag)
51 {
52     char            buf[ MAXPATHLEN + 1];
53     char            *m;
54     DIR             *dp;
55     struct dirent   *de;
56     int             ret;
57     
58
59     if (NULL == ( dp = opendir( name)) ) {
60         if (!flag) {
61             LOG(log_error, logtype_afpd, "%s: opendir %s: %s", from, fullpathname(name),strerror(errno) );
62             return -1;
63         }
64         return 0;
65     }
66     strlcpy( buf, name, sizeof(buf));
67     strlcat( buf, "/", sizeof(buf) );
68     m = strchr( buf, '\0' );
69     ret = 0;
70     while ((de = readdir(dp))) {
71         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
72                 continue;
73         }
74         
75         strlcat(buf, de->d_name, sizeof(buf));
76         if (fn && (ret = fn(de, buf, data, flag))) {
77            closedir(dp);
78            return ret;
79         }
80         *m = 0;
81     }
82     closedir(dp);
83     return ret;
84 }
85
86 /* ------------------------------ */
87 static int ads_chown_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
88 {
89     struct perm   *owner  = data;
90     
91     if (chown( name , owner->uid, owner->gid ) < 0) {
92         return -1;
93     }
94     return 0;
95 }
96
97 static int RF_chown_ads(const struct vol *vol, const char *path, uid_t uid, gid_t gid)
98
99 {
100     struct        stat st;
101     char          *ad_p;
102     struct perm   owner;
103     
104     owner.uid = uid;
105     owner.gid = gid;
106
107
108     ad_p = ad_dir(vol->vfs->ad_path(path, ADFLAGS_HF ));
109
110     if ( stat( ad_p, &st ) < 0 ) {
111         /* ignore */
112         return 0;
113     }
114     
115     if (chown( ad_p, uid, gid ) < 0) {
116         return -1;
117     }
118     return for_each_adouble("chown_ads", ad_p, ads_chown_loop, &owner, 1);
119 }
120
121 /* --------------------------------- */
122 static int deletecurdir_ads1_loop(struct dirent *de _U_, char *name, void *data _U_, int flag _U_)
123 {
124     return netatalk_unlink(name);
125 }
126
127 static int ads_delete_rf(char *name) 
128 {
129     int err;
130
131     if ((err = for_each_adouble("deletecurdir", name, deletecurdir_ads1_loop, NULL, 1))) 
132         return err;
133     /* FIXME 
134      * it's a problem for a nfs mounted folder, there's .nfsxxx around
135      * for linux the following line solve it.
136      * but it could fail if rm .nfsxxx  create a new .nfsyyy :(
137     */
138     if ((err = for_each_adouble("deletecurdir", name, deletecurdir_ads1_loop, NULL, 1))) 
139         return err;
140     return netatalk_rmdir(name);
141 }
142
143 static int deletecurdir_ads_loop(struct dirent *de, char *name, void *data _U_, int flag _U_)
144 {
145     struct stat st;
146     
147     /* bail if the file exists in the current directory.
148      * note: this will not fail with dangling symlinks */
149     
150     if (stat(de->d_name, &st) == 0) {
151         return AFPERR_DIRNEMPT;
152     }
153     return ads_delete_rf(name);
154 }
155
156 static int RF_deletecurdir_ads(const struct vol *vol _U_)
157 {
158     int err;
159     
160     /* delete stray .AppleDouble files. this happens to get .Parent files as well. */
161     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_ads_loop, NULL, 1))) 
162         return err;
163     return netatalk_rmdir( ".AppleDouble" );
164 }
165
166 /* ------------------- */
167 struct set_mode {
168     mode_t mode;
169     struct stat *st;
170 };
171
172 static int ads_setfilmode_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
173 {
174     struct set_mode *param = data;
175
176     return setfilmode(name, param->mode, param->st);
177 }
178
179 static int ads_setfilmode(const char * name, mode_t mode, struct stat *st)
180 {
181     mode_t file_mode = ad_hf_mode(mode);
182     mode_t dir_mode = file_mode;
183     struct set_mode param;
184
185     if ((dir_mode & (S_IRUSR | S_IWUSR )))
186         dir_mode |= S_IXUSR;
187     if ((dir_mode & (S_IRGRP | S_IWGRP )))
188         dir_mode |= S_IXGRP;
189     if ((dir_mode & (S_IROTH | S_IWOTH )))
190         dir_mode |= S_IXOTH;    
191     
192         /* change folder */
193         dir_mode |= DIRBITS;
194     if (dir_rx_set(dir_mode)) {
195         if (chmod( name,  dir_mode ) < 0)
196             return -1;
197     }
198     param.st = st;
199     param.mode = file_mode;
200     if (for_each_adouble("setfilmode_ads", name, ads_setfilmode_loop, &param, 0) < 0)
201         return -1;
202
203     if (!dir_rx_set(dir_mode)) {
204         if (chmod( name,  dir_mode ) < 0)
205             return -1;
206     }
207
208     return 0;
209 }
210
211 static int RF_setfilmode_ads(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
212 {
213     return ads_setfilmode(ad_dir(vol->vfs->ad_path( name, ADFLAGS_HF )), mode, st);
214 }
215
216 /* ------------------- */
217 static int RF_setdirunixmode_ads(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
218 {
219     char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
220     char   ad_p[ MAXPATHLEN + 1];
221     int dropbox = vol->v_flags;
222
223     strlcpy(ad_p,ad_dir(adouble), MAXPATHLEN + 1);
224
225     if (dir_rx_set(mode)) {
226
227         /* .AppleDouble */
228         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, dropbox) < 0) 
229             return -1;
230
231         /* .AppleDouble/.Parent */
232         if (stickydirmode(ad_p, DIRBITS | mode, dropbox) < 0) 
233             return -1;
234     }
235
236     if (ads_setfilmode(ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR)), mode, st) < 0)
237         return -1;
238
239     if (!dir_rx_set(mode)) {
240         if (stickydirmode(ad_p, DIRBITS | mode, dropbox) < 0) 
241             return  -1 ;
242         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, dropbox) < 0) 
243             return -1;
244     }
245     return 0;
246 }
247
248 /* ------------------- */
249 struct dir_mode {
250     mode_t mode;
251     int    dropbox;
252 };
253
254 static int setdirmode_ads_loop(struct dirent *de _U_, char *name, void *data, int flag)
255 {
256
257     struct dir_mode *param = data;
258     int    ret = 0; /* 0 ignore error, -1 */
259
260     if (dir_rx_set(param->mode)) {
261         if (stickydirmode(name, DIRBITS | param->mode, param->dropbox) < 0) {
262             if (flag) {
263                 return 0;
264             }
265             return ret;
266         }
267     }
268     if (ads_setfilmode(name, param->mode, NULL) < 0)
269         return ret;
270
271     if (!dir_rx_set(param->mode)) {
272         if (stickydirmode(name, DIRBITS | param->mode, param->dropbox) < 0) {
273             if (flag) {
274                 return 0;
275             }
276             return ret;
277         }
278     }
279     return 0;
280 }
281
282 static int RF_setdirmode_ads(const struct vol *vol, const char * name, mode_t mode, struct stat *st _U_)
283 {
284     char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
285     char   ad_p[ MAXPATHLEN + 1];
286     struct dir_mode param;
287
288     param.mode = mode;
289     param.dropbox = vol->v_flags;
290
291     strlcpy(ad_p,ad_dir(adouble), sizeof(ad_p));
292
293     if (dir_rx_set(mode)) {
294         /* .AppleDouble */
295         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, param.dropbox) < 0) 
296             return -1;
297     }
298
299     if (for_each_adouble("setdirmode_ads", ad_dir(ad_p), setdirmode_ads_loop, &param, vol_noadouble(vol)))
300         return -1;
301
302     if (!dir_rx_set(mode)) {
303         if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, param.dropbox) < 0 ) 
304             return -1;
305     }
306     return 0;
307 }
308
309 /* ------------------- */
310 static int setdirowner_ads1_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
311 {
312     struct perm   *owner  = data;
313
314     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
315          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
316                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
317          /* return ( -1 ); Sometimes this is okay */
318     }
319     return 0;
320 }
321
322 static int setdirowner_ads_loop(struct dirent *de _U_, char *name, void *data, int flag)
323 {
324     struct perm   *owner  = data;
325
326     if (for_each_adouble("setdirowner", name, setdirowner_ads1_loop, data, flag) < 0)
327         return -1;
328
329     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
330          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
331                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
332          /* return ( -1 ); Sometimes this is okay */
333     }
334     return 0;
335 }
336
337 static int RF_setdirowner_ads(const struct vol *vol, const char *name, uid_t uid, gid_t gid)
338 {
339     int           noadouble = vol_noadouble(vol);
340     char          adouble_p[ MAXPATHLEN + 1];
341     struct stat   st;
342     struct perm   owner;
343     
344     owner.uid = uid;
345     owner.gid = gid;
346
347     strlcpy(adouble_p, ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR )), sizeof(adouble_p));
348
349     if (for_each_adouble("setdirowner", ad_dir(adouble_p), setdirowner_ads_loop, &owner, noadouble)) 
350         return -1;
351
352     /*
353      * We cheat: we know that chown doesn't do anything.
354      */
355     if ( stat( ".AppleDouble", &st ) < 0) {
356         if (errno == ENOENT && noadouble)
357             return 0;
358         LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
359         return -1;
360     }
361     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
362         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
363             uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
364         /* return ( -1 ); Sometimes this is okay */
365     }
366     return 0;
367 }
368
369 /* ------------------- */
370 static int RF_deletefile_ads(const struct vol *vol, const char *file )
371 {
372     char *ad_p = ad_dir(vol->vfs->ad_path(file, ADFLAGS_HF ));
373
374     return ads_delete_rf(ad_p);
375 }
376
377 /* --------------------------- */
378 int RF_renamefile_ads(const struct vol *vol, const char *src, const char *dst)
379 {
380     char  adsrc[ MAXPATHLEN + 1];
381     int   err = 0;
382
383     strcpy( adsrc, ad_dir(vol->vfs->ad_path( src, 0 )));
384     if (unix_rename( adsrc, ad_dir(vol->vfs->ad_path( dst, 0 ))) < 0) {
385         struct stat st;
386
387         err = errno;
388         if (errno == ENOENT) {
389                 struct adouble    ad;
390
391             if (stat(adsrc, &st)) /* source has no ressource fork, */
392                 return AFP_OK;
393             
394             /* We are here  because :
395              * -there's no dest folder. 
396              * -there's no .AppleDouble in the dest folder.
397              * if we use the struct adouble passed in parameter it will not
398              * create .AppleDouble if the file is already opened, so we
399              * use a diff one, it's not a pb,ie it's not the same file, yet.
400              */
401             ad_init(&ad, vol->v_adouble, vol->v_ad_options); 
402             if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
403                 ad_close(&ad, ADFLAGS_HF);
404
405                 /* We must delete it */
406                 RF_deletefile_ads(vol, dst );
407                 if (!unix_rename( adsrc, ad_dir(vol->vfs->ad_path( dst, 0 ))) ) 
408                    err = 0;
409                 else 
410                    err = errno;
411             }
412             else { /* it's something else, bail out */
413                     err = errno;
414                 }
415             }
416         }
417         if (err) {
418                 errno = err;
419                 return -1;
420         }
421         return 0;
422 }
423
424 /* ===================================================
425  classic adouble format 
426 */
427
428 static int validupath_adouble(const struct vol *vol, const char *name) 
429 {
430      return (vol->v_flags & AFPVOL_USEDOTS) ?
431          strcasecmp(name,".AppleDB") &&
432          strcasecmp(name,".AppleDouble") &&
433          strcasecmp(name,".AppleDesktop") &&
434          strcasecmp(name,".Parent")
435                                            : name[0] != '.';
436 }
437
438 /* ----------------- */
439 static int RF_chown_adouble(const struct vol *vol, const char *path, uid_t uid, gid_t gid)
440
441 {
442     struct stat st;
443     char        *ad_p;
444
445     ad_p = vol->vfs->ad_path(path, ADFLAGS_HF );
446
447     if ( stat( ad_p, &st ) < 0 )
448         return 0; /* ignore */
449
450     return chown( ad_p, uid, gid );
451 }
452
453 /* ----------------- */
454 int RF_renamedir_adouble(const struct vol *vol _U_, const char *oldpath _U_, const char *newpath _U_)
455 {
456     return 0;
457 }
458
459 /* ----------------- */
460 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_)
461 {
462     struct stat st;
463     int         err;
464     
465     /* bail if the file exists in the current directory.
466      * note: this will not fail with dangling symlinks */
467     
468     if (stat(de->d_name, &st) == 0)
469         return AFPERR_DIRNEMPT;
470
471     if ((err = netatalk_unlink(name)))
472         return err;
473
474     return 0;
475 }
476
477 static int RF_deletecurdir_adouble(const struct vol *vol _U_)
478 {
479     int err;
480
481     /* delete stray .AppleDouble files. this happens to get .Parent files
482        as well. */
483     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1))) 
484         return err;
485     return netatalk_rmdir( ".AppleDouble" );
486 }
487
488 /* ----------------- */
489 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st)
490 {
491     return setfilmode(name, ad_hf_mode(mode), st);
492 }
493
494 static int RF_setfilmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
495 {
496     return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_HF ), mode, st);
497 }
498
499 /* ----------------- */
500 static int RF_setdirunixmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
501 {
502     char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
503     int  dropbox = vol->v_flags;
504
505     if (dir_rx_set(mode)) {
506         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 ) 
507             return -1;
508     }
509
510     if (adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st) < 0) 
511         return -1;
512
513     if (!dir_rx_set(mode)) {
514         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 ) 
515             return  -1 ;
516     }
517     return 0;
518 }
519
520 /* ----------------- */
521 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag)
522 {
523     mode_t hf_mode = *(mode_t *)data;
524     struct stat st;
525
526     if ( stat( name, &st ) < 0 ) {
527         if (flag)
528             return 0;
529         LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
530     }
531     else if (!S_ISDIR(st.st_mode)) {
532         if (setfilmode(name, hf_mode , &st) < 0) {
533                /* FIXME what do we do then? */
534         }
535     }
536     return 0;
537 }
538
539 static int RF_setdirmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st _U_)
540 {
541     int   dropbox = vol->v_flags;
542     mode_t hf_mode = ad_hf_mode(mode);
543     char  *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
544     char  *adouble_p = ad_dir(adouble);
545
546     if (dir_rx_set(mode)) {
547         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0) 
548             return -1;
549     }
550
551     if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, vol_noadouble(vol)))
552         return -1;
553
554     if (!dir_rx_set(mode)) {
555         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0) 
556             return  -1 ;
557     }
558     return 0;
559 }
560
561 /* ----------------- */
562 static int setdirowner_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
563 {
564     struct perm   *owner  = data;
565
566     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
567          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
568                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
569          /* return ( -1 ); Sometimes this is okay */
570     }
571     return 0;
572 }
573
574 static int RF_setdirowner_adouble(const struct vol *vol, const char *name, uid_t uid, gid_t gid)
575
576 {
577     int           noadouble = vol_noadouble(vol);
578     char          *adouble_p;
579     struct stat   st;
580     struct perm   owner;
581     
582     owner.uid = uid;
583     owner.gid = gid;
584
585     adouble_p = ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR ));
586
587     if (for_each_adouble("setdirowner", adouble_p, setdirowner_adouble_loop, &owner, noadouble)) 
588         return -1;
589
590     /*
591      * We cheat: we know that chown doesn't do anything.
592      */
593     if ( stat( ".AppleDouble", &st ) < 0) {
594         if (errno == ENOENT && noadouble)
595             return 0;
596         LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
597         return -1;
598     }
599     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
600         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
601             uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
602         /* return ( -1 ); Sometimes this is okay */
603     }
604     return 0;
605 }
606
607 /* ----------------- */
608 static int RF_deletefile_adouble(const struct vol *vol, const char *file )
609 {
610         return netatalk_unlink(vol->vfs->ad_path( file, ADFLAGS_HF));
611 }
612
613 /* ----------------- */
614 int RF_renamefile_adouble(const struct vol *vol, const char *src, const char *dst)
615 {
616     char  adsrc[ MAXPATHLEN + 1];
617     int   err = 0;
618
619     strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
620     if (unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) < 0) {
621         struct stat st;
622
623         err = errno;
624         if (errno == ENOENT) {
625                 struct adouble    ad;
626
627             if (stat(adsrc, &st)) /* source has no ressource fork, */
628                 return AFP_OK;
629             
630             /* We are here  because :
631              * -there's no dest folder. 
632              * -there's no .AppleDouble in the dest folder.
633              * if we use the struct adouble passed in parameter it will not
634              * create .AppleDouble if the file is already opened, so we
635              * use a diff one, it's not a pb,ie it's not the same file, yet.
636              */
637             ad_init(&ad, vol->v_adouble, vol->v_ad_options); 
638             if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
639                 ad_close(&ad, ADFLAGS_HF);
640                 if (!unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) ) 
641                    err = 0;
642                 else 
643                    err = errno;
644             }
645             else { /* it's something else, bail out */
646                     err = errno;
647                 }
648             }
649         }
650         if (err) {
651                 errno = err;
652                 return -1;
653         }
654         return 0;
655 }
656
657 #ifdef HAVE_NFSv4_ACLS
658 static int RF_acl(const struct vol *vol, const char *path, int cmd, int count, ace_t *aces)
659 {
660     static char buf[ MAXPATHLEN + 1];
661     struct stat st;
662
663     if ((stat(path, &st)) != 0)
664         return -1;
665     if (S_ISDIR(st.st_mode)) {
666         if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
667             return -1;
668         /* set acl on .AppleDouble dir first */
669         if ((acl(buf, cmd, count, aces)) != 0)
670             return -1;
671         /* now set ACL on ressource fork */
672         if ((acl(vol->vfs->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
673             return -1;
674     } else
675         /* set ACL on ressource fork */
676         if ((acl(vol->vfs->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
677             return -1;
678
679     return 0;
680 }
681
682 static int RF_remove_acl(const struct vol *vol, const char *path, int dir)
683 {
684     int ret;
685     static char buf[ MAXPATHLEN + 1];
686
687     if (dir) {
688         if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
689             return AFPERR_MISC;
690         /* remove ACL from .AppleDouble/.Parent first */
691         if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
692             return ret;
693         /* now remove from .AppleDouble dir */
694         if ((ret = remove_acl(buf)) != AFP_OK)
695             return ret;
696     } else
697         /* remove ACL from ressource fork */
698         if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_HF))) != AFP_OK)
699             return ret;
700
701     return AFP_OK;
702 }
703 #endif
704
705 struct vfs_ops netatalk_adouble = {
706     /* ad_path:           */ ad_path,
707     /* validupath:        */ validupath_adouble,
708     /* rf_chown:          */ RF_chown_adouble,
709     /* rf_renamedir:      */ RF_renamedir_adouble,
710     /* rf_deletecurdir:   */ RF_deletecurdir_adouble,
711     /* rf_setfilmode:     */ RF_setfilmode_adouble,
712     /* rf_setdirmode:     */ RF_setdirmode_adouble,
713     /* rf_setdirunixmode: */ RF_setdirunixmode_adouble,
714     /* rf_setdirowner:    */ RF_setdirowner_adouble,
715     /* rf_deletefile:     */ RF_deletefile_adouble,
716     /* rf_renamefile:     */ RF_renamefile_adouble,
717 #ifdef HAVE_NFSv4_ACLS
718     /* rf_acl:            */ RF_acl,
719     /* rf_remove_acl      */ RF_remove_acl
720 #endif
721 };
722
723 /* =======================================
724  osx adouble format 
725  */
726 static int validupath_osx(const struct vol *vol _U_, const char *name) 
727 {
728     return strncasecmp(name,".Apple", 6) && strncasecmp(name,"._", 2);
729 }
730
731 /* ---------------- */
732 int RF_renamedir_osx(const struct vol *vol, const char *oldpath, const char *newpath)
733 {
734     /* We simply move the corresponding ad file as well */
735     char   tempbuf[258]="._";
736     return rename(vol->vfs->ad_path(oldpath,0),strcat(tempbuf,newpath));
737 }
738
739 /* ---------------- */
740 int RF_deletecurdir_osx(const struct vol *vol)
741 {
742     return netatalk_unlink( vol->vfs->ad_path(".",0) );
743 }
744
745 /* ---------------- */
746 static int RF_setdirunixmode_osx(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
747 {
748     return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st);
749 }
750
751 /* ---------------- */
752 static int 
753 RF_setdirmode_osx(const struct vol *vol _U_, const char *name _U_, mode_t mode _U_, struct stat *st _U_)
754 {
755     return 0;
756 }
757
758 /* ---------------- */
759 static int 
760 RF_setdirowner_osx(const struct vol *vol _U_, const char *path _U_, uid_t uid _U_, gid_t gid _U_)
761 {
762         return 0;
763 }
764
765 /* ---------------- */
766 int RF_renamefile_osx(const struct vol *vol, const char *src, const char *dst)
767 {
768     char  adsrc[ MAXPATHLEN + 1];
769
770     strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
771     return unix_rename( adsrc, vol->vfs->ad_path( dst, 0 ));
772 }
773
774 struct vfs_ops netatalk_adouble_osx = {
775     /* ad_path:          */ ad_path_osx,
776     /* validupath:       */ validupath_osx,
777     /* rf_chown:         */ RF_chown_adouble,
778     /* rf_renamedir:     */ RF_renamedir_osx,
779     /* rf_deletecurdir:  */ RF_deletecurdir_osx,
780     /* rf_setfilmode:    */ RF_setfilmode_adouble,
781     /* rf_setdirmode:    */ RF_setdirmode_osx,
782     /* rf_setdirunixmode:*/ RF_setdirunixmode_osx,
783     /* rf_setdirowner:   */ RF_setdirowner_osx,
784     /* rf_deletefile:    */ RF_deletefile_adouble,
785     /* rf_renamefile:    */ RF_renamefile_osx,
786 };
787
788 /* =======================================
789    samba ads format 
790  */
791 struct vfs_ops netatalk_adouble_ads = {
792     /* ad_path:          */ ad_path_ads,
793     /* validupath:       */ validupath_adouble,
794     /* rf_chown:         */ RF_chown_ads,
795     /* rf_renamedir:     */ RF_renamedir_adouble,
796     /* rf_deletecurdir:  */ RF_deletecurdir_ads,
797     /* rf_setfilmode:    */ RF_setfilmode_ads,
798     /* rf_setdirmode:    */ RF_setdirmode_ads,
799     /* rf_setdirunixmode:*/ RF_setdirunixmode_ads,
800     /* rf_setdirowner:   */ RF_setdirowner_ads,
801     /* rf_deletefile:    */ RF_deletefile_ads,
802     /* rf_renamefile:    */ RF_renamefile_ads,
803 };
804
805 /* =======================================
806    samba sfm format
807    ad_path shouldn't be set here
808  */
809 struct vfs_ops netatalk_adouble_sfm = {
810     /* ad_path:          */ ad_path_sfm,
811     /* validupath:       */ validupath_adouble,
812     /* rf_chown:         */ RF_chown_ads,
813     /* rf_renamedir:     */ RF_renamedir_adouble,
814     /* rf_deletecurdir:  */ RF_deletecurdir_ads,
815     /* rf_setfilmode:    */ RF_setfilmode_ads,
816     /* rf_setdirmode:    */ RF_setdirmode_ads,
817     /* rf_setdirunixmode:*/ RF_setdirunixmode_ads,
818     /* rf_setdirowner:   */ RF_setdirowner_ads,
819     /* rf_deletefile:    */ RF_deletefile_ads,
820     /* rf_renamefile:    */ RF_renamefile_ads,
821 };
822
823 /* ---------------- */
824 void initvol_vfs(struct vol *vol)
825 {
826     if (vol->v_adouble == AD_VERSION2_OSX) {
827         vol->vfs = &netatalk_adouble_osx;
828     }
829     else if (vol->v_adouble == AD_VERSION1_ADS) {
830         vol->vfs = &netatalk_adouble_ads;
831     }
832     else if (vol->v_adouble == AD_VERSION1_SFM) {
833         vol->vfs = &netatalk_adouble_sfm;
834     }
835     else {
836         vol->vfs = &netatalk_adouble;
837     }
838 }
839