Author: hbelusca
Date: Wed Jun 29 00:38:51 2016
New Revision: 71695
URL:
http://svn.reactos.org/svn/reactos?rev=71695&view=rev
Log:
[SUBST]
- Update the resource program description.
- Convert to full UNICODE.
- Use Win32 functions where possible.
- Factor-out the usage of QueryDosDevice into a QuerySubstedDrive function, that returns
error codes according to whether the specified drive is a mapped (substed) drive, or is
just an existing drive that is not a mapping, or if the drive does not exist. This allows
us to detect attempts to use a drive letter that is not a mapped drive, to define a new
mapping, and if so we reject such attempt.
This fixes CORE-10681 #resolve #comment Fixed with another patch according to my last
remark.
Modified:
trunk/reactos/base/system/subst/lang/bg-BG.rc
trunk/reactos/base/system/subst/lang/de-DE.rc
trunk/reactos/base/system/subst/lang/en-US.rc
trunk/reactos/base/system/subst/lang/es-ES.rc
trunk/reactos/base/system/subst/lang/fr-FR.rc
trunk/reactos/base/system/subst/lang/it-IT.rc
trunk/reactos/base/system/subst/lang/pl-PL.rc
trunk/reactos/base/system/subst/lang/ro-RO.rc
trunk/reactos/base/system/subst/lang/ru-RU.rc
trunk/reactos/base/system/subst/lang/sq-AL.rc
trunk/reactos/base/system/subst/lang/tr-TR.rc
trunk/reactos/base/system/subst/lang/uk-UA.rc
trunk/reactos/base/system/subst/lang/zh-CN.rc
trunk/reactos/base/system/subst/lang/zh-TW.rc
trunk/reactos/base/system/subst/resource.h
trunk/reactos/base/system/subst/subst.c
trunk/reactos/base/system/subst/subst.rc
Modified: trunk/reactos/base/system/subst/lang/bg-BG.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/bg-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/bg-BG.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/bg-BG.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -6,6 +6,7 @@
IDS_INVALID_PARAMETER "ÐепÑавилен бÑой клÑÑове -
%s\n"
IDS_INVALID_PARAMETER2 "ÐедопÑÑÑим клÑÑ - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "УÑÑÑойÑÑвоÑо веÑе е
подложено на SUBST\n"
IDS_FAILED_WITH_ERRORCODE "ÐÑеÑка номеÑ: 0x%x: %s\n"
IDS_USAGE "ÐбвÑÑзва пÑÑ Ñ Ð¸Ð¼Ðµ на ÑÑÑÑойÑÑво
(дÑл).\n\n\
Modified: trunk/reactos/base/system/subst/lang/de-DE.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/de-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/de-DE.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/de-DE.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -6,6 +6,7 @@
IDS_INVALID_PARAMETER "Falsche Anzahl an Parametern - %s\n"
IDS_INVALID_PARAMETER2 "Falscher Parameter - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "SUBST wurde hier schon ausgeführt.\n"
IDS_FAILED_WITH_ERRORCODE "Fehlgeschlagen - Code 0x%x: %s\n"
IDS_USAGE "Weist einem Pfad einen Laufwerksbuchstaben zu.n\n\
Modified: trunk/reactos/base/system/subst/lang/en-US.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/en-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/en-US.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/en-US.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -6,6 +6,7 @@
IDS_INVALID_PARAMETER "Incorrect number of parameters - %s\n"
IDS_INVALID_PARAMETER2 "Invalid parameter - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Drive already SUBSTed\n"
IDS_FAILED_WITH_ERRORCODE "Failed with error code 0x%x: %s\n"
IDS_USAGE "Associates a path with a drive letter.\n\n\
Modified: trunk/reactos/base/system/subst/lang/es-ES.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/es-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/es-ES.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/es-ES.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -6,6 +6,7 @@
IDS_INVALID_PARAMETER "Cantidad de parámetros incorrecta - %s\n"
IDS_INVALID_PARAMETER2 "Parámetro incorrecto - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Unidad ya sustituida\n"
IDS_FAILED_WITH_ERRORCODE "Falla con código de error 0x%x: %s\n"
IDS_USAGE "Asocia una ruta con una letra de unidad.\n\n\
Modified: trunk/reactos/base/system/subst/lang/fr-FR.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/fr-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/fr-FR.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/fr-FR.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -6,6 +6,7 @@
IDS_INVALID_PARAMETER "Nombre incorrect de paramètres - %s\n"
IDS_INVALID_PARAMETER2 "Paramètre incorrect - %s\n"
IDS_PATH_NOT_FOUND "Chemin d'accès introuvable - %s\n"
+ IDS_ACCESS_DENIED "Accès refusé - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Disque déjà substitué\n"
IDS_FAILED_WITH_ERRORCODE "Ãchoue avec le code d'erreur 0x%x : %s\n"
IDS_USAGE "Associe un chemin avec une lettre de lecteur.\n\n\
Modified: trunk/reactos/base/system/subst/lang/it-IT.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/it-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/it-IT.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/it-IT.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -6,6 +6,7 @@
IDS_INVALID_PARAMETER "Numero di parametri errato - %s\n"
IDS_INVALID_PARAMETER2 "Parametro errato - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Unità già sostituita\n"
IDS_FAILED_WITH_ERRORCODE "Interruzione con codice di errore 0x%x: %s\n"
IDS_USAGE "Associa un percorso ad una lettera di unità .\n\n\
Modified: trunk/reactos/base/system/subst/lang/pl-PL.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/pl-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/pl-PL.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/pl-PL.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -14,6 +14,7 @@
IDS_INVALID_PARAMETER "NieprawidÅowy parametr - %s\n"
IDS_INVALID_PARAMETER2 "NieprawidÅowe parametry - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Na tym dysku już użyto komendy SUBST\n"
IDS_FAILED_WITH_ERRORCODE "BÅÄ
d 0x%x: %s\n"
IDS_USAGE "PowiÄ
zuje ÅcieżkÄ z literÄ
napÄdu.\n\n\
Modified: trunk/reactos/base/system/subst/lang/ro-RO.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/ro-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/ro-RO.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/ro-RO.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -8,6 +8,7 @@
IDS_INVALID_PARAMETER "NumÄr eronat de parametri - %s\n"
IDS_INVALID_PARAMETER2 "Parametru eronat - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Unitatea este deja SUBSTituitÄ\n"
IDS_FAILED_WITH_ERRORCODE "OperaÈia a eÈuat. Codul de eroare 0x%x: %s\n"
IDS_USAGE "Substituie un director cu o unitate de stocare.\n\n\
Modified: trunk/reactos/base/system/subst/lang/ru-RU.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/ru-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/ru-RU.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/ru-RU.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -8,6 +8,7 @@
IDS_INVALID_PARAMETER "ÐевеÑное ÑиÑло паÑамеÑÑов -
%s\n"
IDS_INVALID_PARAMETER2 "ÐевеÑнÑй паÑамеÑÑ - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "ÐиÑк Ñже ÑопоÑÑавлен\n"
IDS_FAILED_WITH_ERRORCODE "Сбой Ñ ÐºÐ¾Ð´Ð¾Ð¼ оÑибки 0x%x: %s\n"
IDS_USAGE "СопоÑÑавление имени диÑка ÑказанномÑ
пÑÑи.\n\n\
Modified: trunk/reactos/base/system/subst/lang/sq-AL.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/sq-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/sq-AL.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/sq-AL.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -10,6 +10,7 @@
IDS_INVALID_PARAMETER "Numri i parametrave jo i sakte - %s\n"
IDS_INVALID_PARAMETER2 "Parameter i gabuar - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Drive tashme i SUBSTed\n"
IDS_FAILED_WITH_ERRORCODE "Deshtoj me error kode 0x%x: %s\n"
IDS_USAGE "Perputh rrugen e driverit me germën.\n\n\
Modified: trunk/reactos/base/system/subst/lang/tr-TR.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/tr-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/tr-TR.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/tr-TR.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -8,6 +8,7 @@
IDS_INVALID_PARAMETER "DeÄiÅken sayısı yanlıŠ- %s\n"
IDS_INVALID_PARAMETER2 "Geçersiz deÄiÅken - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Sürücü önceden SUBST edilmiÅ.\n"
IDS_FAILED_WITH_ERRORCODE "YanlıÅlık kodu ile baÅarısız oldu 0x%x:
%s\n"
IDS_USAGE "Bir sürücü harfiyle bir yolu iliÅkilendirir.\n\n\
Modified: trunk/reactos/base/system/subst/lang/uk-UA.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/uk-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/uk-UA.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/uk-UA.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -8,6 +8,7 @@
IDS_INVALID_PARAMETER "ÐевÑÑна кÑлÑкÑÑÑÑ Ð¿Ð°ÑамеÑÑÑв -
%s\n"
IDS_INVALID_PARAMETER2 "ÐевÑÑний паÑамеÑÑ - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "Ðо диÑÐºÑ Ð²Ð¶Ðµ заÑÑоÑовано
SUBST\n"
IDS_FAILED_WITH_ERRORCODE "ÐевдаÑа з кодом помилки 0x%x:
%s\n"
IDS_USAGE "ÐÑоÑÑÑÑ ÑлÑÑ
з бÑÐºÐ²Ð¾Ñ Ð´Ð¸ÑкÑ.\n\n\
Modified: trunk/reactos/base/system/subst/lang/zh-CN.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/zh-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/zh-CN.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/zh-CN.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -8,6 +8,7 @@
IDS_INVALID_PARAMETER "åæ°ä¸ªæ°ä¸æ£ç¡® - %s\n"
IDS_INVALID_PARAMETER2 "æ æåæ° - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "驱å¨å¨å·²ç»è¿è¡è¿ SUBST\n"
IDS_FAILED_WITH_ERRORCODE "æä½å¤±è´¥ï¼é误ç 0x%xï¼%s\n"
IDS_USAGE "å°ä¸ä¸ªè·¯å¾ç¨é©±å¨å¨å·å
³èã\n\n\
Modified: trunk/reactos/base/system/subst/lang/zh-TW.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/lang/zh-…
==============================================================================
--- trunk/reactos/base/system/subst/lang/zh-TW.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/lang/zh-TW.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -8,6 +8,7 @@
IDS_INVALID_PARAMETER "åæ¸åæ¸ä¸æ£ç¢º - %s\n"
IDS_INVALID_PARAMETER2 "ç¡æåæ¸ - %s\n"
IDS_PATH_NOT_FOUND "Path not found - %s\n"
+ IDS_ACCESS_DENIED "Access denied - %s\n"
IDS_DRIVE_ALREADY_SUBSTED "é©
åå¨å·²ç¶éè¡é SUBST\n"
IDS_FAILED_WITH_ERRORCODE "æä½å¤±æï¼é¯èª¤ç¢¼ 0x%xï¼%s\n"
IDS_USAGE "å°ä¸åè·¯å¾ç¨é©
åå¨èéè¯ã\n\n\
Modified: trunk/reactos/base/system/subst/resource.h
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/resource…
==============================================================================
--- trunk/reactos/base/system/subst/resource.h [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/resource.h [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -9,4 +9,5 @@
#define IDS_INVALID_PARAMETER 1004
#define IDS_INVALID_PARAMETER2 1005
#define IDS_PATH_NOT_FOUND 1006
-#define IDS_DRIVE_ALREADY_SUBSTED 1007
+#define IDS_ACCESS_DENIED 1007
+#define IDS_DRIVE_ALREADY_SUBSTED 1008
Modified: trunk/reactos/base/system/subst/subst.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/subst.c?…
==============================================================================
--- trunk/reactos/base/system/subst/subst.c [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/subst.c [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -1,212 +1,254 @@
-/* PROJECT: ReactOS Kernel
+/*
+ * PROJECT: ReactOS Subst Command
* LICENSE: GPL - See COPYING in the top level directory
* FILE: base/system/subst/subst.c
- * PURPOSE: Associates a path with a drive letter
+ * PURPOSE: Maps a path with a drive letter
* PROGRAMMERS: Sam Arun Raj
+ * Peter Hater
+ * Hermes Belusca-Maito
*/
/* INCLUDES *****************************************************************/
-#include <stdarg.h>
-#include <stdlib.h>
-#include <tchar.h>
+#include <stdio.h>
#define WIN32_NO_STATUS
#include <windef.h>
#include <winbase.h>
#include <winuser.h>
-#define NDEBUG
-#include <debug.h>
-
#include "resource.h"
/* FUNCTIONS ****************************************************************/
-VOID PrintError(DWORD ErrCode)
-{
- TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
- TCHAR *buffer = (TCHAR*) calloc(2048,
- sizeof(TCHAR));
- TCHAR *msg = NULL;
-
- if (buffer)
- {
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- ErrCode,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (TCHAR*)&msg,
- 0,
- NULL);
- LoadString(GetModuleHandle(NULL),
- IDS_FAILED_WITH_ERRORCODE,
- szFmtString,
- ARRAYSIZE(szFmtString));
- _sntprintf(buffer,
+VOID PrintError(IN DWORD ErrCode)
+{
+ WCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
+ PWSTR buffer, msg = NULL;
+
+ buffer = (PWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2048 * sizeof(WCHAR));
+ if (!buffer)
+ return;
+
+ FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL,
+ ErrCode,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (PWSTR)&msg,
+ 0,
+ NULL);
+ if (msg)
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_FAILED_WITH_ERRORCODE,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ _snwprintf(buffer,
2048,
szFmtString,
ErrCode,
msg);
- _tprintf(_T("%s"),
- buffer);
- if (msg)
- LocalFree(msg);
- free(buffer);
- }
+ wprintf(L"%s", buffer);
+
+ LocalFree(msg);
+ }
+
+ HeapFree(GetProcessHeap(), 0, buffer);
}
VOID DisplaySubstUsage(VOID)
{
- TCHAR szHelp[RC_STRING_MAX_SIZE] = {0};
-
- LoadString(GetModuleHandle(NULL),
+ WCHAR szHelp[RC_STRING_MAX_SIZE] = {0};
+
+ LoadStringW(GetModuleHandleW(NULL),
IDS_USAGE,
szHelp,
ARRAYSIZE(szHelp));
- _tprintf(_T("%s"), szHelp);
-}
-
-BOOLEAN IsSubstedDrive(TCHAR DriveLetter)
-{
- BOOLEAN Result = FALSE;
- TCHAR Drive[3] = _T("A:");
- LPTSTR lpTargetPath = NULL;
- DWORD CharCount, dwSize;
+ wprintf(L"%s", szHelp);
+}
+
+ULONG QuerySubstedDrive(IN WCHAR DriveLetter,
+ IN OUT PWSTR* TargetPath OPTIONAL,
+ IN OUT PULONG Size)
+{
+ ULONG Result = ERROR_INVALID_DRIVE;
+ WCHAR Drive[] = L"A:";
+ DWORD dwSize, CharCount = 0;
+ PWSTR lpTargetPath = NULL, tmp;
Drive[0] = DriveLetter;
+ /* 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();
+
+ if (!CharCount && (Result == ERROR_INSUFFICIENT_BUFFER))
+ {
+ /* Reallocate the buffer with double size */
+ dwSize *= 2;
+ tmp = (PWSTR)HeapReAlloc(GetProcessHeap(), 0, lpTargetPath, dwSize *
sizeof(WCHAR));
+ if (!tmp)
+ {
+ /* Memory problem, bail out */
+ CharCount = 0;
+ Result = ERROR_NOT_ENOUGH_MEMORY;
+ }
+ else
+ {
+ /* Retry again */
+ lpTargetPath = tmp;
+ goto Retry;
+ }
+ }
+
+ if (CharCount)
+ {
+ 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
+ }
+
+ if (!TargetPath)
+ {
+ /* Free the local buffer */
+ HeapFree(GetProcessHeap(), 0, lpTargetPath);
+ }
+ else
+ {
+ /* Update the user-given pointers */
+ *TargetPath = lpTargetPath;
+ *Size = dwSize;
+ }
+
+ return Result;
+}
+
+VOID DumpSubstedDrives(VOID)
+{
+ WCHAR DriveLetter;
+ PWSTR lpTargetPath = NULL;
+ DWORD dwSize;
+ UCHAR i = 0;
+
dwSize = MAX_PATH;
- lpTargetPath = (LPTSTR)malloc(sizeof(TCHAR) * dwSize);
- if (!lpTargetPath)
- return FALSE;
-
- CharCount = QueryDosDevice(Drive,
- lpTargetPath,
- dwSize);
- while (!CharCount &&
- GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- lpTargetPath = (LPTSTR)realloc(lpTargetPath, sizeof(TCHAR) * dwSize);
- if (lpTargetPath)
- {
- CharCount = QueryDosDevice(Drive,
- lpTargetPath,
- dwSize);
- }
- }
-
- if (CharCount)
- {
- Result = _tcsncmp(lpTargetPath, _T("\\??\\"), 4) == 0 &&
- ( (lpTargetPath[4] >= _T('A') &&
- lpTargetPath[4] <= _T('Z')) ||
- (lpTargetPath[4] >= _T('a') &&
- lpTargetPath[4] <= _T('z')) );
- }
-
- free(lpTargetPath);
-
- return Result;
-}
-
-VOID DumpSubstedDrives(VOID)
-{
- TCHAR Drive[3] = _T("A:");
- LPTSTR lpTargetPath = NULL;
- DWORD CharCount, dwSize;
- INT i = 0;
-
- dwSize = MAX_PATH;
- lpTargetPath = (LPTSTR)malloc(sizeof(TCHAR) * dwSize);
+ lpTargetPath = (PWSTR)HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
if (!lpTargetPath)
return;
while (i < 26)
{
- Drive[0] = _T('A') + i;
- CharCount = QueryDosDevice(Drive,
- lpTargetPath,
- dwSize);
- while (!CharCount &&
- GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- lpTargetPath = (LPTSTR)realloc(lpTargetPath, sizeof(TCHAR) * dwSize);
- if (lpTargetPath)
- {
- CharCount = QueryDosDevice(Drive,
- lpTargetPath,
- dwSize);
- }
- }
-
- if (CharCount)
- {
- if ( _tcsncmp(lpTargetPath, _T("\\??\\"), 4) == 0 &&
- ( (lpTargetPath[4] >= _T('A') &&
- lpTargetPath[4] <= _T('Z')) ||
- (lpTargetPath[4] >= _T('a') &&
- lpTargetPath[4] <= _T('z')) ) )
- {
- _tprintf(_T("%s\\: => %s\n"),
- Drive,
- lpTargetPath + 4);
- }
+ DriveLetter = L'A' + i;
+ if (QuerySubstedDrive(DriveLetter, &lpTargetPath, &dwSize) ==
ERROR_IS_SUBSTED)
+ {
+ wprintf(L"%c:\\: => %s\n", DriveLetter, lpTargetPath + 4);
}
i++;
}
- free(lpTargetPath);
-}
-
-INT DeleteSubst(TCHAR* Drive)
-{
- BOOL Result;
- TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
-
- LoadString(GetModuleHandle(NULL),
- IDS_INVALID_PARAMETER2,
- szFmtString,
- ARRAYSIZE(szFmtString));
-
- if ((_tcslen(Drive) != 2) || (Drive[1] != _T(':')))
- {
- _tprintf(szFmtString, Drive);
- return 1;
- }
-
- if (!IsSubstedDrive(Drive[0]))
- {
- _tprintf(szFmtString, Drive);
- return 1;
- }
-
- Result = DefineDosDevice(DDD_REMOVE_DEFINITION,
- Drive,
- NULL);
- if (!Result)
- {
- PrintError(GetLastError());
- return 1;
- }
+ HeapFree(GetProcessHeap(), 0, lpTargetPath);
+}
+
+INT DeleteSubst(IN PWSTR Drive)
+{
+ DWORD dwResult;
+ WCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
+
+ if ((wcslen(Drive) != 2) || (Drive[1] != L':'))
+ {
+ dwResult = ERROR_INVALID_PARAMETER;
+ goto Quit;
+ }
+
+ if (QuerySubstedDrive(Drive[0], NULL, NULL) != ERROR_IS_SUBSTED)
+ {
+ dwResult = ERROR_INVALID_PARAMETER;
+ goto Quit;
+ }
+
+ if (!DefineDosDeviceW(DDD_REMOVE_DEFINITION, Drive, NULL))
+ dwResult = GetLastError();
+ else
+ dwResult = ERROR_SUCCESS;
+
+Quit:
+ switch (dwResult)
+ {
+ case ERROR_SUCCESS:
+ break;
+
+ // case ERROR_INVALID_DRIVE:
+ case ERROR_INVALID_PARAMETER:
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_INVALID_PARAMETER2,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString, Drive);
+ return 1;
+ }
+
+ case ERROR_ACCESS_DENIED:
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_ACCESS_DENIED,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString, Drive);
+ return 1;
+ }
+
+ default:
+ {
+ PrintError(GetLastError());
+ return 1;
+ }
+ }
+
return 0;
}
-INT AddSubst(TCHAR* Drive, TCHAR *Path)
-{
- BOOL Result;
- DWORD dwPathAttr;
- TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
-
- if ((_tcslen(Drive) != 2) || (Drive[1] != _T(':')))
- {
- LoadString(GetModuleHandle(NULL),
- IDS_INVALID_PARAMETER2,
- szFmtString,
- ARRAYSIZE(szFmtString));
- _tprintf(szFmtString, Drive);
- return 1;
+INT AddSubst(IN PWSTR Drive, IN PWSTR Path)
+{
+ DWORD dwResult, dwPathAttr;
+ WCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
+
+ if ((wcslen(Drive) != 2) || (Drive[1] != L':'))
+ {
+ dwResult = ERROR_INVALID_PARAMETER;
+ goto Quit;
}
/*
@@ -215,82 +257,129 @@
* the given path exists and really is a path to a directory, and if not,
* just fail with an error.
*/
- dwPathAttr = GetFileAttributes(Path);
+ dwPathAttr = GetFileAttributesW(Path);
if ( (dwPathAttr == INVALID_FILE_ATTRIBUTES) ||
!(dwPathAttr & FILE_ATTRIBUTE_DIRECTORY) )
{
- LoadString(GetModuleHandle(NULL),
- IDS_PATH_NOT_FOUND,
+ dwResult = ERROR_PATH_NOT_FOUND;
+ goto Quit;
+ }
+
+ /*
+ * 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;
+
+ if (!DefineDosDeviceW(0, Drive, Path))
+ dwResult = GetLastError();
+ else
+ dwResult = ERROR_SUCCESS;
+
+Quit:
+ switch (dwResult)
+ {
+ case ERROR_SUCCESS:
+ break;
+
+ case ERROR_INVALID_DRIVE:
+ case ERROR_INVALID_PARAMETER:
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_INVALID_PARAMETER2,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString, Drive);
+ return 1;
+ }
+
+ case ERROR_IS_SUBSTED:
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_DRIVE_ALREADY_SUBSTED,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString);
+ return 1;
+ }
+
+ case ERROR_FILE_NOT_FOUND:
+ case ERROR_PATH_NOT_FOUND:
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_PATH_NOT_FOUND,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString, Path);
+ return 1;
+ }
+
+ case ERROR_ACCESS_DENIED:
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_ACCESS_DENIED,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString, Path);
+ return 1;
+ }
+
+ default:
+ {
+ PrintError(GetLastError());
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int wmain(int argc, WCHAR* argv[])
+{
+ INT i;
+ WCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
+
+ for (i = 0; i < argc; i++)
+ {
+ if (!_wcsicmp(argv[i], L"/?"))
+ {
+ DisplaySubstUsage();
+ return 0;
+ }
+ }
+
+ if (argc < 3)
+ {
+ if (argc >= 2)
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_INVALID_PARAMETER,
+ szFmtString,
+ ARRAYSIZE(szFmtString));
+ wprintf(szFmtString, argv[1]);
+ return 1;
+ }
+ DumpSubstedDrives();
+ return 0;
+ }
+
+ if (argc > 3)
+ {
+ LoadStringW(GetModuleHandleW(NULL),
+ IDS_INCORRECT_PARAMETER_COUNT,
szFmtString,
ARRAYSIZE(szFmtString));
- _tprintf(szFmtString, Path);
+ wprintf(szFmtString, argv[3]);
return 1;
}
- if (IsSubstedDrive(Drive[0]))
- {
- // ERROR_IS_SUBSTED
- LoadString(GetModuleHandle(NULL),
- IDS_DRIVE_ALREADY_SUBSTED,
- szFmtString,
- ARRAYSIZE(szFmtString));
- _tprintf(szFmtString);
- return 1;
- }
-
- Result = DefineDosDevice(0,
- Drive,
- Path);
- if (!Result)
- {
- PrintError(GetLastError());
- return 1;
- }
- return 0;
-}
-
-int _tmain(int argc, TCHAR* argv[])
-{
- INT i;
- TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
-
- for (i = 0; i < argc; i++)
- {
- if (!_tcsicmp(argv[i], _T("/?")))
- {
- DisplaySubstUsage();
- return 0;
- }
- }
-
- if (argc < 3)
- {
- if (argc >= 2)
- {
- LoadString(GetModuleHandle(NULL),
- IDS_INVALID_PARAMETER,
- szFmtString,
- ARRAYSIZE(szFmtString));
- _tprintf(szFmtString, argv[1]);
- return 1;
- }
- DumpSubstedDrives();
- return 0;
- }
-
- if (argc > 3)
- {
- LoadString(GetModuleHandle(NULL),
- IDS_INCORRECT_PARAMETER_COUNT,
- szFmtString,
- ARRAYSIZE(szFmtString));
- _tprintf(szFmtString, argv[3]);
- return 1;
- }
-
- if (!_tcsicmp(argv[1], _T("/D")))
+ if (!_wcsicmp(argv[1], L"/D"))
return DeleteSubst(argv[2]);
- if (!_tcsicmp(argv[2], _T("/D")))
+ if (!_wcsicmp(argv[2], L"/D"))
return DeleteSubst(argv[1]);
return AddSubst(argv[1], argv[2]);
}
Modified: trunk/reactos/base/system/subst/subst.rc
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/subst/subst.rc…
==============================================================================
--- trunk/reactos/base/system/subst/subst.rc [iso-8859-1] (original)
+++ trunk/reactos/base/system/subst/subst.rc [iso-8859-1] Wed Jun 29 00:38:51 2016
@@ -2,9 +2,9 @@
#include "resource.h"
-#define REACTOS_STR_FILE_DESCRIPTION "ReactOS Virtual Drive Utility Version
1.0"
-#define REACTOS_STR_INTERNAL_NAME "subst"
-#define REACTOS_STR_ORIGINAL_FILENAME "subst.exe"
+#define REACTOS_STR_FILE_DESCRIPTION "ReactOS Drive Letter Mapper Utility"
+#define REACTOS_STR_INTERNAL_NAME "subst"
+#define REACTOS_STR_ORIGINAL_FILENAME "subst.exe"
#include <reactos/version.rc>
/* UTF-8 */