78 lines
2.6 KiB
C++
78 lines
2.6 KiB
C++
#include "files_folder_size.h"
|
|
|
|
#include <windows.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
|
|
namespace fs=std::filesystem;
|
|
using ks::files::measureNtfsFolder;
|
|
|
|
namespace
|
|
{
|
|
struct Fixture
|
|
{
|
|
fs::path path=fs::temp_directory_path()/
|
|
(L"ks-files-size-"+std::to_wstring(GetCurrentProcessId())+L"-"+std::to_wstring(GetTickCount64()));
|
|
Fixture() {fs::create_directory(path);}
|
|
~Fixture() {std::error_code error;fs::remove_all(path,error);}
|
|
};
|
|
|
|
void write(const fs::path &path,const char *data)
|
|
{
|
|
std::ofstream file(path,std::ios::binary);
|
|
if(!(file<<data)) throw std::runtime_error("Could not create scan fixture.");
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
try
|
|
{
|
|
Fixture fixture;
|
|
wchar_t filesystem[32]{};
|
|
if(!GetVolumeInformationW(fixture.path.root_path().c_str(),nullptr,0,nullptr,nullptr,nullptr,
|
|
filesystem,static_cast<DWORD>(std::size(filesystem))) || std::wstring_view(filesystem)!=L"NTFS")
|
|
{
|
|
std::cout<<"Native NTFS scan unavailable on the temporary volume.\n";
|
|
return 77;
|
|
}
|
|
const auto root=fixture.path/L"selected";
|
|
const auto empty=root/L"empty";
|
|
fs::create_directories(empty);
|
|
const auto emptySize=measureNtfsFolder(empty,{});
|
|
if(!emptySize || emptySize->bytes || emptySize->partial) throw std::runtime_error("Empty NTFS folder.");
|
|
const auto source=root/L"source.bin";
|
|
write(source,"abc");
|
|
const auto branch=root/L"nested";
|
|
fs::create_directory(branch);
|
|
fs::create_hard_link(source,branch/L"alias.bin");
|
|
// Force more than one NTFS page and several concurrent directory jobs.
|
|
for(int i=0;i<600;++i)
|
|
{
|
|
const auto suffix=std::to_wstring(i);
|
|
write(branch/(L"long-filename-0123456789-"+suffix+L".txt"),"x");
|
|
if(i<8)
|
|
{
|
|
const auto sibling=root/(L"sibling-"+suffix);
|
|
fs::create_directory(sibling);
|
|
write(sibling/L"payload","12");
|
|
}
|
|
}
|
|
const auto total=measureNtfsFolder(root,{});
|
|
if(!total || total->partial || total->bytes!=622)
|
|
throw std::runtime_error("NTFS scan must sum all pages, branches and hard-link names.");
|
|
std::stop_source canceled;
|
|
canceled.request_stop();
|
|
const auto stopped=measureNtfsFolder(root,canceled.get_token());
|
|
if(!stopped || stopped->bytes) throw std::runtime_error("Canceled scan counted files.");
|
|
std::cout<<"NTFS folder sizes passed.\n";
|
|
return 0;
|
|
}
|
|
catch(const std::exception &error)
|
|
{
|
|
std::cerr<<error.what()<<'\n';
|
|
return 1;
|
|
}
|
|
}
|