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 int dir_rx_set(mode_t mode)
32 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
35 /* --------------------- */
36 int setfilmode(const struct vol *vol, const char *name, mode_t mode, struct stat *st)
39 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
42 if (lstat(name, &sb) != 0)
47 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
49 if (ochmod((char *)name, mode & ~vol->v_umask, st, vol_syml_opt(vol) | O_NETATALK_ACL) < 0 && errno != EPERM ) {
56 * @brief system rmdir with afp error code.
58 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
60 int netatalk_rmdir_all_errors(int dirfd, const char *name)
67 err = unlinkat(dirfd, name, AT_REMOVEDIR);
77 return AFPERR_DIRNEMPT;
91 * @brief System rmdir with afp error code, but ENOENT is not an error.
93 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
95 int netatalk_rmdir(int dirfd, const char *name)
97 int ret = netatalk_rmdir_all_errors(dirfd, name);
98 if (ret == AFPERR_NOOBJ)
103 /* -------------------
104 system unlink with afp error code.
105 ENOENT is not an error.
107 int netatalk_unlink(const char *name)
109 if (unlink(name) < 0) {
117 return AFPERR_ACCESS;
125 /**************************************************************************
126 * *at semnatics support functions (like openat, renameat standard funcs)
127 **************************************************************************/
129 /* Copy all file data from one file fd to another */
130 int copy_file_fd(int sfd, int dfd)
135 char filebuf[NETATALK_DIOSZ_STACK];
137 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
141 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
147 if ((cc = write(dfd, filebuf, buflen)) < 0) {
150 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
162 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
164 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
171 char filebuf[NETATALK_DIOSZ_STACK];
176 sfd = openat(dirfd, src, O_RDONLY);
178 sfd = open(src, O_RDONLY);
181 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
182 src, dst, src, strerror(errno));
186 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
187 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
188 src, dst, dst, strerror(errno));
193 ret = copy_file_fd(sfd, dfd);
204 /* don't bother to report an error if there's already one */
205 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
206 src, dst, dst, strerror(errno));
215 * Copy an EA from one file to another
217 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
219 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
230 EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
232 EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
234 EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
236 if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
237 EC_NULL_LOG( eabuf = malloc(easize));
238 EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
239 EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
252 * at wrapper for netatalk_unlink
254 int netatalk_unlinkat(int dirfd, const char *name)
260 if (unlinkat(dirfd, name, 0) < 0) {
268 return AFPERR_ACCESS;
275 return netatalk_unlink(name);
283 * @brief This is equivalent of unix rename()
285 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
288 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
289 * @param oldpath (r) guess what
290 * @param dfd (r) same as sfd
291 * @param newpath (r) guess what
293 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
301 if (renameat(sfd, oldpath, dfd, newpath) < 0)
304 if (rename(oldpath, newpath) < 0)
306 #endif /* HAVE_ATFUNCS */
312 * @brief stat/fsstatat multiplexer
314 * statat mulitplexes stat 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 statat(int dirfd, const char *path, struct stat *st)
325 return (fstatat(dirfd, path, st, 0));
327 return (stat(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!");