2 * $Id: ad_flush.c,v 1.4 2002-09-29 17:39:59 didg Exp $
4 * Copyright (c) 1990,1991 Regents of The University of Michigan.
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appears in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation, and that the name of The University
12 * of Michigan not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. This software is supplied as is without expressed or
15 * implied warranties of any kind.
17 * Research Systems Unix Group
18 * The University of Michigan
20 * 535 W. William Street
23 * netatalk@itd.umich.edu
28 #endif /* HAVE_CONFIG_H */
35 #endif /* HAVE_FCNTL_H */
38 #endif /* HAVE_UNISTD_H */
39 #include <sys/types.h>
43 #include <netatalk/endian.h>
44 #include <atalk/adouble.h>
46 #include "ad_private.h"
48 /* rebuild the header */
49 void ad_rebuild_header(struct adouble *ad)
58 * Rebuild any header information that might have changed.
62 temp = htonl( ad->ad_magic );
63 memcpy(buf, &temp, sizeof( temp ));
64 buf += sizeof( temp );
66 temp = htonl( ad->ad_version );
67 memcpy(buf, &temp, sizeof( temp ));
68 buf += sizeof( temp );
70 memcpy(buf, ad->ad_filler, sizeof( ad->ad_filler ));
71 buf += sizeof( ad->ad_filler );
74 buf += sizeof( nent );
75 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
76 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
80 memcpy(buf, &temp, sizeof( temp ));
81 buf += sizeof( temp );
83 temp = htonl( ad->ad_eid[ eid ].ade_off );
84 memcpy(buf, &temp, sizeof( temp ));
85 buf += sizeof( temp );
87 temp = htonl( ad->ad_eid[ eid ].ade_len );
88 memcpy(buf, &temp, sizeof( temp ));
89 buf += sizeof( temp );
93 memcpy(nentp, &nent, sizeof( nent ));
97 int ad_flush( ad, adflags )
101 #ifndef USE_MMAPPED_HEADERS
103 #endif /* ! USE_MMAPPED_HEADERS */
105 if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
106 /* sync our header */
107 ad_rebuild_header(ad);
109 #ifdef USE_MMAPPED_HEADERS
112 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
113 MS_SYNC | MS_INVALIDATE);
115 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
118 #else /* USE_MMAPPED_HEADERS */
119 if ( ad->ad_hf.adf_off != 0 ) {
120 if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
123 ad->ad_hf.adf_off = 0;
126 /* now flush it out */
127 len = ad_getentryoff(ad, ADEID_RFORK);
128 if (write( ad->ad_hf.adf_fd, ad->ad_data, len) != len) {
134 ad->ad_hf.adf_off = len;
135 #endif /* USE_MMAPPED_HEADERS */
141 /* use refcounts so that we don't have to re-establish fcntl locks. */
142 int ad_close( ad, adflags )
148 if (( adflags & ADFLAGS_DF ) && ad->ad_df.adf_fd != -1 &&
149 !(--ad->ad_df.adf_refcount)) {
150 if ( close( ad->ad_df.adf_fd ) < 0 ) {
153 ad->ad_df.adf_fd = -1;
154 adf_lock_free(&ad->ad_df);
157 if (( adflags & ADFLAGS_HF ) && ad->ad_hf.adf_fd != -1 &&
158 !(--ad->ad_hf.adf_refcount)) {
159 #ifdef USE_MMAPPED_HEADERS
160 if (ad->ad_data != MAP_FAILED)
161 munmap(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
162 #endif /* USE_MMAPPED_HEADERS */
163 if ( close( ad->ad_hf.adf_fd ) < 0 ) {
166 ad->ad_hf.adf_fd = -1;
167 adf_lock_free(&ad->ad_hf);