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( 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( 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 char *fullpathname(const char *name)
151 static char wd[ MAXPATHLEN + 1];
153 if ( getcwd( wd , MAXPATHLEN) ) {
154 strlcat(wd, "/", MAXPATHLEN);
155 strlcat(wd, name, MAXPATHLEN);
158 strlcpy(wd, name, MAXPATHLEN);
164 /**************************************************************************
165 * *at semnatics support functions (like openat, renameat standard funcs)
166 **************************************************************************/
169 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
171 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
183 sfd = openat(dirfd, src, O_RDONLY);
185 sfd = open(src, O_RDONLY);
188 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
189 src, dst, src, strerror(errno));
193 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
194 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
195 src, dst, dst, strerror(errno));
200 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
204 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
205 src, dst, src, strerror(errno));
212 if ((cc = write(dfd, filebuf, buflen)) < 0) {
215 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
216 src, dst, dst, strerror(errno));
233 /* don't bother to report an error if there's already one */
234 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
235 src, dst, dst, strerror(errno));
244 * at wrapper for netatalk_unlink
246 int netatalk_unlinkat(int dirfd, const char *name)
252 if (unlinkat(dirfd, name, 0) < 0) {
260 return AFPERR_ACCESS;
267 return netatalk_unlink(name);
275 * @brief This is equivalent of unix rename()
277 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
280 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
281 * @param oldpath (r) guess what
282 * @param dfd (r) same as sfd
283 * @param newpath (r) guess what
285 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
293 if (renameat(sfd, oldpath, dfd, newpath) < 0)
296 if (rename(oldpath, newpath) < 0)
298 #endif /* HAVE_ATFUNCS */
304 * @brief stat/fsstatat multiplexer
306 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
308 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
309 * @param path (r) pathname
310 * @param st (rw) pointer to struct stat
312 int statat(int dirfd, const char *path, struct stat *st)
317 return (fstatat(dirfd, path, st, 0));
319 return (stat(path, st));
327 * @brief lstat/fsstatat multiplexer
329 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
331 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
332 * @param path (r) pathname
333 * @param st (rw) pointer to struct stat
335 int lstatat(int dirfd, const char *path, struct stat *st)
340 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
342 return (lstat(path, st));
350 * @brief opendir wrapper for *at semantics support
352 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
354 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
355 * @param path (r) pathname
357 DIR *opendirat(int dirfd, const char *path)
363 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
371 if (dirfd != -1 && fchdir(cwd) != 0) {
372 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");