2 * $Id: ad_write.c,v 1.9 2006-09-29 09:39:16 didg Exp $
4 * Copyright (c) 1990,1995 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #include <atalk/adouble.h>
15 #include <sys/param.h>
20 #define MIN(a,b) ((a)<(b)?(a):(b))
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( ad, eid, off, end, buf, buflen )
61 if ( eid == ADEID_DFORK ) {
63 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
66 off = st.st_size - off;
68 cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
69 } else if ( eid == ADEID_RFORK ) {
73 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));
85 if ( ad->ad_rlen < r_off + cc ) {
86 ad->ad_rlen = r_off + cc;
90 return -1; /* we don't know how to write if it's not a ressource or data fork */
96 * the caller set the locks
97 * ftruncate is undefined when the file length is smaller than 'size'
99 int sys_ftruncate(int fd, off_t length)
109 if (!ftruncate(fd, length)) {
112 /* maybe ftruncate doesn't work if we try to extend the size */
116 /* we only care about file pointer if we don't use pwrite */
117 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
123 if ( fstat( fd, &st ) < 0 ) {
128 if (st.st_size > length) {
133 if (lseek(fd, length -1, SEEK_SET) != length -1) {
138 if (1 != write( fd, &c, 1 )) {
139 /* return the write errno */
144 if (curpos != lseek(fd, curpos, SEEK_SET)) {
153 /* ------------------------ */
154 int ad_rtruncate( ad, size )
158 if ( sys_ftruncate( ad_reso_fileno(ad),
159 size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
167 int ad_dtruncate(ad, size)
171 if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {