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 * This close the whole thing, regardless of what you pass in adflags!
188 int ad_close( struct adouble *ad, int adflags)
192 if (ad->ad_inited == AD_CLOSED) {
193 LOG(log_warning, logtype_default, "ad_close: double close");
197 LOG(log_debug, logtype_default, "ad_close(\"%s\", %s)",
198 cfrombstr(ad->ad_fullpath),
199 adflags2logstr(adflags));
201 if (ad->ad_refcount) {
202 LOG(log_debug, logtype_default, "ad_close(\"%s\"): adouble in use by fork, not closing",
203 cfrombstr(ad->ad_fullpath));
207 if (ad_data_fileno(ad) != -1) {
208 if ((ad_data_fileno(ad) == -2) && (ad->ad_data_fork.adf_syml != NULL)) {
209 free(ad->ad_data_fork.adf_syml);
210 ad->ad_data_fork.adf_syml = NULL;
212 if ( close( ad_data_fileno(ad) ) < 0 )
215 ad_data_fileno(ad) = -1;
216 adf_lock_free(&ad->ad_data_fork);
217 ad->ad_adflags &= ~ADFLAGS_DF;
220 if (ad_meta_fileno(ad) != -1) {
221 if ( close( ad_meta_fileno(ad) ) < 0 )
223 ad_meta_fileno(ad) = -1;
224 adf_lock_free(ad->ad_md);
225 ad->ad_adflags &= ~ADFLAGS_HF;
228 if (ad->ad_resforkbuf) {
229 free(ad->ad_resforkbuf);
230 ad->ad_resforkbuf = NULL;
231 ad->ad_adflags &= ~ADFLAGS_RF;
234 if (ad->ad_fullpath) {
235 bdestroy(ad->ad_fullpath);
236 ad->ad_fullpath = NULL;
239 ad->ad_inited = AD_CLOSED;