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>
21 #define MIN(a,b) ((a)<(b)?(a):(b))
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.
28 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
33 if ( ad_fd->adf_off != offset ) {
34 if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
37 ad_fd->adf_off = offset;
39 cc = write( ad_fd->adf_fd, buf, count );
45 cc = pwrite(ad_fd->adf_fd, buf, count, offset );
51 ssize_t ad_write(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
56 if (ad_data_fileno(ad) == -2) {
62 if ( eid == ADEID_DFORK ) {
64 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
67 off = st.st_size - off;
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) {
74 if ( fstat( ad_data_fileno(ad), &st ) < 0 )
76 off = st.st_size - off -ad_getentryoff(ad, eid);
78 r_off = ad_getentryoff(ad, eid) + off;
79 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
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 size_t roundup = (((off + buflen) / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
89 if ((ad->ad_resforkbuf = realloc(roundup)) == NULL)
91 ad->ad_resforkbufsize = roundup;
93 memcpy(ad->ad_resforkbuf + off, buf, buflen);
94 if ((off + buflen) > ad->ad_rlen)
95 ad->ad_rlen = off + buflen;
97 if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
102 return -1; /* we don't know how to write if it's not a ressource or data fork */
109 * the caller set the locks
110 * ftruncate is undefined when the file length is smaller than 'size'
112 int sys_ftruncate(int fd, off_t length)
122 if (!ftruncate(fd, length)) {
125 /* maybe ftruncate doesn't work if we try to extend the size */
129 /* we only care about file pointer if we don't use pwrite */
130 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
136 if ( fstat( fd, &st ) < 0 ) {
141 if (st.st_size > length) {
146 if (lseek(fd, length -1, SEEK_SET) != length -1) {
151 if (1 != write( fd, &c, 1 )) {
152 /* return the write errno */
157 if (curpos != lseek(fd, curpos, SEEK_SET)) {
166 /* ------------------------ */
167 int ad_rtruncate( struct adouble *ad, const off_t size)
169 if (ad->ad_flags != AD_VERSION_EA)
170 if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
178 int ad_dtruncate(struct adouble *ad, const off_t size)
180 if (sys_ftruncate(ad_data_fileno(ad), size) < 0)