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 );
62 buf += sizeof( nent );
63 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
64 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
67 temp = htonl( DISK_EID(eid) );
68 memcpy(buf, &temp, sizeof( temp ));
69 buf += sizeof( temp );
71 temp = htonl( ad->ad_eid[ eid ].ade_off );
72 memcpy(buf, &temp, sizeof( temp ));
73 buf += sizeof( temp );
75 temp = htonl( ad->ad_eid[ eid ].ade_len );
76 memcpy(buf, &temp, sizeof( temp ));
77 buf += sizeof( temp );
81 memcpy(nentp, &nent, sizeof( nent ));
83 switch (ad->ad_version) {
85 len = ad_getentryoff(ad, ADEID_RFORK);
91 LOG(log_error, logtype_afpd, "Unexpected adouble version");
99 /* -------------------
100 * XXX copy only header with same size or comment
101 * doesn't work well for adouble with different version.
104 int ad_copy_header(struct adouble *add, struct adouble *ads)
109 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
110 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
114 if ( add->ad_eid[ eid ].ade_off == 0 ) {
118 len = ads->ad_eid[ eid ].ade_len;
123 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
127 ad_setentrylen( add, eid, len );
128 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
130 add->ad_rlen = ads->ad_rlen;
134 int ad_flush(struct adouble *ad)
138 if (( ad->ad_md->adf_flags & O_RDWR )) {
139 if (ad_getentryoff(ad, ADEID_RFORK)) {
140 if (ad->ad_rlen > 0xffffffff)
141 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
143 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
145 len = ad->ad_ops->ad_rebuild_header(ad);
147 switch (ad->ad_version) {
149 if (adf_pwrite(ad->ad_md, ad->ad_data, len, 0) != len) {
156 if (sys_fsetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
157 LOG(log_error, logtype_afpd, "Unexpected adouble version");
162 LOG(log_error, logtype_afpd, "Unexpected adouble version");
170 /* use refcounts so that we don't have to re-establish fcntl locks. */
171 int ad_close( struct adouble *ad, int adflags)
175 if ((adflags & ADFLAGS_DF)
176 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
177 && --ad->ad_data_fork.adf_refcount == 0) {
178 if (ad->ad_data_fork.adf_syml != NULL) {
179 free(ad->ad_data_fork.adf_syml);
180 ad->ad_data_fork.adf_syml = 0;
182 if ( close( ad_data_fileno(ad) ) < 0 )
185 ad_data_fileno(ad) = -1;
186 adf_lock_free(&ad->ad_data_fork);
189 if (!( adflags & ADFLAGS_HF )) {
193 /* meta/resource fork */
195 if ( ad_meta_fileno(ad) != -1 && !(--ad->ad_md->adf_refcount)) {
196 if ( close( ad_meta_fileno(ad) ) < 0 ) {
199 ad_meta_fileno(ad) = -1;
200 adf_lock_free(ad->ad_md);