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