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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
33 #endif /* HAVE_CONFIG_H */
35 #include <sys/types.h>
36 #include <sys/param.h>
51 #ifdef HAVE_SOLARIS_ACLS
53 #endif /* HAVE_SOLARIS_ACLS */
55 #ifdef HAVE_POSIX_ACLS
56 #include <sys/types.h>
58 #endif /* HAVE_POSIX_ACLS */
60 #include <atalk/util.h>
61 #include <atalk/cnid.h>
62 #include <atalk/volinfo.h>
63 #include <atalk/bstrlib.h>
64 #include <atalk/bstradd.h>
65 #include <atalk/logger.h>
66 #include <atalk/errchk.h>
67 #include <atalk/unicode.h>
71 int log_verbose; /* Logging flag */
73 void _log(enum logtype lt, char *fmt, ...)
76 static char logbuffer[1024];
79 if ( (lt == STD) || (log_verbose == 1)) {
81 len = vsnprintf(logbuffer, 1023, fmt, args);
85 printf("%s\n", logbuffer);
90 * Load volinfo and initialize struct vol
92 * Only opens "dbd" volumes !
94 * @param path (r) path to evaluate
95 * @param vol (rw) structure to initialize
97 * @returns 0 on success, exits on error
99 int openvol(const char *path, afpvol_t *vol)
103 memset(vol, 0, sizeof(afpvol_t));
105 /* try to find a .AppleDesktop/.volinfo */
106 if (loadvolinfo((char *)path, &vol->volinfo) != 0)
109 if (STRCMP(vol->volinfo.v_cnidscheme, != , "dbd"))
110 ERROR("\"%s\" isn't a \"dbd\" CNID volume!", vol->volinfo.v_path);
112 if (vol_load_charsets(&vol->volinfo) == -1)
113 ERROR("Error loading charsets!");
115 /* Sanity checks to ensure we can touch this volume */
116 if (vol->volinfo.v_adouble != AD_VERSION2)
117 ERROR("Unsupported adouble versions: %u", vol->volinfo.v_adouble);
119 if (vol->volinfo.v_vfs_ea != AFPVOL_EA_SYS)
120 ERROR("Unsupported Extended Attributes option: %u", vol->volinfo.v_vfs_ea);
122 /* initialize sufficient struct vol for VFS initialisation */
123 vol->volume.v_adouble = AD_VERSION2;
124 vol->volume.v_vfs_ea = AFPVOL_EA_SYS;
125 initvol_vfs(&vol->volume);
127 if ((vol->volinfo.v_flags & AFPVOL_NODEV))
128 flags |= CNID_FLAG_NODEV;
130 if ((vol->volume.v_cdb = cnid_open(vol->volinfo.v_path,
134 vol->volinfo.v_dbd_host,
135 vol->volinfo.v_dbd_port)) == NULL)
136 ERROR("Cant initialize CNID database connection for %s", vol->volinfo.v_path);
138 cnid_getstamp(vol->volume.v_cdb,
140 sizeof(vol->db_stamp));
145 void closevol(afpvol_t *vol)
147 if (vol->volume.v_cdb)
148 cnid_close(vol->volume.v_cdb);
150 memset(vol, 0, sizeof(afpvol_t));
154 Taken form afpd/desktop.c
156 char *utompath(const struct volinfo *volinfo, const char *upath)
158 static char mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
161 uint16_t flags = CONV_IGNORE | CONV_UNESCAPEHEX;
169 outlen = strlen(upath);
171 if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
172 flags |= CONV_TOUPPER;
173 else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
174 flags |= CONV_TOLOWER;
176 if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
177 flags |= CONV__EILSEQ;
180 /* convert charsets */
181 if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
183 volinfo->v_maccharset,
184 u, outlen, mpath, MAXPATHLEN, &flags)) ) {
185 SLOG("Conversion from %s to %s for %s failed.",
186 volinfo->v_volcodepage, volinfo->v_maccodepage, u);
195 * Convert dot encoding of basename _in place_
197 * path arg can be "[/][dir/ | ...]filename". It will be converted in place
198 * possible encoding ".file" as ":2efile" which means the result will be
199 * longer then the original which means provide a big enough buffer.
201 * @param svol (r) source volume
202 * @param dvol (r) destinatio volume
203 * @param path (rw) path to convert _in place_
204 * @param buflen (r) size of path buffer (max strlen == buflen -1)
206 * @returns 0 on sucess, -1 on error
208 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
210 static charset_t from = (charset_t) -1;
211 static char buf[MAXPATHLEN+2];
212 char *bname = stripped_slashes_basename(path);
213 int pos = bname - path;
216 if ( ! svol->volinfo.v_path) {
217 /* no source volume: escape special chars (eg ':') */
218 from = dvol->volinfo.v_volcharset; /* src = dst charset */
219 flags |= CONV_ESCAPEHEX;
221 from = svol->volinfo.v_volcharset;
224 if ( (svol->volinfo.v_path)
225 && ! (svol->volinfo.v_flags & AFPVOL_USEDOTS)
226 && (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
227 /* source is without dots, destination is with */
228 flags |= CONV_UNESCAPEHEX;
229 } else if (! (dvol->volinfo.v_flags & AFPVOL_USEDOTS)) {
230 flags |= CONV_ESCAPEDOTS;
233 int len = convert_charset(from,
234 dvol->volinfo.v_volcharset,
235 dvol->volinfo.v_maccharset,
236 bname, strlen(bname),
242 if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
248 * ResolvesCNID of a given paths
252 * "dir/subdir" with cwd: "/afp_volume/topdir"
254 * "/afp_volume/dir/subdir"
256 * path MUST be pointing inside vol, this is usually the case as vol has been build from
257 * path using loadvolinfo and friends.
259 * @param vol (r) pointer to afpvol_t
260 * @param path (r) path, see above
261 * @param did (rw) parent CNID of returned CNID
263 * @returns CNID of path
265 cnid_t cnid_for_path(const afpvol_t *vol,
272 bstring rpath = NULL;
273 bstring statpath = NULL;
274 struct bstrList *l = NULL;
279 EC_NULL(rpath = rel_path_in_vol(path, vol->volinfo.v_path));
280 EC_NULL(statpath = bfromcstr(vol->volinfo.v_path));
281 EC_ZERO(bcatcstr(statpath, "/"));
283 l = bsplit(rpath, '/');
284 for (int i = 0; i < l->qty ; i++) {
287 EC_ZERO(bconcat(statpath, l->entry[i]));
288 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
289 "lstat(rpath: %s, elem: %s): %s: %s",
290 cfrombstr(rpath), cfrombstr(l->entry[i]),
291 cfrombstr(statpath), strerror(errno));
293 if ((cnid = cnid_add(vol->volume.v_cdb,
296 cfrombstr(l->entry[i]),
297 blength(l->entry[i]),
298 0)) == CNID_INVALID) {
301 EC_ZERO(bcatcstr(statpath, "/"));
315 * Resolves CNID of a given paths parent directory
319 * "dir/subdir" with cwd: "/afp_volume/topdir"
321 * "/afp_volume/dir/subdir"
323 * path MUST be pointing inside vol, this is usually the case as vol has been build from
324 * path using loadvolinfo and friends.
326 * @param vol (r) pointer to afpvol_t
327 * @param path (r) path, see above
328 * @param did (rw) parent CNID of returned CNID
330 * @returns CNID of path
332 cnid_t cnid_for_paths_parent(const afpvol_t *vol,
339 bstring rpath = NULL;
340 bstring statpath = NULL;
341 struct bstrList *l = NULL;
347 EC_NULL(rpath = rel_path_in_vol(path, vol->volinfo.v_path));
348 EC_NULL(statpath = bfromcstr(vol->volinfo.v_path));
350 l = bsplit(rpath, '/');
352 /* only one path element, means parent dir cnid is volume root = 2 */
354 for (int i = 0; i < (l->qty - 1); i++) {
356 EC_ZERO(bconcat(statpath, l->entry[i]));
357 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
358 "lstat(rpath: %s, elem: %s): %s: %s",
359 cfrombstr(rpath), cfrombstr(l->entry[i]),
360 cfrombstr(statpath), strerror(errno));
362 if ((cnid = cnid_add(vol->volume.v_cdb,
365 cfrombstr(l->entry[i]),
366 blength(l->entry[i]),
367 0)) == CNID_INVALID) {
370 EC_ZERO(bcatcstr(statpath, "/"));