2 Copyright (c) 2004 Didier Gautheron
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.
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.
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.
21 #endif /* HAVE_CONFIG_H */
29 #include <atalk/adouble.h>
30 #include <atalk/logger.h>
31 #include <atalk/util.h>
33 #include "directory.h"
37 #ifdef HAVE_NFSv4_ACLS
38 extern int remove_acl(const char *name);
46 typedef int (*rf_loop)(struct dirent *, char *, void *, int );
48 /* ----------------------------- */
50 for_each_adouble(const char *from, const char *name, rf_loop fn, void *data, int flag)
52 char buf[ MAXPATHLEN + 1];
59 if (NULL == ( dp = opendir( name)) ) {
61 LOG(log_error, logtype_afpd, "%s: opendir %s: %s", from, fullpathname(name),strerror(errno) );
66 strlcpy( buf, name, sizeof(buf));
67 strlcat( buf, "/", sizeof(buf) );
68 m = strchr( buf, '\0' );
70 while ((de = readdir(dp))) {
71 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
75 strlcat(buf, de->d_name, sizeof(buf));
76 if (fn && (ret = fn(de, buf, data, flag))) {
86 /* ------------------------------ */
87 static int ads_chown_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
89 struct perm *owner = data;
91 if (chown( name , owner->uid, owner->gid ) < 0) {
97 static int RF_chown_ads(const struct vol *vol, const char *path, uid_t uid, gid_t gid)
108 ad_p = ad_dir(vol->vfs->ad_path(path, ADFLAGS_HF ));
110 if ( stat( ad_p, &st ) < 0 ) {
115 if (chown( ad_p, uid, gid ) < 0) {
118 return for_each_adouble("chown_ads", ad_p, ads_chown_loop, &owner, 1);
121 /* --------------------------------- */
122 static int deletecurdir_ads1_loop(struct dirent *de _U_, char *name, void *data _U_, int flag _U_)
124 return netatalk_unlink(name);
127 static int ads_delete_rf(char *name)
131 if ((err = for_each_adouble("deletecurdir", name, deletecurdir_ads1_loop, NULL, 1)))
134 * it's a problem for a nfs mounted folder, there's .nfsxxx around
135 * for linux the following line solve it.
136 * but it could fail if rm .nfsxxx create a new .nfsyyy :(
138 if ((err = for_each_adouble("deletecurdir", name, deletecurdir_ads1_loop, NULL, 1)))
140 return netatalk_rmdir(name);
143 static int deletecurdir_ads_loop(struct dirent *de, char *name, void *data _U_, int flag _U_)
147 /* bail if the file exists in the current directory.
148 * note: this will not fail with dangling symlinks */
150 if (stat(de->d_name, &st) == 0) {
151 return AFPERR_DIRNEMPT;
153 return ads_delete_rf(name);
156 static int RF_deletecurdir_ads(const struct vol *vol _U_)
160 /* delete stray .AppleDouble files. this happens to get .Parent files as well. */
161 if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_ads_loop, NULL, 1)))
163 return netatalk_rmdir( ".AppleDouble" );
166 /* ------------------- */
172 static int ads_setfilmode_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
174 struct set_mode *param = data;
176 return setfilmode(name, param->mode, param->st);
179 static int ads_setfilmode(const char * name, mode_t mode, struct stat *st)
181 mode_t file_mode = ad_hf_mode(mode);
182 mode_t dir_mode = file_mode;
183 struct set_mode param;
185 if ((dir_mode & (S_IRUSR | S_IWUSR )))
187 if ((dir_mode & (S_IRGRP | S_IWGRP )))
189 if ((dir_mode & (S_IROTH | S_IWOTH )))
194 if (dir_rx_set(dir_mode)) {
195 if (chmod( name, dir_mode ) < 0)
199 param.mode = file_mode;
200 if (for_each_adouble("setfilmode_ads", name, ads_setfilmode_loop, ¶m, 0) < 0)
203 if (!dir_rx_set(dir_mode)) {
204 if (chmod( name, dir_mode ) < 0)
211 static int RF_setfilmode_ads(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
213 return ads_setfilmode(ad_dir(vol->vfs->ad_path( name, ADFLAGS_HF )), mode, st);
216 /* ------------------- */
217 static int RF_setdirunixmode_ads(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
219 char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
220 char ad_p[ MAXPATHLEN + 1];
221 int dropbox = vol->v_flags;
223 strlcpy(ad_p,ad_dir(adouble), MAXPATHLEN + 1);
225 if (dir_rx_set(mode)) {
228 if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, dropbox) < 0)
231 /* .AppleDouble/.Parent */
232 if (stickydirmode(ad_p, DIRBITS | mode, dropbox) < 0)
236 if (ads_setfilmode(ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR)), mode, st) < 0)
239 if (!dir_rx_set(mode)) {
240 if (stickydirmode(ad_p, DIRBITS | mode, dropbox) < 0)
242 if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, dropbox) < 0)
248 /* ------------------- */
254 static int setdirmode_ads_loop(struct dirent *de _U_, char *name, void *data, int flag)
257 struct dir_mode *param = data;
258 int ret = 0; /* 0 ignore error, -1 */
260 if (dir_rx_set(param->mode)) {
261 if (stickydirmode(name, DIRBITS | param->mode, param->dropbox) < 0) {
268 if (ads_setfilmode(name, param->mode, NULL) < 0)
271 if (!dir_rx_set(param->mode)) {
272 if (stickydirmode(name, DIRBITS | param->mode, param->dropbox) < 0) {
282 static int RF_setdirmode_ads(const struct vol *vol, const char * name, mode_t mode, struct stat *st _U_)
284 char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
285 char ad_p[ MAXPATHLEN + 1];
286 struct dir_mode param;
289 param.dropbox = vol->v_flags;
291 strlcpy(ad_p,ad_dir(adouble), sizeof(ad_p));
293 if (dir_rx_set(mode)) {
295 if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, param.dropbox) < 0)
299 if (for_each_adouble("setdirmode_ads", ad_dir(ad_p), setdirmode_ads_loop, ¶m, vol_noadouble(vol)))
302 if (!dir_rx_set(mode)) {
303 if (stickydirmode(ad_dir(ad_p), DIRBITS | mode, param.dropbox) < 0 )
309 /* ------------------- */
310 static int setdirowner_ads1_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
312 struct perm *owner = data;
314 if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
315 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
316 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
317 /* return ( -1 ); Sometimes this is okay */
322 static int setdirowner_ads_loop(struct dirent *de _U_, char *name, void *data, int flag)
324 struct perm *owner = data;
326 if (for_each_adouble("setdirowner", name, setdirowner_ads1_loop, data, flag) < 0)
329 if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
330 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
331 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
332 /* return ( -1 ); Sometimes this is okay */
337 static int RF_setdirowner_ads(const struct vol *vol, const char *name, uid_t uid, gid_t gid)
339 int noadouble = vol_noadouble(vol);
340 char adouble_p[ MAXPATHLEN + 1];
347 strlcpy(adouble_p, ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR )), sizeof(adouble_p));
349 if (for_each_adouble("setdirowner", ad_dir(adouble_p), setdirowner_ads_loop, &owner, noadouble))
353 * We cheat: we know that chown doesn't do anything.
355 if ( stat( ".AppleDouble", &st ) < 0) {
356 if (errno == ENOENT && noadouble)
358 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
361 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
362 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
363 uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
364 /* return ( -1 ); Sometimes this is okay */
369 /* ------------------- */
370 static int RF_deletefile_ads(const struct vol *vol, const char *file )
372 char *ad_p = ad_dir(vol->vfs->ad_path(file, ADFLAGS_HF ));
374 return ads_delete_rf(ad_p);
377 /* --------------------------- */
378 int RF_renamefile_ads(const struct vol *vol, const char *src, const char *dst)
380 char adsrc[ MAXPATHLEN + 1];
383 strcpy( adsrc, ad_dir(vol->vfs->ad_path( src, 0 )));
384 if (unix_rename( adsrc, ad_dir(vol->vfs->ad_path( dst, 0 ))) < 0) {
388 if (errno == ENOENT) {
391 if (stat(adsrc, &st)) /* source has no ressource fork, */
394 /* We are here because :
395 * -there's no dest folder.
396 * -there's no .AppleDouble in the dest folder.
397 * if we use the struct adouble passed in parameter it will not
398 * create .AppleDouble if the file is already opened, so we
399 * use a diff one, it's not a pb,ie it's not the same file, yet.
401 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
402 if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
403 ad_close(&ad, ADFLAGS_HF);
405 /* We must delete it */
406 RF_deletefile_ads(vol, dst );
407 if (!unix_rename( adsrc, ad_dir(vol->vfs->ad_path( dst, 0 ))) )
412 else { /* it's something else, bail out */
424 /* ===================================================
425 classic adouble format
428 static int validupath_adouble(const struct vol *vol, const char *name)
430 return (vol->v_flags & AFPVOL_USEDOTS) ?
431 strcasecmp(name,".AppleDB") &&
432 strcasecmp(name,".AppleDouble") &&
433 strcasecmp(name,".AppleDesktop") &&
434 strcasecmp(name,".Parent")
438 /* ----------------- */
439 static int RF_chown_adouble(const struct vol *vol, const char *path, uid_t uid, gid_t gid)
445 ad_p = vol->vfs->ad_path(path, ADFLAGS_HF );
447 if ( stat( ad_p, &st ) < 0 )
448 return 0; /* ignore */
450 return chown( ad_p, uid, gid );
453 /* ----------------- */
454 int RF_renamedir_adouble(const struct vol *vol _U_, const char *oldpath _U_, const char *newpath _U_)
459 /* ----------------- */
460 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_)
465 /* bail if the file exists in the current directory.
466 * note: this will not fail with dangling symlinks */
468 if (stat(de->d_name, &st) == 0)
469 return AFPERR_DIRNEMPT;
471 if ((err = netatalk_unlink(name)))
477 static int RF_deletecurdir_adouble(const struct vol *vol _U_)
481 /* delete stray .AppleDouble files. this happens to get .Parent files
483 if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1)))
485 return netatalk_rmdir( ".AppleDouble" );
488 /* ----------------- */
489 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st)
491 return setfilmode(name, ad_hf_mode(mode), st);
494 static int RF_setfilmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
496 return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_HF ), mode, st);
499 /* ----------------- */
500 static int RF_setdirunixmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
502 char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
503 int dropbox = vol->v_flags;
505 if (dir_rx_set(mode)) {
506 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 )
510 if (adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st) < 0)
513 if (!dir_rx_set(mode)) {
514 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 )
520 /* ----------------- */
521 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag)
523 mode_t hf_mode = *(mode_t *)data;
526 if ( stat( name, &st ) < 0 ) {
529 LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
531 else if (!S_ISDIR(st.st_mode)) {
532 if (setfilmode(name, hf_mode , &st) < 0) {
533 /* FIXME what do we do then? */
539 static int RF_setdirmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st _U_)
541 int dropbox = vol->v_flags;
542 mode_t hf_mode = ad_hf_mode(mode);
543 char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
544 char *adouble_p = ad_dir(adouble);
546 if (dir_rx_set(mode)) {
547 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0)
551 if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, vol_noadouble(vol)))
554 if (!dir_rx_set(mode)) {
555 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0)
561 /* ----------------- */
562 static int setdirowner_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
564 struct perm *owner = data;
566 if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
567 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
568 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
569 /* return ( -1 ); Sometimes this is okay */
574 static int RF_setdirowner_adouble(const struct vol *vol, const char *name, uid_t uid, gid_t gid)
577 int noadouble = vol_noadouble(vol);
585 adouble_p = ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR ));
587 if (for_each_adouble("setdirowner", adouble_p, setdirowner_adouble_loop, &owner, noadouble))
591 * We cheat: we know that chown doesn't do anything.
593 if ( stat( ".AppleDouble", &st ) < 0) {
594 if (errno == ENOENT && noadouble)
596 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
599 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
600 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
601 uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
602 /* return ( -1 ); Sometimes this is okay */
607 /* ----------------- */
608 static int RF_deletefile_adouble(const struct vol *vol, const char *file )
610 return netatalk_unlink(vol->vfs->ad_path( file, ADFLAGS_HF));
613 /* ----------------- */
614 int RF_renamefile_adouble(const struct vol *vol, const char *src, const char *dst)
616 char adsrc[ MAXPATHLEN + 1];
619 strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
620 if (unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) < 0) {
624 if (errno == ENOENT) {
627 if (stat(adsrc, &st)) /* source has no ressource fork, */
630 /* We are here because :
631 * -there's no dest folder.
632 * -there's no .AppleDouble in the dest folder.
633 * if we use the struct adouble passed in parameter it will not
634 * create .AppleDouble if the file is already opened, so we
635 * use a diff one, it's not a pb,ie it's not the same file, yet.
637 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
638 if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
639 ad_close(&ad, ADFLAGS_HF);
640 if (!unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) )
645 else { /* it's something else, bail out */
657 #ifdef HAVE_NFSv4_ACLS
658 static int RF_acl(const struct vol *vol, const char *path, int cmd, int count, ace_t *aces)
660 static char buf[ MAXPATHLEN + 1];
663 if ((stat(path, &st)) != 0)
665 if (S_ISDIR(st.st_mode)) {
666 if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
668 /* set acl on .AppleDouble dir first */
669 if ((acl(buf, cmd, count, aces)) != 0)
671 /* now set ACL on ressource fork */
672 if ((acl(vol->vfs->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
675 /* set ACL on ressource fork */
676 if ((acl(vol->vfs->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
682 static int RF_remove_acl(const struct vol *vol, const char *path, int dir)
685 static char buf[ MAXPATHLEN + 1];
688 if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
690 /* remove ACL from .AppleDouble/.Parent first */
691 if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
693 /* now remove from .AppleDouble dir */
694 if ((ret = remove_acl(buf)) != AFP_OK)
697 /* remove ACL from ressource fork */
698 if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_HF))) != AFP_OK)
705 struct vfs_ops netatalk_adouble = {
706 /* ad_path: */ ad_path,
707 /* validupath: */ validupath_adouble,
708 /* rf_chown: */ RF_chown_adouble,
709 /* rf_renamedir: */ RF_renamedir_adouble,
710 /* rf_deletecurdir: */ RF_deletecurdir_adouble,
711 /* rf_setfilmode: */ RF_setfilmode_adouble,
712 /* rf_setdirmode: */ RF_setdirmode_adouble,
713 /* rf_setdirunixmode: */ RF_setdirunixmode_adouble,
714 /* rf_setdirowner: */ RF_setdirowner_adouble,
715 /* rf_deletefile: */ RF_deletefile_adouble,
716 /* rf_renamefile: */ RF_renamefile_adouble,
717 #ifdef HAVE_NFSv4_ACLS
718 /* rf_acl: */ RF_acl,
719 /* rf_remove_acl */ RF_remove_acl
723 /* =======================================
726 static int validupath_osx(const struct vol *vol _U_, const char *name)
728 return strncasecmp(name,".Apple", 6) && strncasecmp(name,"._", 2);
731 /* ---------------- */
732 int RF_renamedir_osx(const struct vol *vol, const char *oldpath, const char *newpath)
734 /* We simply move the corresponding ad file as well */
735 char tempbuf[258]="._";
736 return rename(vol->vfs->ad_path(oldpath,0),strcat(tempbuf,newpath));
739 /* ---------------- */
740 int RF_deletecurdir_osx(const struct vol *vol)
742 return netatalk_unlink( vol->vfs->ad_path(".",0) );
745 /* ---------------- */
746 static int RF_setdirunixmode_osx(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
748 return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st);
751 /* ---------------- */
753 RF_setdirmode_osx(const struct vol *vol _U_, const char *name _U_, mode_t mode _U_, struct stat *st _U_)
758 /* ---------------- */
760 RF_setdirowner_osx(const struct vol *vol _U_, const char *path _U_, uid_t uid _U_, gid_t gid _U_)
765 /* ---------------- */
766 int RF_renamefile_osx(const struct vol *vol, const char *src, const char *dst)
768 char adsrc[ MAXPATHLEN + 1];
770 strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
771 return unix_rename( adsrc, vol->vfs->ad_path( dst, 0 ));
774 struct vfs_ops netatalk_adouble_osx = {
775 /* ad_path: */ ad_path_osx,
776 /* validupath: */ validupath_osx,
777 /* rf_chown: */ RF_chown_adouble,
778 /* rf_renamedir: */ RF_renamedir_osx,
779 /* rf_deletecurdir: */ RF_deletecurdir_osx,
780 /* rf_setfilmode: */ RF_setfilmode_adouble,
781 /* rf_setdirmode: */ RF_setdirmode_osx,
782 /* rf_setdirunixmode:*/ RF_setdirunixmode_osx,
783 /* rf_setdirowner: */ RF_setdirowner_osx,
784 /* rf_deletefile: */ RF_deletefile_adouble,
785 /* rf_renamefile: */ RF_renamefile_osx,
788 /* =======================================
791 struct vfs_ops netatalk_adouble_ads = {
792 /* ad_path: */ ad_path_ads,
793 /* validupath: */ validupath_adouble,
794 /* rf_chown: */ RF_chown_ads,
795 /* rf_renamedir: */ RF_renamedir_adouble,
796 /* rf_deletecurdir: */ RF_deletecurdir_ads,
797 /* rf_setfilmode: */ RF_setfilmode_ads,
798 /* rf_setdirmode: */ RF_setdirmode_ads,
799 /* rf_setdirunixmode:*/ RF_setdirunixmode_ads,
800 /* rf_setdirowner: */ RF_setdirowner_ads,
801 /* rf_deletefile: */ RF_deletefile_ads,
802 /* rf_renamefile: */ RF_renamefile_ads,
805 /* =======================================
807 ad_path shouldn't be set here
809 struct vfs_ops netatalk_adouble_sfm = {
810 /* ad_path: */ ad_path_sfm,
811 /* validupath: */ validupath_adouble,
812 /* rf_chown: */ RF_chown_ads,
813 /* rf_renamedir: */ RF_renamedir_adouble,
814 /* rf_deletecurdir: */ RF_deletecurdir_ads,
815 /* rf_setfilmode: */ RF_setfilmode_ads,
816 /* rf_setdirmode: */ RF_setdirmode_ads,
817 /* rf_setdirunixmode:*/ RF_setdirunixmode_ads,
818 /* rf_setdirowner: */ RF_setdirowner_ads,
819 /* rf_deletefile: */ RF_deletefile_ads,
820 /* rf_renamefile: */ RF_renamefile_ads,
823 /* ---------------- */
824 void initvol_vfs(struct vol *vol)
826 if (vol->v_adouble == AD_VERSION2_OSX) {
827 vol->vfs = &netatalk_adouble_osx;
829 else if (vol->v_adouble == AD_VERSION1_ADS) {
830 vol->vfs = &netatalk_adouble_ads;
832 else if (vol->v_adouble == AD_VERSION1_SFM) {
833 vol->vfs = &netatalk_adouble_sfm;
836 vol->vfs = &netatalk_adouble;