2 * ad_mmap provides interfaces to memory mapped files. as this is the
3 * case, we don't have to deal w/ temporary buffers such as
4 * ad_data. the ad_mmap routines are designed to not interact w/ the
5 * ad_read/ad_write routines to avoid confusion.
10 #endif /* HAVE_CONFIG_H */
12 #ifdef USE_MMAPPED_HEADERS
15 #include <atalk/adouble.h>
20 static void *ad_mmap(const size_t length, const int prot,
21 const int flags, const int fd,
24 return mmap(0, length, prot, flags, fd, offset);
27 /* this just sets things up for mmap. as mmap can handle offsets,
28 * we need to reset the file position before handing it off */
29 void *ad_mmapread(struct adouble *ad, const u_int32_t eid,
30 const off_t off, const size_t buflen)
33 if ( eid == ADEID_DFORK ) {
34 if ( lseek( ad->ad_df.adf_fd, 0, SEEK_SET ) < 0 ) {
38 ad->ad_df.adf_off = 0;
39 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_PRIVATE,
40 ad->ad_df.adf_fd, off);
45 if ( lseek( ad->ad_hf.adf_fd, 0, SEEK_SET ) < 0 ) {
49 ad->ad_hf.adf_off = 0;
50 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_PRIVATE,
51 ad->ad_hf.adf_fd, ad->ad_eid[eid].ade_off + off);
55 /* to do writeable mmaps correctly, we actually need to make sure that
56 * the file to be mapped is large enough. that's what all the initial
58 void *ad_mmapwrite(struct adouble *ad, const u_int32_t eid,
59 off_t off, const int end, const size_t buflen)
64 if ( eid == ADEID_DFORK ) {
65 if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) {
70 off = st.st_size - off;
73 /* make sure the file is large enough */
74 if (st.st_size < buflen + off)
75 ftruncate(ad->ad_df.adf_fd, buflen + off);
77 if ( lseek( ad->ad_df.adf_fd, 0, SEEK_SET ) < 0 ) {
80 ad->ad_df.adf_off = 0;
81 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_SHARED,
82 ad->ad_df.adf_fd, off);
86 if ( fstat( ad->ad_hf.adf_fd, &st ) < 0 ) {
91 off = ad->ad_eid[ eid ].ade_len - off;
94 off += ad->ad_eid[eid].ade_off;
96 /* make sure the file is large enough */
97 if (st.st_size < buflen + off)
98 ftruncate(ad->ad_hf.adf_fd, buflen + off);
100 if ( lseek( ad->ad_hf.adf_fd, 0, SEEK_SET ) < 0 ) {
103 ad->ad_hf.adf_off = 0;
104 return ad_mmap(buflen, PROT_READ | PROT_WRITE, MAP_SHARED,
105 ad->ad_hf.adf_fd, off);