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 */
54 #ifdef HAVE_FREEBSD_SUNACL
58 #ifdef HAVE_POSIX_ACLS
59 #include <sys/types.h>
61 #endif /* HAVE_POSIX_ACLS */
63 #include <atalk/util.h>
64 #include <atalk/cnid.h>
65 #include <atalk/bstrlib.h>
66 #include <atalk/bstradd.h>
67 #include <atalk/logger.h>
68 #include <atalk/errchk.h>
69 #include <atalk/unicode.h>
70 #include <atalk/globals.h>
71 #include <atalk/netatalk_conf.h>
76 int log_verbose; /* Logging flag */
78 void _log(enum logtype lt, char *fmt, ...)
81 static char logbuffer[1024];
84 if ( (lt == STD) || (log_verbose == 1)) {
86 len = vsnprintf(logbuffer, 1023, fmt, args);
90 printf("%s\n", logbuffer);
95 * Load volinfo and initialize struct vol
97 * Only opens "dbd" volumes !
99 * @param path (r) path to evaluate
100 * @param vol (rw) structure to initialize
102 * @returns 0 on success, exits on error
104 int openvol(AFPObj *obj, const char *path, afpvol_t *vol)
108 memset(vol, 0, sizeof(afpvol_t));
110 if ((vol->vol = getvolbypath(obj, path)) == NULL)
113 if (STRCMP(vol->vol->v_cnidscheme, != , "dbd"))
114 ERROR("\"%s\" isn't a \"dbd\" CNID volume!", vol->vol->v_path);
116 /* Sanity checks to ensure we can touch this volume */
117 if (vol->vol->v_adouble != AD_VERSION2
118 && vol->vol->v_adouble != AD_VERSION_EA)
119 ERROR("Unsupported adouble versions: %u", vol->vol->v_adouble);
121 if (vol->vol->v_vfs_ea != AFPVOL_EA_SYS)
122 ERROR("Unsupported Extended Attributes option: %u", vol->vol->v_vfs_ea);
124 if ((vol->vol->v_flags & AFPVOL_NODEV))
125 flags |= CNID_FLAG_NODEV;
127 if ((vol->vol->v_cdb = cnid_open(vol->vol,
130 ERROR("Cant initialize CNID database connection for %s", vol->vol->v_path);
132 cnid_getstamp(vol->vol->v_cdb,
134 sizeof(vol->db_stamp));
139 void closevol(afpvol_t *vol)
142 if (vol->vol->v_cdb) {
143 cnid_close(vol->vol->v_cdb);
144 vol->vol->v_cdb = NULL;
147 memset(vol, 0, sizeof(afpvol_t));
151 Taken form afpd/desktop.c
153 char *utompath(const struct vol *vol, const char *upath)
155 static char mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
158 uint16_t flags = CONV_IGNORE | CONV_UNESCAPEHEX;
166 outlen = strlen(upath);
168 if ((vol->v_casefold & AFPVOL_UTOMUPPER))
169 flags |= CONV_TOUPPER;
170 else if ((vol->v_casefold & AFPVOL_UTOMLOWER))
171 flags |= CONV_TOLOWER;
173 if ((vol->v_flags & AFPVOL_EILSEQ)) {
174 flags |= CONV__EILSEQ;
177 /* convert charsets */
178 if ((size_t)-1 == ( outlen = convert_charset(vol->v_volcharset,
181 u, outlen, mpath, MAXPATHLEN, &flags)) ) {
182 SLOG("Conversion from %s to %s for %s failed.",
183 vol->v_volcodepage, vol->v_maccodepage, u);
192 * Convert dot encoding of basename _in place_
194 * path arg can be "[/][dir/ | ...]filename". It will be converted in place
195 * possible encoding ".file" as ":2efile" which means the result will be
196 * longer then the original which means provide a big enough buffer.
198 * @param svol (r) source volume
199 * @param dvol (r) destinatio volume
200 * @param path (rw) path to convert _in place_
201 * @param buflen (r) size of path buffer (max strlen == buflen -1)
203 * @returns 0 on sucess, -1 on error
205 int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen)
207 static charset_t from = (charset_t) -1;
208 static char buf[MAXPATHLEN+2];
209 char *bname = stripped_slashes_basename(path);
210 int pos = bname - path;
213 if ( ! svol->vol->v_path) {
214 /* no source volume: escape special chars (eg ':') */
215 from = dvol->vol->v_volcharset; /* src = dst charset */
216 if (dvol->vol->v_adouble == AD_VERSION2)
217 flags |= CONV_ESCAPEHEX;
219 from = svol->vol->v_volcharset;
222 int len = convert_charset(from,
223 dvol->vol->v_volcharset,
224 dvol->vol->v_maccharset,
225 bname, strlen(bname),
231 if (strlcpy(bname, buf, MAXPATHLEN - pos) > MAXPATHLEN - pos)
237 * Resolves CNID of a given paths parent directory
241 * "dir/subdir" with cwd: "/afp_volume/topdir"
243 * "/afp_volume/dir/subdir"
245 * path MUST be pointing inside vol, this is usually the case as vol has been build from
246 * path using loadvolinfo and friends.
248 * @param vol (r) pointer to afpvol_t
249 * @param path (r) path, see above
250 * @param did (rw) parent CNID of returned CNID
252 * @returns CNID of path
254 cnid_t cnid_for_paths_parent(const afpvol_t *vol,
261 bstring rpath = NULL;
262 bstring statpath = NULL;
263 struct bstrList *l = NULL;
269 EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path));
270 EC_NULL(statpath = bfromcstr(vol->vol->v_path));
272 l = bsplit(rpath, '/');
274 /* only one path element, means parent dir cnid is volume root = 2 */
276 for (int i = 0; i < (l->qty - 1); i++) {
278 EC_ZERO(bconcat(statpath, l->entry[i]));
279 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
280 "lstat(rpath: %s, elem: %s): %s: %s",
281 cfrombstr(rpath), cfrombstr(l->entry[i]),
282 cfrombstr(statpath), strerror(errno));
284 if ((cnid = cnid_add(vol->vol->v_cdb,
287 cfrombstr(l->entry[i]),
288 blength(l->entry[i]),
289 0)) == CNID_INVALID) {
292 EC_ZERO(bcatcstr(statpath, "/"));