2 * $Id: ad_mmap.c,v 1.6 2008-12-03 18:35:44 didg Exp $
4 * ad_mmap provides interfaces to memory mapped files. as this is the
5 * case, we don't have to deal w/ temporary buffers such as
6 * ad_data. the ad_mmap routines are designed to not interact w/ the
7 * ad_read/ad_write routines to avoid confusion.
12 #endif /* HAVE_CONFIG_H */
14 #ifdef USE_MMAPPED_HEADERS
17 #include <atalk/adouble.h>
20 #include "ad_private.h"
22 static void *ad_mmap(const size_t length, const int prot,
23 const int flags, const int fd,
26 return mmap(0, length, prot, flags, fd, offset);
29 /* this just sets things up for mmap. as mmap can handle offsets,
30 * we need to reset the file position before handing it off */
31 void *ad_mmapread(struct adouble *ad, const u_int32_t eid,
32 const off_t off, const size_t buflen)
35 if ( eid == ADEID_DFORK ) {
36 if ( lseek( ad->ad_df.adf_fd, 0, SEEK_SET ) < 0 ) {
40 ad->ad_df.adf_off = 0;
41 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_PRIVATE,
42 ad->ad_df.adf_fd, off);
47 if ( lseek( ad->ad_hf.adf_fd, 0, SEEK_SET ) < 0 ) {
51 ad->ad_hf.adf_off = 0;
52 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_PRIVATE,
53 ad->ad_hf.adf_fd, ad->ad_eid[eid].ade_off + off);
57 /* to do writeable mmaps correctly, we actually need to make sure that
58 * the file to be mapped is large enough. that's what all the initial
60 void *ad_mmapwrite(struct adouble *ad, const u_int32_t eid,
61 off_t off, const int end, const size_t buflen)
66 if ( eid == ADEID_DFORK ) {
67 if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) {
72 off = st.st_size - off;
75 /* make sure the file is large enough */
76 if (st.st_size < buflen + off)
77 ftruncate(ad->ad_df.adf_fd, buflen + off);
79 if ( lseek( ad->ad_df.adf_fd, 0, SEEK_SET ) < 0 ) {
82 ad->ad_df.adf_off = 0;
83 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_SHARED,
84 ad->ad_df.adf_fd, off);
88 if ( fstat( ad->ad_hf.adf_fd, &st ) < 0 ) {
93 off = ad->ad_eid[ eid ].ade_len - off;
96 off += ad->ad_eid[eid].ade_off;
98 /* make sure the file is large enough */
99 if (st.st_size < buflen + off)
100 ftruncate(ad->ad_hf.adf_fd, buflen + off);
102 if ( lseek( ad->ad_hf.adf_fd, 0, SEEK_SET ) < 0 ) {
105 ad->ad_hf.adf_off = 0;
106 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_SHARED,
107 ad->ad_hf.adf_fd, off);