]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
6de42aaff7b9dce7864b4bd7fa664bba11400a7a
[netatalk.git] / libatalk / adouble / ad_write.c
1 /*
2  * $Id: ad_write.c,v 1.6 2003-01-16 21:18:15 didg Exp $
3  *
4  * Copyright (c) 1990,1995 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <atalk/adouble.h>
13
14 #include <string.h>
15 #include <sys/param.h>
16 #include <errno.h>
17
18
19 #ifndef MIN
20 #define MIN(a,b)        ((a)<(b)?(a):(b))
21 #endif /* ! MIN */
22
23 /* XXX: locking has to be checked before each stream of consecutive
24  *      ad_writes to prevent a lock in the middle from causing problems. 
25  */
26
27 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
28 {
29     ssize_t             cc;
30
31 #ifndef  HAVE_PWRITE
32     if ( ad_fd->adf_off != off ) {
33         if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
34             return -1;
35         }
36         ad_fd->adf_off = offset;
37     }
38     cc = write( ad_fd->adf_fd, buf, count );
39     if ( cc < 0 ) {
40         return -1;
41     }
42     ad_fd->adf_off += cc;
43 #else
44    cc = pwrite(ad_fd->adf_fd, buf, count, offset );
45 #endif
46     return cc;
47 }
48
49 /* end is always 0 */
50 ssize_t ad_write( ad, eid, off, end, buf, buflen )
51     struct adouble      *ad;
52     const u_int32_t     eid;
53     off_t               off;
54     const int           end;
55     const char          *buf;
56     const size_t        buflen;
57 {
58     struct stat         st;
59     ssize_t             cc;
60     
61     if ( eid == ADEID_DFORK ) {
62         if ( end ) {
63             if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) {
64                 return( -1 );
65             }
66             off = st.st_size - off;
67         }
68         cc = adf_pwrite(&ad->ad_df, buf, buflen, off);
69     } else if ( eid == ADEID_RFORK ) {
70         off_t    r_off;
71
72         if ( end ) {
73             if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) {
74                 return( -1 );
75             }
76             off = st.st_size - off -ad_getentryoff(ad, eid);
77         }
78         r_off = ad_getentryoff(ad, eid) + off;
79         cc = adf_pwrite(&ad->ad_hf, buf, buflen, r_off);
80
81         /* sync up our internal buffer  FIXME always false? */
82         if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
83             memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
84         }
85         if ( ad->ad_rlen  < r_off + cc ) {
86              ad->ad_rlen = r_off + cc;
87         }
88     }
89     else {
90         return -1; /* we don't know how to write if it's not a ressource or data fork */
91     }
92     return( cc );
93 }
94
95 /* 
96  * the caller set the locks
97  * we need tocopy and paste from samba code source/smbd/vfs_wrap.c
98  * ftruncate is undefined when the file length is smaller than 'size'
99  */
100 int ad_rtruncate( ad, size )
101     struct adouble      *ad;
102     const off_t         size;
103 {
104     if ( ftruncate( ad->ad_hf.adf_fd,
105             size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
106         return( -1 );
107     }
108     ad->ad_rlen = size;    
109
110     return( 0 );
111 }
112
113 int ad_dtruncate(ad, size)
114     struct adouble      *ad;
115     const off_t         size;
116 {
117     if (ftruncate(ad->ad_df.adf_fd, size) < 0) {
118       return -1;
119     }
120     return 0;
121 }