Skip to content

Commit 47b53a9

Browse files
committed
ZFS-1459: Delete the driver package from the Driver Store during uninstall.
1 parent 28bd257 commit 47b53a9

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

cmd/os/windows/zfsinstaller/zfsinstaller.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const unsigned char OPEN_ZFS_GUID[] = "c20c603c-afd4-467d-bf76-c0a4c10553df";
5959
const unsigned char LOGGER_SESSION[] = "autosession\\zfsin_trace";
6060
const std::string ETL_FILE("\\ZFSin.etl");
6161
const std::string MANIFEST_FILE("\\OpenZFS.man");
62+
#define DRIVER_SYS_FILE _T("ZFSin.sys")
6263

6364
enum manifest_install_types
6465
{
@@ -476,6 +477,8 @@ zfs_uninstall(char *inf_path)
476477
if (ret == 0) {
477478
ret = uninstallRootDevice(inf_path);
478479
perf_counters_uninstall(inf_path);
480+
DeleteOemInf(inf_path);
481+
DeleteSysFile(DRIVER_SYS_FILE);
479482
}
480483

481484
return (ret);
@@ -895,3 +898,102 @@ uninstallRootDevice(char *inf)
895898
return (failcode);
896899
}
897900
#endif
901+
902+
DWORD
903+
DeleteOemInf(const char* inf_name)
904+
{
905+
TCHAR sysDir[MAX_PATH];
906+
DWORD error = ERROR_SUCCESS;
907+
908+
if (!GetSystemDirectory(sysDir, MAX_PATH))
909+
return GetLastError();
910+
911+
std::wstring infDir = std::wstring(sysDir).substr(0, (int)(_tcslen(sysDir) - _tcslen(_T("system32"))));
912+
infDir += _T("inf\\");
913+
914+
TCHAR inf_nameW[MAX_PATH] = { 0 };
915+
swprintf_s(inf_nameW, MAX_PATH, _T("%S"), inf_name);
916+
917+
std::wstring mFullFileName;
918+
TCHAR full[MAX_PATH];
919+
if (_tfullpath(full, inf_nameW, MAX_PATH))
920+
mFullFileName = full;
921+
else
922+
mFullFileName = inf_nameW;
923+
924+
// Check for existence.
925+
if ((_taccess(mFullFileName.c_str(), 0)) == -1)
926+
return (errno);
927+
928+
std::wstring oemName = infDir;
929+
oemName += _T("oem*");
930+
oemName += _T(".inf");
931+
932+
HANDLE hFile;
933+
WIN32_FIND_DATA findFileData;
934+
TCHAR szDriverFileName[MAX_PATH] = DRIVER_SYS_FILE;
935+
936+
if (INVALID_HANDLE_VALUE == (hFile = FindFirstFile(oemName.c_str(), &findFileData)))
937+
return ERROR_SUCCESS;
938+
939+
do
940+
{
941+
// open each of inf file and search for the .sys file
942+
// under the section [SourceDisksFiles]
943+
TCHAR szValue[MAX_PATH];
944+
std::wstring oemFile;
945+
oemName = infDir + findFileData.cFileName;
946+
947+
if (0 < GetPrivateProfileString(
948+
_T("SourceDisksFiles"), // section name
949+
szDriverFileName, // lpKeyName
950+
NULL,
951+
szValue, // lpReturnedString
952+
MAX_PATH,
953+
oemName.c_str())) // lpFileName
954+
{
955+
BOOL success = SetupUninstallOEMInf(findFileData.cFileName, SUOI_FORCEDELETE, NULL);
956+
if (!success)
957+
{
958+
error = GetLastError();
959+
std::wcout << "\nFailed to delete the driver package from Driver Store.oemfile:" << findFileData.cFileName << ",error:" << error << std::endl;
960+
}
961+
else
962+
std::wcout << "\nSuccessfully deleted the driver package from Driver Store.oemfile:" << findFileData.cFileName << std::endl;
963+
}
964+
} while (FindNextFile(hFile, &findFileData));
965+
966+
FindClose(hFile);
967+
return (error);
968+
}
969+
970+
DWORD
971+
DeleteSysFile(const TCHAR* sysFile)
972+
{
973+
DWORD error = ERROR_SUCCESS;
974+
TCHAR sysDir[MAX_PATH];
975+
976+
if (!GetSystemDirectory(sysDir, MAX_PATH))
977+
return GetLastError();
978+
979+
std::wstring fullPathName = sysDir;
980+
fullPathName += _T("\\drivers\\");
981+
fullPathName += sysFile;
982+
983+
// Check for existence.
984+
if ((_taccess(fullPathName.c_str(), 0)) == -1)
985+
return (errno);
986+
987+
// The delete may be unsuccessful if
988+
// 1.File has already been deleted.
989+
// 2.File has an open handle
990+
if (!DeleteFile(fullPathName.c_str()))
991+
{
992+
error = GetLastError();
993+
std::wcout << "Failed to delete the driver file:" << fullPathName.c_str() << ",error:" << error << std::endl;
994+
}
995+
else
996+
std::wcout << "Successfully deleted the driver file:" << fullPathName.c_str() << std::endl;
997+
998+
return (error);
999+
}

cmd/os/windows/zfsinstaller/zfsinstaller.h

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <SetupAPI.h>
2929
#include <stdio.h>
3030
#include <winsvc.h>
31+
#include <tchar.h>
32+
#include <iostream>
3133

3234
DWORD zfs_install(char *);
3335
DWORD zfs_uninstall(char *);
@@ -37,3 +39,5 @@ void printUsage();
3739
DWORD send_zfs_ioc_unregister_fs();
3840
DWORD installRootDevice(char *inf_path, bool IsServiceRunning);
3941
DWORD uninstallRootDevice(char *inf_path);
42+
DWORD DeleteOemInf(const char* inf_name);
43+
DWORD DeleteSysFile(const TCHAR* sysFile);

0 commit comments

Comments
 (0)