#include <atalk/adouble.h>
#include <atalk/ea.h>
#include <atalk/logger.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
-#include "ad_private.h"
+#include "ad_lock.h"
+
+static const u_int32_t set_eid[] = {
+ 0,1,2,3,4,5,6,7,8,
+ 9,10,11,12,13,14,15,
+ AD_DEV, AD_INO, AD_SYN, AD_ID
+};
+
+#define EID_DISK(a) (set_eid[a])
/*
* Rebuild any header information that might have changed.
memcpy(buf, &temp, sizeof( temp ));
buf += sizeof( temp );
+ buf += sizeof( ad->ad_filler );
+
nentp = buf;
buf += sizeof( nent );
for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
if ( ad->ad_eid[ eid ].ade_off == 0 ) {
continue;
}
- temp = htonl( DISK_EID(eid) );
+ temp = htonl( EID_DISK(eid) );
memcpy(buf, &temp, sizeof( temp ));
buf += sizeof( temp );
nent = htons( nent );
memcpy(nentp, &nent, sizeof( nent ));
- switch (ad->ad_version) {
+ switch (ad->ad_flags) {
case AD_VERSION2:
len = ad_getentryoff(ad, ADEID_RFORK);
break;
}
len = ad->ad_ops->ad_rebuild_header(ad);
- switch (ad->ad_version) {
+ switch (ad->ad_flags) {
case AD_VERSION2:
if (adf_pwrite(ad->ad_md, ad->ad_data, len, 0) != len) {
if (errno == 0)
}
break;
case AD_VERSION_EA:
- if (sys_fsetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
- LOG(log_error, logtype_afpd, "Unexpected adouble version");
+ if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
+ LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
+ strerror(errno));
return -1;
}
break;
default:
- LOG(log_error, logtype_afpd, "Unexpected adouble version");
+ LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
return -1;
}
}
return( 0 );
}
-/* use refcounts so that we don't have to re-establish fcntl locks. */
+/*!
+ * Close a struct adouble freeing all resources
+ *
+ * This close the whole thing, regardless of what you pass in adflags!
+ * When open forks are using this struct adouble (ad_refcount>0) the close
+ * request is ignored.
+ */
int ad_close( struct adouble *ad, int adflags)
{
- int err = 0;
+ int err = 0;
+
+ LOG(log_debug, logtype_default, "ad_close(\"%s\", %s)",
+ cfrombstr(ad->ad_fullpath),
+ adflags2logstr(adflags));
+
+ if (ad->ad_refcount) {
+ LOG(log_debug, logtype_default, "ad_close(\"%s\"): adouble in use by fork, not closing",
+ cfrombstr(ad->ad_fullpath));
+ return 0;
+ }
- if ((adflags & ADFLAGS_DF)
- && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
- && --ad->ad_data_fork.adf_refcount == 0) {
- if (ad->ad_data_fork.adf_syml != NULL) {
+ if (ad_data_fileno(ad) != -1) {
+ if ((ad_data_fileno(ad) == -2) && (ad->ad_data_fork.adf_syml != NULL)) {
free(ad->ad_data_fork.adf_syml);
- ad->ad_data_fork.adf_syml = 0;
+ ad->ad_data_fork.adf_syml = NULL;
} else {
if ( close( ad_data_fileno(ad) ) < 0 )
err = -1;
}
ad_data_fileno(ad) = -1;
adf_lock_free(&ad->ad_data_fork);
+ ad->ad_adflags &= ~ADFLAGS_DF;
}
- if (!( adflags & ADFLAGS_HF )) {
- return err;
- }
-
- /* meta/resource fork */
-
- if ( ad_meta_fileno(ad) != -1 && !(--ad->ad_md->adf_refcount)) {
- if ( close( ad_meta_fileno(ad) ) < 0 ) {
+ if (ad_meta_fileno(ad) != -1) {
+ if ( close( ad_meta_fileno(ad) ) < 0 )
err = -1;
- }
ad_meta_fileno(ad) = -1;
adf_lock_free(ad->ad_md);
+ ad->ad_adflags &= ~ADFLAGS_HF;
+ }
+
+ if (ad->ad_resforkbuf) {
+ free(ad->ad_resforkbuf);
+ ad->ad_resforkbuf = NULL;
+ ad->ad_adflags &= ~ADFLAGS_RF;
+ }
+
+ if (ad->ad_fullpath) {
+ bdestroy(ad->ad_fullpath);
+ ad->ad_fullpath = NULL;
}
return err;