2 * Copyright (c) 1990,1995 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
10 #include <atalk/adouble.h>
13 #include <sys/param.h>
18 #define MIN(a,b) ((a)<(b)?(a):(b))
21 /* XXX: locking has to be checked before each stream of consecutive
22 * ad_writes to prevent a lock in the middle from causing problems.
25 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
30 if ( ad_fd->adf_off != offset ) {
31 if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
34 ad_fd->adf_off = offset;
36 cc = write( ad_fd->adf_fd, buf, count );
42 cc = pwrite(ad_fd->adf_fd, buf, count, offset );
48 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)
53 if (ad_data_fileno(ad) == -2) {
59 if ( eid == ADEID_DFORK ) {
61 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
64 off = st.st_size - off;
66 cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
67 } else if ( eid == ADEID_RFORK ) {
71 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
74 off = st.st_size - off -ad_getentryoff(ad, eid);
76 r_off = ad_getentryoff(ad, eid) + off;
77 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
79 /* sync up our internal buffer FIXME always false? */
80 if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
81 memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
83 if ( ad->ad_rlen < off + cc ) {
84 ad->ad_rlen = off + cc;
88 return -1; /* we don't know how to write if it's not a ressource or data fork */
94 * the caller set the locks
95 * ftruncate is undefined when the file length is smaller than 'size'
97 int sys_ftruncate(int fd, off_t length)
107 if (!ftruncate(fd, length)) {
110 /* maybe ftruncate doesn't work if we try to extend the size */
114 /* we only care about file pointer if we don't use pwrite */
115 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
121 if ( fstat( fd, &st ) < 0 ) {
126 if (st.st_size > length) {
131 if (lseek(fd, length -1, SEEK_SET) != length -1) {
136 if (1 != write( fd, &c, 1 )) {
137 /* return the write errno */
142 if (curpos != lseek(fd, curpos, SEEK_SET)) {
151 /* ------------------------ */
152 int ad_rtruncate( struct adouble *ad, const off_t size)
154 if ( sys_ftruncate( ad_reso_fileno(ad),
155 size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
163 int ad_dtruncate(struct adouble *ad, const off_t size)
165 if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {