Skip to content

<filesystem>: directory_iterator(...) and is_empty(...) not working with empty volumes  #4291

Closed
@MartinKuschnik

Description

@MartinKuschnik

I'm woking with virtual disks and have some trouble if a new volume does not contain any files or directories.

First we have to ceate and attch a new virtual disk:

New-VHD -Path "$env:TEMP\test_volume.vhdx" -SizeBytes 100MB | Mount-VHD -Passthru -NoDriveLetter |Initialize-Disk -Passthru | New-Partition -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false -Force | Format-List -Property Path

The command should return the volume name

Path : \\?\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\

that can be used to access it:

#include <filesystem> 
#include <iostream> 

int wmain(int argc)
{
	constexpr const char* volume = "\\\\?\\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\\";

	std::cout 
		<< std::format("std::filesystem::exists('{0}')) : {1}", volume, std::filesystem::exists(volume)) 
		<< std::endl;

	try
	{
		std::cout 
			<< std::format("std::filesystem::is_empty('{0}')) : {1}", volume, std::filesystem::is_empty(volume)) 
			<< std::endl;
	}
	catch (const std::exception& ex)
	{
		std::cout 
			<< std::format("std::filesystem::is_empty('{0}')) failed: {1}", volume, ex.what()) 
			<< std::endl;
	}

	try
	{
		const auto it = std::filesystem::directory_iterator(volume);

		std::cout 
			<< std::format("std::filesystem::directory_iterator('{0}')) succeeded.", volume) 
			<< std::endl;
	}
	catch (const std::exception& ex)
	{
		std::cout 
			<< std::format("std::filesystem::directory_iterator('{0}')) failed: {1}", volume, ex.what()) 
			<< std::endl;
	}
}

This code will produce the following output:

std::filesystem::exists('\\?\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\')) : true
std::filesystem::is_empty('\\?\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\')) failed: is_empty: The system cannot find the file specified.: "\\?\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\"
std::filesystem::directory_iterator('\\?\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\')) failed: directory_iterator::directory_iterator: The system cannot find the file specified.: "\\?\Volume{069de63e-8072-42fe-9adf-5a9b9cec201d}\"

Expected behavior

  • std::filesystem::is_empty(...) should not throw any exception for an empty volume.
  • std::filesystem::directory_iterator(...) should not throw any exception for an empty volume.

STL version

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.8.3

Additional context

After asigning a drive letter or creating a file or directory the code workes like expacted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfilesystemC++17 filesystemfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions