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>
26 /* -----------------------------
27 a dropbox is a folder where w is set but not r eg:
29 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
31 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
36 * Ignore EPERM errors: We may be dealing with a directory that is
37 * group writable, in which case chmod will fail.
39 if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
40 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
42 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
49 /* ------------------------- */
50 int dir_rx_set(mode_t mode)
52 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
55 /* --------------------- */
56 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
59 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
62 if (lstat(name, &sb) != 0)
67 if (S_ISLNK(st->st_mode))
68 return 0; /* we don't want to change link permissions */
70 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
72 if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
79 * @brief system rmdir with afp error code.
81 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
83 int netatalk_rmdir_all_errors(int dirfd, const char *name)
90 err = unlinkat(dirfd, name, AT_REMOVEDIR);
100 return AFPERR_DIRNEMPT;
103 return AFPERR_ACCESS;
114 * @brief System rmdir with afp error code, but ENOENT is not an error.
116 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
118 int netatalk_rmdir(int dirfd, const char *name)
120 int ret = netatalk_rmdir_all_errors(dirfd, name);
121 if (ret == AFPERR_NOOBJ)
126 /* -------------------
127 system unlink with afp error code.
128 ENOENT is not an error.
130 int netatalk_unlink(const char *name)
132 if (unlink(name) < 0) {
140 return AFPERR_ACCESS;
148 char *fullpathname(const char *name)
150 static char wd[ MAXPATHLEN + 1];
152 if ( getcwd( wd , MAXPATHLEN) ) {
153 strlcat(wd, "/", MAXPATHLEN);
154 strlcat(wd, name, MAXPATHLEN);
157 strlcpy(wd, name, MAXPATHLEN);
163 /**************************************************************************
164 * *at semnatics support functions (like openat, renameat standard funcs)
165 **************************************************************************/
168 * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this
170 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
182 sfd = openat(dirfd, src, O_RDONLY);
184 sfd = open(src, O_RDONLY);
187 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
188 src, dst, src, strerror(errno));
192 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
193 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
194 src, dst, dst, strerror(errno));
199 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
203 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
204 src, dst, src, strerror(errno));
211 if ((cc = write(dfd, filebuf, buflen)) < 0) {
214 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
215 src, dst, dst, strerror(errno));
232 /* don't bother to report an error if there's already one */
233 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
234 src, dst, dst, strerror(errno));
243 * at wrapper for netatalk_unlink
245 int netatalk_unlinkat(int dirfd, const char *name)
251 if (unlinkat(dirfd, name, 0) < 0) {
259 return AFPERR_ACCESS;
266 return netatalk_unlink(name);
274 * @brief This is equivalent of unix rename()
276 * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd
279 * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD
280 * @param oldpath (r) guess what
281 * @param dfd (r) same as sfd
282 * @param newpath (r) guess what
284 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
292 if (renameat(sfd, oldpath, dfd, newpath) < 0)
295 if (rename(oldpath, newpath) < 0)
297 #endif /* HAVE_RENAMEAT */
303 * @brief stat/fsstatat multiplexer
305 * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
307 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
308 * @param path (r) pathname
309 * @param st (rw) pointer to struct stat
311 int statat(int dirfd, const char *path, struct stat *st)
316 return (fstatat(dirfd, path, st, 0));
318 return (stat(path, st));
326 * @brief lstat/fsstatat multiplexer
328 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
330 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
331 * @param path (r) pathname
332 * @param st (rw) pointer to struct stat
334 int lstatat(int dirfd, const char *path, struct stat *st)
339 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
341 return (lstat(path, st));
349 * @brief opendir wrapper for *at semantics support
351 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
353 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
354 * @param path (r) pathname
356 DIR *opendirat(int dirfd, const char *path)
362 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
370 if (dirfd != -1 && fchdir(cwd) != 0) {
371 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");