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])
51 int fsetrsrcea(struct adouble *ad, int fd, const char *eaname, const void *value, size_t size, int flags)
53 if ((ad->ad_maxeafssize == 0) || (ad->ad_maxeafssize >= size)) {
54 LOG(log_debug, logtype_default, "fsetrsrcea(\"%s\"): size: %zu", eaname, size);
55 if (sys_fsetxattr(fd, eaname, value, size, 0) == -1)
60 /* rsrcfork is larger then maximum EA support by fs so we have to split it */
62 int eas = (size / ad->ad_maxeafssize);
63 size_t remain = size - (eas * ad->ad_maxeafssize);
66 LOG(log_debug, logtype_default, "fsetrsrcea(\"%s\"): size: %zu, maxea: %zu, eas: %d, remain: %zu",
67 eaname, size, ad->ad_maxeafssize, eas, remain);
69 for (i = 0; i < eas; i++) {
70 if ((eachunk = bformat("%s.%d", eaname, i + 1)) == NULL)
72 if (sys_fsetxattr(fd, bdata(eachunk), value + (i * ad->ad_maxeafssize), ad->ad_maxeafssize, 0) == -1) {
73 LOG(log_error, logtype_default, "fsetrsrcea(\"%s\"): %s", bdata(eachunk), strerror(errno));
80 if ((eachunk = bformat("%s.%d", eaname, i + 1)) == NULL)
82 if (sys_fsetxattr(fd, bdata(eachunk), value + (i * ad->ad_maxeafssize), remain, 0) == -1) {
83 LOG(log_error, logtype_default, "fsetrsrcea(\"%s\"): %s", bdata(eachunk), strerror(errno));
92 * Rebuild any header information that might have changed.
94 int ad_rebuild_adouble_header(struct adouble *ad)
104 temp = htonl( ad->ad_magic );
105 memcpy(buf, &temp, sizeof( temp ));
106 buf += sizeof( temp );
108 temp = htonl( ad->ad_version );
109 memcpy(buf, &temp, sizeof( temp ));
110 buf += sizeof( temp );
112 buf += sizeof( ad->ad_filler );
115 buf += sizeof( nent );
116 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
117 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
120 temp = htonl( EID_DISK(eid) );
121 memcpy(buf, &temp, sizeof( temp ));
122 buf += sizeof( temp );
124 temp = htonl( ad->ad_eid[ eid ].ade_off );
125 memcpy(buf, &temp, sizeof( temp ));
126 buf += sizeof( temp );
128 temp = htonl( ad->ad_eid[ eid ].ade_len );
129 memcpy(buf, &temp, sizeof( temp ));
130 buf += sizeof( temp );
133 nent = htons( nent );
134 memcpy(nentp, &nent, sizeof( nent ));
136 switch (ad->ad_vers) {
138 len = ad_getentryoff(ad, ADEID_RFORK);
144 LOG(log_error, logtype_afpd, "Unexpected adouble version");
152 /* -------------------
153 * XXX copy only header with same size or comment
154 * doesn't work well for adouble with different version.
157 int ad_copy_header(struct adouble *add, struct adouble *ads)
162 for ( eid = 0; eid < ADEID_MAX; eid++ ) {
163 if ( ads->ad_eid[ eid ].ade_off == 0 ) {
167 if ( add->ad_eid[ eid ].ade_off == 0 ) {
171 len = ads->ad_eid[ eid ].ade_len;
176 if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
180 ad_setentrylen( add, eid, len );
181 memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
183 add->ad_rlen = ads->ad_rlen;
187 int ad_flush(struct adouble *ad)
191 LOG(log_debug, logtype_default, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
195 switch (ad->ad_vers) {
200 adf = &ad->ad_data_fork;
203 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
207 if ((adf->adf_flags & O_RDWR)) {
208 if (ad_getentryoff(ad, ADEID_RFORK)) {
209 if (ad->ad_rlen > 0xffffffff)
210 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
212 ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
214 len = ad->ad_ops->ad_rebuild_header(ad);
216 switch (ad->ad_vers) {
218 if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
225 if (AD_META_OPEN(ad)) {
226 if (sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
227 LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
233 if (AD_RSRC_OPEN(ad) && (ad->ad_rlen > 0)) {
234 if (fsetrsrcea(ad, ad_data_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
240 LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
248 static int ad_data_closefd(struct adouble *ad)
252 if (ad_data_fileno(ad) == -2) {
253 free(ad->ad_data_fork.adf_syml);
254 ad->ad_data_fork.adf_syml = NULL;
256 if (close(ad_data_fileno(ad)) < 0)
259 ad_data_fileno(ad) = -1;
264 * Close a struct adouble freeing all resources
266 int ad_close(struct adouble *ad, int adflags)
273 LOG(log_debug, logtype_default, "ad_close(%s)", adflags2logstr(adflags));
275 if ((adflags & ADFLAGS_DF)
276 && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
277 && --ad->ad_data_fork.adf_refcount == 0) {
278 if (ad_data_closefd(ad) < 0)
280 adf_lock_free(&ad->ad_data_fork);
283 if ((adflags & ADFLAGS_HF)) {
284 switch (ad->ad_vers) {
286 if ((ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
287 if (close( ad_meta_fileno(ad) ) < 0)
289 ad_meta_fileno(ad) = -1;
290 adf_lock_free(ad->ad_mdp);
295 if ((ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
296 && !(--ad->ad_data_fork.adf_refcount)) {
297 if (ad_data_closefd(ad) < 0)
299 adf_lock_free(&ad->ad_data_fork);
304 LOG(log_error, logtype_default, "ad_close: unknown AD version");
310 if ((adflags & ADFLAGS_RF)) {
311 switch (ad->ad_vers) {
313 /* Do nothing as ADFLAGS_RF == ADFLAGS_HF */
318 LOG(log_debug, logtype_default, "ad_close: ad->ad_rlen: %zu", ad->ad_rlen);
320 if (fsetrsrcea(ad, ad_data_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
323 if ((ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
324 && !(--ad->ad_data_fork.adf_refcount)) {
325 if (ad_data_closefd(ad) < 0)
327 adf_lock_free(&ad->ad_data_fork);
328 if (ad->ad_resforkbuf)
329 free(ad->ad_resforkbuf);
330 ad->ad_resforkbuf = NULL;
336 LOG(log_error, logtype_default, "ad_close: unknown AD version");