Search code examples
windowsfileserverjunction

Code to determine target of remote junction


Windows 7/NTFS and later has both symbolic links and junctions, and they are subtly different. (See this excellent set of posts). For logging (and debugging) purposes, I need to be able to resolve the target of a junction on a remote file server. There have been some posts on this topic, but they apply to resolving a junction as the local machine see it.

On \\FileServer, we have a directory G:\Shared that is shared out as PublicShare, so it apears on the network as \\FileServer\PublicShare. Within that directory are sub-directories G:\Shared\SubDir1 and G:\Shared\SubDir2. Also within that directory is a junction G:\Shared\Junc that points to either G:\Shared\SubDir1 or G:\Shared\SubDir2. (The target can change.) Thus, on \\FileServer, one sees

\\FileServer\PublicShare\SubDir1
\\FileServer\PublicShare\SubDir2
\\FileServer\PublicShare\Junc

On a client machine where \\FileServer\PublicShare is mounted (mapped in Windows lingo) as M:\, one thus sees M:\SubDir1, M:\SubDir2, M:\Junc. If, on that client machine, you open a console (cmd.exe) and do dir M:\, Windows gives a nice listing which shows that M:\Junc is a junction and includes the target, G:\Shared\SubDirX, of the junction.

M:\>dir
 Volume in drive M is XXXXXXXXX
 Volume Serial Number is XXXX-XXXX

 Directory of M:\

09/05/2014  07:30 PM    <DIR>          .
09/05/2014  07:30 PM    <DIR>          ..
09/05/2014  01:36 PM    <JUNCTION>     Junc [G:\Shared\SubDir1]
09/06/2014  12:55 PM    <DIR>          SubDir1
09/05/2014  05:15 PM    <DIR>          SubDir2
               0 File(s)              0 bytes
               3 Dir(s)  1,895,493,492,736 bytes free

M:\>

Thus, the target of such a junction is clearly available to a client. Does anyone know how to obtain this information programatically, e.g. which system api to call??? Thanks.


Solution

  • This does the trick:

    fsutil reparsepoint query "M:\Junc"
    

    and it seems very reliable.