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 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 **************************************************************************/
153 /* Copy all file data from one file fd to another */
154 int copy_file_fd(int sfd, int dfd)
159 char filebuf[NETATALK_DIOSZ_STACK];
161 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
165 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
171 if ((cc = write(dfd, filebuf, buflen)) < 0) {
174 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
186 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
188 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
195 char filebuf[NETATALK_DIOSZ_STACK];
200 sfd = openat(dirfd, src, O_RDONLY);
202 sfd = open(src, O_RDONLY);
205 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
206 src, dst, src, strerror(errno));
210 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
211 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
212 src, dst, dst, strerror(errno));
217 ret = copy_file_fd(sfd, dfd);
228 /* don't bother to report an error if there's already one */
229 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
230 src, dst, dst, strerror(errno));
239 * Copy an EA from one file to another
241 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
243 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
254 EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
256 EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
258 EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
260 if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
261 EC_NULL_LOG( eabuf = malloc(easize));
262 EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
263 EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
276 * at wrapper for netatalk_unlink
278 int netatalk_unlinkat(int dirfd, const char *name)
284 if (unlinkat(dirfd, name, 0) < 0) {
292 return AFPERR_ACCESS;
299 return netatalk_unlink(name);
307 * @brief This is equivalent of unix rename()
309 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
312 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
313 * @param oldpath (r) guess what
314 * @param dfd (r) same as sfd
315 * @param newpath (r) guess what
317 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
325 if (renameat(sfd, oldpath, dfd, newpath) < 0)
328 if (rename(oldpath, newpath) < 0)
330 #endif /* HAVE_ATFUNCS */
336 * @brief stat/fsstatat multiplexer
338 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
340 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
341 * @param path (r) pathname
342 * @param st (rw) pointer to struct stat
344 int statat(int dirfd, const char *path, struct stat *st)
349 return (fstatat(dirfd, path, st, 0));
351 return (stat(path, st));
359 * @brief lstat/fsstatat multiplexer
361 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
363 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
364 * @param path (r) pathname
365 * @param st (rw) pointer to struct stat
367 int lstatat(int dirfd, const char *path, struct stat *st)
372 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
374 return (lstat(path, st));
382 * @brief opendir wrapper for *at semantics support
384 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
386 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
387 * @param path (r) pathname
389 DIR *opendirat(int dirfd, const char *path)
395 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
403 if (dirfd != -1 && fchdir(cwd) != 0) {
404 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");