]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/vfs_adouble.c
EAs for dirs did not work
[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 0;
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 netatalk_name(const char *name)
429 {
430     return strcasecmp(name,".AppleDB") &&
431         strcasecmp(name,".AppleDouble") &&
432         strcasecmp(name,".AppleDesktop");
433 }
434
435 static int validupath_adouble(const struct vol *vol, const char *name) 
436 {
437     return (vol->v_flags & AFPVOL_USEDOTS) ? 
438         netatalk_name(name) && strcasecmp(name,".Parent"): name[0] != '.';
439 }                                           
440
441 /* ----------------- */
442 static int RF_chown_adouble(const struct vol *vol, const char *path, uid_t uid, gid_t gid)
443
444 {
445     struct stat st;
446     char        *ad_p;
447
448     ad_p = vol->vfs->ad_path(path, ADFLAGS_HF );
449
450     if ( stat( ad_p, &st ) < 0 )
451         return 0; /* ignore */
452
453     return chown( ad_p, uid, gid );
454 }
455
456 /* ----------------- */
457 int RF_renamedir_adouble(const struct vol *vol _U_, const char *oldpath _U_, const char *newpath _U_)
458 {
459     return 0;
460 }
461
462 /* ----------------- */
463 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_)
464 {
465     struct stat st;
466     int         err;
467     
468     /* bail if the file exists in the current directory.
469      * note: this will not fail with dangling symlinks */
470     
471     if (stat(de->d_name, &st) == 0)
472         return AFPERR_DIRNEMPT;
473
474     if ((err = netatalk_unlink(name)))
475         return err;
476
477     return 0;
478 }
479
480 static int RF_deletecurdir_adouble(const struct vol *vol _U_)
481 {
482     int err;
483
484     /* delete stray .AppleDouble files. this happens to get .Parent files
485        as well. */
486     if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1))) 
487         return err;
488     return netatalk_rmdir( ".AppleDouble" );
489 }
490
491 /* ----------------- */
492 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st)
493 {
494     return setfilmode(name, ad_hf_mode(mode), st);
495 }
496
497 static int RF_setfilmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
498 {
499     return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_HF ), mode, st);
500 }
501
502 /* ----------------- */
503 static int RF_setdirunixmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
504 {
505     char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
506     int  dropbox = vol->v_flags;
507
508     if (dir_rx_set(mode)) {
509         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 ) 
510             return -1;
511     }
512
513     if (adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st) < 0) 
514         return -1;
515
516     if (!dir_rx_set(mode)) {
517         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 ) 
518             return  -1 ;
519     }
520     return 0;
521 }
522
523 /* ----------------- */
524 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag)
525 {
526     mode_t hf_mode = *(mode_t *)data;
527     struct stat st;
528
529     if ( stat( name, &st ) < 0 ) {
530         if (flag)
531             return 0;
532         LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
533     }
534     else if (!S_ISDIR(st.st_mode)) {
535         if (setfilmode(name, hf_mode , &st) < 0) {
536                /* FIXME what do we do then? */
537         }
538     }
539     return 0;
540 }
541
542 static int RF_setdirmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st _U_)
543 {
544     int   dropbox = vol->v_flags;
545     mode_t hf_mode = ad_hf_mode(mode);
546     char  *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
547     char  *adouble_p = ad_dir(adouble);
548
549     if (dir_rx_set(mode)) {
550         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0) 
551             return -1;
552     }
553
554     if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, vol_noadouble(vol)))
555         return -1;
556
557     if (!dir_rx_set(mode)) {
558         if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0) 
559             return  -1 ;
560     }
561     return 0;
562 }
563
564 /* ----------------- */
565 static int setdirowner_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
566 {
567     struct perm   *owner  = data;
568
569     if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
570          LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
571                 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
572          /* return ( -1 ); Sometimes this is okay */
573     }
574     return 0;
575 }
576
577 static int RF_setdirowner_adouble(const struct vol *vol, const char *name, uid_t uid, gid_t gid)
578
579 {
580     int           noadouble = vol_noadouble(vol);
581     char          *adouble_p;
582     struct stat   st;
583     struct perm   owner;
584     
585     owner.uid = uid;
586     owner.gid = gid;
587
588     adouble_p = ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR ));
589
590     if (for_each_adouble("setdirowner", adouble_p, setdirowner_adouble_loop, &owner, noadouble)) 
591         return -1;
592
593     /*
594      * We cheat: we know that chown doesn't do anything.
595      */
596     if ( stat( ".AppleDouble", &st ) < 0) {
597         if (errno == ENOENT && noadouble)
598             return 0;
599         LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
600         return -1;
601     }
602     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
603         LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
604             uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
605         /* return ( -1 ); Sometimes this is okay */
606     }
607     return 0;
608 }
609
610 /* ----------------- */
611 static int RF_deletefile_adouble(const struct vol *vol, const char *file )
612 {
613         return netatalk_unlink(vol->vfs->ad_path( file, ADFLAGS_HF));
614 }
615
616 /* ----------------- */
617 int RF_renamefile_adouble(const struct vol *vol, const char *src, const char *dst)
618 {
619     char  adsrc[ MAXPATHLEN + 1];
620     int   err = 0;
621
622     strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
623     if (unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) < 0) {
624         struct stat st;
625
626         err = errno;
627         if (errno == ENOENT) {
628                 struct adouble    ad;
629
630             if (stat(adsrc, &st)) /* source has no ressource fork, */
631                 return 0;
632             
633             /* We are here  because :
634              * -there's no dest folder. 
635              * -there's no .AppleDouble in the dest folder.
636              * if we use the struct adouble passed in parameter it will not
637              * create .AppleDouble if the file is already opened, so we
638              * use a diff one, it's not a pb,ie it's not the same file, yet.
639              */
640             ad_init(&ad, vol->v_adouble, vol->v_ad_options); 
641             if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
642                 ad_close(&ad, ADFLAGS_HF);
643                 if (!unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) ) 
644                    err = 0;
645                 else 
646                    err = errno;
647             }
648             else { /* it's something else, bail out */
649                     err = errno;
650                 }
651             }
652         }
653         if (err) {
654                 errno = err;
655                 return -1;
656         }
657         return 0;
658 }
659
660 #ifdef HAVE_NFSv4_ACLS
661 static int RF_acl(const struct vol *vol, const char *path, int cmd, int count, ace_t *aces)
662 {
663     static char buf[ MAXPATHLEN + 1];
664     struct stat st;
665
666     if ((stat(path, &st)) != 0)
667         return -1;
668     if (S_ISDIR(st.st_mode)) {
669         if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
670             return -1;
671         /* set acl on .AppleDouble dir first */
672         if ((acl(buf, cmd, count, aces)) != 0)
673             return -1;
674         /* now set ACL on ressource fork */
675         if ((acl(vol->vfs->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
676             return -1;
677     } else
678         /* set ACL on ressource fork */
679         if ((acl(vol->vfs->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
680             return -1;
681
682     return 0;
683 }
684
685 static int RF_remove_acl(const struct vol *vol, const char *path, int dir)
686 {
687     int ret;
688     static char buf[ MAXPATHLEN + 1];
689
690     if (dir) {
691         if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
692             return AFPERR_MISC;
693         /* remove ACL from .AppleDouble/.Parent first */
694         if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
695             return ret;
696         /* now remove from .AppleDouble dir */
697         if ((ret = remove_acl(buf)) != AFP_OK)
698             return ret;
699     } else
700         /* remove ACL from ressource fork */
701         if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_HF))) != AFP_OK)
702             return ret;
703
704     return AFP_OK;
705 }
706 #endif
707
708 struct vfs_ops netatalk_adouble = {
709     /* ad_path:           */ ad_path,
710     /* validupath:        */ validupath_adouble,
711     /* rf_chown:          */ RF_chown_adouble,
712     /* rf_renamedir:      */ RF_renamedir_adouble,
713     /* rf_deletecurdir:   */ RF_deletecurdir_adouble,
714     /* rf_setfilmode:     */ RF_setfilmode_adouble,
715     /* rf_setdirmode:     */ RF_setdirmode_adouble,
716     /* rf_setdirunixmode: */ RF_setdirunixmode_adouble,
717     /* rf_setdirowner:    */ RF_setdirowner_adouble,
718     /* rf_deletefile:     */ RF_deletefile_adouble,
719     /* rf_renamefile:     */ RF_renamefile_adouble,
720 #ifdef HAVE_NFSv4_ACLS
721     /* rf_acl:            */ RF_acl,
722     /* rf_remove_acl      */ RF_remove_acl
723 #endif
724 };
725
726 /* =======================================
727  osx adouble format 
728  */
729 static int validupath_osx(const struct vol *vol, const char *name) 
730 {
731     return strncmp(name,"._", 2) && (
732       (vol->v_flags & AFPVOL_USEDOTS) ? netatalk_name(name): name[0] != '.');
733 }             
734
735 /* ---------------- */
736 int RF_renamedir_osx(const struct vol *vol, const char *oldpath, const char *newpath)
737 {
738     /* We simply move the corresponding ad file as well */
739     char   tempbuf[258]="._";
740     return rename(vol->vfs->ad_path(oldpath,0),strcat(tempbuf,newpath));
741 }
742
743 /* ---------------- */
744 int RF_deletecurdir_osx(const struct vol *vol)
745 {
746     return netatalk_unlink( vol->vfs->ad_path(".",0) );
747 }
748
749 /* ---------------- */
750 static int RF_setdirunixmode_osx(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
751 {
752     return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st);
753 }
754
755 /* ---------------- */
756 static int 
757 RF_setdirmode_osx(const struct vol *vol _U_, const char *name _U_, mode_t mode _U_, struct stat *st _U_)
758 {
759     return 0;
760 }
761
762 /* ---------------- */
763 static int 
764 RF_setdirowner_osx(const struct vol *vol _U_, const char *path _U_, uid_t uid _U_, gid_t gid _U_)
765 {
766         return 0;
767 }
768
769 /* ---------------- */
770 int RF_renamefile_osx(const struct vol *vol, const char *src, const char *dst)
771 {
772     char  adsrc[ MAXPATHLEN + 1];
773     int   err = 0;
774
775     strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
776
777     if (unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) < 0) {
778         struct stat st;
779
780         err = errno;
781         if (errno == ENOENT && stat(adsrc, &st)) /* source has no ressource fork, */
782             return 0;
783         errno = err;
784         return -1;
785     }
786     return 0;
787 }
788
789 struct vfs_ops netatalk_adouble_osx = {
790     /* ad_path:          */ ad_path_osx,
791     /* validupath:       */ validupath_osx,
792     /* rf_chown:         */ RF_chown_adouble,
793     /* rf_renamedir:     */ RF_renamedir_osx,
794     /* rf_deletecurdir:  */ RF_deletecurdir_osx,
795     /* rf_setfilmode:    */ RF_setfilmode_adouble,
796     /* rf_setdirmode:    */ RF_setdirmode_osx,
797     /* rf_setdirunixmode:*/ RF_setdirunixmode_osx,
798     /* rf_setdirowner:   */ RF_setdirowner_osx,
799     /* rf_deletefile:    */ RF_deletefile_adouble,
800     /* rf_renamefile:    */ RF_renamefile_osx,
801 };
802
803 /* =======================================
804    samba sfm format
805    ad_path shouldn't be set here
806  */
807 struct vfs_ops netatalk_adouble_sfm = {
808     /* ad_path:          */ ad_path_sfm,
809     /* validupath:       */ validupath_adouble,
810     /* rf_chown:         */ RF_chown_ads,
811     /* rf_renamedir:     */ RF_renamedir_adouble,
812     /* rf_deletecurdir:  */ RF_deletecurdir_ads,
813     /* rf_setfilmode:    */ RF_setfilmode_ads,
814     /* rf_setdirmode:    */ RF_setdirmode_ads,
815     /* rf_setdirunixmode:*/ RF_setdirunixmode_ads,
816     /* rf_setdirowner:   */ RF_setdirowner_ads,
817     /* rf_deletefile:    */ RF_deletefile_ads,
818     /* rf_renamefile:    */ RF_renamefile_ads,
819 };
820
821 /* ---------------- */
822 void initvol_vfs(struct vol *vol)
823 {
824     if (vol->v_adouble == AD_VERSION2_OSX) {
825         vol->vfs = &netatalk_adouble_osx;
826     }
827     else if (vol->v_adouble == AD_VERSION1_SFM) {
828         vol->vfs = &netatalk_adouble_sfm;
829     }
830     else {
831         vol->vfs = &netatalk_adouble;
832     }
833 }
834