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