Opened 14 years ago
Closed 14 years ago
#2260 closed Bugs (invalid)
Missing slash when iterating windows root path
Reported by: | Owned by: | Beman Dawes | |
---|---|---|---|
Milestone: | To Be Determined | Component: | filesystem |
Version: | Boost 1.36.0 | Severity: | Problem |
Keywords: | directory_iterator seperator | Cc: |
Description
A windows root path without backslash or slash, ex. "C:", will cause the result of directory_iterator missing a slash after ':'. For example, it will produce a broken path such as "C:filename".
Here is a sample to produce this:
#include <iostream> #include <locale> #include <boost/filesystem.hpp> using namespace boost::filesystem; int main() { const char *drive_to_search = "C:"; std::cout.imbue( std::locale()); for( directory_iterator i( path( drive_to_search, native)), end; i != end; ++i) { std::cout << i->path() << std::endl; } return 0; }
By tracing the stack, I think the problem is function boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits>::m_append_separator_if_needed() won't append a slash in case of merging windows root path and filename.
Here is the call stack starting at the constructor of directory_iterator.
boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits>::m_append_separator_if_needed() boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits>::operator/=(const wchar_t *) boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits>::operator/=(const boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> & rhs) boost::filesystem::operator/<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits>(const boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> & lhs, const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > & rhs) boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> >::m_init(const boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> & dir_path) boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> >::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> >(const boost::filesystem::basic_path<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,boost::filesystem::wpath_traits> & dir_path)
"C:filename" is perfectly valid. Windows interprets the directory as being the current working directory on the C drive. This is demonstrated from the command line by:
01/11/2009 07:05 PM <DIR> . 01/11/2009 07:05 PM <DIR> .. 01/11/2009 07:12 PM <DIR> Debug 01/11/2009 07:05 PM 4,070 ticket_2260_test.vcproj 01/11/2009 07:15 PM 1,409 ticket_2260_test.vcproj.BGD.Administrator.user
01/11/2009 07:05 PM <DIR> . 01/11/2009 07:05 PM <DIR> .. 01/11/2009 07:12 PM <DIR> Debug 01/11/2009 07:05 PM 4,070 ticket_2260_test.vcproj 01/11/2009 07:15 PM 1,409 ticket_2260_test.vcproj.BGD.Administrator.user
Note the way that "dir c:" works.
If you want C:\ rather than the current C: directory, change line 10 to:
const char *drive_to_search = "C:
";
--Beman