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