[PATCH] osutil.c: minimal Win32 implementation - no unicode and fancy exe flags

Adrian Buehlmann adrian at cadifra.com
Tue Sep 9 04:10:51 CDT 2008


On 09.09.2008 05:01, Petr Kodl wrote:
> # HG changeset patch
> # User Petr Kodl <petrkodl at gmail.com>
> # Date 1220929179 14400
> # Node ID 5d3d651b86c02a0b8b4fbfd022ebf9b71b6b5ec0
> # Parent  9141bebefe3ed0498ff07271214bfca8b4eb0d27
> osutil: implementation for Win32
> 
> diff -r 9141bebefe3e -r 5d3d651b86c0 mercurial/osutil.c
> --- a/mercurial/osutil.c	Mon Sep 08 14:22:14 2008 +0200
> +++ b/mercurial/osutil.c	Mon Sep 08 22:59:39 2008 -0400
> @@ -9,16 +9,37 @@
>  
>  #define _ATFILE_SOURCE
>  #include <Python.h>
> +#include <string.h>
> +#ifdef _WIN32
> +#include <windows.h>
> +#include <malloc.h>   /* for _alloca */
> +#else
>  #include <dirent.h>
>  #include <fcntl.h>
> -#include <string.h>
>  #include <sys/stat.h>
>  #include <sys/types.h>
>  #include <unistd.h>
> +#endif
> +
> +#ifdef _WIN32
> +/*
> +minimal stat struct compatible with hg expectations
> +supporting large files (64 bit size)
> +*/
> +struct non_posix_stat
> +{
> +	int     st_mode;
> +	__int64 st_size;
> +	int     st_mtime;
> +};
> +typedef struct non_posix_stat hg_stat;
> +#else
> +typedef struct stat hg_stat;
> +#endif
>  
>  struct listdir_stat {
>  	PyObject_HEAD
> -	struct stat st;
> +	hg_stat st;
>  };
>  
>  #define listdir_slot(name) \
> @@ -27,20 +48,32 @@
>          return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
>      }
>  
> +#ifndef _WIN32
>  listdir_slot(st_dev)
> +listdir_slot(st_nlink)
> +listdir_slot(st_ctime)
> +#endif 
> +listdir_slot(st_mtime)
>  listdir_slot(st_mode)
> -listdir_slot(st_nlink)
> +#ifdef _WIN32
> +static PyObject *listdir_stat_st_size(PyObject *self, void *x)
> +{
> +	return PyLong_FromLongLong(
> +		(PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size);
> +}
> +#else
>  listdir_slot(st_size)
> -listdir_slot(st_mtime)
> -listdir_slot(st_ctime)
> +#endif
>  
>  static struct PyGetSetDef listdir_stat_getsets[] = {
> -	{"st_dev", listdir_stat_st_dev, 0, 0, 0},
> -	{"st_mode", listdir_stat_st_mode, 0, 0, 0},
> +#ifndef _WIN32	
> +    {"st_dev", listdir_stat_st_dev, 0, 0, 0},
>  	{"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
> +	{"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
> +#endif
> +    {"st_mode", listdir_stat_st_mode, 0, 0, 0},
>  	{"st_size", listdir_stat_st_size, 0, 0, 0},
>  	{"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
> -	{"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
>  	{0, 0, 0, 0, 0}
>  };
>  
> @@ -95,6 +128,120 @@
>  	0,                         /* tp_alloc */
>  	listdir_stat_new,          /* tp_new */
>  };
> +
> +#ifdef _WIN32
> +
> +static int to_python_time(FILETIME* ms_time)
> +{
> +	/* this is number of 100-nanoseconds between epoch and January 1 1601 */
> +	static __int64 a0 = (__int64)134774L * (__int64)24L
> +						*(__int64)3600L * (__int64)1000L
> +						*(__int64)1000L * (__int64)10L;
> +	/* conversion factor back to 1s resolution required by Python ctime */
> +	static __int64 a1 = 1000 * 1000 * 10;
> +	__int64 tmp;
> +	memcpy(&tmp, ms_time, sizeof(__int64));
> +	return (int)((tmp - a0) / a1);
> +}
> +
> +static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
> +{
> +	PyObject *statobj = NULL,
> +        *list = NULL,
> +        *items = NULL,
> +        *ctor_args = NULL,
> +        *item0 = NULL,
> +        *item1 = NULL,
> +        *py_st = NULL;
> +	HANDLE fh = INVALID_HANDLE_VALUE;
> +	WIN32_FIND_DATAA fd;
> +	hg_stat* stp=NULL;
> +    char path[_MAX_PATH + 1];
> +    char *buffer=path;
> +    int plen = _MAX_PATH-2;
> +	int keepstat = 0;

Sigh. Thanks a lot for your contributions and suggestions, but this is
still an annoying mix of tab and spaces for indentation.

Either you or someone else would have to fix this before
this patch will be included.

Could you please do it like Matt and Patrick have said?

What editor are you using?

I can recommend the GPL licensed notepad++ for Windows (
http://notepad-plus.sourceforge.net/uk/site.htm )

notepad++ can show white space and tab characters (menu view), which
is very helpful to fix such errors.

If this is a problem for you to get right (whatever the reason
may be), I can offer the following procedure:

Send the patch to me by private email and I clean it
up for you and then send it back to you for final testing.
If you have tested the cleaned-up patch, you can then send it
to the list for review.

Let me know if you want to use this procedure and I'll start
doing it by applying it to this version of the patch.

Like that, we might be able to reduce the number of versions
of this patch people have to look at (and hopefully keep
Matt and crew interested).




More information about the Mercurial-devel mailing list