]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_conv.c
ff0eebcc497aa6c24ca5ca5320f8b1163ef7e340
[netatalk.git] / libatalk / adouble / ad_conv.c
1 /*
2  * Copyright (c) 2012 Frank Lahm
3  *
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.
8  *
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.
13  */
14
15 /*!
16  * @file
17  * Part of Netatalk's AppleDouble implementatation
18  * @sa include/atalk/adouble.h
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24
25 #include <errno.h>
26 #include <sys/param.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <arpa/inet.h>
31
32 #include <atalk/logger.h>
33 #include <atalk/adouble.h>
34 #include <atalk/util.h>
35 #include <atalk/unix.h>
36 #include <atalk/ea.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>
42
43 #include "ad_lock.h"
44
45 static char emptyfilad[32] = {0,0,0,0,0,0,0,0,
46                               0,0,0,0,0,0,0,0,
47                               0,0,0,0,0,0,0,0,
48                               0,0,0,0,0,0,0,0};
49
50 static char emptydirad[32] = {0,0,0,0,0,0,0,0,
51                               0,0,0,0,0,0,1,0,
52                               0,0,0,0,0,0,0,0,
53                               0,0,0,0,0,0,0,0};
54
55 static int ad_conv_v22ea_hf(const char *path, const struct stat *sp, const struct vol *vol)
56 {
57     EC_INIT;
58     struct adouble adv2;
59     struct adouble adea;
60     int adflags;
61     uint32_t ctime, mtime, afpinfo = 0;
62     char *emptyad;
63
64     LOG(log_debug, logtype_ad,"ad_conv_v22ea_hf(\"%s\"): BEGIN", fullpathname(path));
65
66     switch (S_IFMT & sp->st_mode) {
67     case S_IFREG:
68         break;
69     default:
70         return 0;
71     }
72
73     ad_init(&adea, vol);
74     ad_init_old(&adv2, AD_VERSION2, adea.ad_options);
75
76     adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;
77
78     /* Open and lock adouble:v2 file */
79     EC_ZERO( ad_open(&adv2, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR) );
80
81     EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) );
82     EC_NEG1_LOG( adv2.ad_ops->ad_header_read(path, &adv2, sp) );
83
84     /* Check if it's a non-empty header */
85     if (S_ISREG(sp->st_mode))
86         emptyad = &emptyfilad[0];
87     else
88         emptyad = &emptydirad[0];
89
90     if (ad_getentrylen(&adv2, ADEID_COMMENT) != 0)
91         goto copy;
92     if (ad_getentryoff(&adv2, ADEID_FINDERI)
93         && (ad_getentrylen(&adv2, ADEID_FINDERI) == ADEDLEN_FINDERI)
94         && (memcmp(ad_entry(&adv2, ADEID_FINDERI), emptyad, ADEDLEN_FINDERI) != 0))
95         goto copy;
96     if (ad_getentryoff(&adv2, ADEID_FILEDATESI)) {
97         EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_CREATE | AD_DATE_UNIX, &ctime) );
98         EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_MODIFY | AD_DATE_UNIX, &mtime) );
99         if ((ctime != mtime) || (mtime != sp->st_mtime))
100             goto copy;
101     }
102     if (ad_getentryoff(&adv2, ADEID_AFPFILEI)) {
103         if (memcmp(ad_entry(&adv2, ADEID_AFPFILEI), &afpinfo, ADEDLEN_AFPFILEI) != 0)
104             goto copy;
105     }
106
107     LOG(log_debug, logtype_ad,"ad_conv_v22ea_hf(\"%s\"): default adouble", fullpathname(path), ret);
108     goto EC_CLEANUP;
109
110 copy:
111     /* Create a adouble:ea meta EA */
112     LOG(log_debug, logtype_ad,"ad_conv_v22ea_hf(\"%s\"): copying adouble", fullpathname(path), ret);
113     EC_ZERO_LOGSTR( ad_open(&adea, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE),
114                     "ad_conv_v22ea_hf(\"%s\"): error creating metadata EA: %s",
115                     fullpathname(path), strerror(errno));
116     EC_ZERO_LOG( ad_copy_header(&adea, &adv2) );
117     ad_flush(&adea);
118
119 EC_CLEANUP:
120     EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_SETSHRMD) );
121     EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_SETSHRMD) );
122     LOG(log_debug, logtype_ad,"ad_conv_v22ea_hf(\"%s\"): END: %d", fullpathname(path), ret);
123     EC_EXIT;
124 }
125
126 static int ad_conv_v22ea_rf(const char *path, const struct stat *sp, const struct vol *vol)
127 {
128     EC_INIT;
129     struct adouble adv2;
130     struct adouble adea;
131
132     LOG(log_debug, logtype_ad,"ad_conv_v22ea_rf(\"%s\"): BEGIN", fullpathname(path));
133
134     switch (S_IFMT & sp->st_mode) {
135     case S_IFREG:
136         break;
137     default:
138         return 0;
139     }
140
141     if (S_ISDIR(sp->st_mode))
142         return 0;
143
144     ad_init(&adea, vol);
145     ad_init_old(&adv2, AD_VERSION2, adea.ad_options);
146
147     /* Open and lock adouble:v2 file */
148     EC_ZERO( ad_open(&adv2, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR) );
149
150     if (adv2.ad_rlen > 0) {
151         EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) );
152
153         /* Create a adouble:ea resource fork */
154         EC_ZERO_LOG( ad_open(&adea, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) );
155
156         EC_ZERO_LOG( copy_fork(ADEID_RFORK, &adea, &adv2) );
157         adea.ad_rlen = adv2.ad_rlen;
158         ad_flush(&adea);
159     }
160
161 EC_CLEANUP:
162     EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_RF) );
163     EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_RF) );
164     LOG(log_debug, logtype_ad,"ad_conv_v22ea_rf(\"%s\"): END: %d", fullpathname(path), ret);
165     EC_EXIT;
166 }
167
168 static int ad_conv_v22ea(const char *path, const struct stat *sp, const struct vol *vol)
169 {
170     EC_INIT;
171     const char *adpath;
172     int adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;
173
174     become_root();
175
176     EC_ZERO( ad_conv_v22ea_hf(path, sp, vol) );
177     EC_ZERO( ad_conv_v22ea_rf(path, sp, vol) );
178
179     EC_NULL( adpath = ad_path(path, adflags) );
180     LOG(log_debug, logtype_ad,"ad_conv_v22ea_hf(\"%s\"): deleting adouble:v2 file: \"%s\"",
181         path, fullpathname(adpath));
182
183     unlink(adpath);
184
185 EC_CLEANUP:
186     if (errno == ENOENT)
187         EC_STATUS(0);
188
189     unbecome_root();
190
191     EC_EXIT;
192 }
193
194 /*!
195  * Remove hexencoded dots and slashes (":2e" and ":2f")
196  */
197 static int ad_conv_dehex(const char *path, const struct stat *sp, const struct vol *vol, const char **newpathp)
198 {
199     EC_INIT;
200     static char buf[MAXPATHLEN];
201     const char *p;
202     int adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;
203     bstring newpath = NULL;
204
205     LOG(log_debug, logtype_ad,"ad_conv_dehex(\"%s\"): BEGIN", fullpathname(path));
206
207     *newpathp = NULL;
208
209     if ((p = strchr(path, ':')) == NULL)
210         goto EC_CLEANUP;
211
212     EC_NULL( newpath = bfromcstr(path) );
213
214     EC_ZERO( bfindreplace(newpath, bfromcstr(":2e"), bfromcstr("."), 0) );
215     EC_ZERO( bfindreplace(newpath, bfromcstr(":2f"), bfromcstr(":"), 0) );
216     
217     become_root();
218     if (adflags != ADFLAGS_DIR)
219         rename(vol->ad_path(path, 0), vol->ad_path(bdata(newpath), 0));
220     rename(path, bdata(newpath));
221     unbecome_root();
222
223     strlcpy(buf, bdata(newpath), sizeof(buf));
224     *newpathp = buf;
225
226 EC_CLEANUP:
227     if (newpath)
228         bdestroy(newpath);
229     EC_EXIT;
230 }
231
232 /*!
233  * AppleDouble and encoding conversion on the fly
234  *
235  * @param path      (r) path to file or directory
236  * @param sp        (r) stat(path)
237  * @param vol       (r) volume handle
238  * @param newpath   (w) if encoding changed, new name. Can be NULL.
239  *
240  * @returns         -1 on internal error, otherwise 0. newpath is NULL if no character conversion was done,
241  *                  otherwise newpath points to a static string with the converted name
242  */
243 int ad_convert(const char *path, const struct stat *sp, const struct vol *vol, const char **newpath)
244 {
245     EC_INIT;
246     const char *p;
247
248     LOG(log_debug, logtype_ad,"ad_convert(\"%s\"): BEGIN", fullpathname(path));
249
250     if (newpath)
251         *newpath = NULL;
252
253     if ((vol->v_adouble == AD_VERSION_EA) && !(vol->v_flags & AFPVOL_NOV2TOEACONV))
254         EC_ZERO( ad_conv_v22ea(path, sp, vol) );
255
256     if (vol->v_adouble == AD_VERSION_EA) {
257         EC_ZERO( ad_conv_dehex(path, sp, vol, &p) );
258         if (p && newpath)
259             *newpath = p;
260     }
261
262 EC_CLEANUP:
263     LOG(log_debug, logtype_ad,"ad_convert(\"%s\"): END: %d", fullpathname(path), ret);
264     EC_EXIT;
265 }
266