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