2016-06-29 00:38:51 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Subst Command
|
2011-04-18 21:48:19 +00:00
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: base/system/subst/subst.c
|
2016-06-29 00:38:51 +00:00
|
|
|
* PURPOSE: Maps a path with a drive letter
|
2011-04-18 21:48:19 +00:00
|
|
|
* PROGRAMMERS: Sam Arun Raj
|
2016-06-29 00:38:51 +00:00
|
|
|
* Peter Hater
|
|
|
|
* Hermes Belusca-Maito
|
2011-04-18 21:48:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
#include <stdio.h>
|
2016-06-28 20:22:07 +00:00
|
|
|
|
|
|
|
#define WIN32_NO_STATUS
|
2013-01-24 23:00:42 +00:00
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
2016-10-07 20:37:36 +00:00
|
|
|
|
|
|
|
#include <conutils.h>
|
2013-01-24 23:00:42 +00:00
|
|
|
|
2011-04-25 16:08:00 +00:00
|
|
|
#include "resource.h"
|
2011-04-18 21:48:19 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
VOID PrintError(IN DWORD ErrCode)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-10-07 20:44:15 +00:00
|
|
|
// DWORD dwLength = 0;
|
2016-10-07 20:37:36 +00:00
|
|
|
PWSTR pMsgBuf = NULL;
|
2016-06-29 00:38:51 +00:00
|
|
|
|
2016-10-07 20:37:36 +00:00
|
|
|
#if 0
|
|
|
|
if (ErrCode == ERROR_SUCCESS)
|
2016-06-29 00:38:51 +00:00
|
|
|
return;
|
2016-10-07 20:37:36 +00:00
|
|
|
#endif
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-10-07 20:37:36 +00:00
|
|
|
/* Retrieve the message string without appending extra newlines */
|
2016-10-07 20:44:15 +00:00
|
|
|
// dwLength =
|
|
|
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
|
|
|
NULL,
|
|
|
|
ErrCode,
|
|
|
|
LANG_USER_DEFAULT,
|
|
|
|
(PWSTR)&pMsgBuf,
|
|
|
|
0, NULL);
|
2016-10-07 20:37:36 +00:00
|
|
|
if (pMsgBuf /* && dwLength */)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_FAILED_WITH_ERRORCODE,
|
|
|
|
ErrCode, pMsgBuf);
|
|
|
|
LocalFree(pMsgBuf);
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
ULONG QuerySubstedDrive(IN WCHAR DriveLetter,
|
|
|
|
IN OUT PWSTR* TargetPath OPTIONAL,
|
|
|
|
IN OUT PULONG Size)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
ULONG Result = ERROR_INVALID_DRIVE;
|
|
|
|
WCHAR Drive[] = L"A:";
|
|
|
|
DWORD dwSize, CharCount = 0;
|
|
|
|
PWSTR lpTargetPath = NULL, tmp;
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-28 20:22:07 +00:00
|
|
|
Drive[0] = DriveLetter;
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
/* Check whether the user has given a pointer to a target path buffer */
|
|
|
|
if (!TargetPath)
|
|
|
|
{
|
|
|
|
/* No, therefore use a local buffer */
|
|
|
|
dwSize = MAX_PATH;
|
|
|
|
lpTargetPath = (PWSTR)HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
|
|
|
|
if (!lpTargetPath)
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just use the user-given pointer to a buffer; Size should point to a valid ULONG */
|
|
|
|
if (!Size)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
lpTargetPath = *TargetPath;
|
|
|
|
dwSize = *Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Retry:
|
|
|
|
/* Try querying DOS device information */
|
|
|
|
CharCount = QueryDosDeviceW(Drive, lpTargetPath, dwSize);
|
|
|
|
if (!CharCount)
|
|
|
|
Result = GetLastError();
|
2016-06-28 19:21:08 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!CharCount && (Result == ERROR_INSUFFICIENT_BUFFER))
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
/* Reallocate the buffer with double size */
|
|
|
|
dwSize *= 2;
|
|
|
|
tmp = (PWSTR)HeapReAlloc(GetProcessHeap(), 0, lpTargetPath, dwSize * sizeof(WCHAR));
|
|
|
|
if (!tmp)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
/* Memory problem, bail out */
|
|
|
|
CharCount = 0;
|
|
|
|
Result = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Retry again */
|
|
|
|
lpTargetPath = tmp;
|
|
|
|
goto Retry;
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
2016-06-28 19:21:08 +00:00
|
|
|
}
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-28 19:21:08 +00:00
|
|
|
if (CharCount)
|
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
if ( wcsncmp(lpTargetPath, L"\\??\\", 4) == 0 &&
|
|
|
|
( (lpTargetPath[4] >= L'A' && lpTargetPath[4] <= L'Z') ||
|
|
|
|
(lpTargetPath[4] >= L'a' && lpTargetPath[4] <= L'z') ) )
|
|
|
|
{
|
|
|
|
/* The drive exists and is SUBSTed */
|
|
|
|
Result = ERROR_IS_SUBSTED;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The drive exists but is not SUBSTed */
|
|
|
|
Result = ERROR_INVALID_DRIVE;
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
2016-06-28 19:21:08 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!TargetPath)
|
|
|
|
{
|
|
|
|
/* Free the local buffer */
|
|
|
|
HeapFree(GetProcessHeap(), 0, lpTargetPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Update the user-given pointers */
|
|
|
|
*TargetPath = lpTargetPath;
|
|
|
|
*Size = dwSize;
|
|
|
|
}
|
2016-06-28 19:21:08 +00:00
|
|
|
|
2011-04-18 21:48:19 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-06-28 20:22:07 +00:00
|
|
|
VOID DumpSubstedDrives(VOID)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
WCHAR DriveLetter;
|
|
|
|
PWSTR lpTargetPath = NULL;
|
|
|
|
DWORD dwSize;
|
|
|
|
UCHAR i = 0;
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-28 19:21:08 +00:00
|
|
|
dwSize = MAX_PATH;
|
2016-06-29 00:38:51 +00:00
|
|
|
lpTargetPath = (PWSTR)HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
|
2016-06-28 19:21:08 +00:00
|
|
|
if (!lpTargetPath)
|
2011-04-18 21:48:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
while (i < 26)
|
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
DriveLetter = L'A' + i;
|
|
|
|
if (QuerySubstedDrive(DriveLetter, &lpTargetPath, &dwSize) == ERROR_IS_SUBSTED)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConPrintf(StdOut, L"%c:\\: => %s\n", DriveLetter, lpTargetPath + 4);
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
2016-06-28 19:21:08 +00:00
|
|
|
|
2011-04-18 21:48:19 +00:00
|
|
|
i++;
|
|
|
|
}
|
2016-06-28 19:21:08 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
HeapFree(GetProcessHeap(), 0, lpTargetPath);
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
INT DeleteSubst(IN PWSTR Drive)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
DWORD dwResult;
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if ((wcslen(Drive) != 2) || (Drive[1] != L':'))
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
dwResult = ERROR_INVALID_PARAMETER;
|
|
|
|
goto Quit;
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if (QuerySubstedDrive(Drive[0], NULL, NULL) != ERROR_IS_SUBSTED)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
dwResult = ERROR_INVALID_PARAMETER;
|
|
|
|
goto Quit;
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!DefineDosDeviceW(DDD_REMOVE_DEFINITION, Drive, NULL))
|
|
|
|
dwResult = GetLastError();
|
|
|
|
else
|
|
|
|
dwResult = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
Quit:
|
|
|
|
switch (dwResult)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
case ERROR_SUCCESS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// case ERROR_INVALID_DRIVE:
|
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_INVALID_PARAMETER2, Drive);
|
2016-06-29 00:38:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ACCESS_DENIED, Drive);
|
2016-06-29 00:38:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
PrintError(GetLastError());
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
2016-06-29 00:38:51 +00:00
|
|
|
|
2011-04-18 21:48:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
INT AddSubst(IN PWSTR Drive, IN PWSTR Path)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
DWORD dwResult, dwPathAttr;
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if ((wcslen(Drive) != 2) || (Drive[1] != L':'))
|
2011-04-25 16:08:00 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
dwResult = ERROR_INVALID_PARAMETER;
|
|
|
|
goto Quit;
|
2011-04-25 16:08:00 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 20:22:07 +00:00
|
|
|
/*
|
|
|
|
* Even if DefineDosDevice allows to map files to drive letters (yes yes!!)
|
|
|
|
* it is not the purpose of SUBST to allow that. Therefore check whether
|
|
|
|
* the given path exists and really is a path to a directory, and if not,
|
|
|
|
* just fail with an error.
|
|
|
|
*/
|
2016-06-29 00:38:51 +00:00
|
|
|
dwPathAttr = GetFileAttributesW(Path);
|
2016-06-28 20:22:07 +00:00
|
|
|
if ( (dwPathAttr == INVALID_FILE_ATTRIBUTES) ||
|
|
|
|
!(dwPathAttr & FILE_ATTRIBUTE_DIRECTORY) )
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
dwResult = ERROR_PATH_NOT_FOUND;
|
|
|
|
goto Quit;
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
/*
|
|
|
|
* QuerySubstedDrive (via QueryDosDevice) returns ERROR_FILE_NOT_FOUND only
|
|
|
|
* if there is no already existing drive mapping. For all other results
|
|
|
|
* (existing drive, be it already subst'ed or not, or other errors...)
|
|
|
|
* no attempt at defining a drive mapping should be done.
|
|
|
|
*/
|
|
|
|
dwResult = QuerySubstedDrive(Drive[0], NULL, NULL);
|
|
|
|
if (dwResult != ERROR_FILE_NOT_FOUND)
|
|
|
|
goto Quit;
|
2011-04-18 21:48:19 +00:00
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!DefineDosDeviceW(0, Drive, Path))
|
|
|
|
dwResult = GetLastError();
|
|
|
|
else
|
|
|
|
dwResult = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
Quit:
|
|
|
|
switch (dwResult)
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
case ERROR_SUCCESS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERROR_INVALID_DRIVE:
|
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_INVALID_PARAMETER2, Drive);
|
2016-06-29 00:38:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ERROR_IS_SUBSTED:
|
|
|
|
{
|
2016-10-07 21:57:12 +00:00
|
|
|
ConResPuts(StdErr, IDS_DRIVE_ALREADY_SUBSTED);
|
2016-06-29 00:38:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_PATH_NOT_FOUND, Path);
|
2016-06-29 00:38:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ACCESS_DENIED, Path);
|
2016-06-29 00:38:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
PrintError(GetLastError());
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-18 21:48:19 +00:00
|
|
|
}
|
2016-06-29 00:38:51 +00:00
|
|
|
|
2011-04-18 21:48:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
int wmain(int argc, WCHAR* argv[])
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
|
|
|
INT i;
|
2016-10-07 20:37:36 +00:00
|
|
|
|
|
|
|
/* Initialize the Console Standard Streams */
|
|
|
|
ConInitStdStreams();
|
2011-04-18 21:48:19 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!_wcsicmp(argv[i], L"/?"))
|
2011-04-18 21:48:19 +00:00
|
|
|
{
|
2016-10-07 21:57:12 +00:00
|
|
|
ConResPuts(StdOut, IDS_USAGE);
|
2011-04-18 21:48:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
if (argc >= 2)
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_INVALID_PARAMETER, argv[1]);
|
2011-04-18 21:48:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
DumpSubstedDrives();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 3)
|
|
|
|
{
|
2016-10-07 20:37:36 +00:00
|
|
|
ConResPrintf(StdErr, IDS_INCORRECT_PARAMETER_COUNT, argv[3]);
|
2011-04-18 21:48:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!_wcsicmp(argv[1], L"/D"))
|
2011-04-18 21:48:19 +00:00
|
|
|
return DeleteSubst(argv[2]);
|
2016-06-29 00:38:51 +00:00
|
|
|
if (!_wcsicmp(argv[2], L"/D"))
|
2011-04-18 21:48:19 +00:00
|
|
|
return DeleteSubst(argv[1]);
|
|
|
|
return AddSubst(argv[1], argv[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|