2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
15 * Research Systems Unix Group
16 * The University of Michigan
18 * 535 W. William Street
21 * netatalk@itd.umich.edu
29 #include <sys/types.h>
33 #include <netatalk/endian.h>
34 #include <atalk/adouble.h>
36 #include "ad_private.h"
38 /* rebuild the header */
39 void ad_rebuild_header(struct adouble *ad)
46 * Rebuild any header information that might have changed.
49 ad->ad_magic = htonl( ad->ad_magic );
50 memcpy(buf, &ad->ad_magic, sizeof( ad->ad_magic ));
51 ad->ad_magic = ntohl( ad->ad_magic );
52 buf += sizeof( ad->ad_magic );
54 ad->ad_version = htonl( ad->ad_version );
55 memcpy(buf, &ad->ad_version, sizeof( ad->ad_version ));
56 ad->ad_version = ntohl( ad->ad_version );
57 buf += sizeof( ad->ad_version );
58 memcpy(buf, ad->ad_filler, sizeof( ad->ad_filler ));
59 buf += sizeof( ad->ad_filler );
62 buf += sizeof( nent );
63 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
64 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
68 memcpy(buf, &eid, sizeof( eid ));
71 ad->ad_eid[ eid ].ade_off = htonl( ad->ad_eid[ eid ].ade_off );
72 memcpy(buf, &ad->ad_eid[ eid ].ade_off,
73 sizeof( ad->ad_eid[ eid ].ade_off ));
74 ad->ad_eid[ eid ].ade_off = ntohl( ad->ad_eid[ eid ].ade_off );
75 buf += sizeof( ad->ad_eid[ eid ].ade_off );
76 ad->ad_eid[ eid ].ade_len = htonl( ad->ad_eid[ eid ].ade_len );
77 memcpy(buf, &ad->ad_eid[ eid ].ade_len,
78 sizeof( ad->ad_eid[ eid ].ade_len ));
79 ad->ad_eid[ eid ].ade_len = ntohl( ad->ad_eid[ eid ].ade_len );
80 buf += sizeof( ad->ad_eid[ eid ].ade_len );
84 memcpy(nentp, &nent, sizeof( nent ));
88 int ad_flush( ad, adflags )
92 #ifndef USE_MMAPPED_HEADERS
96 if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
98 ad_rebuild_header(ad);
100 #ifdef USE_MMAPPED_HEADERS
103 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
104 MS_SYNC | MS_INVALIDATE);
106 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
110 if ( ad->ad_hf.adf_off != 0 ) {
111 if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
114 ad->ad_hf.adf_off = 0;
117 /* now flush it out */
118 len = ad_getentryoff(ad, ADEID_RFORK);
119 if (write( ad->ad_hf.adf_fd, ad->ad_data, len) != len) {
125 ad->ad_hf.adf_off = len;
132 /* use refcounts so that we don't have to re-establish fcntl locks. */
133 int ad_close( ad, adflags )
139 if (( adflags & ADFLAGS_DF ) && ad->ad_df.adf_fd != -1 &&
140 !(--ad->ad_df.adf_refcount)) {
141 if ( close( ad->ad_df.adf_fd ) < 0 ) {
144 ad->ad_df.adf_fd = -1;
145 adf_lock_free(&ad->ad_df);
148 if (( adflags & ADFLAGS_HF ) && ad->ad_hf.adf_fd != -1 &&
149 !(--ad->ad_hf.adf_refcount)) {
150 #ifdef USE_MMAPPED_HEADERS
151 if (ad->ad_data != MAP_FAILED)
152 munmap(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
154 if ( close( ad->ad_hf.adf_fd ) < 0 ) {
157 ad->ad_hf.adf_fd = -1;
158 adf_lock_free(&ad->ad_hf);