]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
Reading/writing rfork
[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                 free(ad->ad_resforkbuf);
89                 size_t roundup = (((off + buflen) / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
90                 if ((ad->ad_resforkbuf = malloc(roundup)) == NULL)
91                     return -1;
92                 ad->ad_resforkbufsize = roundup;
93             }
94             memcpy(ad->ad_resforkbuf + off, buf, buflen);
95             if ((off + buflen) > ad->ad_rlen)
96                 ad->ad_rlen = off + buflen;
97             
98             if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
99                 return -1;
100             cc = buflen;
101         }
102     } else {
103         return -1; /* we don't know how to write if it's not a ressource or data fork */
104     }
105
106     return( cc );
107 }
108
109 /* 
110  * the caller set the locks
111  * ftruncate is undefined when the file length is smaller than 'size'
112  */
113 int sys_ftruncate(int fd, off_t length)
114 {
115
116 #ifndef  HAVE_PWRITE
117 off_t           curpos;
118 #endif
119 int             err;
120 struct stat     st;
121 char            c = 0;
122
123     if (!ftruncate(fd, length)) {
124         return 0;
125     }
126     /* maybe ftruncate doesn't work if we try to extend the size */
127     err = errno;
128
129 #ifndef  HAVE_PWRITE
130     /* we only care about file pointer if we don't use pwrite */
131     if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
132         errno = err;
133         return -1;
134     }
135 #endif
136
137     if ( fstat( fd, &st ) < 0 ) {
138         errno = err;
139         return -1;
140     }
141     
142     if (st.st_size > length) {
143         errno = err;
144         return -1;
145     }
146
147     if (lseek(fd, length -1, SEEK_SET) != length -1) {
148         errno = err;
149         return -1;
150     }
151
152     if (1 != write( fd, &c, 1 )) {
153         /* return the write errno */
154         return -1;
155     }
156
157 #ifndef  HAVE_PWRITE
158     if (curpos != lseek(fd, curpos,  SEEK_SET)) {
159         errno = err;
160         return -1;
161     }
162 #endif
163
164     return 0;    
165 }
166
167 /* ------------------------ */
168 int ad_rtruncate( struct adouble *ad, const off_t size)
169 {
170     if (ad->ad_flags != AD_VERSION_EA)
171         if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
172             return -1;
173
174     ad->ad_rlen = size;    
175
176     return 0;
177 }
178
179 int ad_dtruncate(struct adouble *ad, const off_t size)
180 {
181     if (sys_ftruncate(ad_data_fileno(ad), size) < 0)
182         return -1;
183
184     return 0;
185 }