]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/unix.c
fce: afpd: fix event names array
[netatalk.git] / libatalk / vfs / unix.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <unistd.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <string.h>
17
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>
27 #include <atalk/ea.h>
28
29 /* ------------------------- */
30 int dir_rx_set(mode_t mode)
31 {
32     return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
33 }
34
35 /* --------------------- */
36 int setfilmode(const struct vol *vol, const char *name, mode_t mode, struct stat *st)
37 {
38     struct stat sb;
39     mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
40
41     if (!st) {
42         if (lstat(name, &sb) != 0)
43             return -1;
44         st = &sb;
45     }
46
47     mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
48
49     if (ochmod((char *)name,
50                mode & ~vol->v_umask,
51                st,
52                vol_syml_opt(vol) | vol_chmod_opt(vol)
53             ) < 0 && errno != EPERM ) {
54         return -1;
55     }
56     return 0;
57 }
58
59 /*
60  * @brief system rmdir with afp error code.
61  *
62  * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
63  */
64 int netatalk_rmdir_all_errors(int dirfd, const char *name)
65 {
66     int err;
67
68 #ifdef HAVE_ATFUNCS
69     if (dirfd == -1)
70         dirfd = AT_FDCWD;
71     err = unlinkat(dirfd, name, AT_REMOVEDIR);
72 #else
73     err = rmdir(name);
74 #endif
75
76     if (err < 0) {
77         switch ( errno ) {
78         case ENOENT :
79             return AFPERR_NOOBJ;
80         case ENOTEMPTY :
81         case EEXIST:
82             return AFPERR_DIRNEMPT;
83         case EPERM:
84         case EACCES :
85             return AFPERR_ACCESS;
86         case EROFS:
87             return AFPERR_VLOCK;
88         default :
89             return AFPERR_PARAM;
90         }
91     }
92     return AFP_OK;
93 }
94
95 /*
96  * @brief System rmdir with afp error code, but ENOENT is not an error.
97  *
98  * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
99  */
100 int netatalk_rmdir(int dirfd, const char *name)
101 {
102     int ret = netatalk_rmdir_all_errors(dirfd, name);
103     if (ret == AFPERR_NOOBJ)
104         return AFP_OK;
105     return ret;
106 }
107
108 /* -------------------
109    system unlink with afp error code.
110    ENOENT is not an error.
111 */
112 int netatalk_unlink(const char *name)
113 {
114     if (unlink(name) < 0) {
115         switch (errno) {
116         case ENOENT :
117             break;
118         case EROFS:
119             return AFPERR_VLOCK;
120         case EPERM:
121         case EACCES :
122             return AFPERR_ACCESS;
123         default :
124             return AFPERR_PARAM;
125         }
126     }
127     return AFP_OK;
128 }
129
130 /**************************************************************************
131  * *at semnatics support functions (like openat, renameat standard funcs)
132  **************************************************************************/
133
134 /* Copy all file data from one file fd to another */
135 int copy_file_fd(int sfd, int dfd)
136 {
137     EC_INIT;
138     ssize_t cc;
139     size_t  buflen;
140     char   filebuf[NETATALK_DIOSZ_STACK];
141
142     while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
143         if (cc < 0) {
144             if (errno == EINTR)
145                 continue;
146             LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
147             EC_FAIL;
148         }
149
150         buflen = cc;
151         while (buflen > 0) {
152             if ((cc = write(dfd, filebuf, buflen)) < 0) {
153                 if (errno == EINTR)
154                     continue;
155                 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
156                 EC_FAIL;
157             }
158             buflen -= cc;
159         }
160     }
161
162 EC_CLEANUP:
163     EC_EXIT;
164 }
165
166 /* 
167  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
168  */
169 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
170 {
171     int    ret = 0;
172     int    sfd = -1;
173     int    dfd = -1;
174
175 #ifdef HAVE_ATFUNCS
176     if (dirfd == -1)
177         dirfd = AT_FDCWD;
178     sfd = openat(dirfd, src, O_RDONLY);
179 #else
180     sfd = open(src, O_RDONLY);
181 #endif
182     if (sfd < 0) {
183         LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
184             src, dst, src, strerror(errno));
185         return -1;
186     }
187
188     if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
189         LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
190             src, dst, dst, strerror(errno));
191         ret = -1;
192         goto exit;
193     }
194
195     ret = copy_file_fd(sfd, dfd);
196
197 exit:
198     if (sfd != -1)
199         close(sfd);
200
201     if (dfd != -1) {
202         int err;
203
204         err = close(dfd);
205         if (!ret && err) {
206             /* don't bother to report an error if there's already one */
207             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
208                 src, dst, dst, strerror(errno));
209             ret = -1;
210         }
211     }
212
213     return ret;
214 }
215
216 /*!
217  * Copy an EA from one file to another
218  *
219  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
220  */
221 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
222 {
223     EC_INIT;
224     int    sfd = -1;
225     int    dfd = -1;
226     size_t easize;
227     char   *eabuf = NULL;
228
229 #ifdef HAVE_ATFUNCS
230     if (dirfd == -1)
231         dirfd = AT_FDCWD;
232     EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
233 #else
234     EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
235 #endif
236     EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
237
238     if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
239         EC_NULL_LOG( eabuf = malloc(easize));
240         EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
241         EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
242     }
243
244 EC_CLEANUP:
245     if (sfd != -1)
246         close(sfd);
247     if (dfd != -1)
248         close(dfd);
249     free(eabuf);
250     EC_EXIT;
251 }
252
253 /* 
254  * at wrapper for netatalk_unlink
255  */
256 int netatalk_unlinkat(int dirfd, const char *name)
257 {
258 #ifdef HAVE_ATFUNCS
259     if (dirfd == -1)
260         dirfd = AT_FDCWD;
261
262     if (unlinkat(dirfd, name, 0) < 0) {
263         switch (errno) {
264         case ENOENT :
265             break;
266         case EROFS:
267             return AFPERR_VLOCK;
268         case EPERM:
269         case EACCES :
270             return AFPERR_ACCESS;
271         default :
272             return AFPERR_PARAM;
273         }
274     }
275     return AFP_OK;
276 #else
277     return netatalk_unlink(name);
278 #endif
279
280     /* DEADC0DE */
281     return 0;
282 }
283
284 /*
285  * @brief This is equivalent of unix rename()
286  *
287  * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
288  * are ignored.
289  *
290  * @param sfd        (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
291  * @param oldpath    (r) guess what
292  * @param dfd        (r) same as sfd
293  * @param newpath    (r) guess what
294  */
295 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
296 {
297 #ifdef HAVE_ATFUNCS
298     if (sfd == -1)
299         sfd = AT_FDCWD;
300     if (dfd == -1)
301         dfd = AT_FDCWD;
302
303     if (renameat(sfd, oldpath, dfd, newpath) < 0)
304         return -1;        
305 #else
306     if (rename(oldpath, newpath) < 0)
307         return -1;
308 #endif  /* HAVE_ATFUNCS */
309
310     return 0;
311 }
312
313 /* 
314  * @brief stat/fsstatat multiplexer
315  *
316  * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
317  *
318  * @param dirfd   (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
319  * @param path    (r) pathname
320  * @param st      (rw) pointer to struct stat
321  */
322 int statat(int dirfd, const char *path, struct stat *st)
323 {
324 #ifdef HAVE_ATFUNCS
325     if (dirfd == -1)
326         dirfd = AT_FDCWD;
327     return (fstatat(dirfd, path, st, 0));
328 #else
329     return (stat(path, st));
330 #endif            
331
332     /* DEADC0DE */
333     return -1;
334 }
335
336 /* 
337  * @brief opendir wrapper for *at semantics support
338  *
339  * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
340  *
341  * @param dirfd   (r) if != -1, chdir(dirfd) before opendir(path)
342  * @param path    (r) pathname
343  */
344 DIR *opendirat(int dirfd, const char *path)
345 {
346     DIR *ret;
347     int cwd = -1;
348
349     if (dirfd != -1) {
350         if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
351             ret = NULL;
352             goto exit;
353         }
354     }
355
356     ret = opendir(path);
357
358     if (dirfd != -1 && fchdir(cwd) != 0) {
359         LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");
360         exit(EXITERR_SYS);
361     }
362
363 exit:
364     if (cwd != -1)
365         close(cwd);
366
367     return ret;
368 }