]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_flush.c
Initial revision
[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 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #include <errno.h>
32
33 #include <netatalk/endian.h>
34 #include <atalk/adouble.h>
35
36 #include "ad_private.h"
37
38 /* rebuild the header */
39 void ad_rebuild_header(struct adouble *ad)
40 {
41     u_int32_t           eid;
42     u_int16_t           nent;
43     char                *buf, *nentp;
44
45     /*
46      * Rebuild any header information that might have changed.
47      */
48     buf = ad->ad_data;
49     ad->ad_magic = htonl( ad->ad_magic );
50     memcpy(buf, &ad->ad_magic, sizeof( ad->ad_magic ));
51     ad->ad_magic = ntohl( ad->ad_magic );
52     buf += sizeof( ad->ad_magic );
53     
54     ad->ad_version = htonl( ad->ad_version );
55     memcpy(buf, &ad->ad_version, sizeof( ad->ad_version ));
56     ad->ad_version = ntohl( ad->ad_version );
57     buf += sizeof( ad->ad_version );
58     memcpy(buf, ad->ad_filler, sizeof( ad->ad_filler ));
59     buf += sizeof( ad->ad_filler );
60     
61     nentp = buf;
62     buf += sizeof( nent );
63     for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
64       if ( ad->ad_eid[ eid ].ade_off == 0 ) {
65         continue;
66       }
67       eid = htonl( eid );
68       memcpy(buf, &eid, sizeof( eid ));
69       eid = ntohl( eid );
70       buf += sizeof( eid );
71       ad->ad_eid[ eid ].ade_off = htonl( ad->ad_eid[ eid ].ade_off );
72       memcpy(buf, &ad->ad_eid[ eid ].ade_off,
73              sizeof( ad->ad_eid[ eid ].ade_off ));
74       ad->ad_eid[ eid ].ade_off = ntohl( ad->ad_eid[ eid ].ade_off );
75       buf += sizeof( ad->ad_eid[ eid ].ade_off );
76       ad->ad_eid[ eid ].ade_len = htonl( ad->ad_eid[ eid ].ade_len );
77       memcpy(buf, &ad->ad_eid[ eid ].ade_len, 
78              sizeof( ad->ad_eid[ eid ].ade_len ));
79       ad->ad_eid[ eid ].ade_len = ntohl( ad->ad_eid[ eid ].ade_len );
80       buf += sizeof( ad->ad_eid[ eid ].ade_len );
81       nent++;
82     }
83     nent = htons( nent );
84     memcpy(nentp, &nent, sizeof( nent ));
85 }
86
87
88 int ad_flush( ad, adflags )
89     struct adouble      *ad;
90     int                 adflags;
91 {
92 #ifndef USE_MMAPPED_HEADERS
93     int len;
94 #endif
95
96     if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
97         /* sync our header */
98         ad_rebuild_header(ad);
99
100 #ifdef USE_MMAPPED_HEADERS
101         /* now sync it */
102 #ifdef MS_SYNC
103         msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
104               MS_SYNC | MS_INVALIDATE);
105 #else
106         msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
107 #endif
108
109 #else
110         if ( ad->ad_hf.adf_off != 0 ) {
111             if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
112                 return( -1 );
113             }
114             ad->ad_hf.adf_off = 0;
115         }
116
117         /* now flush it out */
118         len = ad_getentryoff(ad, ADEID_RFORK);
119         if (write( ad->ad_hf.adf_fd, ad->ad_data, len) != len) {
120             if ( errno == 0 ) {
121                 errno = EIO;
122             }
123             return( -1 );
124         }
125         ad->ad_hf.adf_off = len;
126 #endif
127     }
128
129     return( 0 );
130 }
131
132 /* use refcounts so that we don't have to re-establish fcntl locks. */
133 int ad_close( ad, adflags )
134     struct adouble      *ad;
135     int                 adflags;
136 {
137     int                 err = 0;
138
139     if (( adflags & ADFLAGS_DF ) && ad->ad_df.adf_fd != -1 &&
140         !(--ad->ad_df.adf_refcount)) {
141         if ( close( ad->ad_df.adf_fd ) < 0 ) {
142             err = -1;
143         }
144         ad->ad_df.adf_fd = -1;
145         adf_lock_free(&ad->ad_df);
146     }
147
148     if (( adflags & ADFLAGS_HF ) && ad->ad_hf.adf_fd != -1 &&
149         !(--ad->ad_hf.adf_refcount)) {
150 #ifdef USE_MMAPPED_HEADERS
151         if (ad->ad_data != MAP_FAILED)
152           munmap(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
153 #endif
154         if ( close( ad->ad_hf.adf_fd ) < 0 ) {
155             err = -1;
156         }
157         ad->ad_hf.adf_fd = -1;
158         adf_lock_free(&ad->ad_hf);
159     }
160
161     return( err );
162 }