Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C++

Windows 2000 Junction Points

Rate me:
Please Sign up or sign in to vote.
4.93/5 (42 votes)
5 Jan 2000 444.8K   3K   83  
Explains how reparse points are used to create filesystem links
// Copyright 2000 Mikael Nordell (tamlin@algonet.se)
// Distributed without warranties. Use as you see fit, except for
// commercial purposes.
#include "../FSLinks/FSLinks.h"
#include <cstdio>
#include <cstdlib>	// definition of _MAX_PATH
#include <winioctl.h>
#include <tchar.h>


using namespace FSLinks;


void usage()
{
	const char szUsage[] =
		"MakeLink - Copyright 2000 Mikael Nordell\n"
		"\n"
		"Displays and/or modifies NTFS5 Junction Points\n"
		"\n"
		"MakeLink [junction where_to] [/L junction] [/D junction]\n"
		"\n"
		"  /D  Deletes Junction Point from specified junction directory.\n"
		"  /L  Lists where junction points to\n"
		"\n"
		"Examples:\n"
		"  MakeLink D:\\foo \"C:\\Program Files\"\n"
		"  MakeLink /L D:\\foo\n"
		"  MakeLink /D D:\\foo\n"
		"\n"
		"To mount a complete volume, use MOUNTVOL or Disk Administrator.\n";
	puts(szUsage);
}

void PrintJunctionInfo(LPCTSTR szSrc)
{
	const int cchDest = 512;
	TCHAR szDest[cchDest];
	
	const DWORD dwRet = GetJunctionPointInfo(szSrc, szDest, cchDest);
	if (dwRet == 0 || dwRet > cchDest) {
		printf("  Failed to get junction info for dir ");
		_tprintf(TEXT("\"%s\"\n"), szSrc);
		return;
	}

	printf("Junction points to ");
	_tprintf(TEXT("\"%s\"\n"), szDest);
}


extern "C" { void _setenvp() {} }

int _tmain(int argc, TCHAR *argv[])
{
	if (argc != 3) {
		usage();
		return 0;
	}

	if ((argv[1][0] == '/' || argv[1][0] == '-')) {
		switch (argv[1][1]) {
			case 'D':
			case 'd':
				PrintJunctionInfo(argv[2]);
				puts("");
				if (DeleteJunctionPoint(argv[2])) {
					puts("Deleted the above junction point.");
				} else {
					printf("Couldn't delete junction point ");
					_tprintf(TEXT("\"%s\"\n"), argv[2]);
				}
				break;

			case 'L':
			case 'l':
				PrintJunctionInfo(argv[2]);
				break;

			default:
				printf("Unknown option ");
				_tprintf(TEXT("%s\n"), argv[1]);
				usage();
		}
		return 0;
	}

	LPCTSTR szSrc = argv[1];

	TCHAR szVol[4] = TEXT("x:\\");
	if (szSrc[1] == ':') {
		szVol[0] = szSrc[0];
	} else {
        TCHAR curdirstr[_MAX_PATH];
        if (GetCurrentDirectory(sizeof(curdirstr), curdirstr)) {
			if (curdirstr[1] == ':') {
				szVol[0] = curdirstr[0];
			}
		}
	}

	DWORD dwCompLen;
	DWORD dwFsFlags;
	if (!GetVolumeInformation(szVol, 0, 0, 0, &dwCompLen, &dwFsFlags, 0, 0)) {
		const DWORD dwErr = GetLastError();
		printf("  GetVolumeInformation for ");
		_tprintf(TEXT("\"%s\""), szVol);
		printf(" failed (%d)!\n", dwErr);
		return -1;
	}

	szVol[2] = 0;

	if (!(dwFsFlags & FILE_SUPPORTS_REPARSE_POINTS)) {
		printf("  The filesystem on ");
		_tprintf(TEXT("\"%s\""), szVol);
		puts(" does not support Reparse Points");
		return -1;
	}

	const DWORD dwFA = GetFileAttributes(szSrc);
	if (dwFA == 0xffffffff) {
		printf("  Can't get info for ");
		_tprintf(TEXT("\"%s\""), szSrc);
		puts("\n  Does the directory really exist?");
		return -1;
	}

	int nRet = 0;
	if (!CreateJunctionPoint(argv[1], argv[2])) {
		printf("Couldn't create link ");
		nRet = -1;
	} else {
		printf("Created link ");
	}
	_tprintf(TEXT("\"%s\" -> \"%s\"\n"), argv[1], argv[2]);

	return nRet;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions