2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
13 #include <sys/param.h>
14 #include <sys/types.h>
18 #include <atalk/afp.h>
19 #include <atalk/util.h>
20 #include <atalk/directory.h>
21 #include <atalk/volume.h>
22 #include <atalk/logger.h>
23 #include <atalk/unix.h>
24 #include <atalk/acl.h>
25 #include <atalk/compat.h>
26 #include <atalk/errchk.h>
29 /* -----------------------------
30 a dropbox is a folder where w is set but not r eg:
32 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
34 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
39 * Ignore EPERM errors: We may be dealing with a directory that is
40 * group writable, in which case chmod will fail.
42 if ( (chmod_acl( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
43 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
45 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
52 /* ------------------------- */
53 int dir_rx_set(mode_t mode)
55 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
58 /* --------------------- */
59 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
62 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
65 if (lstat(name, &sb) != 0)
70 if (S_ISLNK(st->st_mode))
71 return 0; /* we don't want to change link permissions */
73 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
75 if ( chmod_acl( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
82 * @brief system rmdir with afp error code.
84 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
86 int netatalk_rmdir_all_errors(int dirfd, const char *name)
93 err = unlinkat(dirfd, name, AT_REMOVEDIR);
103 return AFPERR_DIRNEMPT;
106 return AFPERR_ACCESS;
117 * @brief System rmdir with afp error code, but ENOENT is not an error.
119 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
121 int netatalk_rmdir(int dirfd, const char *name)
123 int ret = netatalk_rmdir_all_errors(dirfd, name);
124 if (ret == AFPERR_NOOBJ)
129 /* -------------------
130 system unlink with afp error code.
131 ENOENT is not an error.
133 int netatalk_unlink(const char *name)
135 if (unlink(name) < 0) {
143 return AFPERR_ACCESS;
151 /**************************************************************************
152 * *at semnatics support functions (like openat, renameat standard funcs)
153 **************************************************************************/
155 /* Copy all file data from one file fd to another */
156 int copy_file_fd(int sfd, int dfd)
161 char filebuf[NETATALK_DIOSZ_STACK];
163 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
167 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
173 if ((cc = write(dfd, filebuf, buflen)) < 0) {
176 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
188 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
190 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
197 char filebuf[NETATALK_DIOSZ_STACK];
202 sfd = openat(dirfd, src, O_RDONLY);
204 sfd = open(src, O_RDONLY);
207 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
208 src, dst, src, strerror(errno));
212 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
213 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
214 src, dst, dst, strerror(errno));
219 ret = copy_file_fd(sfd, dfd);
230 /* don't bother to report an error if there's already one */
231 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
232 src, dst, dst, strerror(errno));
241 * Copy an EA from one file to another
243 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
245 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
256 EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
258 EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
260 EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
262 if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
263 EC_NULL_LOG( eabuf = malloc(easize));
264 EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
265 EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
278 * at wrapper for netatalk_unlink
280 int netatalk_unlinkat(int dirfd, const char *name)
286 if (unlinkat(dirfd, name, 0) < 0) {
294 return AFPERR_ACCESS;
301 return netatalk_unlink(name);
309 * @brief This is equivalent of unix rename()
311 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
314 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
315 * @param oldpath (r) guess what
316 * @param dfd (r) same as sfd
317 * @param newpath (r) guess what
319 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
327 if (renameat(sfd, oldpath, dfd, newpath) < 0)
330 if (rename(oldpath, newpath) < 0)
332 #endif /* HAVE_ATFUNCS */
338 * @brief stat/fsstatat multiplexer
340 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
342 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
343 * @param path (r) pathname
344 * @param st (rw) pointer to struct stat
346 int statat(int dirfd, const char *path, struct stat *st)
351 return (fstatat(dirfd, path, st, 0));
353 return (stat(path, st));
361 * @brief lstat/fsstatat multiplexer
363 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
365 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
366 * @param path (r) pathname
367 * @param st (rw) pointer to struct stat
369 int lstatat(int dirfd, const char *path, struct stat *st)
374 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
376 return (lstat(path, st));
384 * @brief opendir wrapper for *at semantics support
386 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
388 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
389 * @param path (r) pathname
391 DIR *opendirat(int dirfd, const char *path)
397 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
405 if (dirfd != -1 && fchdir(cwd) != 0) {
406 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");