Opened 9 years ago

Last modified 9 years ago

#8946 new Bugs

filesystem::directory_iterator returns incorrect listing

Reported by: l.alebarde@… Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.54.0 Severity: Problem
Keywords: directory_iterator Cc:

Description

Both directory_iterator and recursive_directory_iterator do not work correctly under linux with g++-4.7.3 : one item, assumed to be the last one, is not listed.

Simple code to reproduce the problem :

#include <iostream>
#include <stdio.h>
#include <boost/filesystem.hpp>

using namespace std;
namespace bfs = boost::filesystem;

int main () {
	system("mkdir mydir");
	system("touch mydir/file1");
	system("touch mydir/file2");
	bfs::directory_iterator bfs_dir_i_end;
	bfs::directory_iterator bfs_dir_i("mydir/.");
	while (++bfs_dir_i != bfs_dir_i_end) {
		bfs::file_type type = (*bfs_dir_i).status().type();
		bfs::path source = (*bfs_dir_i).path();
		cout << source << "  " << type << endl;
	}
}

Build with :

g++-4.7.3 -I/usr/include/crypto++ -O0 -g3 -Wall -c -fmessage-length=0  -std=c++11 show_bug_directory_iterator.cpp
g++-4.7.3 -lboost_filesystem -o show_bug_directory_iterator show_bug_directory_iterator.o

Code result :

$ ./show_bug_directory_iterator
"mydir/./file1"  2

Should be :

$ ./show_bug_directory_iterator
"mydir/./file1"  2
"mydir/./file2"  2

Shell result :

$ ls -lR .
.:
total 1620
drwxr-xr-x 2 alain alain    4096 31 juil. 10:48 mydir
-rwxr-xr-x 1 alain alain  639766 31 juil. 10:48 show_bug_directory_iterator
-rw-r--r-- 1 alain alain     497 31 juil. 10:44 show_bug_directory_iterator.cpp
-rw-r--r-- 1 alain alain 1006376 31 juil. 10:48 show_bug_directory_iterator.o

./mydir:
total 0
-rw-r--r-- 1 alain alain 0 31 juil. 10:48 file1
-rw-r--r-- 1 alain alain 0 31 juil. 10:48 file2

Notes :

1) Not tested under other gcc versions.

2) Tested with same results on Boost 1.52.0 and 1.53.0

3) suspected regression, cf https://svn.boost.org/trac/boost/ticket/257

Change History (4)

comment:1 by anonymous, 9 years ago

Version: Boost 1.53.0Boost 1.54.0

in reply to:  1 comment:2 by l.alebarde@…, 9 years ago

Replying to anonymous: Sorry, change made by myself. So, same result on versions 1.52.0, 1.53.0, and 1.54.0

comment:3 by nikita.trophimov@…, 9 years ago

You need to increment bfs_dir_i after all of the actions in the loop:

#include <iostream>
#include <stdio.h>
#include <boost/filesystem.hpp>

using namespace std;
namespace bfs = boost::filesystem;

int main () {
	system("mkdir mydir");
	system("touch mydir/file1");
	system("touch mydir/file2");
	bfs::directory_iterator bfs_dir_i_end;
	bfs::directory_iterator bfs_dir_i("mydir");
	while (bfs_dir_i != bfs_dir_i_end) {
		bfs::file_type type = (*bfs_dir_i).status().type();
		bfs::path source = (*bfs_dir_i).path();
		cout << source << "  " << type << endl;
		++bfs_dir_i;
	}
}

comment:4 by l.alebarde@…, 9 years ago

Thanks nikita. The documentation should be modified then (http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-directory_iterator):

To iterate over the current directory, use directory_iterator(".")

This lead me to increment first the iterator.

Note: See TracTickets for help on using tickets.