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)
57 if (ad_data_fileno(ad) == AD_SYMLINK) {
62 LOG(log_debug, logtype_ad, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
63 (uintmax_t)off, buflen, ad->ad_rlen);
65 if ( eid == ADEID_DFORK ) {
67 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
70 off = st.st_size - off;
72 cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
73 } else if ( eid == ADEID_RFORK ) {
75 if (fstat( ad_reso_fileno(ad), &st ) < 0)
77 off = st.st_size - off - ad_getentryoff(ad, eid);
79 if (ad->ad_vers == AD_VERSION_EA) {
83 r_off = ADEDOFF_RFORK_OSX + off;
86 r_off = ad_getentryoff(ad, eid) + off;
88 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
90 if ( ad->ad_rlen < off + cc )
91 ad->ad_rlen = off + cc;
93 return -1; /* we don't know how to write if it's not a ressource or data fork */
102 * the caller set the locks
103 * ftruncate is undefined when the file length is smaller than 'size'
105 int sys_ftruncate(int fd, off_t length)
115 if (!ftruncate(fd, length)) {
118 /* maybe ftruncate doesn't work if we try to extend the size */
122 /* we only care about file pointer if we don't use pwrite */
123 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
129 if ( fstat( fd, &st ) < 0 ) {
134 if (st.st_size > length) {
139 if (lseek(fd, length -1, SEEK_SET) != length -1) {
144 if (1 != write( fd, &c, 1 )) {
145 /* return the write errno */
150 if (curpos != lseek(fd, curpos, SEEK_SET)) {
159 /* ------------------------ */
160 int ad_rtruncate(struct adouble *ad, const char *uname, const off_t size)
165 if (ad->ad_vers == AD_VERSION_EA && size == 0)
166 EC_NEG1( unlink(ad->ad_ops->ad_path(uname, 0)) );
169 EC_NEG1( sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off) );
175 LOG(log_error, logtype_ad, "ad_rtruncate(\"%s\"): %s",
176 fullpathname(uname), strerror(errno));
180 int ad_dtruncate(struct adouble *ad, const off_t size)
182 if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {
183 LOG(log_error, logtype_ad, "sys_ftruncate(fd: %d): %s",
184 ad_data_fileno(ad), strerror(errno));
191 /* ----------------------- */
192 static int copy_all(const int dfd, const void *buf,
198 if ((cc = write(dfd, buf, buflen)) < 0) {
212 /* --------------------------
213 * copy only the fork data stream
215 int copy_fork(int eid, struct adouble *add, struct adouble *ads)
222 if (eid == ADEID_DFORK) {
223 sfd = ad_data_fileno(ads);
224 dfd = ad_data_fileno(add);
227 sfd = ad_reso_fileno(ads);
228 dfd = ad_reso_fileno(add);
231 if ((off_t)-1 == lseek(sfd, ad_getentryoff(ads, eid), SEEK_SET))
234 if ((off_t)-1 == lseek(dfd, ad_getentryoff(add, eid), SEEK_SET))
237 #if 0 /* ifdef SENDFILE_FLAVOR_LINUX */
238 /* doesn't work With 2.6 FIXME, only check for EBADFD ? */
242 #define BUF 128*1024*1024
244 if (fstat(sfd, &st) == 0) {
247 if ( offset >= st.st_size) {
250 size = (st.st_size -offset > BUF)?BUF:st.st_size -offset;
251 if ((cc = sys_sendfile(dfd, sfd, &offset, size)) < 0) {
254 case EINVAL: /* there's no guarantee that all fs support sendfile */
263 lseek(sfd, offset, SEEK_SET);
267 if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
274 if (!cc || ((err = copy_all(dfd, filebuf, cc)) < 0)) {