X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=libatalk%2Fvfs%2Funix.c;h=79e4fd33c29b151a08cd4afb4b157c638ea444b4;hp=83c6016903e42e8615fe4232b2b4118b755ea4ce;hb=85f0a9871848c178e301a804c9d9081f3fbaee33;hpb=cdb7047fb6897c1ad8b4ac264001d44154bf144d diff --git a/libatalk/vfs/unix.c b/libatalk/vfs/unix.c index 83c60169..79e4fd33 100644 --- a/libatalk/vfs/unix.c +++ b/libatalk/vfs/unix.c @@ -23,29 +23,8 @@ #include #include #include - -/* ----------------------------- - a dropbox is a folder where w is set but not r eg: - rwx-wx-wx or rwx-wx-- - rwx----wx (is not asked by a Mac with OS >= 8.0 ?) -*/ -int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask) -{ - int retval = 0; - - /* - * Ignore EPERM errors: We may be dealing with a directory that is - * group writable, in which case chmod will fail. - */ - if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM && - !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) ) - { - LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) ); - retval = -1; - } - - return retval; -} +#include +#include /* ------------------------- */ int dir_rx_set(mode_t mode) @@ -54,7 +33,7 @@ int dir_rx_set(mode_t mode) } /* --------------------- */ -int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask) +int setfilmode(const struct vol *vol, const char *name, mode_t mode, struct stat *st) { struct stat sb; mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */ @@ -65,12 +44,9 @@ int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask) st = &sb; } - if (S_ISLNK(st->st_mode)) - return 0; /* we don't want to change link permissions */ - mode |= st->st_mode & ~mask; /* keep other bits from previous mode */ - if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) { + if (ochmod((char *)name, mode & ~vol->v_umask, st, vol_syml_opt(vol) | O_NETATALK_ACL) < 0 && errno != EPERM ) { return -1; } return 0; @@ -79,15 +55,15 @@ int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask) /* * @brief system rmdir with afp error code. * - * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this. + * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this. */ int netatalk_rmdir_all_errors(int dirfd, const char *name) { int err; -#ifdef HAVE_RENAMEAT +#ifdef HAVE_ATFUNCS if (dirfd == -1) - dirfd = ATFD_CWD; + dirfd = AT_FDCWD; err = unlinkat(dirfd, name, AT_REMOVEDIR); #else err = rmdir(name); @@ -98,6 +74,7 @@ int netatalk_rmdir_all_errors(int dirfd, const char *name) case ENOENT : return AFPERR_NOOBJ; case ENOTEMPTY : + case EEXIST: return AFPERR_DIRNEMPT; case EPERM: case EACCES : @@ -114,7 +91,7 @@ int netatalk_rmdir_all_errors(int dirfd, const char *name) /* * @brief System rmdir with afp error code, but ENOENT is not an error. * - * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this. + * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this. */ int netatalk_rmdir(int dirfd, const char *name) { @@ -146,80 +123,72 @@ int netatalk_unlink(const char *name) return AFP_OK; } -char *fullpathname(const char *name) +/************************************************************************** + * *at semnatics support functions (like openat, renameat standard funcs) + **************************************************************************/ + +/* Copy all file data from one file fd to another */ +int copy_file_fd(int sfd, int dfd) { - static char wd[ MAXPATHLEN + 1]; + EC_INIT; + ssize_t cc; + size_t buflen; + char filebuf[NETATALK_DIOSZ_STACK]; - if ( getcwd( wd , MAXPATHLEN) ) { - strlcat(wd, "/", MAXPATHLEN); - strlcat(wd, name, MAXPATHLEN); - } - else { - strlcpy(wd, name, MAXPATHLEN); - } - return wd; -} + while ((cc = read(sfd, filebuf, sizeof(filebuf)))) { + if (cc < 0) { + if (errno == EINTR) + continue; + LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno)); + EC_FAIL; + } + buflen = cc; + while (buflen > 0) { + if ((cc = write(dfd, filebuf, buflen)) < 0) { + if (errno == EINTR) + continue; + LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno)); + EC_FAIL; + } + buflen -= cc; + } + } -/************************************************************************** - * *at semnatics support functions (like openat, renameat standard funcs) - **************************************************************************/ +EC_CLEANUP: + EC_EXIT; +} /* - * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this + * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this */ int copy_file(int dirfd, const char *src, const char *dst, mode_t mode) { int ret = 0; int sfd = -1; int dfd = -1; - ssize_t cc; - size_t buflen; - char filebuf[8192]; -#ifdef HAVE_RENAMEAT +#ifdef HAVE_ATFUNCS if (dirfd == -1) - dirfd = ATFD_CWD; + dirfd = AT_FDCWD; sfd = openat(dirfd, src, O_RDONLY); #else sfd = open(src, O_RDONLY); #endif if (sfd < 0) { - LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s", + LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s", src, dst, src, strerror(errno)); return -1; } if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) { - LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s", + LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s", src, dst, dst, strerror(errno)); ret = -1; goto exit; } - while ((cc = read(sfd, filebuf, sizeof(filebuf)))) { - if (cc < 0) { - if (errno == EINTR) - continue; - LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s", - src, dst, src, strerror(errno)); - ret = -1; - goto exit; - } - - buflen = cc; - while (buflen > 0) { - if ((cc = write(dfd, filebuf, buflen)) < 0) { - if (errno == EINTR) - continue; - LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s", - src, dst, dst, strerror(errno)); - ret = -1; - goto exit; - } - buflen -= cc; - } - } + ret = copy_file_fd(sfd, dfd); exit: if (sfd != -1) @@ -240,12 +209,49 @@ exit: return ret; } +/*! + * Copy an EA from one file to another + * + * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this + */ +int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode) +{ + EC_INIT; + int sfd = -1; + int dfd = -1; + size_t easize; + char *eabuf = NULL; + +#ifdef HAVE_ATFUNCS + if (dirfd == -1) + dirfd = AT_FDCWD; + EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) ); +#else + EC_NEG1_LOG( sfd = open(src, O_RDONLY) ); +#endif + EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) ); + + if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) { + EC_NULL_LOG( eabuf = malloc(easize)); + EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) ); + EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) ); + } + +EC_CLEANUP: + if (sfd != -1) + close(sfd); + if (dfd != -1) + close(dfd); + free(eabuf); + EC_EXIT; +} + /* * at wrapper for netatalk_unlink */ int netatalk_unlinkat(int dirfd, const char *name) { -#ifdef HAVE_RENAMEAT +#ifdef HAVE_ATFUNCS if (dirfd == -1) dirfd = AT_FDCWD; @@ -274,17 +280,17 @@ int netatalk_unlinkat(int dirfd, const char *name) /* * @brief This is equivalent of unix rename() * - * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd + * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd * are ignored. * - * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD + * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD * @param oldpath (r) guess what * @param dfd (r) same as sfd * @param newpath (r) guess what */ int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath) { -#ifdef HAVE_RENAMEAT +#ifdef HAVE_ATFUNCS if (sfd == -1) sfd = AT_FDCWD; if (dfd == -1) @@ -295,7 +301,7 @@ int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath) #else if (rename(oldpath, newpath) < 0) return -1; -#endif /* HAVE_RENAMEAT */ +#endif /* HAVE_ATFUNCS */ return 0; } @@ -303,15 +309,15 @@ int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath) /* * @brief stat/fsstatat multiplexer * - * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored. + * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored. * - * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD + * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD * @param path (r) pathname * @param st (rw) pointer to struct stat */ int statat(int dirfd, const char *path, struct stat *st) { -#ifdef HAVE_RENAMEAT +#ifdef HAVE_ATFUNCS if (dirfd == -1) dirfd = AT_FDCWD; return (fstatat(dirfd, path, st, 0)); @@ -323,29 +329,6 @@ int statat(int dirfd, const char *path, struct stat *st) return -1; } -/* - * @brief lstat/fsstatat multiplexer - * - * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored. - * - * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD - * @param path (r) pathname - * @param st (rw) pointer to struct stat - */ -int lstatat(int dirfd, const char *path, struct stat *st) -{ -#ifdef HAVE_RENAMEAT - if (dirfd == -1) - dirfd = AT_FDCWD; - return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW)); -#else - return (lstat(path, st)); -#endif - - /* DEADC0DE */ - return -1; -} - /* * @brief opendir wrapper for *at semantics support *