Skip to content

Commit

Permalink
ZFS-1459: Delete the driver package from the Driver Store during unin…
Browse files Browse the repository at this point in the history
…stall.
  • Loading branch information
datacore-rm committed Jul 12, 2024
1 parent 28bd257 commit 3c8d667
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
102 changes: 102 additions & 0 deletions cmd/os/windows/zfsinstaller/zfsinstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const unsigned char OPEN_ZFS_GUID[] = "c20c603c-afd4-467d-bf76-c0a4c10553df";
const unsigned char LOGGER_SESSION[] = "autosession\\zfsin_trace";
const std::string ETL_FILE("\\ZFSin.etl");
const std::string MANIFEST_FILE("\\OpenZFS.man");
#define DRIVER_SYS_FILE _T("ZFSin.sys")

enum manifest_install_types
{
Expand Down Expand Up @@ -476,6 +477,8 @@ zfs_uninstall(char *inf_path)
if (ret == 0) {
ret = uninstallRootDevice(inf_path);
perf_counters_uninstall(inf_path);
DeleteOemInf(inf_path);
DeleteSysFile(DRIVER_SYS_FILE);
}

return (ret);
Expand Down Expand Up @@ -895,3 +898,102 @@ uninstallRootDevice(char *inf)
return (failcode);
}
#endif

DWORD
DeleteOemInf(const char* inf_name)
{
TCHAR sysDir[MAX_PATH];
DWORD error = ERROR_SUCCESS;

if (!GetSystemDirectory(sysDir, MAX_PATH))
return GetLastError();

std::wstring infDir = std::wstring(sysDir).substr(0, (int)(_tcslen(sysDir) - _tcslen(_T("system32"))));
infDir += _T("inf\\");

TCHAR inf_nameW[MAX_PATH] = { 0 };
swprintf_s(inf_nameW, MAX_PATH, _T("%S"), inf_name);

std::wstring mFullFileName;
TCHAR full[MAX_PATH];
if (_tfullpath(full, inf_nameW, MAX_PATH))
mFullFileName = full;
else
mFullFileName = inf_nameW;

// Check for existence.
if ((_taccess(mFullFileName.c_str(), 0)) == -1)
return (errno);

std::wstring oemName = infDir;
oemName += _T("oem*");
oemName += _T(".inf");

HANDLE hFile;
WIN32_FIND_DATA findFileData;
TCHAR szDriverFileName[MAX_PATH] = DRIVER_SYS_FILE;

if (INVALID_HANDLE_VALUE == (hFile = FindFirstFile(oemName.c_str(), &findFileData)))
return ERROR_SUCCESS;

do
{
// open each of inf file and search for the .sys file
// under the section [SourceDisksFiles]
TCHAR szValue[MAX_PATH];
std::wstring oemFile;
oemName = infDir + findFileData.cFileName;

if (0 < GetPrivateProfileString(
_T("SourceDisksFiles"), // section name
szDriverFileName, // lpKeyName
NULL,
szValue, // lpReturnedString
MAX_PATH,
oemName.c_str())) // lpFileName
{
BOOL success = SetupUninstallOEMInf(findFileData.cFileName, SUOI_FORCEDELETE, NULL);
if (!success)
{
error = GetLastError();
std::wcout << "\nFailed to delete the driver package from Driver Store.oemfile:" << findFileData.cFileName << ",error:" << error << std::endl;
}
else
std::wcout << "\nSuccessfully deleted the driver package from Driver Store.oemfile:" << findFileData.cFileName << std::endl;
}
} while (FindNextFile(hFile, &findFileData));

FindClose(hFile);
return (error);
}

DWORD
DeleteSysFile(const TCHAR* sysFile)
{
DWORD error = ERROR_SUCCESS;
TCHAR sysDir[MAX_PATH];

if (!GetSystemDirectory(sysDir, MAX_PATH))
return GetLastError();

std::wstring fullPathName = sysDir;
fullPathName += _T("\\drivers\\");
fullPathName += sysFile;

// Check for existence.
if ((_taccess(fullPathName.c_str(), 0)) == -1)
return (errno);

// The delete may be unsuccessful if
// 1.File has already been deleted.
// 2.File has an open handle
if (!DeleteFile(fullPathName.c_str()))
{
error = GetLastError();
std::wcout << "Failed to delete the driver file:" << fullPathName.c_str() << ",error:" << error << std::endl;
}
else
std::wcout << "Successfully deleted the driver file:" << fullPathName.c_str() << std::endl;

return (error);
}
4 changes: 4 additions & 0 deletions cmd/os/windows/zfsinstaller/zfsinstaller.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <SetupAPI.h>
#include <stdio.h>
#include <winsvc.h>
#include <tchar.h>
#include <iostream>

DWORD zfs_install(char *);
DWORD zfs_uninstall(char *);
Expand All @@ -37,3 +39,5 @@ void printUsage();
DWORD send_zfs_ioc_unregister_fs();
DWORD installRootDevice(char *inf_path, bool IsServiceRunning);
DWORD uninstallRootDevice(char *inf_path);
DWORD DeleteOemInf(const char* inf_name);
DWORD DeleteSysFile(const TCHAR* sysFile);

0 comments on commit 3c8d667

Please sign in to comment.