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>
27 /* -----------------------------
28 a dropbox is a folder where w is set but not r eg:
30 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
32 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
37 * Ignore EPERM errors: We may be dealing with a directory that is
38 * group writable, in which case chmod will fail.
40 if ( (chmod_acl( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
41 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
43 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
50 /* ------------------------- */
51 int dir_rx_set(mode_t mode)
53 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
56 /* --------------------- */
57 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
60 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
63 if (lstat(name, &sb) != 0)
68 if (S_ISLNK(st->st_mode))
69 return 0; /* we don't want to change link permissions */
71 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
73 if ( chmod_acl( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
80 * @brief system rmdir with afp error code.
82 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
84 int netatalk_rmdir_all_errors(int dirfd, const char *name)
91 err = unlinkat(dirfd, name, AT_REMOVEDIR);
101 return AFPERR_DIRNEMPT;
104 return AFPERR_ACCESS;
115 * @brief System rmdir with afp error code, but ENOENT is not an error.
117 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
119 int netatalk_rmdir(int dirfd, const char *name)
121 int ret = netatalk_rmdir_all_errors(dirfd, name);
122 if (ret == AFPERR_NOOBJ)
127 /* -------------------
128 system unlink with afp error code.
129 ENOENT is not an error.
131 int netatalk_unlink(const char *name)
133 if (unlink(name) < 0) {
141 return AFPERR_ACCESS;
149 /**************************************************************************
150 * *at semnatics support functions (like openat, renameat standard funcs)
151 **************************************************************************/
154 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
156 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
168 sfd = openat(dirfd, src, O_RDONLY);
170 sfd = open(src, O_RDONLY);
173 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
174 src, dst, src, strerror(errno));
178 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
179 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
180 src, dst, dst, strerror(errno));
185 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
189 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
190 src, dst, src, strerror(errno));
197 if ((cc = write(dfd, filebuf, buflen)) < 0) {
200 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
201 src, dst, dst, strerror(errno));
218 /* don't bother to report an error if there's already one */
219 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
220 src, dst, dst, strerror(errno));
229 * at wrapper for netatalk_unlink
231 int netatalk_unlinkat(int dirfd, const char *name)
237 if (unlinkat(dirfd, name, 0) < 0) {
245 return AFPERR_ACCESS;
252 return netatalk_unlink(name);
260 * @brief This is equivalent of unix rename()
262 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
265 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
266 * @param oldpath (r) guess what
267 * @param dfd (r) same as sfd
268 * @param newpath (r) guess what
270 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
278 if (renameat(sfd, oldpath, dfd, newpath) < 0)
281 if (rename(oldpath, newpath) < 0)
283 #endif /* HAVE_ATFUNCS */
289 * @brief stat/fsstatat multiplexer
291 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
293 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
294 * @param path (r) pathname
295 * @param st (rw) pointer to struct stat
297 int statat(int dirfd, const char *path, struct stat *st)
302 return (fstatat(dirfd, path, st, 0));
304 return (stat(path, st));
312 * @brief lstat/fsstatat multiplexer
314 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
316 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
317 * @param path (r) pathname
318 * @param st (rw) pointer to struct stat
320 int lstatat(int dirfd, const char *path, struct stat *st)
325 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
327 return (lstat(path, st));
335 * @brief opendir wrapper for *at semantics support
337 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
339 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
340 * @param path (r) pathname
342 DIR *opendirat(int dirfd, const char *path)
348 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
356 if (dirfd != -1 && fchdir(cwd) != 0) {
357 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");