]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_flush.c
implemented config.h
[netatalk.git] / libatalk / adouble / ad_flush.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
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.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/mman.h>
35 #include <errno.h>
36
37 #include <netatalk/endian.h>
38 #include <atalk/adouble.h>
39
40 #include "ad_private.h"
41
42 /* rebuild the header */
43 void ad_rebuild_header(struct adouble *ad)
44 {
45     u_int32_t           eid;
46     u_int16_t           nent;
47     char                *buf, *nentp;
48
49     /*
50      * Rebuild any header information that might have changed.
51      */
52     buf = ad->ad_data;
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 );
57     
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 );
64     
65     nentp = buf;
66     buf += sizeof( nent );
67     for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
68       if ( ad->ad_eid[ eid ].ade_off == 0 ) {
69         continue;
70       }
71       eid = htonl( eid );
72       memcpy(buf, &eid, sizeof( eid ));
73       eid = ntohl( eid );
74       buf += 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 );
85       nent++;
86     }
87     nent = htons( nent );
88     memcpy(nentp, &nent, sizeof( nent ));
89 }
90
91
92 int ad_flush( ad, adflags )
93     struct adouble      *ad;
94     int                 adflags;
95 {
96 #ifndef USE_MMAPPED_HEADERS
97     int len;
98 #endif
99
100     if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
101         /* sync our header */
102         ad_rebuild_header(ad);
103
104 #ifdef USE_MMAPPED_HEADERS
105         /* now sync it */
106 #ifdef MS_SYNC
107         msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
108               MS_SYNC | MS_INVALIDATE);
109 #else
110         msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
111 #endif
112
113 #else
114         if ( ad->ad_hf.adf_off != 0 ) {
115             if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
116                 return( -1 );
117             }
118             ad->ad_hf.adf_off = 0;
119         }
120
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) {
124             if ( errno == 0 ) {
125                 errno = EIO;
126             }
127             return( -1 );
128         }
129         ad->ad_hf.adf_off = len;
130 #endif
131     }
132
133     return( 0 );
134 }
135
136 /* use refcounts so that we don't have to re-establish fcntl locks. */
137 int ad_close( ad, adflags )
138     struct adouble      *ad;
139     int                 adflags;
140 {
141     int                 err = 0;
142
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 ) {
146             err = -1;
147         }
148         ad->ad_df.adf_fd = -1;
149         adf_lock_free(&ad->ad_df);
150     }
151
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));
157 #endif
158         if ( close( ad->ad_hf.adf_fd ) < 0 ) {
159             err = -1;
160         }
161         ad->ad_hf.adf_fd = -1;
162         adf_lock_free(&ad->ad_hf);
163     }
164
165     return( err );
166 }