You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2 KiB

#include <Windows.h>
#include <psapi.h>
#include <locale>
#include <codecvt>
#include <string>
#include "fdelete.hpp"
namespace fs = std::filesystem;
auto delete_dir(vdm::vdm_ctx* vdm, const wchar_t* dir_path) -> bool
{
for (auto& file : fs::directory_iterator(dir_path))
{
std::wstring string_path = file.path().wstring();
if (file.is_directory())
{
if (!delete_dir(vdm, string_path.data()))
return false;
}
else
{
if (!fdelete::remove(vdm, string_path.data()))
return false;
else
std::printf("deleted %ws\n", string_path.data());
}
}
try
{
fs::remove(fs::path(dir_path));
}
catch (fs::filesystem_error& fs_error)
{
std::printf("failed to delete directory... reason: %s\n",
fs_error.code().message().c_str());
}
return true;
}
auto main(int argc, char** argv) -> int
{
if (argc < 2 || (strcmp(argv[1], "--file") && strcmp(argv[1], "--dir")))
{
std::printf("usage: [OPTION] [File Path/Directory Path]\n");
std::printf(" --file, delete a specific file...\n");
std::printf(" --dir, delete an entire directory...\n");
return -1;
}
auto [drv_handle, drv_key, drv_status] = vdm::load_drv();
if (drv_status != STATUS_SUCCESS or drv_handle == INVALID_HANDLE_VALUE)
{
std::printf("load driver failed... reason: 0x%x\n", drv_status);
return -1;
}
std::string file_path(argv[2]);
if (file_path.substr(0, 4).compare("\\??\\") != 0)
file_path = "\\??\\" + file_path;
vdm::read_phys_t _read_phys =
[&](void* addr, void* buffer, std::size_t size) -> bool
{
return vdm::read_phys(addr, buffer, size);
};
vdm::write_phys_t _write_phys =
[&](void* addr, void* buffer, std::size_t size) -> bool
{
return vdm::write_phys(addr, buffer, size);
};
vdm::vdm_ctx vdm(_read_phys, _write_phys);
std::wstring wfile_path(file_path.begin(), file_path.end());
if (strcmp(argv[1], "--file") == 0)
std::printf("delete %ws result: %d\n",
wfile_path.data(), fdelete::remove(&vdm, wfile_path.data()));
if (strcmp(argv[1], "--dir") == 0)
std::printf("deleted %ws result: %d\n",
wfile_path.data(), delete_dir(&vdm, wfile_path.data()));
if ((drv_status = vdm::unload_drv(drv_handle, drv_key)) != STATUS_SUCCESS)
{
std::printf("failed to unload driver... reason: 0x%x\n", drv_status);
return -1;
}
}