Fixed bugs in scanFolder() and fileIterator.

This commit is contained in:
Vincent Richard 2004-12-19 00:05:44 +00:00
parent 5c8333cb1e
commit 510979b5ef
3 changed files with 62 additions and 18 deletions

View File

@ -32,7 +32,8 @@ namespace messaging {
maildirFolder::maildirFolder(const folder::path& path, maildirStore* store) maildirFolder::maildirFolder(const folder::path& path, maildirStore* store)
: m_store(store), m_path(path), m_name(path.getLastComponent()), m_mode(-1), m_open(false) : m_store(store), m_path(path), m_name(path.getLastComponent()), m_mode(-1), m_open(false),
m_unreadMessageCount(0), m_messageCount(0)
{ {
m_store->registerFolder(this); m_store->registerFolder(this);
} }
@ -261,6 +262,9 @@ void maildirFolder::scanFolder()
{ {
try try
{ {
m_messageCount = 0;
m_unreadMessageCount = 0;
utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory(); utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();
utility::file::path newDirPath = maildirUtils::getFolderFSPath utility::file::path newDirPath = maildirUtils::getFolderFSPath
@ -272,7 +276,7 @@ void maildirFolder::scanFolder()
utility::auto_ptr <utility::file> curDir = fsf->create(curDirPath); utility::auto_ptr <utility::file> curDir = fsf->create(curDirPath);
// New received messages (new/) // New received messages (new/)
utility::auto_ptr <utility::fileIterator> nit = newDir->getFiles(); utility::fileIterator* nit = newDir->getFiles();
std::vector <utility::file::path::component> newMessageFilenames; std::vector <utility::file::path::component> newMessageFilenames;
while (nit->hasMoreElements()) while (nit->hasMoreElements())
@ -281,8 +285,10 @@ void maildirFolder::scanFolder()
newMessageFilenames.push_back(file->getFullPath().getLastComponent()); newMessageFilenames.push_back(file->getFullPath().getLastComponent());
} }
delete (nit); // Free directory
// Current messages (cur/) // Current messages (cur/)
utility::auto_ptr <utility::fileIterator> cit = curDir->getFiles(); utility::fileIterator* cit = curDir->getFiles();
std::vector <utility::file::path::component> curMessageFilenames; std::vector <utility::file::path::component> curMessageFilenames;
while (cit->hasMoreElements()) while (cit->hasMoreElements())
@ -291,6 +297,8 @@ void maildirFolder::scanFolder()
curMessageFilenames.push_back(file->getFullPath().getLastComponent()); curMessageFilenames.push_back(file->getFullPath().getLastComponent());
} }
delete (cit); // Free directory
// Update/delete existing messages (found in previous scan) // Update/delete existing messages (found in previous scan)
for (unsigned int i = 0 ; i < m_messageInfos.size() ; ++i) for (unsigned int i = 0 ; i < m_messageInfos.size() ; ++i)
{ {
@ -322,6 +330,9 @@ void maildirFolder::scanFolder()
} }
} }
m_messageInfos.reserve(m_messageInfos.size()
+ newMessageFilenames.size() + curMessageFilenames.size());
// Add new messages from 'new': we are responsible to move the files // Add new messages from 'new': we are responsible to move the files
// from the 'new' directory to the 'cur' directory, and append them // from the 'new' directory to the 'cur' directory, and append them
// to our message list. // to our message list.
@ -458,28 +469,38 @@ void maildirFolder::listFolders(std::vector <folder*>& list, const bool recursiv
utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory(); utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();
utility::auto_ptr <utility::file> rootDir = fsf->create utility::auto_ptr <utility::file> rootDir = fsf->create
(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_CONTAINER)); (maildirUtils::getFolderFSPath(m_store, m_path,
utility::auto_ptr <utility::fileIterator> it = rootDir->getFiles(); m_path.isEmpty() ? maildirUtils::FOLDER_PATH_ROOT
: maildirUtils::FOLDER_PATH_CONTAINER));
while (it->hasMoreElements()) if (rootDir->exists())
{ {
utility::auto_ptr <utility::file> file = it->nextElement(); utility::auto_ptr <utility::fileIterator> it = rootDir->getFiles();
if (maildirUtils::isSubfolderDirectory(*file)) while (it->hasMoreElements())
{ {
const utility::path subPath = m_path / file->getFullPath().getLastComponent(); utility::auto_ptr <utility::file> file = it->nextElement();
maildirFolder* subFolder = new maildirFolder(subPath, m_store);
list.push_back(subFolder); if (maildirUtils::isSubfolderDirectory(*file))
{
const utility::path subPath = m_path / file->getFullPath().getLastComponent();
maildirFolder* subFolder = new maildirFolder(subPath, m_store);
if (recursive) list.push_back(subFolder);
subFolder->listFolders(list, true);
if (recursive)
subFolder->listFolders(list, true);
}
} }
} }
else
{
// No sub-folder
}
} }
catch (exceptions::filesystem_exception& e) catch (exceptions::filesystem_exception& e)
{ {
throw exceptions::command_error("LIST", e.what()); throw exceptions::command_error("LIST", "", "", e);
} }
} }

