]> arthur.barton.de Git - netatalk.git/blob - bin/ad/ad_util.c
ad cp -R now works with all sources
[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/cnid.h>
52 #include <atalk/volinfo.h>
53 #include <atalk/bstrlib.h>
54 #include <atalk/bstradd.h>
55 #include <atalk/util.h>
56 #include <atalk/logger.h>
57 #include <atalk/errchk.h>
58 #include "ad.h"
59
60 int log_verbose;             /* Logging flag */
61
62 void _log(enum logtype lt, char *fmt, ...)
63 {
64     int len;
65     static char logbuffer[1024];
66     va_list args;
67
68     if ( (lt == STD) || (log_verbose == 1)) {
69         va_start(args, fmt);
70         len = vsnprintf(logbuffer, 1023, fmt, args);
71         va_end(args);
72         logbuffer[1023] = 0;
73
74         printf("%s\n", logbuffer);
75     }
76 }
77
78 /*!
79  * Load volinfo and initialize struct vol
80  *
81  * @param path   (r)  path to evaluate
82  * @param vol    (rw) structure to initialize 
83 *
84  * @returns 0 on success, exits on error
85  */
86 int openvol(const char *path, afpvol_t *vol)
87 {
88     int flags = 0;
89
90     memset(vol, 0, sizeof(afpvol_t));
91
92     /* try to find a .AppleDesktop/.volinfo */
93     if (loadvolinfo((char *)path, &vol->volinfo) == 0) {
94
95         if (vol_load_charsets(&vol->volinfo) == -1)
96             ERROR("Error loading charsets!");
97
98         /* Sanity checks to ensure we can touch this volume */
99         if (vol->volinfo.v_adouble != AD_VERSION2)
100             ERROR("Unsupported adouble versions: %u", vol->volinfo.v_adouble);
101
102         if (vol->volinfo.v_vfs_ea != AFPVOL_EA_SYS)
103             ERROR("Unsupported Extended Attributes option: %u", vol->volinfo.v_vfs_ea);
104
105         /* initialize sufficient struct vol for VFS initialisation */
106         vol->volume.v_adouble = AD_VERSION2;
107         vol->volume.v_vfs_ea = AFPVOL_EA_SYS;
108         initvol_vfs(&vol->volume);
109
110         if ((vol->volinfo.v_flags & AFPVOL_NODEV))
111             flags |= CNID_FLAG_NODEV;
112
113         if ((vol->volume.v_cdb = cnid_open(vol->volinfo.v_dbpath,
114                                            0000,
115                                            "dbd",
116                                            flags,
117                                            vol->volinfo.v_dbd_host,
118                                            vol->volinfo.v_dbd_port)) == NULL)
119             ERROR("Cant initialize CNID database connection for %s", vol->volinfo.v_path);
120
121         cnid_getstamp(vol->volume.v_cdb,
122                       vol->db_stamp,
123                       sizeof(vol->db_stamp));
124     }
125
126     return 0;
127 }
128
129 void closevol(afpvol_t *vol)
130 {
131     if (vol->volume.v_cdb)
132         cnid_close(vol->volume.v_cdb);
133
134     memset(vol, 0, sizeof(afpvol_t));
135 }
136
137 /*
138   Taken form afpd/desktop.c
139 */
140 char *utompath(const struct volinfo *volinfo, const char *upath)
141 {
142     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
143     char         *m;
144     const char   *u;
145     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
146     size_t       outlen;
147
148     if (!upath)
149         return NULL;
150
151     m = mpath;
152     u = upath;
153     outlen = strlen(upath);
154
155     if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
156         flags |= CONV_TOUPPER;
157     else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
158         flags |= CONV_TOLOWER;
159
160     if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
161         flags |= CONV__EILSEQ;
162     }
163
164     /* convert charsets */
165     if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
166                                                  CH_UTF8_MAC,
167                                                  volinfo->v_maccharset,
168                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
169         SLOG("Conversion from %s to %s for %s failed.",
170             volinfo->v_volcodepage, volinfo->v_maccodepage, u);
171         return NULL;
172     }
173
174     return(m);
175 }
176
177 /*!
178  * Build path relativ to volume root
179  *
180  * path might be:
181  * (a) relative:
182  *     "dir/subdir" with cwd: "/afp_volume/topdir"
183  * (b) absolute:
184  *     "/afp_volume/dir/subdir"
185  *
186  * @param path     (r) path relative to cwd() or absolute
187  * @param volpath  (r) volume path that path is a subdir of (has been computed in volinfo funcs) 
188  *
189  * @returns relative path in new bstring, caller must bdestroy it
190  */
191 static bstring rel_path_in_vol(const char *path, const char *volpath)
192 {
193     EC_INIT;
194
195     if (path == NULL || volpath == NULL)
196         return NULL;
197
198     bstring fpath = NULL;
199
200     /* Make path absolute by concetanating for case (a) */
201     if (path[0] != '/') {
202         EC_NULL(fpath = bfromcstr(getcwdpath()));
203         if (bchar(fpath, blength(fpath) - 1) != '/')
204             EC_ZERO(bcatcstr(fpath, "/"));
205         EC_ZERO(bcatcstr(fpath, path));
206         BSTRING_STRIP_SLASH(fpath);
207         SLOG("Built path: %s", cfrombstr(fpath));
208     } else {
209         EC_NULL(fpath = bfromcstr(path));
210         BSTRING_STRIP_SLASH(fpath);
211         SLOG("Built path: %s", cfrombstr(fpath));
212     }
213
214     /*
215      * Now we have eg:
216      *   fpath:   /Volume/netatalk/dir/bla
217      *   volpath: /Volume/netatalk/
218      * we want: "dir/bla"
219      */
220     EC_ZERO(bdelete(fpath, 0, strlen(volpath)));
221     SLOG("rel path: %s", cfrombstr(fpath));    
222     return fpath;
223
224 EC_CLEANUP:
225     bdestroy(fpath);
226     return NULL;
227 }
228
229 /*!
230  * ResolvesCNID of a given paths
231  *
232  * path might be:
233  * (a) relative:
234  *     "dir/subdir" with cwd: "/afp_volume/topdir"
235  * (b) absolute:
236  *     "/afp_volume/dir/subdir"
237  *
238  * 1) start recursive CNID search with
239  *    a) DID:2 / "topdir"
240  *    b) DID:2 / "dir"
241  * 2) ...until we have the CNID for
242  *    a) "/afp_volume/topdir/dir"
243  *    b) "/afp_volume/dir" (no recursion required)
244  */
245 cnid_t cnid_for_path(const struct volinfo *vi,
246                      const struct vol *vol,
247                      const char *path)
248 {
249     EC_INIT;
250
251     cnid_t did;
252     cnid_t cnid;
253     bstring rpath = NULL;
254     bstring statpath = NULL;
255     struct bstrList *l = NULL;
256     struct stat st;
257
258     EC_NULL(rpath = rel_path_in_vol(path, vi->v_path));
259     SLOG("vol:%s, path: %s, rpath: %s", vi->v_path, path, bdata(rpath));
260
261     cnid = htonl(2);
262
263     EC_NULL(statpath = bfromcstr(vi->v_path));
264
265     l = bsplit(rpath, '/');
266     SLOG("elem: %s, qty: %u", cfrombstr(l->entry[0]), l->qty);
267     for(int i = 0; i < l->qty ; i++) {
268         did = cnid;
269         EC_ZERO(bconcat(statpath, l->entry[i]));
270         SLOG("statpath: %s", cfrombstr(statpath));
271         EC_ZERO_LOG(stat(cfrombstr(statpath), &st));
272         SLOG("db query: did: %u, name: %s, dev: %08x, ino: %08x",
273              ntohl(did), cfrombstr(l->entry[i]), st.st_dev, st.st_ino);
274         cnid = cnid_add(vol->v_cdb,
275                         &st,
276                         did,
277                         cfrombstr(l->entry[i]),
278                         blength(l->entry[i]),
279                         0);
280
281         EC_ZERO(bcatcstr(statpath, "/"));
282     }
283
284 EC_CLEANUP:
285     bdestroy(rpath);
286     bstrListDestroy(l);
287     bdestroy(statpath);
288     if (ret != 0)
289         return CNID_INVALID;
290
291     return cnid;
292 }
293
294