2

Task and the background:
To create, read back, remove links to directories (as of now) in win32 platform.
The approach that am following in my python code is, in order
a. Tries to create real symlink, if supported (via mklink cmd)
b. If unsupported, falls back to creating junction pt, if it's supported(via linkd cmd)
Reading and removal follows same approach.

Problem:
Creation and removal of both symlink and junc pt is straightforward, but i couldn't figure out how to read the target i.e. pointing directory.

Progress:
a. For junction points, i can use 'linkd ' and parse the stdout in an ugly manner to get target dir.
b. For symlink there seem to be no such solution.
c. I see that there is native support in python 3.1.1[1] for win32 symlinks but am using v2.7.1 and can't upgrade now. d. There is method in pywin32 module to create symlink but nothing i could see to read it back

p.s. am pretty new to Python so if there is an alternative approach to easily accomplish this, please let me know.

[1] [http://bugs.python.org/issue1578269][1]

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Hari
  • 384
  • 1
  • 6
  • 20

3 Answers3

1

It's all about reparse points.

This Code Project article should give you the information you need. You'll have to translate it into Python with either ctypes or win32api.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks David. Fortunately, i got hold of a python module http://pypi.python.org/pypi/jaraco.windows to read back symlinks. However it doesn't seem to be supporting junction points. Am not good enough in Python to do the translation that you mentioned, but will give a try. Btw, if any one could point me any existing python module to do the same it would be great. – Hari Mar 23 '11 at 17:25
0

Win32file library doesn't work for me too. But if you have Junction you can use this (just change Junction path to fit yours):

def getOriginalPath(junctionPath):
    # TCPROGS - system env. variable
    cmd = subprocess.Popen('%TCPROGS%\Junction\junction.exe '+junctionPath, shell=True, stdout=subprocess.PIPE)
    for line in cmd.stdout:
        if line.find('Substitute Name:') >= 0:
            start = line.find(':')
            return line[start+1:].strip()
            break

You could also parse DIR /A:L output if you don't want to depend on Junction.

Nux
  • 9,276
  • 5
  • 59
  • 72
0

You'll find a snippet for reading symbolic links using the win32file module here, in addition to a few posts on how to create symbolic links using the ctypes module. I've also written a Python C-Extension here implementing junctions, symbolic links, and hard links on Windows, though I should warn you that it needs a serious overhaul. (Wrote it before really reading up on Python C-Extensions, so I didn't really put appropriate Py_XREF, etcs in place)

There's also an implementation of reading, writing, and checking junctions using just the ctypes module somewhere in that repository.

Community
  • 1
  • 1
Charles Grunwald
  • 1,441
  • 18
  • 22