git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 14/17] Add ftruncate implementation for MSVC

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 14, 2009, 19:48 UTC
Message-ID
<7vws41b946.fsf@alter.siamese.dyndns.org>
In-Reply-To
<c95a367520435feec0833d2d0edc33d86cf939a8.1252925290.git.mstormo@gmail.com>
Marius Storm-Olsen <mstormo@gmail.com> writes:
Show 25 quoted lines
> The MSVC Posix implementation doesn't contain ftruncate, so add our own
> which can handle large files (64bit offsets).
>
> Signed-off-by: Marius Storm-Olsen <mstormo@gmail.com>
> ---
>  compat/msvc.c |    8 ++++++++
>  compat/msvc.h |    2 ++
>  2 files changed, 10 insertions(+), 0 deletions(-)
>
> diff --git a/compat/msvc.c b/compat/msvc.c
> index ac04a4c..b96b045 100644
> --- a/compat/msvc.c
> +++ b/compat/msvc.c
> @@ -32,4 +32,12 @@ int closedir(DIR *dir)
>  	return 0;
>  }
>  
> +int ftruncate(int fd, __int64 length)
> +{
> +	HANDLE fh = (HANDLE)_get_osfhandle(fd);
> +	if (!fh || _lseeki64(fd, length, SEEK_SET))
> +		return -1;
> +	return SetEndOfFile(fh) ? 0 : -1;
> +}
> +

Wouldn't it break this toy program? ftruncate() should preserve the file offset and use of the seek in the above feels iffy.

#include <stdlib.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h>

int main(int ac, char **av)
{
        int fd = open("testfile", O_CREAT|O_WRONLY, 0666);
        if (fd < 0)
                exit(1);
        write(fd, "abcdefghijklmnopqrstuvwxyz\n", 27);
        lseek(fd, 7, SEEK_SET);
        write(fd, "H", 1);
        ftruncate(fd, 24);
        write(fd, "IJ", 2);
        close(fd);
        exit(0);
}

The only two in-tree users of ftruncate() do not care about this, though. They both seek to the beginning and then truncate to zero length.

Previous: Marius Storm-OlsenNext: Junio C Hamano
Message 23 of 35 in “Build Git with MSVC”
  1. 00/17 Build Git with MSVCMarius Storm-Olsen, Sep 14, 2009
  2. 01/17 Avoid declaration after statementMarius Storm-Olsen, Sep 14, 2009
  3. 02/17 boolean is a typedef under MSVC, so rename variable to 'i_boolean'Marius Storm-Olsen, Sep 14, 2009
  4. 03/17 Define SNPRINTF_SIZE_CORR=1 for Microsoft Visual C++Marius Storm-Olsen, Sep 14, 2009
  5. 04/17 Add define guards to compat/win32.hMarius Storm-Olsen, Sep 14, 2009
  6. 05/17 Change regerror() declaration from K&R style to ANSI C (C89)Marius Storm-Olsen, Sep 14, 2009
  7. 06/17 mingw.c: Use the O_BINARY flag to open filesMarius Storm-Olsen, Sep 14, 2009
  8. 07/17 Fix __stdcall/WINAPI placement and function prototypeMarius Storm-Olsen, Sep 14, 2009
  9. 08/17 Test for WIN32 instead of __MINGW32_Marius Storm-Olsen, Sep 14, 2009
  10. 09/17 Add empty header files for MSVC portMarius Storm-Olsen, Sep 14, 2009
  11. 10/17 Add MinGW header files to build git with MSVCMarius Storm-Olsen, Sep 14, 2009
  12. 11/17 Add platform files for MSVC portingMarius Storm-Olsen, Sep 14, 2009
  13. 12/17 Make usage of windows.h lean and meanMarius Storm-Olsen, Sep 14, 2009
  14. 13/17 Define strncasecmp as _strnicmp for MSVCMarius Storm-Olsen, Sep 14, 2009
  15. 14/17 Add ftruncate implementation for MSVCMarius Storm-Olsen, Sep 14, 2009
  16. 15/17 Add MSVC to MakefileMarius Storm-Olsen, Sep 14, 2009
  17. 16/17 Add README for MSVC buildMarius Storm-Olsen, Sep 14, 2009
  18. 17/17 Add scripts to generate projects for other buildsystems (MSVC vcproj, QMake)Marius Storm-Olsen, Sep 14, 2009
  19. Alex RiesenSep 14, 2009
  20. Marius Storm-OlsenSep 14, 2009
  21. Alex RiesenSep 14, 2009
  22. Marius Storm-OlsenSep 14, 2009
  23. Junio C HamanoSep 14, 2009
  24. Junio C HamanoSep 14, 2009
  25. Marius Storm-OlsenSep 14, 2009
  26. Johannes SixtSep 14, 2009
  27. Johannes SchindelinSep 15, 2009
  28. Marius Storm-OlsenSep 15, 2009
  29. Alexey BorzenkovSep 14, 2009
  30. Marius Storm-OlsenSep 14, 2009
  31. Alex RiesenSep 14, 2009
  32. Marius Storm-OlsenSep 14, 2009
  33. Frank LiSep 14, 2009
  34. Alex RiesenSep 14, 2009
  35. Marius Storm-OlsenSep 14, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.