View File

@ -28,6 +28,8 @@
#include <dirent.h> #include <dirent.h>
#include <string.h>
#include "../../exception.hpp" #include "../../exception.hpp"
@ -49,7 +51,7 @@ posixFileIterator::posixFileIterator(const vmime::utility::file::path& path, con
if ((m_dir = ::opendir(m_nativePath.c_str())) == NULL) if ((m_dir = ::opendir(m_nativePath.c_str())) == NULL)
posixFileSystemFactory::reportError(path, errno); posixFileSystemFactory::reportError(path, errno);
m_dirEntry = ::readdir(m_dir); getNextElement();
} }
@ -70,12 +72,28 @@ vmime::utility::file* posixFileIterator::nextElement()
{ {
posixFile* file = new posixFile(m_path / vmime::utility::file::path::component(m_dirEntry->d_name)); posixFile* file = new posixFile(m_path / vmime::utility::file::path::component(m_dirEntry->d_name));
m_dirEntry = ::readdir(m_dir); getNextElement();
return (file); return (file);
} }
void posixFileIterator::getNextElement()
{
while (m_dirEntry = ::readdir(m_dir))
{
const char* name = m_dirEntry->d_name;
const int len = ::strlen(name);
if (!(len == 1 && name[0] == '.') &&
!(len == 2 && name[0] == '.' && name[1] == '.'))
{
break;
}
}
}
// //
// posixFileWriterOutputStream // posixFileWriterOutputStream
@ -399,6 +417,9 @@ const vmime::utility::file::path posixFileSystemFactory::stringToPathImpl(const
offset++; offset++;
} }
if (prev < str.length())
path.appendComponent(vmime::string(str.begin() + prev, str.end()));
return (path); return (path);
} }
@ -409,7 +430,7 @@ const vmime::string posixFileSystemFactory::pathToStringImpl(const vmime::utilit
for (int i = 0 ; i < path.getSize() ; ++i) for (int i = 0 ; i < path.getSize() ; ++i)
{ {
if (i >= 0) if (i > 0)
native += "/"; native += "/";
native += path[i].getBuffer(); native += path[i].getBuffer();
@ -460,7 +481,7 @@ void posixFileSystemFactory::reportError(const vmime::utility::path& path, const
default: default:
std::ostringstream oss; std::ostringstream oss;
oss << "Unknown error " << err << "."; oss << ::strerror(err) << ".";
desc = oss.str(); desc = oss.str();
break; break;

View File

@ -121,6 +121,8 @@ public:
private: private:
void getNextElement();
vmime::utility::file::path m_path; vmime::utility::file::path m_path;
vmime::string m_nativePath; vmime::string m_nativePath;