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>
43 static const uint32_t set_eid[] = {
46 AD_DEV, AD_INO, AD_SYN, AD_ID
49 #define EID_DISK(a) (set_eid[a])
52 * Rebuild any header information that might have changed.
54 int ad_rebuild_adouble_header(struct adouble *ad)
64 temp = htonl( ad->ad_magic );
65 memcpy(buf, &temp, sizeof( temp ));
66 buf += sizeof( temp );
68 temp = htonl( ad->ad_version );
69 memcpy(buf, &temp, sizeof( temp ));
70 buf += sizeof( temp );
72 buf += sizeof( ad->ad_filler );
75 buf += sizeof( nent );
76 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
77 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
80 temp = htonl( EID_DISK(eid) );
81 memcpy(buf, &temp, sizeof( temp ));
82 buf += sizeof( temp );
84 temp = htonl( ad->ad_eid[ eid ].ade_off );
85 memcpy(buf, &temp, sizeof( temp ));
86 buf += sizeof( temp );
88 temp = htonl( ad->ad_eid[ eid ].ade_len );
89 memcpy(buf, &temp, sizeof( temp ));
90 buf += sizeof( temp );
94 memcpy(nentp, &nent, sizeof( nent ));
96 switch (ad->ad_vers) {
98 len = ad_getentryoff(ad, ADEID_RFORK);
104 LOG(log_error, logtype_afpd, "Unexpected adouble version");
112 /* -------------------
113 * XXX copy only header with same size or comment
114 * doesn't work well for adouble with different version.
117 int ad_copy_header(struct adouble *add, struct adouble *ads)
122 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
123 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
127 if ( add->ad_eid[ eid ].ade_off == 0 ) {
131 len = ads->ad_eid[ eid ].ade_len;
136 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
140 ad_setentrylen( add, eid, len );
141 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
143 add->ad_rlen = ads->ad_rlen;
147 int ad_flush(struct adouble *ad)
152 switch (ad->ad_vers) {
157 adf = &ad->ad_data_fork;
160 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
164 if ((adf->adf_flags & O_RDWR)) {
165 if (ad_getentryoff(ad, ADEID_RFORK)) {
166 if (ad->ad_rlen > 0xffffffff)
167 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
169 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
171 len = ad->ad_ops->ad_rebuild_header(ad);
173 switch (ad->ad_vers) {
175 if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
182 if (sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
183 LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
189 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
198 * Close a struct adouble freeing all resources
200 int ad_close(struct adouble *ad, int adflags)
207 LOG(log_debug, logtype_default, "ad_close(%s)", adflags2logstr(adflags));
209 if ((adflags & ADFLAGS_DF)
210 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
211 && --ad->ad_data_fork.adf_refcount == 0) {
212 if (ad->ad_data_fork.adf_syml != NULL) {
213 free(ad->ad_data_fork.adf_syml);
214 ad->ad_data_fork.adf_syml = 0;
216 if ( close( ad_data_fileno(ad) ) < 0 )
219 ad_data_fileno(ad) = -1;
220 adf_lock_free(&ad->ad_data_fork);
223 if ((adflags & ADFLAGS_HF)) {
224 switch (ad->ad_vers) {
226 if ((ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
227 if (close( ad_meta_fileno(ad) ) < 0)
229 ad_meta_fileno(ad) = -1;
230 adf_lock_free(ad->ad_mdp);
235 if ((ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
236 && !(--ad->ad_data_fork.adf_refcount)) {
237 if (close( ad_data_fileno(ad) ) < 0)
239 ad_data_fileno(ad) = -1;
240 adf_lock_free(&ad->ad_data_fork);
245 LOG(log_error, logtype_default, "ad_close: unknown AD version");
251 if ((adflags & ADFLAGS_RF)) {
252 switch (ad->ad_vers) {
254 /* Do nothing as ADFLAGS_RF == ADFLAGS_HF */
258 if ((ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
259 && !(--ad->ad_data_fork.adf_refcount)) {
260 if (close( ad_data_fileno(ad) ) < 0)
262 ad_data_fileno(ad) = -1;
263 adf_lock_free(&ad->ad_data_fork);
268 LOG(log_error, logtype_default, "ad_close: unknown AD version");