2 * $Id: ad_write.c,v 1.10 2009-10-13 22:55:37 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(struct adouble *ad, const u_int32_t eid, off_t off, const int end, const char *buf, const size_t buflen)
55 if ( eid == ADEID_DFORK ) {
57 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
60 off = st.st_size - off;
62 cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
63 } else if ( eid == ADEID_RFORK ) {
67 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
70 off = st.st_size - off -ad_getentryoff(ad, eid);
72 r_off = ad_getentryoff(ad, eid) + off;
73 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
75 /* sync up our internal buffer FIXME always false? */
76 if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
77 memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
79 if ( ad->ad_rlen < r_off + cc ) {
80 ad->ad_rlen = r_off + cc;
84 return -1; /* we don't know how to write if it's not a ressource or data fork */
90 * the caller set the locks
91 * ftruncate is undefined when the file length is smaller than 'size'
93 int sys_ftruncate(int fd, off_t length)
103 if (!ftruncate(fd, length)) {
106 /* maybe ftruncate doesn't work if we try to extend the size */
110 /* we only care about file pointer if we don't use pwrite */
111 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
117 if ( fstat( fd, &st ) < 0 ) {
122 if (st.st_size > length) {
127 if (lseek(fd, length -1, SEEK_SET) != length -1) {
132 if (1 != write( fd, &c, 1 )) {
133 /* return the write errno */
138 if (curpos != lseek(fd, curpos, SEEK_SET)) {
147 /* ------------------------ */
148 int ad_rtruncate( struct adouble *ad, const off_t size)
150 if ( sys_ftruncate( ad_reso_fileno(ad),
151 size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
159 int ad_dtruncate(struct adouble *ad, const off_t size)
161 if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {