]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/unix.c
Mege maste
[netatalk.git] / libatalk / util / unix.c
1 /*
2   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 */
14
15 /*!
16  * @file
17  * Netatalk utility functions
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <sys/time.h>
34 #include <time.h>
35
36 #include <atalk/adouble.h>
37 #include <atalk/ea.h>
38 #include <atalk/afp.h>
39 #include <atalk/logger.h>
40 #include <atalk/vfs.h>
41 #include <atalk/util.h>
42 #include <atalk/unix.h>
43
44 /*!
45  * @brief get cwd in static buffer
46  *
47  * @returns pointer to path or pointer to error messages on error
48  */
49 const char *getcwdpath(void)
50 {
51     static char cwd[MAXPATHLEN + 1];
52     char *p;
53
54     if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
55         return p;
56     else
57         return strerror(errno);
58 }
59
60 /*!
61  * Make argument path absoulte
62  *
63  * @returns pointer to path or pointer to error messages on error
64  */
65 const char *abspath(const char *name)
66 {
67     static char buf[MAXPATHLEN + 1];
68     char *p;
69     int n;
70
71     if (name[0] == '/')
72         return name;
73
74     if ((p = getcwd(buf, MAXPATHLEN)) == NULL)
75         return strerror(errno);
76
77     n = strlen(buf);
78     if (buf[n-1] != '/')
79         buf[n++] = '/';
80         
81     strlcpy(buf + n, name, MAXPATHLEN - n);
82     return buf;
83 }
84
85 /*!
86  * Takes a buffer with a path, strips slashs, returns basename
87  *
88  * @param p (rw) path
89  *        path may be
90  *          "[/][dir/[...]]file"
91  *        or
92  *          "[/][dir/[...]]dir/[/]"
93  *        Result is "file" or "dir" 
94  *
95  * @returns pointer to basename in path buffer, buffer is possibly modified
96  */
97 char *stripped_slashes_basename(char *p)
98 {
99     int i = strlen(p) - 1;
100     while (i > 0 && p[i] == '/')
101         p[i--] = 0;
102     return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
103 }
104
105 /*!
106  * @brief symlink safe chdir replacement
107  *
108  * Only chdirs to dir if it doesn't contain symlinks.
109  *
110  * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
111  */
112 int lchdir(const char *dir)
113 {
114     char buf[MAXPATHLEN+1];
115     char cwd[MAXPATHLEN+1];
116     char *test;
117     int  i;
118
119     /*
120      dir is a canonical path (without "../" "./" "//" )
121      but may end with a / 
122     */
123     *cwd = 0;
124     if (*dir != '/') {
125         if (getcwd(cwd, MAXPATHLEN) == NULL)
126             return -1;
127     }
128     if (chdir(dir) != 0)
129         return -1;
130
131     /* 
132      * Cases:
133      * chdir request   | realpath result | ret
134      * (after getwcwd) |                 |
135      * =======================================
136      * /a/b/.          | /a/b            | 0
137      * /a/b/.          | /c              | 1
138      * /a/b/.          | /c/d/e/f        | 1
139      */
140     if (getcwd(buf, MAXPATHLEN) == NULL)
141         return 1;
142
143     i = 0;
144     if (*cwd) {
145         /* relative path requested, 
146          * Same directory?
147         */
148         for (; cwd[i]; i++) {
149             if (buf[i] != cwd[i])
150                 return 1;
151         }
152         if (buf[i]) {
153             if (buf[i] != '/')
154                 return 1;
155             i++;
156         }                    
157     }
158
159     test = &buf[i];    
160     for (i = 0; test[i]; i++) {
161         if (test[i] != dir[i]) {
162             return 1;
163         }
164     }
165     /* trailing '/' ? */
166     if (!dir[i])
167         return 0;
168
169     if (dir[i] != '/')
170         return 1;
171
172     i++;
173     if (dir[i])
174         return 1;
175
176     return 0;
177 }
178
179 /*!
180  * Store n random bytes an buf
181  */
182 void randombytes(void *buf, int n)
183 {
184     char *p = (char *)buf;
185     int fd, i;
186     struct timeval tv;
187
188     if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
189         /* generate from /dev/urandom */
190         if (read(fd, buf, n) != n) {
191             close(fd);
192             fd = -1;
193         } else {
194             close(fd);
195             /* fd now != -1, so srandom wont be called below */
196         }
197     }
198
199     if (fd == -1) {
200         gettimeofday(&tv, NULL);
201         srandom((unsigned int)tv.tv_usec);
202         for (i=0 ; i < n ; i++)
203             p[i] = random() & 0xFF;
204     }
205
206     return;
207 }