2 * $Id: ad_flush.c,v 1.3 2001-06-29 14:14:46 rufustfirefly 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)
56 * Rebuild any header information that might have changed.
59 ad->ad_magic = htonl( ad->ad_magic );
60 memcpy(buf, &ad->ad_magic, sizeof( ad->ad_magic ));
61 ad->ad_magic = ntohl( ad->ad_magic );
62 buf += sizeof( ad->ad_magic );
64 ad->ad_version = htonl( ad->ad_version );
65 memcpy(buf, &ad->ad_version, sizeof( ad->ad_version ));
66 ad->ad_version = ntohl( ad->ad_version );
67 buf += sizeof( ad->ad_version );
68 memcpy(buf, ad->ad_filler, sizeof( ad->ad_filler ));
69 buf += sizeof( ad->ad_filler );
72 buf += sizeof( nent );
73 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
74 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
78 memcpy(buf, &eid, sizeof( eid ));
81 ad->ad_eid[ eid ].ade_off = htonl( ad->ad_eid[ eid ].ade_off );
82 memcpy(buf, &ad->ad_eid[ eid ].ade_off,
83 sizeof( ad->ad_eid[ eid ].ade_off ));
84 ad->ad_eid[ eid ].ade_off = ntohl( ad->ad_eid[ eid ].ade_off );
85 buf += sizeof( ad->ad_eid[ eid ].ade_off );
86 ad->ad_eid[ eid ].ade_len = htonl( ad->ad_eid[ eid ].ade_len );
87 memcpy(buf, &ad->ad_eid[ eid ].ade_len,
88 sizeof( ad->ad_eid[ eid ].ade_len ));
89 ad->ad_eid[ eid ].ade_len = ntohl( ad->ad_eid[ eid ].ade_len );
90 buf += sizeof( ad->ad_eid[ eid ].ade_len );
94 memcpy(nentp, &nent, sizeof( nent ));
98 int ad_flush( ad, adflags )
102 #ifndef USE_MMAPPED_HEADERS
104 #endif /* ! USE_MMAPPED_HEADERS */
106 if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
107 /* sync our header */
108 ad_rebuild_header(ad);
110 #ifdef USE_MMAPPED_HEADERS
113 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
114 MS_SYNC | MS_INVALIDATE);
116 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
119 #else /* USE_MMAPPED_HEADERS */
120 if ( ad->ad_hf.adf_off != 0 ) {
121 if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
124 ad->ad_hf.adf_off = 0;
127 /* now flush it out */
128 len = ad_getentryoff(ad, ADEID_RFORK);
129 if (write( ad->ad_hf.adf_fd, ad->ad_data, len) != len) {
135 ad->ad_hf.adf_off = len;
136 #endif /* USE_MMAPPED_HEADERS */
142 /* use refcounts so that we don't have to re-establish fcntl locks. */
143 int ad_close( ad, adflags )
149 if (( adflags & ADFLAGS_DF ) && ad->ad_df.adf_fd != -1 &&
150 !(--ad->ad_df.adf_refcount)) {
151 if ( close( ad->ad_df.adf_fd ) < 0 ) {
154 ad->ad_df.adf_fd = -1;
155 adf_lock_free(&ad->ad_df);
158 if (( adflags & ADFLAGS_HF ) && ad->ad_hf.adf_fd != -1 &&
159 !(--ad->ad_hf.adf_refcount)) {
160 #ifdef USE_MMAPPED_HEADERS
161 if (ad->ad_data != MAP_FAILED)
162 munmap(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
163 #endif /* USE_MMAPPED_HEADERS */
164 if ( close( ad->ad_hf.adf_fd ) < 0 ) {
167 ad->ad_hf.adf_fd = -1;
168 adf_lock_free(&ad->ad_hf);