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)
173 sfd = openat(dirfd, src, O_RDONLY);
175 sfd = open(src, O_RDONLY);
178 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
179 src, dst, src, strerror(errno));
183 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
184 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
185 src, dst, dst, strerror(errno));
190 ret = copy_file_fd(sfd, dfd);
201 /* don't bother to report an error if there's already one */
202 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
203 src, dst, dst, strerror(errno));
212 * Copy an EA from one file to another
214 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
216 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
227 EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
229 EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
231 EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
233 if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
234 EC_NULL_LOG( eabuf = malloc(easize));
235 EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
236 EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
249 * at wrapper for netatalk_unlink
251 int netatalk_unlinkat(int dirfd, const char *name)
257 if (unlinkat(dirfd, name, 0) < 0) {
265 return AFPERR_ACCESS;
272 return netatalk_unlink(name);
280 * @brief This is equivalent of unix rename()
282 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
285 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
286 * @param oldpath (r) guess what
287 * @param dfd (r) same as sfd
288 * @param newpath (r) guess what
290 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
298 if (renameat(sfd, oldpath, dfd, newpath) < 0)
301 if (rename(oldpath, newpath) < 0)
303 #endif /* HAVE_ATFUNCS */
309 * @brief stat/fsstatat multiplexer
311 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
313 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
314 * @param path (r) pathname
315 * @param st (rw) pointer to struct stat
317 int statat(int dirfd, const char *path, struct stat *st)
322 return (fstatat(dirfd, path, st, 0));
324 return (stat(path, st));
332 * @brief opendir wrapper for *at semantics support
334 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
336 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
337 * @param path (r) pathname
339 DIR *opendirat(int dirfd, const char *path)
345 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
353 if (dirfd != -1 && fchdir(cwd) != 0) {
354 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");