2 * Copyright (c) 1990,1995 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
15 #include <atalk/adouble.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>
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.
27 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
32 if ( ad_fd->adf_off != offset ) {
33 if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
36 ad_fd->adf_off = offset;
38 cc = write( ad_fd->adf_fd, buf, count );
44 cc = pwrite(ad_fd->adf_fd, buf, count, offset );
50 ssize_t ad_write(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
58 if (ad_data_fileno(ad) == -2) {
64 LOG(log_debug, logtype_default, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
65 (uintmax_t)off, buflen, ad->ad_rlen);
67 if ( eid == ADEID_DFORK ) {
69 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
72 off = st.st_size - off;
74 cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
75 } else if ( eid == ADEID_RFORK ) {
77 if (fstat( ad_reso_fileno(ad), &st ) < 0)
79 off = st.st_size - off - ad_getentryoff(ad, eid);
81 if (ad->ad_vers == AD_VERSION_EA) {
85 r_off = ADEDOFF_RFORK_OSX + off;
88 r_off = ad_getentryoff(ad, eid) + off;
90 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
92 if ( ad->ad_rlen < off + cc )
93 ad->ad_rlen = off + cc;
95 return -1; /* we don't know how to write if it's not a ressource or data fork */
105 * the caller set the locks
106 * ftruncate is undefined when the file length is smaller than 'size'
108 int sys_ftruncate(int fd, off_t length)
118 if (!ftruncate(fd, length)) {
121 /* maybe ftruncate doesn't work if we try to extend the size */
125 /* we only care about file pointer if we don't use pwrite */
126 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
132 if ( fstat( fd, &st ) < 0 ) {
137 if (st.st_size > length) {
142 if (lseek(fd, length -1, SEEK_SET) != length -1) {
147 if (1 != write( fd, &c, 1 )) {
148 /* return the write errno */
153 if (curpos != lseek(fd, curpos, SEEK_SET)) {
162 /* ------------------------ */
163 int ad_rtruncate( struct adouble *ad, const off_t size)
168 if (ad->ad_vers == AD_VERSION_EA && size == 0)
169 EC_NEG1( unlink(ad->ad_ops->ad_path(ad->ad_name, 0)) );
172 EC_NEG1( sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off) );
178 LOG(log_error, logtype_default, "ad_rtruncate(\"%s\"): %s",
179 fullpathname(ad->ad_name), strerror(errno));
183 int ad_dtruncate(struct adouble *ad, const off_t size)
185 if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {
186 LOG(log_error, logtype_default, "sys_ftruncate(fd: %d): %s",
187 ad_data_fileno(ad), strerror(errno));
194 /* ----------------------- */
195 static int copy_all(const int dfd, const void *buf,
201 if ((cc = write(dfd, buf, buflen)) < 0) {
215 /* --------------------------
216 * copy only the fork data stream
218 int copy_fork(int eid, struct adouble *add, struct adouble *ads)
225 if (eid == ADEID_DFORK) {
226 sfd = ad_data_fileno(ads);
227 dfd = ad_data_fileno(add);
230 sfd = ad_reso_fileno(ads);
231 dfd = ad_reso_fileno(add);
234 if ((off_t)-1 == lseek(sfd, ad_getentryoff(ads, eid), SEEK_SET))
237 if ((off_t)-1 == lseek(dfd, ad_getentryoff(add, eid), SEEK_SET))
240 #if 0 /* ifdef SENDFILE_FLAVOR_LINUX */
241 /* doesn't work With 2.6 FIXME, only check for EBADFD ? */
245 #define BUF 128*1024*1024
247 if (fstat(sfd, &st) == 0) {
250 if ( offset >= st.st_size) {
253 size = (st.st_size -offset > BUF)?BUF:st.st_size -offset;
254 if ((cc = sys_sendfile(dfd, sfd, &offset, size)) < 0) {
257 case EINVAL: /* there's no guarantee that all fs support sendfile */
266 lseek(sfd, offset, SEEK_SET);
270 if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
277 if (!cc || ((err = copy_all(dfd, filebuf, cc)) < 0)) {