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 char * name, mode_t mode, struct stat *st, mode_t v_umask)
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 if (S_ISLNK(st->st_mode))
48 return 0; /* we don't want to change link permissions */
50 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
52 if ( chmod_acl( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
59 * @brief system rmdir with afp error code.
61 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
63 int netatalk_rmdir_all_errors(int dirfd, const char *name)
70 err = unlinkat(dirfd, name, AT_REMOVEDIR);
80 return AFPERR_DIRNEMPT;
94 * @brief System rmdir with afp error code, but ENOENT is not an error.
96 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
98 int netatalk_rmdir(int dirfd, const char *name)
100 int ret = netatalk_rmdir_all_errors(dirfd, name);
101 if (ret == AFPERR_NOOBJ)
106 /* -------------------
107 system unlink with afp error code.
108 ENOENT is not an error.
110 int netatalk_unlink(const char *name)
112 if (unlink(name) < 0) {
120 return AFPERR_ACCESS;
128 /**************************************************************************
129 * *at semnatics support functions (like openat, renameat standard funcs)
130 **************************************************************************/
132 /* Copy all file data from one file fd to another */
133 int copy_file_fd(int sfd, int dfd)
138 char filebuf[NETATALK_DIOSZ_STACK];
140 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
144 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
150 if ((cc = write(dfd, filebuf, buflen)) < 0) {
153 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
165 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
167 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
174 char filebuf[NETATALK_DIOSZ_STACK];
179 sfd = openat(dirfd, src, O_RDONLY);
181 sfd = open(src, O_RDONLY);
184 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
185 src, dst, src, strerror(errno));
189 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
190 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
191 src, dst, dst, strerror(errno));
196 ret = copy_file_fd(sfd, dfd);
207 /* don't bother to report an error if there's already one */
208 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
209 src, dst, dst, strerror(errno));
218 * Copy an EA from one file to another
220 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
222 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
233 EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
235 EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
237 EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
239 if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
240 EC_NULL_LOG( eabuf = malloc(easize));
241 EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
242 EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
255 * at wrapper for netatalk_unlink
257 int netatalk_unlinkat(int dirfd, const char *name)
263 if (unlinkat(dirfd, name, 0) < 0) {
271 return AFPERR_ACCESS;
278 return netatalk_unlink(name);
286 * @brief This is equivalent of unix rename()
288 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
291 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
292 * @param oldpath (r) guess what
293 * @param dfd (r) same as sfd
294 * @param newpath (r) guess what
296 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
304 if (renameat(sfd, oldpath, dfd, newpath) < 0)
307 if (rename(oldpath, newpath) < 0)
309 #endif /* HAVE_ATFUNCS */
315 * @brief stat/fsstatat multiplexer
317 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
319 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
320 * @param path (r) pathname
321 * @param st (rw) pointer to struct stat
323 int statat(int dirfd, const char *path, struct stat *st)
328 return (fstatat(dirfd, path, st, 0));
330 return (stat(path, st));
338 * @brief lstat/fsstatat multiplexer
340 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
342 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
343 * @param path (r) pathname
344 * @param st (rw) pointer to struct stat
346 int lstatat(int dirfd, const char *path, struct stat *st)
351 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
353 return (lstat(path, st));
361 * @brief opendir wrapper for *at semantics support
363 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
365 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
366 * @param path (r) pathname
368 DIR *opendirat(int dirfd, const char *path)
374 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
382 if (dirfd != -1 && fchdir(cwd) != 0) {
383 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");