2 * Copyright (c) 2012 Frank Lahm
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.
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.
17 * Part of Netatalk's AppleDouble implementatation
18 * @sa include/atalk/adouble.h
23 #endif /* HAVE_CONFIG_H */
26 #include <sys/param.h>
30 #include <arpa/inet.h>
32 #include <atalk/logger.h>
33 #include <atalk/adouble.h>
34 #include <atalk/util.h>
35 #include <atalk/unix.h>
37 #include <atalk/bstrlib.h>
38 #include <atalk/bstradd.h>
39 #include <atalk/compat.h>
40 #include <atalk/errchk.h>
41 #include <atalk/volume.h>
45 static char emptyfilad[32] = {0,0,0,0,0,0,0,0,
50 static char emptydirad[32] = {0,0,0,0,0,0,0,0,
55 static int ad_conv_v22ea_hf(const char *path, const struct stat *sp, const struct vol *vol)
62 uint32_t ctime, mtime, afpinfo = 0;
65 LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): BEGIN", fullpathname(path));
68 ad_init_old(&adv2, AD_VERSION2, adea.ad_options);
69 adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;
71 /* Open and lock adouble:v2 file */
72 EC_ZERO( ad_open(&adv2, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR) );
74 EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) );
75 EC_NEG1_LOG( adv2.ad_ops->ad_header_read(path, &adv2, sp) );
77 /* Check if it's a non-empty header */
78 if (S_ISREG(sp->st_mode))
79 emptyad = &emptyfilad[0];
81 emptyad = &emptydirad[0];
83 if (ad_getentrylen(&adv2, ADEID_COMMENT) != 0)
85 if (ad_getentryoff(&adv2, ADEID_FINDERI)
86 && (ad_getentrylen(&adv2, ADEID_FINDERI) == ADEDLEN_FINDERI)
87 && (memcmp(ad_entry(&adv2, ADEID_FINDERI), emptyad, ADEDLEN_FINDERI) != 0))
89 if (ad_getentryoff(&adv2, ADEID_FILEDATESI)) {
90 EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_CREATE | AD_DATE_UNIX, &ctime) );
91 EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_MODIFY | AD_DATE_UNIX, &mtime) );
92 if ((ctime != mtime) || (mtime != sp->st_mtime))
95 if (ad_getentryoff(&adv2, ADEID_AFPFILEI)) {
96 if (memcmp(ad_entry(&adv2, ADEID_AFPFILEI), &afpinfo, ADEDLEN_AFPFILEI) != 0)
100 LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): default adouble", fullpathname(path), ret);
104 /* Create a adouble:ea meta EA */
105 LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): copying adouble", fullpathname(path), ret);
106 EC_ZERO_LOG( ad_open(&adea, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE) );
107 EC_ZERO_LOG( ad_copy_header(&adea, &adv2) );
111 EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_SETSHRMD) );
112 EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_SETSHRMD) );
113 LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): END: %d", fullpathname(path), ret);
117 static int ad_conv_v22ea_rf(const char *path, const struct stat *sp, const struct vol *vol)
123 LOG(log_debug, logtype_default,"ad_conv_v22ea_rf(\"%s\"): BEGIN", fullpathname(path));
125 if (S_ISDIR(sp->st_mode))
129 ad_init_old(&adv2, AD_VERSION2, adea.ad_options);
131 /* Open and lock adouble:v2 file */
132 EC_ZERO( ad_open(&adv2, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR) );
134 if (adv2.ad_rlen > 0) {
135 EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) );
137 /* Create a adouble:ea resource fork */
138 EC_ZERO_LOG( ad_open(&adea, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) );
140 EC_ZERO_LOG( copy_fork(ADEID_RFORK, &adea, &adv2) );
141 adea.ad_rlen = adv2.ad_rlen;
146 EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_RF) );
147 EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_RF) );
148 LOG(log_debug, logtype_default,"ad_conv_v22ea_rf(\"%s\"): END: %d", fullpathname(path), ret);
152 static int ad_conv_v22ea(const char *path, const struct stat *sp, const struct vol *vol)
156 int adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;
158 EC_ZERO( ad_conv_v22ea_hf(path, sp, vol) );
159 EC_ZERO( ad_conv_v22ea_rf(path, sp, vol) );
161 EC_NULL( adpath = ad_path(path, adflags) );
162 LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): deleting adouble:v2 file: \"%s\"",
163 path, fullpathname(adpath));
166 EC_ZERO_LOG( unlink(adpath) );
176 * Remove hexencoded dots and slashes (":2e" and ":2f")
178 static int ad_conv_dehex(const char *path, const struct stat *sp, const struct vol *vol, const char **newpathp)
181 static char buf[MAXPATHLEN];
182 const char *adpath, *p;
183 int adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;
184 bstring newpath = NULL;
186 LOG(log_debug, logtype_default,"ad_conv_dehex(\"%s\"): BEGIN", fullpathname(path));
190 if ((p = strchr(path, ':')) == NULL)
193 EC_NULL( newpath = bfromcstr(path) );
195 EC_ZERO( bfindreplace(newpath, bfromcstr(":2e"), bfromcstr("."), 0) );
196 EC_ZERO( bfindreplace(newpath, bfromcstr(":2f"), bfromcstr(":"), 0) );
199 if (adflags != ADFLAGS_DIR)
200 rename(vol->ad_path(path, 0), vol->ad_path(bdata(newpath), 0));
201 rename(path, bdata(newpath));
204 strlcpy(buf, bdata(newpath), sizeof(buf));
214 * AppleDouble and encoding conversion on the fly
216 * @param path (r) path to file or directory
217 * @param sp (r) stat(path)
218 * @param vol (r) volume handle
219 * @param newpath (w) if encoding changed, new name. Can be NULL.
221 * @returns -1 on internal error, otherwise 0. newpath is NULL if no character conversion was done,
222 * otherwise newpath points to a static string with the converted name
224 int ad_convert(const char *path, const struct stat *sp, const struct vol *vol, const char **newpath)
229 LOG(log_debug, logtype_default,"ad_convert(\"%s\"): BEGIN", fullpathname(path));
234 if ((vol->v_adouble == AD_VERSION_EA) && !(vol->v_flags & AFPVOL_NOV2TOEACONV))
235 EC_ZERO( ad_conv_v22ea(path, sp, vol) );
237 if (vol->v_adouble == AD_VERSION_EA) {
238 EC_ZERO( ad_conv_dehex(path, sp, vol, &p) );
244 LOG(log_debug, logtype_default,"ad_convert(\"%s\"): END: %d", fullpathname(path), ret);