X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Fadouble%2Fad_write.c;h=35ebc222675f33787ca59ced3e7214ab9624db6c;hb=296eb8dfd48fb30bbc4b2371c3e1dabefe9d833d;hp=f5c4eee8aa27908a0edd9f05cf8bd6d9f0211534;hpb=55ff66657f3b5fcd4e7c0f112af315018747990b;p=netatalk.git diff --git a/libatalk/adouble/ad_write.c b/libatalk/adouble/ad_write.c index f5c4eee8..35ebc222 100644 --- a/libatalk/adouble/ad_write.c +++ b/libatalk/adouble/ad_write.c @@ -1,5 +1,5 @@ /* - * $Id: ad_write.c,v 1.7 2003-02-09 15:36:13 didg Exp $ + * $Id: ad_write.c,v 1.11 2010-03-30 12:55:26 franklahm Exp $ * * Copyright (c) 1990,1995 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -47,36 +47,36 @@ ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t off } /* end is always 0 */ -ssize_t ad_write( ad, eid, off, end, buf, buflen ) - struct adouble *ad; - const u_int32_t eid; - off_t off; - const int end; - const char *buf; - const size_t buflen; +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) { struct stat st; ssize_t cc; + + if (ad_data_fileno(ad) == -2) { + /* It's a symlink */ + errno = EACCES; + return -1; + } if ( eid == ADEID_DFORK ) { if ( end ) { - if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) { + if ( fstat( ad_data_fileno(ad), &st ) < 0 ) { return( -1 ); } off = st.st_size - off; } - cc = adf_pwrite(&ad->ad_df, buf, buflen, off); + cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off); } else if ( eid == ADEID_RFORK ) { off_t r_off; if ( end ) { - if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) { + if ( fstat( ad_data_fileno(ad), &st ) < 0 ) { return( -1 ); } off = st.st_size - off -ad_getentryoff(ad, eid); } r_off = ad_getentryoff(ad, eid) + off; - cc = adf_pwrite(&ad->ad_hf, buf, buflen, r_off); + cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off); /* sync up our internal buffer FIXME always false? */ if (r_off < ad_getentryoff(ad, ADEID_RFORK)) { @@ -94,27 +94,77 @@ ssize_t ad_write( ad, eid, off, end, buf, buflen ) /* * the caller set the locks - * we need tocopy and paste from samba code source/smbd/vfs_wrap.c * ftruncate is undefined when the file length is smaller than 'size' */ -int ad_rtruncate( ad, size ) - struct adouble *ad; - const off_t size; +int sys_ftruncate(int fd, off_t length) { - if ( ftruncate( ad->ad_hf.adf_fd, + +#ifndef HAVE_PWRITE +off_t curpos; +#endif +int err; +struct stat st; +char c = 0; + + if (!ftruncate(fd, length)) { + return 0; + } + /* maybe ftruncate doesn't work if we try to extend the size */ + err = errno; + +#ifndef HAVE_PWRITE + /* we only care about file pointer if we don't use pwrite */ + if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) { + errno = err; + return -1; + } +#endif + + if ( fstat( fd, &st ) < 0 ) { + errno = err; + return -1; + } + + if (st.st_size > length) { + errno = err; + return -1; + } + + if (lseek(fd, length -1, SEEK_SET) != length -1) { + errno = err; + return -1; + } + + if (1 != write( fd, &c, 1 )) { + /* return the write errno */ + return -1; + } + +#ifndef HAVE_PWRITE + if (curpos != lseek(fd, curpos, SEEK_SET)) { + errno = err; + return -1; + } +#endif + + return 0; +} + +/* ------------------------ */ +int ad_rtruncate( struct adouble *ad, const off_t size) +{ + if ( sys_ftruncate( ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) { - return( -1 ); + return -1; } ad->ad_rlen = size; - return( 0 ); + return 0; } -int ad_dtruncate(ad, size) - struct adouble *ad; - const off_t size; +int ad_dtruncate(struct adouble *ad, const off_t size) { - if (ftruncate(ad->ad_df.adf_fd, size) < 0) { + if (sys_ftruncate(ad_data_fileno(ad), size) < 0) { return -1; } return 0;