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 netatalk_name(const char *name)
430 return strcasecmp(name,".AppleDB") &&
431 strcasecmp(name,".AppleDouble") &&
432 strcasecmp(name,".AppleDesktop");
435 static int validupath_adouble(const struct vol *vol, const char *name)
437 return (vol->v_flags & AFPVOL_USEDOTS) ?
438 netatalk_name(name) && strcasecmp(name,".Parent"): name[0] != '.';
441 /* ----------------- */
442 static int RF_chown_adouble(const struct vol *vol, const char *path, uid_t uid, gid_t gid)
448 ad_p = vol->vfs->ad_path(path, ADFLAGS_HF );
450 if ( stat( ad_p, &st ) < 0 )
451 return 0; /* ignore */
453 return chown( ad_p, uid, gid );
456 /* ----------------- */
457 int RF_renamedir_adouble(const struct vol *vol _U_, const char *oldpath _U_, const char *newpath _U_)
462 /* ----------------- */
463 static int deletecurdir_adouble_loop(struct dirent *de, char *name, void *data _U_, int flag _U_)
468 /* bail if the file exists in the current directory.
469 * note: this will not fail with dangling symlinks */
471 if (stat(de->d_name, &st) == 0)
472 return AFPERR_DIRNEMPT;
474 if ((err = netatalk_unlink(name)))
480 static int RF_deletecurdir_adouble(const struct vol *vol _U_)
484 /* delete stray .AppleDouble files. this happens to get .Parent files
486 if ((err = for_each_adouble("deletecurdir", ".AppleDouble", deletecurdir_adouble_loop, NULL, 1)))
488 return netatalk_rmdir( ".AppleDouble" );
491 /* ----------------- */
492 static int adouble_setfilmode(const char * name, mode_t mode, struct stat *st)
494 return setfilmode(name, ad_hf_mode(mode), st);
497 static int RF_setfilmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
499 return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_HF ), mode, st);
502 /* ----------------- */
503 static int RF_setdirunixmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
505 char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
506 int dropbox = vol->v_flags;
508 if (dir_rx_set(mode)) {
509 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 )
513 if (adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st) < 0)
516 if (!dir_rx_set(mode)) {
517 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 )
523 /* ----------------- */
524 static int setdirmode_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag)
526 mode_t hf_mode = *(mode_t *)data;
529 if ( stat( name, &st ) < 0 ) {
532 LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", name, strerror(errno) );
534 else if (!S_ISDIR(st.st_mode)) {
535 if (setfilmode(name, hf_mode , &st) < 0) {
536 /* FIXME what do we do then? */
542 static int RF_setdirmode_adouble(const struct vol *vol, const char * name, mode_t mode, struct stat *st _U_)
544 int dropbox = vol->v_flags;
545 mode_t hf_mode = ad_hf_mode(mode);
546 char *adouble = vol->vfs->ad_path( name, ADFLAGS_DIR );
547 char *adouble_p = ad_dir(adouble);
549 if (dir_rx_set(mode)) {
550 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0)
554 if (for_each_adouble("setdirmode", adouble_p, setdirmode_adouble_loop, &hf_mode, vol_noadouble(vol)))
557 if (!dir_rx_set(mode)) {
558 if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0)
564 /* ----------------- */
565 static int setdirowner_adouble_loop(struct dirent *de _U_, char *name, void *data, int flag _U_)
567 struct perm *owner = data;
569 if ( chown( name, owner->uid, owner->gid ) < 0 && errno != EPERM ) {
570 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
571 owner->uid, owner->gid, fullpathname(name), strerror(errno) );
572 /* return ( -1 ); Sometimes this is okay */
577 static int RF_setdirowner_adouble(const struct vol *vol, const char *name, uid_t uid, gid_t gid)
580 int noadouble = vol_noadouble(vol);
588 adouble_p = ad_dir(vol->vfs->ad_path( name, ADFLAGS_DIR ));
590 if (for_each_adouble("setdirowner", adouble_p, setdirowner_adouble_loop, &owner, noadouble))
594 * We cheat: we know that chown doesn't do anything.
596 if ( stat( ".AppleDouble", &st ) < 0) {
597 if (errno == ENOENT && noadouble)
599 LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
602 if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 && errno != EPERM ) {
603 LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
604 uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
605 /* return ( -1 ); Sometimes this is okay */
610 /* ----------------- */
611 static int RF_deletefile_adouble(const struct vol *vol, const char *file )
613 return netatalk_unlink(vol->vfs->ad_path( file, ADFLAGS_HF));
616 /* ----------------- */
617 int RF_renamefile_adouble(const struct vol *vol, const char *src, const char *dst)
619 char adsrc[ MAXPATHLEN + 1];
622 strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
623 if (unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) < 0) {
627 if (errno == ENOENT) {
630 if (stat(adsrc, &st)) /* source has no ressource fork, */
633 /* We are here because :
634 * -there's no dest folder.
635 * -there's no .AppleDouble in the dest folder.
636 * if we use the struct adouble passed in parameter it will not
637 * create .AppleDouble if the file is already opened, so we
638 * use a diff one, it's not a pb,ie it's not the same file, yet.
640 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
641 if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
642 ad_close(&ad, ADFLAGS_HF);
643 if (!unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) )
648 else { /* it's something else, bail out */
660 #ifdef HAVE_NFSv4_ACLS
661 static int RF_acl(const struct vol *vol, const char *path, int cmd, int count, ace_t *aces)
663 static char buf[ MAXPATHLEN + 1];
666 if ((stat(path, &st)) != 0)
668 if (S_ISDIR(st.st_mode)) {
669 if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
671 /* set acl on .AppleDouble dir first */
672 if ((acl(buf, cmd, count, aces)) != 0)
674 /* now set ACL on ressource fork */
675 if ((acl(vol->vfs->ad_path(path, ADFLAGS_DIR), cmd, count, aces)) != 0)
678 /* set ACL on ressource fork */
679 if ((acl(vol->vfs->ad_path(path, ADFLAGS_HF), cmd, count, aces)) != 0)
685 static int RF_remove_acl(const struct vol *vol, const char *path, int dir)
688 static char buf[ MAXPATHLEN + 1];
691 if ((snprintf(buf, MAXPATHLEN, "%s/.AppleDouble",path)) < 0)
693 /* remove ACL from .AppleDouble/.Parent first */
694 if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_DIR))) != AFP_OK)
696 /* now remove from .AppleDouble dir */
697 if ((ret = remove_acl(buf)) != AFP_OK)
700 /* remove ACL from ressource fork */
701 if ((ret = remove_acl(vol->vfs->ad_path(path, ADFLAGS_HF))) != AFP_OK)
708 struct vfs_ops netatalk_adouble = {
709 /* ad_path: */ ad_path,
710 /* validupath: */ validupath_adouble,
711 /* rf_chown: */ RF_chown_adouble,
712 /* rf_renamedir: */ RF_renamedir_adouble,
713 /* rf_deletecurdir: */ RF_deletecurdir_adouble,
714 /* rf_setfilmode: */ RF_setfilmode_adouble,
715 /* rf_setdirmode: */ RF_setdirmode_adouble,
716 /* rf_setdirunixmode: */ RF_setdirunixmode_adouble,
717 /* rf_setdirowner: */ RF_setdirowner_adouble,
718 /* rf_deletefile: */ RF_deletefile_adouble,
719 /* rf_renamefile: */ RF_renamefile_adouble,
720 #ifdef HAVE_NFSv4_ACLS
721 /* rf_acl: */ RF_acl,
722 /* rf_remove_acl */ RF_remove_acl
726 /* =======================================
729 static int validupath_osx(const struct vol *vol, const char *name)
731 return strncmp(name,"._", 2) && (
732 (vol->v_flags & AFPVOL_USEDOTS) ? netatalk_name(name): name[0] != '.');
735 /* ---------------- */
736 int RF_renamedir_osx(const struct vol *vol, const char *oldpath, const char *newpath)
738 /* We simply move the corresponding ad file as well */
739 char tempbuf[258]="._";
740 return rename(vol->vfs->ad_path(oldpath,0),strcat(tempbuf,newpath));
743 /* ---------------- */
744 int RF_deletecurdir_osx(const struct vol *vol)
746 return netatalk_unlink( vol->vfs->ad_path(".",0) );
749 /* ---------------- */
750 static int RF_setdirunixmode_osx(const struct vol *vol, const char * name, mode_t mode, struct stat *st)
752 return adouble_setfilmode(vol->vfs->ad_path( name, ADFLAGS_DIR ), mode, st);
755 /* ---------------- */
757 RF_setdirmode_osx(const struct vol *vol _U_, const char *name _U_, mode_t mode _U_, struct stat *st _U_)
762 /* ---------------- */
764 RF_setdirowner_osx(const struct vol *vol _U_, const char *path _U_, uid_t uid _U_, gid_t gid _U_)
769 /* ---------------- */
770 int RF_renamefile_osx(const struct vol *vol, const char *src, const char *dst)
772 char adsrc[ MAXPATHLEN + 1];
775 strcpy( adsrc, vol->vfs->ad_path( src, 0 ));
777 if (unix_rename( adsrc, vol->vfs->ad_path( dst, 0 )) < 0) {
781 if (errno == ENOENT && stat(adsrc, &st)) /* source has no ressource fork, */
789 struct vfs_ops netatalk_adouble_osx = {
790 /* ad_path: */ ad_path_osx,
791 /* validupath: */ validupath_osx,
792 /* rf_chown: */ RF_chown_adouble,
793 /* rf_renamedir: */ RF_renamedir_osx,
794 /* rf_deletecurdir: */ RF_deletecurdir_osx,
795 /* rf_setfilmode: */ RF_setfilmode_adouble,
796 /* rf_setdirmode: */ RF_setdirmode_osx,
797 /* rf_setdirunixmode:*/ RF_setdirunixmode_osx,
798 /* rf_setdirowner: */ RF_setdirowner_osx,
799 /* rf_deletefile: */ RF_deletefile_adouble,
800 /* rf_renamefile: */ RF_renamefile_osx,
803 /* =======================================
805 ad_path shouldn't be set here
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,
821 /* ---------------- */
822 void initvol_vfs(struct vol *vol)
824 if (vol->v_adouble == AD_VERSION2_OSX) {
825 vol->vfs = &netatalk_adouble_osx;
827 else if (vol->v_adouble == AD_VERSION1_SFM) {
828 vol->vfs = &netatalk_adouble_sfm;
831 vol->vfs = &netatalk_adouble;