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
33 #include <sys/types.h>
37 #include <netatalk/endian.h>
38 #include <atalk/adouble.h>
40 #include "ad_private.h"
42 /* rebuild the header */
43 void ad_rebuild_header(struct adouble *ad)
50 * Rebuild any header information that might have changed.
53 ad->ad_magic = htonl( ad->ad_magic );
54 memcpy(buf, &ad->ad_magic, sizeof( ad->ad_magic ));
55 ad->ad_magic = ntohl( ad->ad_magic );
56 buf += sizeof( ad->ad_magic );
58 ad->ad_version = htonl( ad->ad_version );
59 memcpy(buf, &ad->ad_version, sizeof( ad->ad_version ));
60 ad->ad_version = ntohl( ad->ad_version );
61 buf += sizeof( ad->ad_version );
62 memcpy(buf, ad->ad_filler, sizeof( ad->ad_filler ));
63 buf += sizeof( ad->ad_filler );
66 buf += sizeof( nent );
67 for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
68 if ( ad->ad_eid[ eid ].ade_off == 0 ) {
72 memcpy(buf, &eid, sizeof( eid ));
75 ad->ad_eid[ eid ].ade_off = htonl( ad->ad_eid[ eid ].ade_off );
76 memcpy(buf, &ad->ad_eid[ eid ].ade_off,
77 sizeof( ad->ad_eid[ eid ].ade_off ));
78 ad->ad_eid[ eid ].ade_off = ntohl( ad->ad_eid[ eid ].ade_off );
79 buf += sizeof( ad->ad_eid[ eid ].ade_off );
80 ad->ad_eid[ eid ].ade_len = htonl( ad->ad_eid[ eid ].ade_len );
81 memcpy(buf, &ad->ad_eid[ eid ].ade_len,
82 sizeof( ad->ad_eid[ eid ].ade_len ));
83 ad->ad_eid[ eid ].ade_len = ntohl( ad->ad_eid[ eid ].ade_len );
84 buf += sizeof( ad->ad_eid[ eid ].ade_len );
88 memcpy(nentp, &nent, sizeof( nent ));
92 int ad_flush( ad, adflags )
96 #ifndef USE_MMAPPED_HEADERS
100 if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
101 /* sync our header */
102 ad_rebuild_header(ad);
104 #ifdef USE_MMAPPED_HEADERS
107 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
108 MS_SYNC | MS_INVALIDATE);
110 msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
114 if ( ad->ad_hf.adf_off != 0 ) {
115 if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
118 ad->ad_hf.adf_off = 0;
121 /* now flush it out */
122 len = ad_getentryoff(ad, ADEID_RFORK);
123 if (write( ad->ad_hf.adf_fd, ad->ad_data, len) != len) {
129 ad->ad_hf.adf_off = len;
136 /* use refcounts so that we don't have to re-establish fcntl locks. */
137 int ad_close( ad, adflags )
143 if (( adflags & ADFLAGS_DF ) && ad->ad_df.adf_fd != -1 &&
144 !(--ad->ad_df.adf_refcount)) {
145 if ( close( ad->ad_df.adf_fd ) < 0 ) {
148 ad->ad_df.adf_fd = -1;
149 adf_lock_free(&ad->ad_df);
152 if (( adflags & ADFLAGS_HF ) && ad->ad_hf.adf_fd != -1 &&
153 !(--ad->ad_hf.adf_refcount)) {
154 #ifdef USE_MMAPPED_HEADERS
155 if (ad->ad_data != MAP_FAILED)
156 munmap(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
158 if ( close( ad->ad_hf.adf_fd ) < 0 ) {
161 ad->ad_hf.adf_fd = -1;
162 adf_lock_free(&ad->ad_hf);