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>
41 #include <atalk/util.h>
45 static const uint32_t set_eid[] = {
48 AD_DEV, AD_INO, AD_SYN, AD_ID
51 #define EID_DISK(a) (set_eid[a])
54 * Prepare ad->ad_data buffer from struct adouble for writing on disk
56 int ad_rebuild_adouble_header(struct adouble *ad)
64 LOG(log_debug, logtype_default, "ad_rebuild_adouble_header");
68 temp = htonl( ad->ad_magic );
69 memcpy(buf, &temp, sizeof( temp ));
70 buf += sizeof( temp );
72 temp = htonl( ad->ad_version );
73 memcpy(buf, &temp, sizeof( temp ));
74 buf += sizeof( temp );
76 buf += sizeof( ad->ad_filler );
79 buf += sizeof( nent );
80 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
81 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
84 temp = htonl( EID_DISK(eid) );
85 memcpy(buf, &temp, sizeof( temp ));
86 buf += sizeof( temp );
88 temp = htonl( ad->ad_eid[ eid ].ade_off );
89 memcpy(buf, &temp, sizeof( temp ));
90 buf += sizeof( temp );
92 temp = htonl( ad->ad_eid[ eid ].ade_len );
93 memcpy(buf, &temp, sizeof( temp ));
94 buf += sizeof( temp );
98 memcpy(nentp, &nent, sizeof( nent ));
100 switch (ad->ad_vers) {
102 len = ad_getentryoff(ad, ADEID_RFORK);
108 LOG(log_error, logtype_afpd, "Unexpected adouble version");
117 * Prepare adbuf buffer from struct adouble for writing on disk
119 static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
126 LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_osx");
130 temp = htonl( ad->ad_magic );
131 memcpy(buf, &temp, sizeof( temp ));
132 buf += sizeof( temp );
134 temp = htonl( ad->ad_version );
135 memcpy(buf, &temp, sizeof( temp ));
136 buf += sizeof( temp );
138 buf += sizeof( ad->ad_filler );
140 nent = htons(ADEID_NUM_OSX);
141 memcpy(buf, &nent, sizeof( nent ));
142 buf += sizeof( nent );
145 temp = htonl(EID_DISK(ADEID_FINDERI));
146 memcpy(buf, &temp, sizeof( temp ));
147 buf += sizeof( temp );
149 temp = htonl(ADEDOFF_FINDERI_OSX);
150 memcpy(buf, &temp, sizeof( temp ));
151 buf += sizeof( temp );
153 temp = htonl(ADEDLEN_FINDERI);
154 memcpy(buf, &temp, sizeof( temp ));
155 buf += sizeof( temp );
157 memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
160 temp = htonl( EID_DISK(ADEID_RFORK) );
161 memcpy(buf, &temp, sizeof( temp ));
162 buf += sizeof( temp );
164 temp = htonl(ADEDOFF_RFORK_OSX);
165 memcpy(buf, &temp, sizeof( temp ));
166 buf += sizeof( temp );
168 temp = htonl( ad->ad_rlen);
169 memcpy(buf, &temp, sizeof( temp ));
170 buf += sizeof( temp );
172 return AD_DATASZ_OSX;
175 /* -------------------
176 * XXX copy only header with same size or comment
177 * doesn't work well for adouble with different version.
180 int ad_copy_header(struct adouble *add, struct adouble *ads)
185 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
186 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
190 if ( add->ad_eid[ eid ].ade_off == 0 ) {
194 len = ads->ad_eid[ eid ].ade_len;
199 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
203 ad_setentrylen( add, eid, len );
204 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
206 add->ad_rlen = ads->ad_rlen;
210 static int ad_flush_hf(struct adouble *ad)
216 LOG(log_debug, logtype_default, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
220 switch (ad->ad_vers) {
225 adf = &ad->ad_data_fork;
228 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
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);
238 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
240 len = ad->ad_ops->ad_rebuild_header(ad);
242 switch (ad->ad_vers) {
244 if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
251 if (AD_META_OPEN(ad)) {
252 if (ad->ad_adflags & ADFLAGS_DIR) {
253 EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
254 EC_ZERO_LOG( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
256 EC_ZERO_LOG( sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
261 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
268 if (fchdir(cwd) != 0) {
269 AFP_PANIC("ad_flush: cant fchdir");
276 /* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
277 static int ad_flush_rf(struct adouble *ad)
280 char adbuf[AD_DATASZ_OSX];
285 if (ad->ad_vers != AD_VERSION_EA)
288 LOG(log_debug, logtype_default, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
290 if ((ad->ad_rfp->adf_flags & O_RDWR)) {
291 if (ad_getentryoff(ad, ADEID_RFORK)) {
292 if (ad->ad_rlen > 0xffffffff)
293 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
295 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
297 len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
299 if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
308 int ad_flush(struct adouble *ad)
312 LOG(log_debug, logtype_default, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
314 if (AD_META_OPEN(ad)) {
315 EC_ZERO( ad_flush_hf(ad) );
318 if (AD_RSRC_OPEN(ad)) {
319 EC_ZERO( ad_flush_rf(ad) );
326 static int ad_data_closefd(struct adouble *ad)
330 if (ad_data_fileno(ad) == -2) {
331 free(ad->ad_data_fork.adf_syml);
332 ad->ad_data_fork.adf_syml = NULL;
334 if (close(ad_data_fileno(ad)) < 0)
337 ad_data_fileno(ad) = -1;
342 * Close a struct adouble freeing all resources
344 int ad_close(struct adouble *ad, int adflags)
351 LOG(log_debug, logtype_default, "ad_close(%s)", adflags2logstr(adflags));
353 /* cf ad_open(): we opened the datafork too for sharemode locks */
354 if ((ad->ad_vers == AD_VERSION_EA) && (ad->ad_adflags & ADFLAGS_SETSHRMD))
355 adflags |= ADFLAGS_DF;
357 if ((adflags & ADFLAGS_DF)
358 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
359 && --ad->ad_data_fork.adf_refcount == 0) {
360 if (ad_data_closefd(ad) < 0)
362 adf_lock_free(&ad->ad_data_fork);
365 if ((adflags & ADFLAGS_HF)
366 && (ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
367 if (close( ad_meta_fileno(ad)) < 0)
369 ad_meta_fileno(ad) = -1;
370 if (ad->ad_vers == AD_VERSION2)
371 adf_lock_free(ad->ad_mdp);
374 if ((adflags & ADFLAGS_RF) && (ad->ad_vers == AD_VERSION_EA)) {
375 if ((ad_reso_fileno(ad) != -1)
376 && !(--ad->ad_rfp->adf_refcount)) {
377 if (close(ad->ad_rfp->adf_fd) < 0)
380 ad_reso_fileno(ad) = -1;