2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
3 * Copyright (c) 2010 Frank Lahm
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.
16 * Research Systems Unix Group
17 * The University of Michigan
19 * 535 W. William Street
22 * netatalk@itd.umich.edu
27 #endif /* HAVE_CONFIG_H */
33 #include <arpa/inet.h>
35 #include <atalk/adouble.h>
37 #include <atalk/logger.h>
38 #include <atalk/bstrlib.h>
39 #include <atalk/bstradd.h>
40 #include <atalk/errchk.h>
44 static const uint32_t set_eid[] = {
47 AD_DEV, AD_INO, AD_SYN, AD_ID
50 #define EID_DISK(a) (set_eid[a])
53 * Prepare ad->ad_data buffer from struct adouble for writing on disk
55 int ad_rebuild_adouble_header(struct adouble *ad)
65 temp = htonl( ad->ad_magic );
66 memcpy(buf, &temp, sizeof( temp ));
67 buf += sizeof( temp );
69 temp = htonl( ad->ad_version );
70 memcpy(buf, &temp, sizeof( temp ));
71 buf += sizeof( temp );
73 buf += sizeof( ad->ad_filler );
76 buf += sizeof( nent );
77 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
78 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
81 temp = htonl( EID_DISK(eid) );
82 memcpy(buf, &temp, sizeof( temp ));
83 buf += sizeof( temp );
85 temp = htonl( ad->ad_eid[ eid ].ade_off );
86 memcpy(buf, &temp, sizeof( temp ));
87 buf += sizeof( temp );
89 temp = htonl( ad->ad_eid[ eid ].ade_len );
90 memcpy(buf, &temp, sizeof( temp ));
91 buf += sizeof( temp );
95 memcpy(nentp, &nent, sizeof( nent ));
97 switch (ad->ad_vers) {
99 len = ad_getentryoff(ad, ADEID_RFORK);
105 LOG(log_error, logtype_afpd, "Unexpected adouble version");
114 * Prepare adbuf buffer from struct adouble for writing on disk
116 static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
123 LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_osx");
127 temp = htonl( ad->ad_magic );
128 memcpy(buf, &temp, sizeof( temp ));
129 buf += sizeof( temp );
131 temp = htonl( ad->ad_version );
132 memcpy(buf, &temp, sizeof( temp ));
133 buf += sizeof( temp );
135 buf += sizeof( ad->ad_filler );
137 nent = htons(ADEID_NUM_OSX);
138 memcpy(buf, &nent, sizeof( nent ));
139 buf += sizeof( nent );
142 temp = htonl(EID_DISK(ADEID_FINDERI));
143 memcpy(buf, &temp, sizeof( temp ));
144 buf += sizeof( temp );
146 temp = htonl(ADEDOFF_FINDERI_OSX);
147 memcpy(buf, &temp, sizeof( temp ));
148 buf += sizeof( temp );
150 temp = htonl(ADEDLEN_FINDERI);
151 memcpy(buf, &temp, sizeof( temp ));
152 buf += sizeof( temp );
154 memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
157 temp = htonl( EID_DISK(ADEID_RFORK) );
158 memcpy(buf, &temp, sizeof( temp ));
159 buf += sizeof( temp );
161 temp = htonl(ADEDOFF_RFORK_OSX);
162 memcpy(buf, &temp, sizeof( temp ));
163 buf += sizeof( temp );
165 temp = htonl( ad->ad_rlen);
166 memcpy(buf, &temp, sizeof( temp ));
167 buf += sizeof( temp );
169 return AD_DATASZ_OSX;
172 /* -------------------
173 * XXX copy only header with same size or comment
174 * doesn't work well for adouble with different version.
177 int ad_copy_header(struct adouble *add, struct adouble *ads)
182 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
183 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
187 if ( add->ad_eid[ eid ].ade_off == 0 ) {
191 len = ads->ad_eid[ eid ].ade_len;
196 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
200 ad_setentrylen( add, eid, len );
201 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
203 add->ad_rlen = ads->ad_rlen;
207 static int ad_flush_hf(struct adouble *ad)
211 LOG(log_debug, logtype_default, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
215 switch (ad->ad_vers) {
220 adf = &ad->ad_data_fork;
223 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
227 if ((adf->adf_flags & O_RDWR)) {
228 if (ad_getentryoff(ad, ADEID_RFORK)) {
229 if (ad->ad_rlen > 0xffffffff)
230 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
232 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
234 len = ad->ad_ops->ad_rebuild_header(ad);
236 switch (ad->ad_vers) {
238 if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
245 if (AD_META_OPEN(ad)) {
246 if (sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
247 LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
254 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
262 /* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
263 static int ad_flush_rf(struct adouble *ad)
266 char adbuf[AD_DATASZ_OSX];
271 if (ad->ad_vers != AD_VERSION_EA)
274 LOG(log_debug, logtype_default, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
276 if ((ad->ad_rfp->adf_flags & O_RDWR)) {
277 if (ad_getentryoff(ad, ADEID_RFORK)) {
278 if (ad->ad_rlen > 0xffffffff)
279 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
281 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
283 len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
285 if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
294 int ad_flush(struct adouble *ad)
298 LOG(log_debug, logtype_default, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
300 if (AD_META_OPEN(ad)) {
301 EC_ZERO( ad_flush_hf(ad) );
304 if (AD_RSRC_OPEN(ad)) {
305 EC_ZERO( ad_flush_rf(ad) );
312 static int ad_data_closefd(struct adouble *ad)
316 if (ad_data_fileno(ad) == -2) {
317 free(ad->ad_data_fork.adf_syml);
318 ad->ad_data_fork.adf_syml = NULL;
320 if (close(ad_data_fileno(ad)) < 0)
323 ad_data_fileno(ad) = -1;
328 * Close a struct adouble freeing all resources
330 int ad_close(struct adouble *ad, int adflags)
337 LOG(log_debug, logtype_default, "ad_close(%s)", adflags2logstr(adflags));
339 /* cf ad_open(): we opened the datafork too for sharemode locks */
340 if ((ad->ad_vers == AD_VERSION_EA) && (ad->ad_adflags & ADFLAGS_SETSHRMD))
341 adflags |= ADFLAGS_DF;
343 if ((adflags & ADFLAGS_DF)
344 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
345 && --ad->ad_data_fork.adf_refcount == 0) {
346 if (ad_data_closefd(ad) < 0)
348 adf_lock_free(&ad->ad_data_fork);
351 if ((adflags & ADFLAGS_HF)
352 && (ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
353 if (close( ad_meta_fileno(ad)) < 0)
355 ad_meta_fileno(ad) = -1;
356 if (ad->ad_vers == AD_VERSION2)
357 adf_lock_free(ad->ad_mdp);
360 if ((adflags & ADFLAGS_RF) && (ad->ad_vers == AD_VERSION_EA)) {
361 if ((ad_reso_fileno(ad) != -1)
362 && !(--ad->ad_rfp->adf_refcount)) {
363 if (close(ad->ad_rfp->adf_fd) < 0)
366 ad_reso_fileno(ad) = -1;