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 ) {
76 switch (ad->ad_vers) {
80 if ( fstat( ad_data_fileno(ad), &st ) < 0 )
82 off = st.st_size - off -ad_getentryoff(ad, eid);
84 r_off = ad_getentryoff(ad, eid) + off;
85 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
87 /* sync up our internal buffer FIXME always false? */
88 if (r_off < ad_getentryoff(ad, ADEID_RFORK))
89 memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
90 if ( ad->ad_rlen < off + cc )
91 ad->ad_rlen = off + cc;
96 LOG(log_debug, logtype_default, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
97 (uintmax_t)off, buflen, ad->ad_rlen);
98 if (ad->ad_rlen == 0) {
99 EC_NULL_LOG( ad->ad_resforkbuf = malloc(off + buflen) );
100 ad->ad_rlen = off + buflen;
101 } else if ((off + buflen) > ad->ad_rlen) {
102 EC_NULL_LOG( ad->ad_resforkbuf = realloc(ad->ad_resforkbuf, off + buflen) );
103 ad->ad_rlen = off + buflen;
106 memcpy(ad->ad_resforkbuf + off, buf, buflen);
108 if (fsetrsrcea(ad, ad_data_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
111 LOG(log_debug, logtype_default, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
112 (uintmax_t)off, buflen, ad->ad_rlen);
120 return -1; /* we don't know how to write if it's not a ressource or data fork */
130 * the caller set the locks
131 * ftruncate is undefined when the file length is smaller than 'size'
133 int sys_ftruncate(int fd, off_t length)
143 if (!ftruncate(fd, length)) {
146 /* maybe ftruncate doesn't work if we try to extend the size */
150 /* we only care about file pointer if we don't use pwrite */
151 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
157 if ( fstat( fd, &st ) < 0 ) {
162 if (st.st_size > length) {
167 if (lseek(fd, length -1, SEEK_SET) != length -1) {
172 if (1 != write( fd, &c, 1 )) {
173 /* return the write errno */
178 if (curpos != lseek(fd, curpos, SEEK_SET)) {
187 /* ------------------------ */
188 int ad_rtruncate( struct adouble *ad, const off_t size)
190 if (ad->ad_vers != AD_VERSION_EA)
191 if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
199 int ad_dtruncate(struct adouble *ad, const off_t size)
201 if (sys_ftruncate(ad_data_fileno(ad), size) < 0)