cURL / Mailing Lists / curl-users / Single Mail


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

From: gdb007 <>
Date: Fri, 14 Dec 2007 23:27:09 +0800

On Dec 14, 2007 6:00 PM, Daniel Stenberg <> wrote:
> Hm, so then basically we can assume that _chsize_s() will work just as good as
> the _lseeki64() does? And since we use that unconditionally for all
> non-borland win32 builds, we could probably use _chsize_s() instead on those
> builds. Sounds like src/main.c:ftruncate64() can be made simpler then!
> Anyone with some insights? Or can someone possibly try it out and see?

Just made some testing, with result below:
dumpbin /exports c:\windows\system32\MSVCRT.DLL|grep -E
        287 11E 0005E3A1 _chsize
        288 11F 0005E28A _chsize_s
        427 1AA 0000AEE5 _get_osfhandle
        588 24B 00019E5C _lseeki64

dumpbin /exports MSVCRT.DLL|grep -E "chsize|lseeki64|get_osfhandle"
        178 B1 0001A66B _chsize
        246 F5 00012372 _get_osfhandle
        326 145 0001B6B8 _lseeki64

The first msvcrt.dll is from windows vista, version 7.0.6000, the
second one is downloaded from version 6.00.8397
(which should be from windows 98).
so i think _chsize_s won't work on old system like win98 unless you
have newer version of VC dlls installed on your system. the MSDN
document might have completely dropped support for win98 so you can't
get to know which API support those old system from it.

Also, tested with MS VC6.0 and VC express 2008(9.0), VC6.0 can link
_chsize(), _get_osfhandle(), _lseeki64() correctly, but failed on
_chsize_s, while VC 9.0 link fine with for all those functions.
i am using following code as ftruncate64() and it seems work fine for
me (built with VC6):

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

  if (!SetEndOfFile((HANDLE)_get_osfhandle(fd)))
    return -1;

  return 0;

The above function uses _get_osfhandle() as Brian Dessent said, also i
think some file seeking and 0 byte write() code is no longer needed
with SetEndOfFile() called so removed them. Yang Tse's ftruncate64()
should also work for the what curl need but there is no actual file
truncating in it.

One thing need to concern for above code is:
_get_osfhandle() is also some MS private non-standard CRT function, as
MS suggested in;en-us;99173
>The _get_osfhandle() C Run-time call is provided for informational
purposes only. Problems may occur if you read or write to the file
using the operating system handle returned from _get_osfhandle(); for
these reasons we recommend that you do not use the returned handle to
read or write to the file.

And i agree with Alessandro Vesely:
<It is an attempt to lock programmers into using MS products.

It is not easy and fun to make cross-platform programs, you did well
for making curl supporting so many platform already. and the decision
is up to you.
Received on 2007-12-14