]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_flush.c
massive commenting/autoconf changes
[netatalk.git] / libatalk / adouble / ad_flush.c
1 /*
2  * $Id: ad_flush.c,v 1.3 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4  * Copyright (c) 1990,1991 Regents of The University of Michigan.
5  * All Rights Reserved.
6  *
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.
16  *
17  *      Research Systems Unix Group
18  *      The University of Michigan
19  *      c/o Mike Clark
20  *      535 W. William Street
21  *      Ann Arbor, Michigan
22  *      +1-313-763-0525
23  *      netatalk@itd.umich.edu
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif /* HAVE_FCNTL_H */
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39 #include <sys/types.h>
40 #include <sys/mman.h>
41 #include <errno.h>
42
43 #include <netatalk/endian.h>
44 #include <atalk/adouble.h>
45
46 #include "ad_private.h"
47
48 /* rebuild the header */
49 void ad_rebuild_header(struct adouble *ad)
50 {
51     u_int32_t           eid;
52     u_int16_t           nent;
53     char                *buf, *nentp;
54
55     /*
56      * Rebuild any header information that might have changed.
57      */
58     buf = ad->ad_data;
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 );
63     
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 );
70     
71     nentp = buf;
72     buf += sizeof( nent );
73     for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
74       if ( ad->ad_eid[ eid ].ade_off == 0 ) {
75         continue;
76       }
77       eid = htonl( eid );
78       memcpy(buf, &eid, sizeof( eid ));
79       eid = ntohl( eid );
80       buf += 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 );
91       nent++;
92     }
93     nent = htons( nent );
94     memcpy(nentp, &nent, sizeof( nent ));
95 }
96
97
98 int ad_flush( ad, adflags )
99     struct adouble      *ad;
100     int                 adflags;
101 {
102 #ifndef USE_MMAPPED_HEADERS
103     int len;
104 #endif /* ! USE_MMAPPED_HEADERS */
105
106     if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
107         /* sync our header */
108         ad_rebuild_header(ad);
109
110 #ifdef USE_MMAPPED_HEADERS
111         /* now sync it */
112 #ifdef MS_SYNC
113         msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
114               MS_SYNC | MS_INVALIDATE);
115 #else
116         msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
117 #endif /* MS_SYNC */
118
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 ) {
122                 return( -1 );
123             }
124             ad->ad_hf.adf_off = 0;
125         }
126
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) {
130             if ( errno == 0 ) {
131                 errno = EIO;
132             }
133             return( -1 );
134         }
135         ad->ad_hf.adf_off = len;
136 #endif /* USE_MMAPPED_HEADERS */
137     }
138
139     return( 0 );
140 }
141
142 /* use refcounts so that we don't have to re-establish fcntl locks. */
143 int ad_close( ad, adflags )
144     struct adouble      *ad;
145     int                 adflags;
146 {
147     int                 err = 0;
148
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 ) {
152             err = -1;
153         }
154         ad->ad_df.adf_fd = -1;
155         adf_lock_free(&ad->ad_df);
156     }
157
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 ) {
165             err = -1;
166         }
167         ad->ad_hf.adf_fd = -1;
168         adf_lock_free(&ad->ad_hf);
169     }
170
171     return( err );
172 }