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