]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_flush.c
Merge remote-tracking branch 'origin/develop' into spotlight
[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_v2(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_ad, "ad_rebuild_adouble_header_v2");
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         temp = htonl( EID_DISK(eid) );
84         memcpy(buf, &temp, sizeof( temp ));
85         buf += sizeof( temp );
86
87         temp = htonl( ad->ad_eid[ eid ].ade_off );
88         memcpy(buf, &temp, sizeof( temp ));
89         buf += sizeof( temp );
90
91         temp = htonl( ad->ad_eid[ eid ].ade_len );
92         memcpy(buf, &temp, sizeof( temp ));
93         buf += sizeof( temp );
94         nent++;
95     }
96     nent = htons( nent );
97     memcpy(nentp, &nent, sizeof( nent ));
98
99     return ad_getentryoff(ad, ADEID_RFORK);
100 }
101
102 int ad_rebuild_adouble_header_ea(struct adouble *ad)
103 {
104     uint32_t       eid;
105     uint32_t       temp;
106     uint16_t       nent;
107     char        *buf, *nentp;
108     int             len;
109
110     LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_ea");
111
112     buf = ad->ad_data;
113
114     temp = htonl( ad->ad_magic );
115     memcpy(buf, &temp, sizeof( temp ));
116     buf += sizeof( temp );
117
118     temp = htonl( ad->ad_version );
119     memcpy(buf, &temp, sizeof( temp ));
120     buf += sizeof( temp );
121
122     buf += sizeof( ad->ad_filler );
123
124     nentp = buf;
125     buf += sizeof( nent );
126     for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
127         if ((ad->ad_eid[ eid ].ade_off == 0) || (eid == ADEID_RFORK))
128             continue;
129         temp = htonl( EID_DISK(eid) );
130         memcpy(buf, &temp, sizeof( temp ));
131         buf += sizeof( temp );
132
133         temp = htonl( ad->ad_eid[ eid ].ade_off );
134         memcpy(buf, &temp, sizeof( temp ));
135         buf += sizeof( temp );
136
137         temp = htonl( ad->ad_eid[ eid ].ade_len );
138         memcpy(buf, &temp, sizeof( temp ));
139         buf += sizeof( temp );
140         nent++;
141     }
142     nent = htons( nent );
143     memcpy(nentp, &nent, sizeof( nent ));
144
145     return AD_DATASZ_EA;
146 }
147
148 /*!
149  * Prepare adbuf buffer from struct adouble for writing on disk
150  */
151 static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
152 {
153     uint32_t       temp;
154     uint16_t       nent;
155     char           *buf;
156     int            len;
157
158     LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_osx");
159
160     buf = &adbuf[0];
161
162     temp = htonl( ad->ad_magic );
163     memcpy(buf, &temp, sizeof( temp ));
164     buf += sizeof( temp );
165
166     temp = htonl( ad->ad_version );
167     memcpy(buf, &temp, sizeof( temp ));
168     buf += sizeof( temp );
169
170     memcpy(buf, "Netatalk        ", 16);
171     buf += sizeof( ad->ad_filler );
172
173     nent = htons(ADEID_NUM_OSX);
174     memcpy(buf, &nent, sizeof( nent ));
175     buf += sizeof( nent );
176
177     /* FinderInfo */
178     temp = htonl(EID_DISK(ADEID_FINDERI));
179     memcpy(buf, &temp, sizeof( temp ));
180     buf += sizeof( temp );
181
182     temp = htonl(ADEDOFF_FINDERI_OSX);
183     memcpy(buf, &temp, sizeof( temp ));
184     buf += sizeof( temp );
185
186     temp = htonl(ADEDLEN_FINDERI);
187     memcpy(buf, &temp, sizeof( temp ));
188     buf += sizeof( temp );
189
190     memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
191
192     /* rfork */
193     temp = htonl( EID_DISK(ADEID_RFORK) );
194     memcpy(buf, &temp, sizeof( temp ));
195     buf += sizeof( temp );
196
197     temp = htonl(ADEDOFF_RFORK_OSX);
198     memcpy(buf, &temp, sizeof( temp ));
199     buf += sizeof( temp );
200
201     temp = htonl( ad->ad_rlen);
202     memcpy(buf, &temp, sizeof( temp ));
203     buf += sizeof( temp );
204
205     return AD_DATASZ_OSX;
206 }
207
208 /* -------------------
209  * XXX copy only header with same size or comment
210  * doesn't work well for adouble with different version.
211  *
212  */
213 int ad_copy_header(struct adouble *add, struct adouble *ads)
214 {
215     uint32_t       eid;
216     uint32_t       len;
217
218     for ( eid = 0; eid < ADEID_MAX; eid++ ) {
219         if ( ads->ad_eid[ eid ].ade_off == 0 || add->ad_eid[ eid ].ade_off == 0 )
220             continue;
221
222         len = ads->ad_eid[ eid ].ade_len;
223         if (!len || len != add->ad_eid[ eid ].ade_len)
224             continue;
225
226         switch (eid) {
227         case ADEID_COMMENT:
228         case ADEID_PRIVDEV:
229         case ADEID_PRIVINO:
230         case ADEID_PRIVSYN:
231         case ADEID_PRIVID:
232             continue;
233         default:
234             ad_setentrylen( add, eid, len );
235             memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
236         }
237     }
238     add->ad_rlen = ads->ad_rlen;
239
240     if (((ads->ad_vers == AD_VERSION2) && (add->ad_vers == AD_VERSION_EA))
241         || ((ads->ad_vers == AD_VERSION_EA) && (add->ad_vers == AD_VERSION2))) {
242         cnid_t id;
243         memcpy(&id, ad_entry(add, ADEID_PRIVID), sizeof(cnid_t));
244         id = htonl(id);
245         memcpy(ad_entry(add, ADEID_PRIVID), &id, sizeof(cnid_t));
246     }
247     return 0;
248 }
249
250 static int ad_flush_hf(struct adouble *ad)
251 {
252     EC_INIT;
253     int len;
254     int cwd = -1;
255
256     LOG(log_debug, logtype_ad, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
257
258     struct ad_fd *adf;
259
260     switch (ad->ad_vers) {
261     case AD_VERSION2:
262         adf = ad->ad_mdp;
263         break;
264     case AD_VERSION_EA:
265         adf = &ad->ad_data_fork;
266         break;
267     default:
268         LOG(log_error, logtype_ad, "ad_flush: unexpected adouble version");
269         return -1;
270     }
271
272     if ((adf->adf_flags & O_RDWR)
273         || ((ad->ad_adflags & ADFLAGS_DIR) && (ad->ad_adflags & ADFLAGS_RDWR))) {
274         if (ad_getentryoff(ad, ADEID_RFORK)) {
275             if (ad->ad_rlen > 0xffffffff)
276                 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
277             else
278                 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
279         }
280         len = ad->ad_ops->ad_rebuild_header(ad);
281
282         switch (ad->ad_vers) {
283         case AD_VERSION2:
284             if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
285                 if (errno == 0)
286                     errno = EIO;
287                 return( -1 );
288             }
289             break;
290         case AD_VERSION_EA:
291             if (AD_META_OPEN(ad)) {
292                 if (ad->ad_adflags & ADFLAGS_DIR) {
293                     EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
294                     EC_NEG1_LOG( fchdir(ad_data_fileno(ad)) );
295                     EC_ZERO_LOGSTR( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0),
296                                     "sys_lsetxattr(\"%s\"): %s", fullpathname(".") ,strerror(errno));
297                     EC_NEG1_LOG( fchdir(cwd) );
298                     EC_NEG1_LOG( close(cwd) );
299                     cwd = -1;
300                 } else {
301                     EC_ZERO_LOG( sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
302                 }
303             }
304             break;
305         default:
306             LOG(log_error, logtype_ad, "ad_flush: unexpected adouble version");
307             return -1;
308         }
309     }
310
311 EC_CLEANUP:
312     if (cwd != -1) {
313         if (fchdir(cwd) != 0) {
314             AFP_PANIC("ad_flush: cant fchdir");
315         }
316         close(cwd);
317     }
318     EC_EXIT;
319 }
320
321 /* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
322 static int ad_flush_rf(struct adouble *ad)
323 {
324     ssize_t len;
325     char    adbuf[AD_DATASZ_OSX];
326
327 #ifdef HAVE_EAFD
328     return 0;
329 #endif
330     if (ad->ad_vers != AD_VERSION_EA)
331         return 0;
332
333     LOG(log_debug, logtype_ad, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
334
335     if ((ad->ad_rfp->adf_flags & O_RDWR)) {
336         if (ad_getentryoff(ad, ADEID_RFORK)) {
337             if (ad->ad_rlen > 0xffffffff)
338                 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
339             else
340                 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
341         }
342         len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
343
344         if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
345             if (errno == 0)
346                 errno = EIO;
347             return -1;
348         }
349     }
350     return 0;
351 }
352
353 int ad_flush(struct adouble *ad)
354 {
355     EC_INIT;
356
357     LOG(log_debug, logtype_ad, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
358
359     if (AD_META_OPEN(ad)) {
360         EC_ZERO( ad_flush_hf(ad) );
361     }
362
363     if (AD_RSRC_OPEN(ad)) {
364         EC_ZERO( ad_flush_rf(ad) );
365     }
366
367 EC_CLEANUP:
368     EC_EXIT;
369 }
370
371 static int ad_data_closefd(struct adouble *ad)
372 {
373     int ret = 0;
374
375     if (ad_data_fileno(ad) == AD_SYMLINK) {
376         free(ad->ad_data_fork.adf_syml);
377         ad->ad_data_fork.adf_syml = NULL;
378     } else {
379         if (close(ad_data_fileno(ad)) < 0)
380             ret = -1;
381     }
382     ad_data_fileno(ad) = -1;
383     return ret;
384 }
385
386 /*!
387  * Close a struct adouble freeing all resources
388  */
389 int ad_close(struct adouble *ad, int adflags)
390 {
391     int err = 0;
392
393     if (ad == NULL)
394         return err;
395
396
397     LOG(log_debug, logtype_ad,
398         "ad_close(%s): BEGIN: {d: %d, m: %d, r: %d} "
399         "[dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
400         adflags2logstr(adflags),
401         ad->ad_data_refcount, ad->ad_meta_refcount, ad->ad_reso_refcount,
402         ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
403         ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
404         ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
405
406     if (adflags & (ADFLAGS_SETSHRMD | ADFLAGS_CHECK_OF)) {
407         /* sharemode locks are stored in the data fork, adouble:v2 needs this extra handling */
408         adflags |= ADFLAGS_DF;
409     }
410
411     if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF))
412         adflags |= ADFLAGS_HF;
413
414     if ((adflags & ADFLAGS_DF) && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == AD_SYMLINK)) {        
415         if (ad->ad_data_refcount)
416             if (--ad->ad_data_refcount == 0)
417                 adf_lock_free(&ad->ad_data_fork);                
418         if (--ad->ad_data_fork.adf_refcount == 0) {
419             if (ad_data_closefd(ad) < 0)
420                 err = -1;
421         }
422     }
423
424     if ((adflags & ADFLAGS_HF) && (ad_meta_fileno(ad) != -1)) {
425         if (ad->ad_meta_refcount)
426             ad->ad_meta_refcount--;
427         if (!(--ad->ad_mdp->adf_refcount)) {
428             if (close( ad_meta_fileno(ad)) < 0)
429                 err = -1;
430             ad_meta_fileno(ad) = -1;
431         }
432     }
433
434     if (adflags & ADFLAGS_RF) {
435         if (ad->ad_reso_refcount)
436             if (--ad->ad_reso_refcount == 0)
437                 adf_lock_free(ad->ad_rfp);
438         if (ad->ad_vers == AD_VERSION_EA) {
439             if ((ad_reso_fileno(ad) != -1)
440                 && !(--ad->ad_rfp->adf_refcount)) {
441                 if (close(ad->ad_rfp->adf_fd) < 0)
442                     err = -1;
443                 ad->ad_rlen = 0;
444                 ad_reso_fileno(ad) = -1;
445             }
446         }
447     }
448
449     LOG(log_debug, logtype_ad,
450         "ad_close(%s): END: %d {d: %d, m: %d, r: %d} "
451         "[dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
452         adflags2logstr(adflags), err,
453         ad->ad_data_refcount, ad->ad_meta_refcount, ad->ad_reso_refcount,
454         ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
455         ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
456         ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
457
458     return err;
459 }