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