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>
38 #include "ad_private.h"
41 * Rebuild any header information that might have changed.
43 int ad_rebuild_adouble_header(struct adouble *ad)
53 temp = htonl( ad->ad_magic );
54 memcpy(buf, &temp, sizeof( temp ));
55 buf += sizeof( temp );
57 temp = htonl( ad->ad_version );
58 memcpy(buf, &temp, sizeof( temp ));
59 buf += sizeof( temp );
61 buf += sizeof( ad->ad_filler );
64 buf += sizeof( nent );
65 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
66 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
69 temp = htonl( DISK_EID(eid) );
70 memcpy(buf, &temp, sizeof( temp ));
71 buf += sizeof( temp );
73 temp = htonl( ad->ad_eid[ eid ].ade_off );
74 memcpy(buf, &temp, sizeof( temp ));
75 buf += sizeof( temp );
77 temp = htonl( ad->ad_eid[ eid ].ade_len );
78 memcpy(buf, &temp, sizeof( temp ));
79 buf += sizeof( temp );
83 memcpy(nentp, &nent, sizeof( nent ));
85 switch (ad->ad_flags) {
87 len = ad_getentryoff(ad, ADEID_RFORK);
93 LOG(log_error, logtype_afpd, "Unexpected adouble version");
101 /* -------------------
102 * XXX copy only header with same size or comment
103 * doesn't work well for adouble with different version.
106 int ad_copy_header(struct adouble *add, struct adouble *ads)
111 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
112 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
116 if ( add->ad_eid[ eid ].ade_off == 0 ) {
120 len = ads->ad_eid[ eid ].ade_len;
125 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
129 ad_setentrylen( add, eid, len );
130 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
132 add->ad_rlen = ads->ad_rlen;
136 int ad_flush(struct adouble *ad)
140 if (( ad->ad_md->adf_flags & O_RDWR )) {
141 if (ad_getentryoff(ad, ADEID_RFORK)) {
142 if (ad->ad_rlen > 0xffffffff)
143 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
145 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
147 len = ad->ad_ops->ad_rebuild_header(ad);
149 switch (ad->ad_flags) {
151 if (adf_pwrite(ad->ad_md, ad->ad_data, len, 0) != len) {
158 if (sys_fsetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
159 LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
165 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
173 /* use refcounts so that we don't have to re-establish fcntl locks. */
174 int ad_close( struct adouble *ad, int adflags)
178 if ((adflags & ADFLAGS_DF)
179 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
180 && --ad->ad_data_fork.adf_refcount == 0) {
181 if (ad->ad_data_fork.adf_syml != NULL) {
182 free(ad->ad_data_fork.adf_syml);
183 ad->ad_data_fork.adf_syml = 0;
185 if ( close( ad_data_fileno(ad) ) < 0 )
188 ad_data_fileno(ad) = -1;
189 adf_lock_free(&ad->ad_data_fork);
192 if (!( adflags & ADFLAGS_HF )) {
196 /* meta/resource fork */
198 if ( ad_meta_fileno(ad) != -1 && !(--ad->ad_md->adf_refcount)) {
199 if ( close( ad_meta_fileno(ad) ) < 0 ) {
202 ad_meta_fileno(ad) = -1;
203 adf_lock_free(ad->ad_md);