]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
Merge master
[netatalk.git] / libatalk / adouble / ad_write.c
1 /*
2  * Copyright (c) 1990,1995 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/param.h>
13 #include <errno.h>
14
15 #include <atalk/adouble.h>
16 #include <atalk/ea.h>
17 #include <atalk/bstrlib.h>
18 #include <atalk/bstradd.h>
19
20 #ifndef MIN
21 #define MIN(a,b)        ((a)<(b)?(a):(b))
22 #endif /* ! MIN */
23
24 /* XXX: locking has to be checked before each stream of consecutive
25  *      ad_writes to prevent a lock in the middle from causing problems. 
26  */
27
28 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
29 {
30     ssize_t             cc;
31
32 #ifndef  HAVE_PWRITE
33     if ( ad_fd->adf_off != offset ) {
34         if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
35             return -1;
36         }
37         ad_fd->adf_off = offset;
38     }
39     cc = write( ad_fd->adf_fd, buf, count );
40     if ( cc < 0 ) {
41         return -1;
42     }
43     ad_fd->adf_off += cc;
44 #else
45    cc = pwrite(ad_fd->adf_fd, buf, count, offset );
46 #endif
47     return cc;
48 }
49
50 /* end is always 0 */
51 ssize_t ad_write(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
52 {
53     struct stat         st;
54     ssize_t             cc;
55
56     if (ad_data_fileno(ad) == -2) {
57         /* It's a symlink */
58         errno = EACCES;
59         return -1;
60     }
61     
62     if ( eid == ADEID_DFORK ) {
63         if ( end ) {
64             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
65                 return( -1 );
66             }
67             off = st.st_size - off;
68         }
69         cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
70     } else if ( eid == ADEID_RFORK ) {
71         if (ad->ad_flags != AD_VERSION_EA) {
72             off_t    r_off;
73             if ( end ) {
74                 if ( fstat( ad_data_fileno(ad), &st ) < 0 )
75                     return( -1 );
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_resource_fork, 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             if ( ad->ad_rlen < off + cc )
85                 ad->ad_rlen = off + cc;
86         } else { /* AD_VERSION_EA */
87             if ((off + buflen) > ad->ad_resforkbufsize) {
88                 size_t roundup = (((off + buflen) / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
89                 if ((ad->ad_resforkbuf = realloc(ad->ad_resforkbuf, roundup)) == NULL)
90                     return -1;
91                 ad->ad_resforkbufsize = roundup;
92             }
93             memcpy(ad->ad_resforkbuf + off, buf, buflen);
94             if ((off + buflen) > ad->ad_rlen)
95                 ad->ad_rlen = off + buflen;
96             
97             if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
98                 return -1;
99             cc = buflen;
100         }
101     } else {
102         return -1; /* we don't know how to write if it's not a ressource or data fork */
103     }
104
105     return( cc );
106 }
107
108 /* 
109  * the caller set the locks
110  * ftruncate is undefined when the file length is smaller than 'size'
111  */
112 int sys_ftruncate(int fd, off_t length)
113 {
114
115 #ifndef  HAVE_PWRITE
116 off_t           curpos;
117 #endif
118 int             err;
119 struct stat     st;
120 char            c = 0;
121
122     if (!ftruncate(fd, length)) {
123         return 0;
124     }
125     /* maybe ftruncate doesn't work if we try to extend the size */
126     err = errno;
127
128 #ifndef  HAVE_PWRITE
129     /* we only care about file pointer if we don't use pwrite */
130     if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
131         errno = err;
132         return -1;
133     }
134 #endif
135
136     if ( fstat( fd, &st ) < 0 ) {
137         errno = err;
138         return -1;
139     }
140     
141     if (st.st_size > length) {
142         errno = err;
143         return -1;
144     }
145
146     if (lseek(fd, length -1, SEEK_SET) != length -1) {
147         errno = err;
148         return -1;
149     }
150
151     if (1 != write( fd, &c, 1 )) {
152         /* return the write errno */
153         return -1;
154     }
155
156 #ifndef  HAVE_PWRITE
157     if (curpos != lseek(fd, curpos,  SEEK_SET)) {
158         errno = err;
159         return -1;
160     }
161 #endif
162
163     return 0;    
164 }
165
166 /* ------------------------ */
167 int ad_rtruncate( struct adouble *ad, const off_t size)
168 {
169     if (ad->ad_flags != AD_VERSION_EA)
170         if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
171             return -1;
172
173     ad->ad_rlen = size;    
174
175     return 0;
176 }
177
178 int ad_dtruncate(struct adouble *ad, const off_t size)
179 {
180     if (sys_ftruncate(ad_data_fileno(ad), size) < 0)
181         return -1;
182
183     return 0;
184 }