]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/unix.c
adouble:ea VFS stuff
[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    a dropbox is a folder where w is set but not r eg:
31    rwx-wx-wx or rwx-wx--
32    rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
33 */
34 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
35 {
36     int retval = 0;
37
38     /*
39      *  Ignore EPERM errors:  We may be dealing with a directory that is
40      *  group writable, in which case chmod will fail.
41      */
42     if ( (chmod_acl( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
43          !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
44     {
45         LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
46         retval = -1;
47     }
48
49     return retval;
50 }
51
52 /* ------------------------- */
53 int dir_rx_set(mode_t mode)
54 {
55     return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
56 }
57
58 /* --------------------- */
59 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
60 {
61     struct stat sb;
62     mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
63
64     if (!st) {
65         if (lstat(name, &sb) != 0)
66             return -1;
67         st = &sb;
68     }
69
70     if (S_ISLNK(st->st_mode))
71         return 0; /* we don't want to change link permissions */
72     
73     mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
74
75     if ( chmod_acl( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
76         return -1;
77     }
78     return 0;
79 }
80
81 /*
82  * @brief system rmdir with afp error code.
83  *
84  * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
85  */
86 int netatalk_rmdir_all_errors(int dirfd, const char *name)
87 {
88     int err;
89
90 #ifdef HAVE_ATFUNCS
91     if (dirfd == -1)
92         dirfd = AT_FDCWD;
93     err = unlinkat(dirfd, name, AT_REMOVEDIR);
94 #else
95     err = rmdir(name);
96 #endif
97
98     if (err < 0) {
99         switch ( errno ) {
100         case ENOENT :
101             return AFPERR_NOOBJ;
102         case ENOTEMPTY :
103             return AFPERR_DIRNEMPT;
104         case EPERM:
105         case EACCES :
106             return AFPERR_ACCESS;
107         case EROFS:
108             return AFPERR_VLOCK;
109         default :
110             return AFPERR_PARAM;
111         }
112     }
113     return AFP_OK;
114 }
115
116 /*
117  * @brief System rmdir with afp error code, but ENOENT is not an error.
118  *
119  * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
120  */
121 int netatalk_rmdir(int dirfd, const char *name)
122 {
123     int ret = netatalk_rmdir_all_errors(dirfd, name);
124     if (ret == AFPERR_NOOBJ)
125         return AFP_OK;
126     return ret;
127 }
128
129 /* -------------------
130    system unlink with afp error code.
131    ENOENT is not an error.
132 */
133 int netatalk_unlink(const char *name)
134 {
135     if (unlink(name) < 0) {
136         switch (errno) {
137         case ENOENT :
138             break;
139         case EROFS:
140             return AFPERR_VLOCK;
141         case EPERM:
142         case EACCES :
143             return AFPERR_ACCESS;
144         default :
145             return AFPERR_PARAM;
146         }
147     }
148     return AFP_OK;
149 }
150
151 /**************************************************************************
152  * *at semnatics support functions (like openat, renameat standard funcs)
153  **************************************************************************/
154
155 /* Copy all file data from one file fd to another */
156 int copy_file_fd(int sfd, int dfd)
157 {
158     EC_INIT;
159     ssize_t cc;
160     size_t  buflen;
161     char   filebuf[NETATALK_DIOSZ_STACK];
162
163     while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
164         if (cc < 0) {
165             if (errno == EINTR)
166                 continue;
167             LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
168             EC_FAIL;
169         }
170
171         buflen = cc;
172         while (buflen > 0) {
173             if ((cc = write(dfd, filebuf, buflen)) < 0) {
174                 if (errno == EINTR)
175                     continue;
176                 LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
177                 EC_FAIL;
178             }
179             buflen -= cc;
180         }
181     }
182
183 EC_CLEANUP:
184     EC_EXIT;
185 }
186
187 /* 
188  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
189  */
190 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
191 {
192     int    ret = 0;
193     int    sfd = -1;
194     int    dfd = -1;
195     ssize_t cc;
196     size_t  buflen;
197     char   filebuf[NETATALK_DIOSZ_STACK];
198
199 #ifdef HAVE_ATFUNCS
200     if (dirfd == -1)
201         dirfd = AT_FDCWD;
202     sfd = openat(dirfd, src, O_RDONLY);
203 #else
204     sfd = open(src, O_RDONLY);
205 #endif
206     if (sfd < 0) {
207         LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
208             src, dst, src, strerror(errno));
209         return -1;
210     }
211
212     if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
213         LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
214             src, dst, dst, strerror(errno));
215         ret = -1;
216         goto exit;
217     }
218
219     ret = copy_file_fd(sfd, dfd);
220
221 exit:
222     if (sfd != -1)
223         close(sfd);
224
225     if (dfd != -1) {
226         int err;
227
228         err = close(dfd);
229         if (!ret && err) {
230             /* don't bother to report an error if there's already one */
231             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
232                 src, dst, dst, strerror(errno));
233             ret = -1;
234         }
235     }
236
237     return ret;
238 }
239
240 /*!
241  * Copy an EA from one file to another
242  *
243  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
244  */
245 int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
246 {
247     EC_INIT;
248     int    sfd = -1;
249     int    dfd = -1;
250     size_t easize;
251     char   *eabuf = NULL;
252
253 #ifdef HAVE_ATFUNCS
254     if (dirfd == -1)
255         dirfd = AT_FDCWD;
256     EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
257 #else
258     EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
259 #endif
260     EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
261
262     if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
263         EC_NULL_LOG( eabuf = malloc(easize));
264         EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
265         EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
266     }
267
268 EC_CLEANUP:
269     if (sfd != -1)
270         close(sfd);
271     if (dfd != -1)
272         close(dfd);
273     free(eabuf);
274     EC_EXIT;
275 }
276
277 /* 
278  * at wrapper for netatalk_unlink
279  */
280 int netatalk_unlinkat(int dirfd, const char *name)
281 {
282 #ifdef HAVE_ATFUNCS
283     if (dirfd == -1)
284         dirfd = AT_FDCWD;
285
286     if (unlinkat(dirfd, name, 0) < 0) {
287         switch (errno) {
288         case ENOENT :
289             break;
290         case EROFS:
291             return AFPERR_VLOCK;
292         case EPERM:
293         case EACCES :
294             return AFPERR_ACCESS;
295         default :
296             return AFPERR_PARAM;
297         }
298     }
299     return AFP_OK;
300 #else
301     return netatalk_unlink(name);
302 #endif
303
304     /* DEADC0DE */
305     return 0;
306 }
307
308 /*
309  * @brief This is equivalent of unix rename()
310  *
311  * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
312  * are ignored.
313  *
314  * @param sfd        (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
315  * @param oldpath    (r) guess what
316  * @param dfd        (r) same as sfd
317  * @param newpath    (r) guess what
318  */
319 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
320 {
321 #ifdef HAVE_ATFUNCS
322     if (sfd == -1)
323         sfd = AT_FDCWD;
324     if (dfd == -1)
325         dfd = AT_FDCWD;
326
327     if (renameat(sfd, oldpath, dfd, newpath) < 0)
328         return -1;        
329 #else
330     if (rename(oldpath, newpath) < 0)
331         return -1;
332 #endif  /* HAVE_ATFUNCS */
333
334     return 0;
335 }
336
337 /* 
338  * @brief stat/fsstatat multiplexer
339  *
340  * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
341  *
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
345  */
346 int statat(int dirfd, const char *path, struct stat *st)
347 {
348 #ifdef HAVE_ATFUNCS
349     if (dirfd == -1)
350         dirfd = AT_FDCWD;
351     return (fstatat(dirfd, path, st, 0));
352 #else
353     return (stat(path, st));
354 #endif            
355
356     /* DEADC0DE */
357     return -1;
358 }
359
360 /* 
361  * @brief lstat/fsstatat multiplexer
362  *
363  * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
364  *
365  * @param dirfd   (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
366  * @param path    (r) pathname
367  * @param st      (rw) pointer to struct stat
368  */
369 int lstatat(int dirfd, const char *path, struct stat *st)
370 {
371 #ifdef HAVE_ATFUNCS
372     if (dirfd == -1)
373         dirfd = AT_FDCWD;
374     return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
375 #else
376     return (lstat(path, st));
377 #endif            
378
379     /* DEADC0DE */
380     return -1;
381 }
382
383 /* 
384  * @brief opendir wrapper for *at semantics support
385  *
386  * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
387  *
388  * @param dirfd   (r) if != -1, chdir(dirfd) before opendir(path)
389  * @param path    (r) pathname
390  */
391 DIR *opendirat(int dirfd, const char *path)
392 {
393     DIR *ret;
394     int cwd = -1;
395
396     if (dirfd != -1) {
397         if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
398             ret = NULL;
399             goto exit;
400         }
401     }
402
403     ret = opendir(path);
404
405     if (dirfd != -1 && fchdir(cwd) != 0) {
406         LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");
407         exit(EXITERR_SYS);
408     }
409
410 exit:
411     if (cwd != -1)
412         close(cwd);
413
414     return ret;
415 }