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 */
34 #include <atalk/adouble.h>
36 #include <atalk/logger.h>
37 #include <atalk/bstrlib.h>
38 #include <atalk/bstradd.h>
42 static const u_int32_t set_eid[] = {
45 AD_DEV, AD_INO, AD_SYN, AD_ID
48 #define EID_DISK(a) (set_eid[a])
51 * Rebuild any header information that might have changed.
53 int ad_rebuild_adouble_header(struct adouble *ad)
63 temp = htonl( ad->ad_magic );
64 memcpy(buf, &temp, sizeof( temp ));
65 buf += sizeof( temp );
67 temp = htonl( ad->ad_version );
68 memcpy(buf, &temp, sizeof( temp ));
69 buf += sizeof( temp );
71 buf += sizeof( ad->ad_filler );
74 buf += sizeof( nent );
75 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
76 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
79 temp = htonl( EID_DISK(eid) );
80 memcpy(buf, &temp, sizeof( temp ));
81 buf += sizeof( temp );
83 temp = htonl( ad->ad_eid[ eid ].ade_off );
84 memcpy(buf, &temp, sizeof( temp ));
85 buf += sizeof( temp );
87 temp = htonl( ad->ad_eid[ eid ].ade_len );
88 memcpy(buf, &temp, sizeof( temp ));
89 buf += sizeof( temp );
93 memcpy(nentp, &nent, sizeof( nent ));
95 switch (ad->ad_flags) {
97 len = ad_getentryoff(ad, ADEID_RFORK);
103 LOG(log_error, logtype_afpd, "Unexpected adouble version");
111 /* -------------------
112 * XXX copy only header with same size or comment
113 * doesn't work well for adouble with different version.
116 int ad_copy_header(struct adouble *add, struct adouble *ads)
121 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
122 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
126 if ( add->ad_eid[ eid ].ade_off == 0 ) {
130 len = ads->ad_eid[ eid ].ade_len;
135 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
139 ad_setentrylen( add, eid, len );
140 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
142 add->ad_rlen = ads->ad_rlen;
146 int ad_flush(struct adouble *ad)
150 if (( ad->ad_md->adf_flags & O_RDWR )) {
151 if (ad_getentryoff(ad, ADEID_RFORK)) {
152 if (ad->ad_rlen > 0xffffffff)
153 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
155 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
157 len = ad->ad_ops->ad_rebuild_header(ad);
159 switch (ad->ad_flags) {
161 if (adf_pwrite(ad->ad_md, ad->ad_data, len, 0) != len) {
168 if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
169 LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
175 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
184 * Close a struct adouble freeing all resources
186 int ad_close( struct adouble *ad, int adflags _U_)
190 if (ad_data_fileno(ad) != -1) {
191 if ((ad_data_fileno(ad) == -2) && (ad->ad_data_fork.adf_syml != NULL)) {
192 free(ad->ad_data_fork.adf_syml);
193 ad->ad_data_fork.adf_syml = NULL;
195 if ( close( ad_data_fileno(ad) ) < 0 )
198 ad_data_fileno(ad) = -1;
199 adf_lock_free(&ad->ad_data_fork);
200 ad->ad_adflags &= ~ADFLAGS_DF;
203 if (ad_meta_fileno(ad) != -1) {
204 if ( close( ad_meta_fileno(ad) ) < 0 )
206 ad_meta_fileno(ad) = -1;
207 adf_lock_free(ad->ad_md);
208 ad->ad_adflags &= ~ADFLAGS_HF;
211 if (ad->ad_resforkbuf) {
212 free(ad->ad_resforkbuf);
213 ad->ad_resforkbuf = NULL;
214 ad->ad_adflags &= ~ADFLAGS_RF;
217 if (ad->ad_fullpath) {
218 bdestroy(ad->ad_fullpath);
219 ad->ad_fullpath = NULL;