cURL / Mailing Lists / curl-users / Single Mail

curl-users

Re: Bug report: curl win32 ftruncate64 not working correctly.

From: Yang Tse <yangsita_at_gmail.com>
Date: Fri, 14 Dec 2007 14:39:09 +0100

My 2 cents...

All line number references are relative to
http://cool.haxx.se/cvs.cgi/curl/src/main.c?annotate=1.437

POSIX ftruncate() truncates the file but does not move the file
pointer. This is the reason for line 4634 trying to reposition the
file pointer to the new truncated position.

Our ftruncate64() tries to emulate POSIX ftruncate() keeping the file
pointer at its original position. BUT this is the actual problem. See
line 339. It shouldn't move the file pointer to its original position,
just keep it at the truncated position. Otherwise no truncation will
actually happen.

Since we only use ftruncate() to truncate the file and not extend it
beyond its actual end.

The following ftruncate64() should do the trick:

static int ftruncate64 (int fd, curl_off_t where)
{
  if(_lseeki64(fd, 0, SEEK_CUR) < 0)
    return -1;

  if (_lseeki64(fd, where, SEEK_SET) < 0)
    return -1;

  if(_write(fd, 0, 0) < 0)
    return -1;

  return 0:
}

But it shoul be taken into consideration that the lines 4634 and 4640
should not be executed when our ftruncate64() is used.

So most probably a private function called ftruncate_and_set_fp() or
something similar should be used to wrap POSIX and WIN32
functionality.

-- 
-=[Yang]=-
Received on 2007-12-14