+int sys_ftruncate(int fd, off_t length)
+{
+
+#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 char *uname, const off_t size)
+{
+ EC_INIT;
+
+ /*
+ * We can't delete 0 byte size resource forks either, because a
+ * fork may reference the adouble handle with an open fd for the
+ * file, which means we would only delete the directory entry, not
+ * the file. Subsequently all code that works with fork handles
+ * finds the fork open, so eg flushing a fork (ad_flush()) will
+ * recreate ._ files. The correct place to delete 0 byte sized
+ * resource forks is in of_closefork().
+ */
+
+ EC_NEG1( sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off) );
+
+ ad->ad_rlen = size;
+
+EC_CLEANUP:
+ if (ret != 0)
+ LOG(log_error, logtype_ad, "ad_rtruncate(\"%s\"): %s",
+ fullpathname(uname), strerror(errno));
+ EC_EXIT;
+}
+
+int ad_dtruncate(struct adouble *ad, const off_t size)