]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
libatalk:config: deleting volumes
[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 #include <atalk/logger.h>
20 #include <atalk/util.h>
21 #include <atalk/errchk.h>
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 != offset ) {
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(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
51 {
52     EC_INIT;
53     struct stat         st;
54     ssize_t             cc;
55     off_t    r_off;
56
57     if (ad_data_fileno(ad) == AD_SYMLINK) {
58         errno = EACCES;
59         return -1;
60     }
61
62     LOG(log_debug, logtype_ad, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
63         (uintmax_t)off, buflen, ad->ad_rlen);
64     
65     if ( eid == ADEID_DFORK ) {
66         if ( end ) {
67             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
68                 return( -1 );
69             }
70             off = st.st_size - off;
71         }
72         cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
73     } else if ( eid == ADEID_RFORK ) {
74         if (end) {
75             if (fstat( ad_reso_fileno(ad), &st ) < 0)
76                 return(-1);
77             off = st.st_size - off - ad_getentryoff(ad, eid);
78         }
79         if (ad->ad_vers == AD_VERSION_EA) {
80 #ifdef HAVE_EAFD
81             r_off = off;
82 #else
83             r_off = ADEDOFF_RFORK_OSX + off;
84 #endif
85         } else {
86             r_off = ad_getentryoff(ad, eid) + off;
87         }
88         cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
89
90         if ( ad->ad_rlen < off + cc )
91             ad->ad_rlen = off + cc;
92     } else {
93         return -1; /* we don't know how to write if it's not a ressource or data fork */
94     }
95
96     if (ret != 0)
97         return ret;
98     return( cc );
99 }
100
101 /* 
102  * the caller set the locks
103  * ftruncate is undefined when the file length is smaller than 'size'
104  */
105 int sys_ftruncate(int fd, off_t length)
106 {
107
108 #ifndef  HAVE_PWRITE
109 off_t           curpos;
110 #endif
111 int             err;
112 struct stat     st;
113 char            c = 0;
114
115     if (!ftruncate(fd, length)) {
116         return 0;
117     }
118     /* maybe ftruncate doesn't work if we try to extend the size */
119     err = errno;
120
121 #ifndef  HAVE_PWRITE
122     /* we only care about file pointer if we don't use pwrite */
123     if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
124         errno = err;
125         return -1;
126     }
127 #endif
128
129     if ( fstat( fd, &st ) < 0 ) {
130         errno = err;
131         return -1;
132     }
133     
134     if (st.st_size > length) {
135         errno = err;
136         return -1;
137     }
138
139     if (lseek(fd, length -1, SEEK_SET) != length -1) {
140         errno = err;
141         return -1;
142     }
143
144     if (1 != write( fd, &c, 1 )) {
145         /* return the write errno */
146         return -1;
147     }
148
149 #ifndef  HAVE_PWRITE
150     if (curpos != lseek(fd, curpos,  SEEK_SET)) {
151         errno = err;
152         return -1;
153     }
154 #endif
155
156     return 0;    
157 }
158
159 /* ------------------------ */
160 int ad_rtruncate(struct adouble *ad, const char *uname, const off_t size)
161 {
162     EC_INIT;
163
164     /*
165      * We can't delete 0 byte size resource forks either, because a
166      * fork may reference the adouble handle with an open fd for the
167      * file, which means we would only delete the directory entry, not
168      * the file. Subsequently all code that works with fork handles
169      * finds the fork open, so eg flushing a fork (ad_flush()) will
170      * recreate ._ files.  The correct place to delete 0 byte sized
171      * resource forks is in of_closefork().
172      */
173
174     EC_NEG1( sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off) );
175
176     ad->ad_rlen = size;
177
178 EC_CLEANUP:
179     if (ret != 0)
180         LOG(log_error, logtype_ad, "ad_rtruncate(\"%s\"): %s",
181             fullpathname(uname), strerror(errno));
182     EC_EXIT;
183 }
184
185 int ad_dtruncate(struct adouble *ad, const off_t size)
186 {
187     if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {
188         LOG(log_error, logtype_ad, "sys_ftruncate(fd: %d): %s",
189             ad_data_fileno(ad), strerror(errno));
190         return -1;
191     }
192
193     return 0;
194 }
195
196 /* ----------------------- */
197 static int copy_all(const int dfd, const void *buf,
198                                size_t buflen)
199 {
200     ssize_t cc;
201
202     while (buflen > 0) {
203         if ((cc = write(dfd, buf, buflen)) < 0) {
204             switch (errno) {
205             case EINTR:
206                 continue;
207             default:
208                 return -1;
209             }
210         }
211         buflen -= cc;
212     }
213
214     return 0;
215 }
216
217 /* -------------------------- 
218  * copy only the fork data stream
219 */
220 int copy_fork(int eid, struct adouble *add, struct adouble *ads)
221 {
222     ssize_t cc;
223     int     err = 0;
224     char    filebuf[8192];
225     int     sfd, dfd;
226
227     if (eid == ADEID_DFORK) {
228         sfd = ad_data_fileno(ads);
229         dfd = ad_data_fileno(add);
230     }
231     else {
232         sfd = ad_reso_fileno(ads);
233         dfd = ad_reso_fileno(add);
234     }        
235
236     if ((off_t)-1 == lseek(sfd, ad_getentryoff(ads, eid), SEEK_SET))
237         return -1;
238
239     if ((off_t)-1 == lseek(dfd, ad_getentryoff(add, eid), SEEK_SET))
240         return -1;
241         
242 #if 0 /* ifdef SENDFILE_FLAVOR_LINUX */
243     /* doesn't work With 2.6 FIXME, only check for EBADFD ? */
244     off_t   offset = 0;
245     size_t  size;
246     struct stat         st;
247     #define BUF 128*1024*1024
248
249     if (fstat(sfd, &st) == 0) {
250         
251         while (1) {
252             if ( offset >= st.st_size) {
253                return 0;
254             }
255             size = (st.st_size -offset > BUF)?BUF:st.st_size -offset;
256             if ((cc = sys_sendfile(dfd, sfd, &offset, size)) < 0) {
257                 switch (errno) {
258                 case ENOSYS:
259                 case EINVAL:  /* there's no guarantee that all fs support sendfile */
260                     goto no_sendfile;
261                 default:
262                     return -1;
263                 }
264             }
265         }
266     }
267     no_sendfile:
268     lseek(sfd, offset, SEEK_SET);
269 #endif 
270
271     while (1) {
272         if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
273             if (errno == EINTR)
274                 continue;
275             err = -1;
276             break;
277         }
278
279         if (!cc || ((err = copy_all(dfd, filebuf, cc)) < 0)) {
280             break;
281         }
282     }
283     return err;
284 }