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