]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
ad mv nearly done, only updating adouble file missing
[netatalk.git] / bin / ad / ad_util.c
1 /* 
2  * Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
3  * Copyright (c) 1991, 1993, 1994
4  * The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.   
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif /* HAVE_CONFIG_H */
34
35 #include <sys/types.h>
36 #include <sys/acl.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48 #include <stdarg.h>
49 #include <string.h>
50
51 #include <atalk/util.h>
52 #include <atalk/cnid.h>
53 #include <atalk/volinfo.h>
54 #include <atalk/bstrlib.h>
55 #include <atalk/bstradd.h>
56 #include <atalk/logger.h>
57 #include <atalk/errchk.h>
58 #include <atalk/unicode.h>
59
60 #include "ad.h"
61
62 int log_verbose;             /* Logging flag */
63
64 void _log(enum logtype lt, char *fmt, ...)
65 {
66     int len;
67     static char logbuffer[1024];
68     va_list args;
69
70     if ( (lt == STD) || (log_verbose == 1)) {
71         va_start(args, fmt);
72         len = vsnprintf(logbuffer, 1023, fmt, args);
73         va_end(args);
74         logbuffer[1023] = 0;
75
76         printf("%s\n", logbuffer);
77     }
78 }
79
80 /*!
81  * Load volinfo and initialize struct vol
82  *
83  * Only opens "dbd" volumes ! 
84  *
85  * @param path   (r)  path to evaluate
86  * @param vol    (rw) structure to initialize 
87  *
88  * @returns 0 on success, exits on error
89  */
90 int openvol(const char *path, afpvol_t *vol)
91 {
92     int flags = 0;
93
94     memset(vol, 0, sizeof(afpvol_t));
95
96     /* try to find a .AppleDesktop/.volinfo */
97     if (loadvolinfo((char *)path, &vol->volinfo) == 0) {
98
99         if (STRCMP(vol->volinfo.v_cnidscheme, != , "dbd"))
100             ERROR("\"%s\" isn't a \"dbd\" CNID volume!", vol->volinfo.v_path);
101
102         if (vol_load_charsets(&vol->volinfo) == -1)
103             ERROR("Error loading charsets!");
104
105         /* Sanity checks to ensure we can touch this volume */
106         if (vol->volinfo.v_adouble != AD_VERSION2)
107             ERROR("Unsupported adouble versions: %u", vol->volinfo.v_adouble);
108
109         if (vol->volinfo.v_vfs_ea != AFPVOL_EA_SYS)
110             ERROR("Unsupported Extended Attributes option: %u", vol->volinfo.v_vfs_ea);
111
112         /* initialize sufficient struct vol for VFS initialisation */
113         vol->volume.v_adouble = AD_VERSION2;
114         vol->volume.v_vfs_ea = AFPVOL_EA_SYS;
115         initvol_vfs(&vol->volume);
116
117         if ((vol->volinfo.v_flags & AFPVOL_NODEV))
118             flags |= CNID_FLAG_NODEV;
119
120         if ((vol->volume.v_cdb = cnid_open(vol->volinfo.v_dbpath,
121                                            0000,
122                                            "dbd",
123                                            flags,
124                                            vol->volinfo.v_dbd_host,
125                                            vol->volinfo.v_dbd_port)) == NULL)
126             ERROR("Cant initialize CNID database connection for %s", vol->volinfo.v_path);
127
128         cnid_getstamp(vol->volume.v_cdb,
129                       vol->db_stamp,
130                       sizeof(vol->db_stamp));
131     }
132
133     return 0;
134 }
135
136 void closevol(afpvol_t *vol)
137 {
138     if (vol->volume.v_cdb)
139         cnid_close(vol->volume.v_cdb);
140
141     memset(vol, 0, sizeof(afpvol_t));
142 }
143
144 /*
145   Taken form afpd/desktop.c
146 */
147 char *utompath(const struct volinfo *volinfo, const char *upath)
148 {
149     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
150     char         *m;
151     const char   *u;
152     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
153     size_t       outlen;
154
155     if (!upath)
156         return NULL;
157
158     m = mpath;
159     u = upath;
160     outlen = strlen(upath);
161
162     if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
163         flags |= CONV_TOUPPER;
164     else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
165         flags |= CONV_TOLOWER;
166
167     if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
168         flags |= CONV__EILSEQ;
169     }
170
171     /* convert charsets */
172     if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
173                                                  CH_UTF8_MAC,
174                                                  volinfo->v_maccharset,
175                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
176         SLOG("Conversion from %s to %s for %s failed.",
177             volinfo->v_volcodepage, volinfo->v_maccodepage, u);
178         return NULL;
179     }
180
181     return(m);
182 }
183
184 /*!
185  * Build path relativ to volume root
186  *
187  * path might be:
188  * (a) relative:
189  *     "dir/subdir" with cwd: "/afp_volume/topdir"
190  * (b) absolute:
191  *     "/afp_volume/dir/subdir"
192  *
193  * @param path     (r) path relative to cwd() or absolute
194  * @param volpath  (r) volume path that path is a subdir of (has been computed in volinfo funcs) 
195  *
196  * @returns relative path in new bstring, caller must bdestroy it
197  */
198 static bstring rel_path_in_vol(const char *path, const char *volpath)
199 {
200     EC_INIT;
201
202     if (path == NULL || volpath == NULL)
203         return NULL;
204
205     bstring fpath = NULL;
206
207     /* Make path absolute by concetanating for case (a) */
208     if (path[0] != '/') {
209         EC_NULL(fpath = bfromcstr(getcwdpath()));
210         if (bchar(fpath, blength(fpath) - 1) != '/')
211             EC_ZERO(bcatcstr(fpath, "/"));
212         EC_ZERO(bcatcstr(fpath, path));
213         BSTRING_STRIP_SLASH(fpath);
214     } else {
215         EC_NULL(fpath = bfromcstr(path));
216         BSTRING_STRIP_SLASH(fpath);
217     }
218
219     /*
220      * Now we have eg:
221      *   fpath:   /Volume/netatalk/dir/bla
222      *   volpath: /Volume/netatalk/
223      * we want: "dir/bla"
224      */
225     EC_ZERO(bdelete(fpath, 0, strlen(volpath)));
226     return fpath;
227
228 EC_CLEANUP:
229     bdestroy(fpath);
230     return NULL;
231 }
232
233 /*!
234  * Convert dot encoding of basename _in place_
235  *
236  * path arg can be "[/][dir/ | ...]filename". It will be converted in place
237  * possible encoding ".file" as ":2efile" which means the result will be
238  * longer then the original which means provide a big enough buffer.
239  *
240  * @param svol   (r)  source volume
241  * @param dvol   (r)  destinatio volume
242  * @param path   (rw) path to convert _in place_
243  * @param buflen (r)  size of path buffer (max strlen == buflen -1)
244  *
245  * @returns 0 on sucess, -1 on error
246  */
247 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
248 {
249     static charset_t from = (charset_t) -1;
250     static char buf[MAXPATHLEN+2];
251     char *bname = stripped_slashes_basename(path);
252     int pos = bname - path;
253     uint16_t flags = 0;
254
255     if ( ! svol->volinfo.v_path) {
256         /* no source volume: escape special chars (eg ':') */
257         from = dvol->volinfo.v_volcharset; /* src = dst charset */
258         flags |= CONV_ESCAPEHEX;
259     } else {
260         from = svol->volinfo.v_volcharset;
261     }
262
263     if ( (svol->volinfo.v_path)
264          && ! (svol->volinfo.v_flags & AFPVOL_USEDOTS)
265          && (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
266         /* source is without dots, destination is with */
267         flags |= CONV_UNESCAPEHEX;
268     } else if (! (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
269         flags |= CONV_ESCAPEDOTS;
270     }
271
272     int len = convert_charset(from,
273                               dvol->volinfo.v_volcharset,
274                               dvol->volinfo.v_maccharset,
275                               bname, strlen(bname),
276                               buf, MAXPATHLEN,
277                               &flags);
278     if (len == -1)
279         return -1;
280
281     if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
282         return -1;
283     return 0;
284 }
285
286 /*!
287  * ResolvesCNID of a given paths
288  *
289  * path might be:
290  * (a) relative:
291  *     "dir/subdir" with cwd: "/afp_volume/topdir"
292  * (b) absolute:
293  *     "/afp_volume/dir/subdir"
294  *
295  * 1) start recursive CNID search with
296  *    a) DID:2 / "topdir"
297  *    b) DID:2 / "dir"
298  * 2) ...until we have the CNID for
299  *    a) "/afp_volume/topdir/dir"
300  *    b) "/afp_volume/dir" (no recursion required)
301  *
302  * @param vol  (r) pointer to afpvol_t
303  * @param path (r) path, see above
304  * @param did  (rw) parent CNID of returned CNID
305  *
306  * @returns CNID of path
307  */
308 cnid_t cnid_for_path(const afpvol_t *vol,
309                      const char *path,
310                      cnid_t *did)
311 {
312     EC_INIT;
313
314     cnid_t cnid;
315     bstring rpath = NULL;
316     bstring statpath = NULL;
317     struct bstrList *l = NULL;
318     struct stat st;
319
320     cnid = *did = htonl(2);
321
322     EC_NULL(rpath = rel_path_in_vol(path, vol->volinfo.v_path));
323     EC_NULL(statpath = bfromcstr(vol->volinfo.v_path));
324
325     l = bsplit(rpath, '/');
326     for(int i = 0; i < l->qty ; i++) {
327         *did = cnid;
328         EC_ZERO(bconcat(statpath, l->entry[i]));
329         EC_ZERO_LOG(stat(cfrombstr(statpath), &st));
330
331         cnid = cnid_add(vol->volume.v_cdb,
332                         &st,
333                         *did,
334                         cfrombstr(l->entry[i]),
335                         blength(l->entry[i]),
336                         0);
337
338         EC_ZERO(bcatcstr(statpath, "/"));
339         
340     }
341
342 EC_CLEANUP:
343     bdestroy(rpath);
344     bstrListDestroy(l);
345     bdestroy(statpath);
346     if (ret != 0)
347         return CNID_INVALID;
348
349     return cnid;
350 }
351
352