]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_flush.c
Fix
[netatalk.git] / libatalk / adouble / ad_flush.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * Copyright (c) 2010      Frank Lahm
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software and
7  * its documentation for any purpose and without fee is hereby granted,
8  * provided that the above copyright notice appears in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation, and that the name of The University
11  * of Michigan not be used in advertising or publicity pertaining to
12  * distribution of the software without specific, written prior
13  * permission. This software is supplied as is without expressed or
14  * implied warranties of any kind.
15  *
16  *  Research Systems Unix Group
17  *  The University of Michigan
18  *  c/o Mike Clark
19  *  535 W. William Street
20  *  Ann Arbor, Michigan
21  *  +1-313-763-0525
22  *  netatalk@itd.umich.edu
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif /* HAVE_CONFIG_H */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <arpa/inet.h>
34
35 #include <atalk/adouble.h>
36 #include <atalk/ea.h>
37 #include <atalk/logger.h>
38 #include <atalk/bstrlib.h>
39 #include <atalk/bstradd.h>
40 #include <atalk/errchk.h>
41 #include <atalk/util.h>
42
43 #include "ad_lock.h"
44
45 static const uint32_t set_eid[] = {
46     0,1,2,3,4,5,6,7,8,
47     9,10,11,12,13,14,15,
48     AD_DEV, AD_INO, AD_SYN, AD_ID
49 };
50
51 #define EID_DISK(a) (set_eid[a])
52
53 /*
54  * Prepare ad->ad_data buffer from struct adouble for writing on disk
55  */
56 int ad_rebuild_adouble_header(struct adouble *ad)
57 {
58     uint32_t       eid;
59     uint32_t       temp;
60     uint16_t       nent;
61     char        *buf, *nentp;
62     int             len;
63
64     LOG(log_debug, logtype_default, "ad_rebuild_adouble_header");
65
66     buf = ad->ad_data;
67
68     temp = htonl( ad->ad_magic );
69     memcpy(buf, &temp, sizeof( temp ));
70     buf += sizeof( temp );
71
72     temp = htonl( ad->ad_version );
73     memcpy(buf, &temp, sizeof( temp ));
74     buf += sizeof( temp );
75
76     buf += sizeof( ad->ad_filler );
77
78     nentp = buf;
79     buf += sizeof( nent );
80     for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
81         if ( ad->ad_eid[ eid ].ade_off == 0 ) {
82             continue;
83         }
84         temp = htonl( EID_DISK(eid) );
85         memcpy(buf, &temp, sizeof( temp ));
86         buf += sizeof( temp );
87
88         temp = htonl( ad->ad_eid[ eid ].ade_off );
89         memcpy(buf, &temp, sizeof( temp ));
90         buf += sizeof( temp );
91
92         temp = htonl( ad->ad_eid[ eid ].ade_len );
93         memcpy(buf, &temp, sizeof( temp ));
94         buf += sizeof( temp );
95         nent++;
96     }
97     nent = htons( nent );
98     memcpy(nentp, &nent, sizeof( nent ));
99
100     switch (ad->ad_vers) {
101     case AD_VERSION2:
102         len = ad_getentryoff(ad, ADEID_RFORK);
103         break;
104     case AD_VERSION_EA:
105         len = AD_DATASZ_EA;
106         break;
107     default:
108         LOG(log_error, logtype_afpd, "Unexpected adouble version");
109         len = 0;
110         break;
111     }
112
113     return len;
114 }
115
116 /*!
117  * Prepare adbuf buffer from struct adouble for writing on disk
118  */
119 static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
120 {
121     uint32_t       temp;
122     uint16_t       nent;
123     char           *buf;
124     int            len;
125
126     LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_osx");
127
128     buf = &adbuf[0];
129
130     temp = htonl( ad->ad_magic );
131     memcpy(buf, &temp, sizeof( temp ));
132     buf += sizeof( temp );
133
134     temp = htonl( ad->ad_version );
135     memcpy(buf, &temp, sizeof( temp ));
136     buf += sizeof( temp );
137
138     buf += sizeof( ad->ad_filler );
139
140     nent = htons(ADEID_NUM_OSX);
141     memcpy(buf, &nent, sizeof( nent ));
142     buf += sizeof( nent );
143
144     /* FinderInfo */
145     temp = htonl(EID_DISK(ADEID_FINDERI));
146     memcpy(buf, &temp, sizeof( temp ));
147     buf += sizeof( temp );
148
149     temp = htonl(ADEDOFF_FINDERI_OSX);
150     memcpy(buf, &temp, sizeof( temp ));
151     buf += sizeof( temp );
152
153     temp = htonl(ADEDLEN_FINDERI);
154     memcpy(buf, &temp, sizeof( temp ));
155     buf += sizeof( temp );
156
157     memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
158
159     /* rfork */
160     temp = htonl( EID_DISK(ADEID_RFORK) );
161     memcpy(buf, &temp, sizeof( temp ));
162     buf += sizeof( temp );
163
164     temp = htonl(ADEDOFF_RFORK_OSX);
165     memcpy(buf, &temp, sizeof( temp ));
166     buf += sizeof( temp );
167
168     temp = htonl( ad->ad_rlen);
169     memcpy(buf, &temp, sizeof( temp ));
170     buf += sizeof( temp );
171
172     return AD_DATASZ_OSX;
173 }
174
175 /* -------------------
176  * XXX copy only header with same size or comment
177  * doesn't work well for adouble with different version.
178  *
179  */
180 int ad_copy_header(struct adouble *add, struct adouble *ads)
181 {
182     uint32_t       eid;
183     uint32_t       len;
184
185     for ( eid = 0; eid < ADEID_MAX; eid++ ) {
186         if ( ads->ad_eid[ eid ].ade_off == 0 ) {
187             continue;
188         }
189
190         if ( add->ad_eid[ eid ].ade_off == 0 ) {
191             continue;
192         }
193
194         len = ads->ad_eid[ eid ].ade_len;
195         if (!len) {
196             continue;
197         }
198
199         if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
200             continue;
201         }
202
203         ad_setentrylen( add, eid, len );
204         memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
205     }
206     add->ad_rlen = ads->ad_rlen;
207     return 0;
208 }
209
210 static int ad_flush_hf(struct adouble *ad)
211 {
212     EC_INIT;
213     int len;
214     int cwd = -1;
215
216     LOG(log_debug, logtype_default, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
217
218     struct ad_fd *adf;
219
220     switch (ad->ad_vers) {
221     case AD_VERSION2:
222         adf = ad->ad_mdp;
223         break;
224     case AD_VERSION_EA:
225         adf = &ad->ad_data_fork;
226         break;
227     default:
228         LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
229         return -1;
230     }
231
232     if ((adf->adf_flags & O_RDWR)
233         || ((ad->ad_adflags & ADFLAGS_DIR) && (ad->ad_adflags & ADFLAGS_RDWR))) {
234         if (ad_getentryoff(ad, ADEID_RFORK)) {
235             if (ad->ad_rlen > 0xffffffff)
236                 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
237             else
238                 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
239         }
240         len = ad->ad_ops->ad_rebuild_header(ad);
241
242         switch (ad->ad_vers) {
243         case AD_VERSION2:
244             if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
245                 if (errno == 0)
246                     errno = EIO;
247                 return( -1 );
248             }
249             break;
250         case AD_VERSION_EA:
251             if (AD_META_OPEN(ad)) {
252                 if (ad->ad_adflags & ADFLAGS_DIR) {
253                     EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
254                     EC_NEG1_LOG( fchdir(ad_data_fileno(ad)) );
255                     EC_ZERO_LOG( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
256                     EC_NEG1_LOG( fchdir(cwd) );
257                     cwd = -1;
258                 } else {
259                     EC_ZERO_LOG( sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
260                 }
261             }
262             break;
263         default:
264             LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
265             return -1;
266         }
267     }
268
269 EC_CLEANUP:
270     if (cwd != -1) {
271         if (fchdir(cwd) != 0) {
272             AFP_PANIC("ad_flush: cant fchdir");
273         }
274         close(cwd);
275     }
276     EC_EXIT;
277 }
278
279 /* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
280 static int ad_flush_rf(struct adouble *ad)
281 {
282     ssize_t len;
283     char    adbuf[AD_DATASZ_OSX];
284
285 #ifdef HAVE_EAFD
286     return 0;
287 #endif
288     if (ad->ad_vers != AD_VERSION_EA)
289         return 0;
290
291     LOG(log_debug, logtype_default, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
292
293     if ((ad->ad_rfp->adf_flags & O_RDWR)) {
294         if (ad_getentryoff(ad, ADEID_RFORK)) {
295             if (ad->ad_rlen > 0xffffffff)
296                 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
297             else
298                 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
299         }
300         len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
301
302         if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
303             if (errno == 0)
304                 errno = EIO;
305             return -1;
306         }
307     }
308     return 0;
309 }
310
311 int ad_flush(struct adouble *ad)
312 {
313     EC_INIT;
314
315     LOG(log_debug, logtype_default, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
316
317     if (AD_META_OPEN(ad)) {
318         EC_ZERO( ad_flush_hf(ad) );
319     }
320
321     if (AD_RSRC_OPEN(ad)) {
322         EC_ZERO( ad_flush_rf(ad) );
323     }
324
325 EC_CLEANUP:
326     EC_EXIT;
327 }
328
329 static int ad_data_closefd(struct adouble *ad)
330 {
331     int ret = 0;
332
333     if (ad_data_fileno(ad) == -2) {
334         free(ad->ad_data_fork.adf_syml);
335         ad->ad_data_fork.adf_syml = NULL;
336     } else {
337         if (close(ad_data_fileno(ad)) < 0)
338             ret = -1;
339     }
340     ad_data_fileno(ad) = -1;
341     return ret;
342 }
343
344 /*!
345  * Close a struct adouble freeing all resources
346  */
347 int ad_close(struct adouble *ad, int adflags)
348 {
349     int err = 0;
350
351     if (ad == NULL)
352         return err;
353
354     LOG(log_debug, logtype_default,
355         "ad_close(%s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
356         adflags2logstr(adflags),
357         ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
358         ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
359         ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
360
361     /* cf ad_open(): we opened the datafork too for sharemode locks */
362     if ((ad->ad_vers == AD_VERSION_EA) && (ad->ad_adflags & ADFLAGS_SETSHRMD))
363         adflags |= ADFLAGS_DF;
364
365     if ((adflags & ADFLAGS_DF)
366         && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
367         && --ad->ad_data_fork.adf_refcount == 0) {
368         if (ad_data_closefd(ad) < 0)
369             err = -1;
370         adf_lock_free(&ad->ad_data_fork);
371     }
372
373     if ((adflags & ADFLAGS_HF)
374         && (ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
375         if (close( ad_meta_fileno(ad)) < 0)
376             err = -1;
377         ad_meta_fileno(ad) = -1;
378         if (ad->ad_vers == AD_VERSION2)
379             adf_lock_free(ad->ad_mdp);
380     }
381
382     if ((adflags & ADFLAGS_RF) && (ad->ad_vers == AD_VERSION_EA)) {
383         if ((ad_reso_fileno(ad) != -1)
384             && !(--ad->ad_rfp->adf_refcount)) {
385             if (close(ad->ad_rfp->adf_fd) < 0)
386                 err = -1;
387             ad->ad_rlen = 0;
388             ad_reso_fileno(ad) = -1;
389         }
390     }
391
392     LOG(log_debug, logtype_default,
393         "ad_close(%s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
394         err, adflags2logstr(adflags),
395         ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
396         ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
397         ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
398
399     return err;
400 }