Path fun on Windows

Adrian Buehlmann adrian at cadifra.com
Wed Jun 6 15:02:43 CDT 2012


On 2012-06-06 21:04, Eduard-Cristian Stefan wrote:
> On Wed, Jun 6, 2012 at 3:15 PM, Adrian Buehlmann <adrian at cadifra.com> wrote:
>> BTW, we shouldn't reintroduce depending on win32api, but it shouldn't be
>> a problem to call the Windows API directly via Python's ctypes (see
>> mercurial/win32.py).
>   
> This works better on my PC [1] [2]:
> 
> import ctypes
> GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
> GetLongPathName.argtypes = [ctypes.c_wchar_p,
>                              ctypes.c_wchar_p,
>                              ctypes.c_uint]
> 
> 
> def getrealcase(path):
> 
>      buffsize = GetLongPathName(path, None, 0)
>      buff = ctypes.create_unicode_buffer(buffsize)
>      exitcode = GetLongPathName(path, buff, buffsize)
>      if exitcode == 0 or exitcode >= buffsize:
>          # something went the wrong way
>          return path
>      else:
>          return buff.value
> 
> 
> import os
> 
> path = os.path.normcase(os.environ["TEMP"])
> print 'in  1:', path
> print 'out 1:', getrealcase(path)
> print 'in  2:', 'C:'
> print 'out 2:', getrealcase('C:')
> print 'in  3:', 'c:'
> print 'out 3:', getrealcase('c:')
> 
> However, this is windows! The results are listed below:
> 
> in  1: c:\users\admini~1\appdata\local\temp
> out 1: c:\Users\Administrator\AppData\Local\Temp
> in  2: C:
> out 2: C:
> in  3: c:
> out 3: c:
> 
> It seems that all your drive letters are belong to us,
> so this might be a dead end.

Why? If it's only a problem with respect to the drive letter, then we
can probably work around it. In other words, we can surely uppercase all
drive letters.

I'll play with this. Haven't done so yet.

> Maybe the old way of normcasing all the paths
> might be better in this particular case for windows platform.
> 
> [1] http://stackoverflow.com/a/3694799/19756
> [2] http://msdn.microsoft.com/en-us/library/windows/desktop/aa364980%28v=vs.85%29.aspx



More information about the Mercurial-devel mailing list