From 8b8cf2b54361041a7af454973ff72b633ffdf0a3 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sun, 26 Dec 2004 23:22:07 +0000 Subject: Renamed files in POSIX platform handler. --- src/platforms/posix/file.cpp | 500 ----------------------------------- src/platforms/posix/handler.cpp | 202 -------------- src/platforms/posix/posixFile.cpp | 500 +++++++++++++++++++++++++++++++++++ src/platforms/posix/posixHandler.cpp | 202 ++++++++++++++ src/platforms/posix/posixSocket.cpp | 191 +++++++++++++ src/platforms/posix/socket.cpp | 191 ------------- 6 files changed, 893 insertions(+), 893 deletions(-) delete mode 100644 src/platforms/posix/file.cpp delete mode 100644 src/platforms/posix/handler.cpp create mode 100644 src/platforms/posix/posixFile.cpp create mode 100644 src/platforms/posix/posixHandler.cpp create mode 100644 src/platforms/posix/posixSocket.cpp delete mode 100644 src/platforms/posix/socket.cpp (limited to 'src/platforms') diff --git a/src/platforms/posix/file.cpp b/src/platforms/posix/file.cpp deleted file mode 100644 index 2854f54d..00000000 --- a/src/platforms/posix/file.cpp +++ /dev/null @@ -1,500 +0,0 @@ -// -// VMime library (http://vmime.sourceforge.net) -// Copyright (C) 2002-2004 Vincent Richard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// - -#include "vmime/platforms/posix/file.hpp" - -#include -#include -#include - -#include -#include - -#include - -#include - -#include "vmime/exception.hpp" - - -#if VMIME_HAVE_FILESYSTEM_FEATURES - - -namespace vmime { -namespace platforms { -namespace posix { - - -// -// posixFileIterator -// - -posixFileIterator::posixFileIterator(const vmime::utility::file::path& path, const vmime::string& nativePath) - : m_path(path), m_nativePath(nativePath), m_dir(NULL), m_dirEntry(NULL) -{ - if ((m_dir = ::opendir(m_nativePath.c_str())) == NULL) - posixFileSystemFactory::reportError(path, errno); - - getNextElement(); -} - - -posixFileIterator::~posixFileIterator() -{ - if (m_dir != NULL) - ::closedir(m_dir); -} - - -const bool posixFileIterator::hasMoreElements() const -{ - return (m_dirEntry != NULL); -} - - -vmime::utility::file* posixFileIterator::nextElement() -{ - posixFile* file = new posixFile(m_path / vmime::utility::file::path::component(m_dirEntry->d_name)); - - getNextElement(); - - 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::posixFileWriterOutputStream(const vmime::utility::file::path& path, const int fd) - : m_path(path), m_fd(fd) -{ -} - - -posixFileWriterOutputStream::~posixFileWriterOutputStream() -{ - ::close(m_fd); -} - - -void posixFileWriterOutputStream::write(const value_type* const data, const size_type count) -{ - if (::write(m_fd, data, count) == -1) - posixFileSystemFactory::reportError(m_path, errno); -} - - - -// -// posixFileReaderInputStream -// - -posixFileReaderInputStream::posixFileReaderInputStream(const vmime::utility::file::path& path, const int fd) - : m_path(path), m_fd(fd), m_eof(false) -{ -} - - -posixFileReaderInputStream::~posixFileReaderInputStream() -{ - ::close(m_fd); -} - - -const bool posixFileReaderInputStream::eof() const -{ - return (m_eof); -} - - -void posixFileReaderInputStream::reset() -{ - ::lseek(m_fd, 0, SEEK_SET); -} - - -const vmime::utility::stream::size_type posixFileReaderInputStream::read - (value_type* const data, const size_type count) -{ - ssize_t c = 0; - - if ((c = ::read(m_fd, data, count)) == -1) - posixFileSystemFactory::reportError(m_path, errno); - - if (c == 0) - m_eof = true; - - return static_cast (c); -} - - -const vmime::utility::stream::size_type posixFileReaderInputStream::skip(const size_type count) -{ - const off_t curPos = ::lseek(m_fd, 0, SEEK_CUR); - const off_t newPos = ::lseek(m_fd, count, SEEK_CUR); - - return static_cast (newPos - curPos); -} - - - -// -// posixFileWriter -// - -posixFileWriter::posixFileWriter(const vmime::utility::file::path& path, const vmime::string& nativePath) - : m_path(path), m_nativePath(nativePath) -{ -} - - -vmime::utility::outputStream* posixFileWriter::getOutputStream() -{ - int fd = 0; - - if ((fd = ::open(m_nativePath.c_str(), O_WRONLY, 0660)) == -1) - posixFileSystemFactory::reportError(m_path, errno); - - return new posixFileWriterOutputStream(m_path, fd); -} - - - -// -// posixFileReader -// - -posixFileReader::posixFileReader(const vmime::utility::file::path& path, const vmime::string& nativePath) - : m_path(path), m_nativePath(nativePath) -{ -} - - -vmime::utility::inputStream* posixFileReader::getInputStream() -{ - int fd = 0; - - if ((fd = ::open(m_nativePath.c_str(), O_RDONLY, 0640)) == -1) - posixFileSystemFactory::reportError(m_path, errno); - - return new posixFileReaderInputStream(m_path, fd); -} - - - -// -// posixFile -// - -posixFile::posixFile(const vmime::utility::file::path& path) - : m_path(path), m_nativePath(posixFileSystemFactory::pathToStringImpl(path)) -{ -} - - -void posixFile::createFile() -{ - int fd = 0; - - if ((fd = ::open(m_nativePath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660)) == -1) - posixFileSystemFactory::reportError(m_path, errno); - - ::close(fd); -} - - -void posixFile::createDirectory(const bool createAll) -{ - createDirectoryImpl(m_path, m_path, createAll); -} - - -const bool posixFile::isFile() const -{ - struct stat buf; - return (::stat(m_nativePath.c_str(), &buf) == 0 && S_ISREG(buf.st_mode)); -} - - -const bool posixFile::isDirectory() const -{ - struct stat buf; - return (::stat(m_nativePath.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)); -} - - -const bool posixFile::canRead() const -{ - struct stat buf; - return (::stat(m_nativePath.c_str(), &buf) == 0 && - S_ISREG(buf.st_mode) && - ::access(m_nativePath.c_str(), R_OK | F_OK) == 0); -} - - -const bool posixFile::canWrite() const -{ - struct stat buf; - return (::stat(m_nativePath.c_str(), &buf) == 0 && - S_ISREG(buf.st_mode) && - ::access(m_nativePath.c_str(), W_OK | F_OK) == 0); -} - - -const posixFile::length_type posixFile::getLength() -{ - struct stat buf; - - if (::stat(m_nativePath.c_str(), &buf) != 0) - posixFileSystemFactory::reportError(m_path, errno); - - return static_cast (buf.st_size); -} - - -const posixFile::path& posixFile::getFullPath() const -{ - return (m_path); -} - - -const bool posixFile::exists() const -{ - struct stat buf; - return (::stat(m_nativePath.c_str(), &buf) == 0); -} - - -const vmime::utility::file* posixFile::getParent() const -{ - if (m_path.isEmpty()) - return NULL; - else - return new posixFile(m_path.getParent()); -} - - -void posixFile::rename(const path& newName) -{ - const vmime::string newNativePath = posixFileSystemFactory::pathToStringImpl(newName); - - if (::rename(m_nativePath.c_str(), newNativePath.c_str()) != 0) - posixFileSystemFactory::reportError(m_path, errno); - - m_path = newName; - m_nativePath = newNativePath; -} - - -void posixFile::remove() -{ - struct stat buf; - - if (::stat(m_nativePath.c_str(), &buf) != 0) - posixFileSystemFactory::reportError(m_path, errno); - - if (S_ISDIR(buf.st_mode)) - { - if (::rmdir(m_nativePath.c_str()) != 0) - posixFileSystemFactory::reportError(m_path, errno); - } - else if (S_ISREG(buf.st_mode)) - { - if (::unlink(m_nativePath.c_str()) != 0) - posixFileSystemFactory::reportError(m_path, errno); - } -} - - -vmime::utility::fileWriter* posixFile::getFileWriter() -{ - return new posixFileWriter(m_path, m_nativePath); -} - - -vmime::utility::fileReader* posixFile::getFileReader() -{ - return new posixFileReader(m_path, m_nativePath); -} - - -vmime::utility::fileIterator* posixFile::getFiles() const -{ - if (!isDirectory()) - throw vmime::exceptions::not_a_directory(m_path); - - return new posixFileIterator(m_path, m_nativePath); -} - - -void posixFile::createDirectoryImpl(const vmime::utility::file::path& fullPath, - const vmime::utility::file::path& path, const bool recursive) -{ - const vmime::string nativePath = posixFileSystemFactory::pathToStringImpl(path); - struct stat buf; - - if (::stat(nativePath.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)) - return; - - if (!path.isEmpty() && recursive) - createDirectoryImpl(fullPath, path.getParent(), true); - - if (::mkdir(nativePath.c_str(), 0750) != 0) - posixFileSystemFactory::reportError(fullPath, errno); -} - - - -// -// posixFileSystemFactory -// - -vmime::utility::file* posixFileSystemFactory::create(const vmime::utility::file::path& path) const -{ - return new posixFile(path); -} - - -const vmime::utility::file::path posixFileSystemFactory::stringToPath(const vmime::string& str) const -{ - return (stringToPathImpl(str)); -} - - -const vmime::string posixFileSystemFactory::pathToString(const vmime::utility::file::path& path) const -{ - return (pathToStringImpl(path)); -} - - -const vmime::utility::file::path posixFileSystemFactory::stringToPathImpl(const vmime::string& str) -{ - vmime::string::size_type offset = 0; - vmime::string::size_type prev = 0; - - vmime::utility::file::path path; - - while ((offset = str.find_first_of("/", offset)) != vmime::string::npos) - { - if (offset != prev) - path.appendComponent(vmime::string(str.begin() + prev, str.begin() + offset)); - - prev = offset + 1; - offset++; - } - - if (prev < str.length()) - path.appendComponent(vmime::string(str.begin() + prev, str.end())); - - return (path); -} - - -const vmime::string posixFileSystemFactory::pathToStringImpl(const vmime::utility::file::path& path) -{ - vmime::string native = "/"; - - for (int i = 0 ; i < path.getSize() ; ++i) - { - if (i > 0) - native += "/"; - - native += path[i].getBuffer(); - } - - return (native); -} - - -const bool posixFileSystemFactory::isValidPathComponent(const vmime::utility::file::path::component& comp) const -{ - return (comp.getBuffer().find_first_of("/*") == vmime::string::npos); -} - - -const bool posixFileSystemFactory::isValidPath(const vmime::utility::file::path& path) const -{ - for (int i = 0 ; i < path.getSize() ; ++i) - { - if (!isValidPathComponent(path[i])) - return false; - } - - return true; -} - - -void posixFileSystemFactory::reportError(const vmime::utility::path& path, const int err) -{ - vmime::string desc; - - switch (err) - { - case EEXIST: desc = "EEXIST: file already exists."; break; - case EISDIR: desc = "EISDIR: path refers to a directory."; break; - case EACCES: desc = "EACCES: permission denied"; break; - case ENAMETOOLONG: desc = "ENAMETOOLONG: path too long."; break; - case ENOENT: desc = "ENOENT: a directory in the path does not exist."; break; - case ENOTDIR: desc = "ENOTDIR: path is not a directory."; break; - case EROFS: desc = "EROFS: read-only filesystem."; break; - case ELOOP: desc = "ELOOP: too many symbolic links."; break; - case ENOSPC: desc = "ENOSPC: no space left on device."; break; - case ENOMEM: desc = "ENOMEM: insufficient kernel memory."; break; - case EMFILE: desc = "ENFILE: limit on number of files open by the process has been reached."; break; - case ENFILE: desc = "ENFILE: limit on number of files open on the system has been reached."; break; - case ENOTEMPTY: desc = "ENOTEMPTY: directory is not empty."; break; - - default: - - std::ostringstream oss; - oss << ::strerror(err) << "."; - - desc = oss.str(); - break; - } - - throw vmime::exceptions::filesystem_exception(desc, path); -} - - - -} // posix -} // platforms -} // vmime - - -#endif // VMIME_HAVE_FILESYSTEM_FEATURES diff --git a/src/platforms/posix/handler.cpp b/src/platforms/posix/handler.cpp deleted file mode 100644 index 1084d59e..00000000 --- a/src/platforms/posix/handler.cpp +++ /dev/null @@ -1,202 +0,0 @@ -// -// VMime library (http://vmime.sourceforge.net) -// Copyright (C) 2002-2004 Vincent Richard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// - -#include "vmime/platforms/posix/handler.hpp" - -#include - -#include -#include -#include -#include - -#include -#include - -#include - -#include - - -namespace vmime { -namespace platforms { -namespace posix { - - -posixHandler::posixHandler() -{ -#if VMIME_HAVE_MESSAGING_FEATURES - m_socketFactory = new posixSocketFactory(); -#endif -#if VMIME_HAVE_FILESYSTEM_FEATURES - m_fileSysFactory = new posixFileSystemFactory(); -#endif -} - - -posixHandler::~posixHandler() -{ -#if VMIME_HAVE_MESSAGING_FEATURES - delete (m_socketFactory); -#endif -#if VMIME_HAVE_FILESYSTEM_FEATURES - delete (m_fileSysFactory); -#endif -} - - -const unsigned int posixHandler::getUnixTime() const -{ - return ::time(NULL); -} - - -const vmime::datetime posixHandler::getCurrentLocalTime() const -{ - const time_t t(::time(NULL)); - - // Get the local time -#ifdef _REENTRANT - tm local; - ::localtime_r(&t, &local); -#else - tm local = *::localtime(&t); // WARNING: this is not thread-safe! -#endif - - // Get the UTC time -#ifdef _REENTRANT - tm gmt; - ::gmtime_r(&t, &gmt); -#else - tm gmt = *::gmtime(&t); // WARNING: this is not thread-safe! -#endif - - // "A negative value for tm_isdst causes mktime() to attempt - // to determine whether Daylight Saving Time is in effect - // for the specified time." - local.tm_isdst = -1; - gmt.tm_isdst = -1; - - // Calculate the difference (in seconds) - const int diff = ::mktime(&local) - ::mktime(&gmt); - - // Return the date - return vmime::datetime(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, - local.tm_hour, local.tm_min, local.tm_sec, diff / 60); // minutes needed -} - - -const vmime::charset posixHandler::getLocaleCharset() const -{ - vmime::string prevLocale(::setlocale(::LC_ALL, "")); - vmime::charset ch(::nl_langinfo(::CODESET)); - ::setlocale(::LC_ALL, prevLocale.c_str()); - - return (ch); -} - - -const vmime::string posixHandler::getHostName() const -{ - std::vector hostnames; - char buffer[256]; - - // Try with 'gethostname' - ::gethostname(buffer, sizeof(buffer)); - buffer[sizeof(buffer) - 1] = '\0'; - - if (::strlen(buffer) == 0) - ::strcpy(buffer, "localhost"); - - hostnames.push_back(buffer); - - // And with 'gethostbyname' - struct hostent* he = ::gethostbyname(buffer); - - if (he != NULL) - { - if (::strlen(he->h_name) != 0) - hostnames.push_back(he->h_name); - - char** alias = he->h_aliases; - - while (alias && *alias) - { - if (::strlen(*alias) != 0) - hostnames.push_back(*alias); - - ++alias; - } - } - - // Find a Fully-Qualified Domain Name (FQDN) - for (unsigned int i = 0 ; i < hostnames.size() ; ++i) - { - if (hostnames[i].find_first_of(".") != vmime::string::npos) - return (hostnames[i]); - } - - return (hostnames[0]); -} - - -const unsigned int posixHandler::getProcessId() const -{ - return (::getpid()); -} - - -#if VMIME_HAVE_MESSAGING_FEATURES - -vmime::messaging::socketFactory* posixHandler::getSocketFactory - (const vmime::string& /* name */) const -{ - return (m_socketFactory); -} - - -vmime::messaging::timeoutHandlerFactory* posixHandler::getTimeoutHandlerFactory - (const vmime::string& /* name */) const -{ - // Not used by default - return (NULL); -} - -#endif - - -#if VMIME_HAVE_FILESYSTEM_FEATURES - -vmime::utility::fileSystemFactory* posixHandler::getFileSystemFactory() const -{ - return (m_fileSysFactory); -} - -#endif - - -void posixHandler::wait() const -{ - ::sleep(1); -} - - -} // posix -} // platforms -} // vmime diff --git a/src/platforms/posix/posixFile.cpp b/src/platforms/posix/posixFile.cpp new file mode 100644 index 00000000..fa5c4ae9 --- /dev/null +++ b/src/platforms/posix/posixFile.cpp @@ -0,0 +1,500 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "vmime/platforms/posix/posixFile.hpp" + +#include +#include +#include + +#include +#include + +#include + +#include + +#include "vmime/exception.hpp" + + +#if VMIME_HAVE_FILESYSTEM_FEATURES + + +namespace vmime { +namespace platforms { +namespace posix { + + +// +// posixFileIterator +// + +posixFileIterator::posixFileIterator(const vmime::utility::file::path& path, const vmime::string& nativePath) + : m_path(path), m_nativePath(nativePath), m_dir(NULL), m_dirEntry(NULL) +{ + if ((m_dir = ::opendir(m_nativePath.c_str())) == NULL) + posixFileSystemFactory::reportError(path, errno); + + getNextElement(); +} + + +posixFileIterator::~posixFileIterator() +{ + if (m_dir != NULL) + ::closedir(m_dir); +} + + +const bool posixFileIterator::hasMoreElements() const +{ + return (m_dirEntry != NULL); +} + + +vmime::utility::file* posixFileIterator::nextElement() +{ + posixFile* file = new posixFile(m_path / vmime::utility::file::path::component(m_dirEntry->d_name)); + + getNextElement(); + + 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::posixFileWriterOutputStream(const vmime::utility::file::path& path, const int fd) + : m_path(path), m_fd(fd) +{ +} + + +posixFileWriterOutputStream::~posixFileWriterOutputStream() +{ + ::close(m_fd); +} + + +void posixFileWriterOutputStream::write(const value_type* const data, const size_type count) +{ + if (::write(m_fd, data, count) == -1) + posixFileSystemFactory::reportError(m_path, errno); +} + + + +// +// posixFileReaderInputStream +// + +posixFileReaderInputStream::posixFileReaderInputStream(const vmime::utility::file::path& path, const int fd) + : m_path(path), m_fd(fd), m_eof(false) +{ +} + + +posixFileReaderInputStream::~posixFileReaderInputStream() +{ + ::close(m_fd); +} + + +const bool posixFileReaderInputStream::eof() const +{ + return (m_eof); +} + + +void posixFileReaderInputStream::reset() +{ + ::lseek(m_fd, 0, SEEK_SET); +} + + +const vmime::utility::stream::size_type posixFileReaderInputStream::read + (value_type* const data, const size_type count) +{ + ssize_t c = 0; + + if ((c = ::read(m_fd, data, count)) == -1) + posixFileSystemFactory::reportError(m_path, errno); + + if (c == 0) + m_eof = true; + + return static_cast (c); +} + + +const vmime::utility::stream::size_type posixFileReaderInputStream::skip(const size_type count) +{ + const off_t curPos = ::lseek(m_fd, 0, SEEK_CUR); + const off_t newPos = ::lseek(m_fd, count, SEEK_CUR); + + return static_cast (newPos - curPos); +} + + + +// +// posixFileWriter +// + +posixFileWriter::posixFileWriter(const vmime::utility::file::path& path, const vmime::string& nativePath) + : m_path(path), m_nativePath(nativePath) +{ +} + + +vmime::utility::outputStream* posixFileWriter::getOutputStream() +{ + int fd = 0; + + if ((fd = ::open(m_nativePath.c_str(), O_WRONLY, 0660)) == -1) + posixFileSystemFactory::reportError(m_path, errno); + + return new posixFileWriterOutputStream(m_path, fd); +} + + + +// +// posixFileReader +// + +posixFileReader::posixFileReader(const vmime::utility::file::path& path, const vmime::string& nativePath) + : m_path(path), m_nativePath(nativePath) +{ +} + + +vmime::utility::inputStream* posixFileReader::getInputStream() +{ + int fd = 0; + + if ((fd = ::open(m_nativePath.c_str(), O_RDONLY, 0640)) == -1) + posixFileSystemFactory::reportError(m_path, errno); + + return new posixFileReaderInputStream(m_path, fd); +} + + + +// +// posixFile +// + +posixFile::posixFile(const vmime::utility::file::path& path) + : m_path(path), m_nativePath(posixFileSystemFactory::pathToStringImpl(path)) +{ +} + + +void posixFile::createFile() +{ + int fd = 0; + + if ((fd = ::open(m_nativePath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660)) == -1) + posixFileSystemFactory::reportError(m_path, errno); + + ::close(fd); +} + + +void posixFile::createDirectory(const bool createAll) +{ + createDirectoryImpl(m_path, m_path, createAll); +} + + +const bool posixFile::isFile() const +{ + struct stat buf; + return (::stat(m_nativePath.c_str(), &buf) == 0 && S_ISREG(buf.st_mode)); +} + + +const bool posixFile::isDirectory() const +{ + struct stat buf; + return (::stat(m_nativePath.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)); +} + + +const bool posixFile::canRead() const +{ + struct stat buf; + return (::stat(m_nativePath.c_str(), &buf) == 0 && + S_ISREG(buf.st_mode) && + ::access(m_nativePath.c_str(), R_OK | F_OK) == 0); +} + + +const bool posixFile::canWrite() const +{ + struct stat buf; + return (::stat(m_nativePath.c_str(), &buf) == 0 && + S_ISREG(buf.st_mode) && + ::access(m_nativePath.c_str(), W_OK | F_OK) == 0); +} + + +const posixFile::length_type posixFile::getLength() +{ + struct stat buf; + + if (::stat(m_nativePath.c_str(), &buf) != 0) + posixFileSystemFactory::reportError(m_path, errno); + + return static_cast (buf.st_size); +} + + +const posixFile::path& posixFile::getFullPath() const +{ + return (m_path); +} + + +const bool posixFile::exists() const +{ + struct stat buf; + return (::stat(m_nativePath.c_str(), &buf) == 0); +} + + +const vmime::utility::file* posixFile::getParent() const +{ + if (m_path.isEmpty()) + return NULL; + else + return new posixFile(m_path.getParent()); +} + + +void posixFile::rename(const path& newName) +{ + const vmime::string newNativePath = posixFileSystemFactory::pathToStringImpl(newName); + + if (::rename(m_nativePath.c_str(), newNativePath.c_str()) != 0) + posixFileSystemFactory::reportError(m_path, errno); + + m_path = newName; + m_nativePath = newNativePath; +} + + +void posixFile::remove() +{ + struct stat buf; + + if (::stat(m_nativePath.c_str(), &buf) != 0) + posixFileSystemFactory::reportError(m_path, errno); + + if (S_ISDIR(buf.st_mode)) + { + if (::rmdir(m_nativePath.c_str()) != 0) + posixFileSystemFactory::reportError(m_path, errno); + } + else if (S_ISREG(buf.st_mode)) + { + if (::unlink(m_nativePath.c_str()) != 0) + posixFileSystemFactory::reportError(m_path, errno); + } +} + + +vmime::utility::fileWriter* posixFile::getFileWriter() +{ + return new posixFileWriter(m_path, m_nativePath); +} + + +vmime::utility::fileReader* posixFile::getFileReader() +{ + return new posixFileReader(m_path, m_nativePath); +} + + +vmime::utility::fileIterator* posixFile::getFiles() const +{ + if (!isDirectory()) + throw vmime::exceptions::not_a_directory(m_path); + + return new posixFileIterator(m_path, m_nativePath); +} + + +void posixFile::createDirectoryImpl(const vmime::utility::file::path& fullPath, + const vmime::utility::file::path& path, const bool recursive) +{ + const vmime::string nativePath = posixFileSystemFactory::pathToStringImpl(path); + struct stat buf; + + if (::stat(nativePath.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)) + return; + + if (!path.isEmpty() && recursive) + createDirectoryImpl(fullPath, path.getParent(), true); + + if (::mkdir(nativePath.c_str(), 0750) != 0) + posixFileSystemFactory::reportError(fullPath, errno); +} + + + +// +// posixFileSystemFactory +// + +vmime::utility::file* posixFileSystemFactory::create(const vmime::utility::file::path& path) const +{ + return new posixFile(path); +} + + +const vmime::utility::file::path posixFileSystemFactory::stringToPath(const vmime::string& str) const +{ + return (stringToPathImpl(str)); +} + + +const vmime::string posixFileSystemFactory::pathToString(const vmime::utility::file::path& path) const +{ + return (pathToStringImpl(path)); +} + + +const vmime::utility::file::path posixFileSystemFactory::stringToPathImpl(const vmime::string& str) +{ + vmime::string::size_type offset = 0; + vmime::string::size_type prev = 0; + + vmime::utility::file::path path; + + while ((offset = str.find_first_of("/", offset)) != vmime::string::npos) + { + if (offset != prev) + path.appendComponent(vmime::string(str.begin() + prev, str.begin() + offset)); + + prev = offset + 1; + offset++; + } + + if (prev < str.length()) + path.appendComponent(vmime::string(str.begin() + prev, str.end())); + + return (path); +} + + +const vmime::string posixFileSystemFactory::pathToStringImpl(const vmime::utility::file::path& path) +{ + vmime::string native = "/"; + + for (int i = 0 ; i < path.getSize() ; ++i) + { + if (i > 0) + native += "/"; + + native += path[i].getBuffer(); + } + + return (native); +} + + +const bool posixFileSystemFactory::isValidPathComponent(const vmime::utility::file::path::component& comp) const +{ + return (comp.getBuffer().find_first_of("/*") == vmime::string::npos); +} + + +const bool posixFileSystemFactory::isValidPath(const vmime::utility::file::path& path) const +{ + for (int i = 0 ; i < path.getSize() ; ++i) + { + if (!isValidPathComponent(path[i])) + return false; + } + + return true; +} + + +void posixFileSystemFactory::reportError(const vmime::utility::path& path, const int err) +{ + vmime::string desc; + + switch (err) + { + case EEXIST: desc = "EEXIST: file already exists."; break; + case EISDIR: desc = "EISDIR: path refers to a directory."; break; + case EACCES: desc = "EACCES: permission denied"; break; + case ENAMETOOLONG: desc = "ENAMETOOLONG: path too long."; break; + case ENOENT: desc = "ENOENT: a directory in the path does not exist."; break; + case ENOTDIR: desc = "ENOTDIR: path is not a directory."; break; + case EROFS: desc = "EROFS: read-only filesystem."; break; + case ELOOP: desc = "ELOOP: too many symbolic links."; break; + case ENOSPC: desc = "ENOSPC: no space left on device."; break; + case ENOMEM: desc = "ENOMEM: insufficient kernel memory."; break; + case EMFILE: desc = "ENFILE: limit on number of files open by the process has been reached."; break; + case ENFILE: desc = "ENFILE: limit on number of files open on the system has been reached."; break; + case ENOTEMPTY: desc = "ENOTEMPTY: directory is not empty."; break; + + default: + + std::ostringstream oss; + oss << ::strerror(err) << "."; + + desc = oss.str(); + break; + } + + throw vmime::exceptions::filesystem_exception(desc, path); +} + + + +} // posix +} // platforms +} // vmime + + +#endif // VMIME_HAVE_FILESYSTEM_FEATURES diff --git a/src/platforms/posix/posixHandler.cpp b/src/platforms/posix/posixHandler.cpp new file mode 100644 index 00000000..5ecbbce4 --- /dev/null +++ b/src/platforms/posix/posixHandler.cpp @@ -0,0 +1,202 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "vmime/platforms/posix/posixHandler.hpp" + +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include + + +namespace vmime { +namespace platforms { +namespace posix { + + +posixHandler::posixHandler() +{ +#if VMIME_HAVE_MESSAGING_FEATURES + m_socketFactory = new posixSocketFactory(); +#endif +#if VMIME_HAVE_FILESYSTEM_FEATURES + m_fileSysFactory = new posixFileSystemFactory(); +#endif +} + + +posixHandler::~posixHandler() +{ +#if VMIME_HAVE_MESSAGING_FEATURES + delete (m_socketFactory); +#endif +#if VMIME_HAVE_FILESYSTEM_FEATURES + delete (m_fileSysFactory); +#endif +} + + +const unsigned int posixHandler::getUnixTime() const +{ + return ::time(NULL); +} + + +const vmime::datetime posixHandler::getCurrentLocalTime() const +{ + const time_t t(::time(NULL)); + + // Get the local time +#ifdef _REENTRANT + tm local; + ::localtime_r(&t, &local); +#else + tm local = *::localtime(&t); // WARNING: this is not thread-safe! +#endif + + // Get the UTC time +#ifdef _REENTRANT + tm gmt; + ::gmtime_r(&t, &gmt); +#else + tm gmt = *::gmtime(&t); // WARNING: this is not thread-safe! +#endif + + // "A negative value for tm_isdst causes mktime() to attempt + // to determine whether Daylight Saving Time is in effect + // for the specified time." + local.tm_isdst = -1; + gmt.tm_isdst = -1; + + // Calculate the difference (in seconds) + const int diff = ::mktime(&local) - ::mktime(&gmt); + + // Return the date + return vmime::datetime(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, + local.tm_hour, local.tm_min, local.tm_sec, diff / 60); // minutes needed +} + + +const vmime::charset posixHandler::getLocaleCharset() const +{ + vmime::string prevLocale(::setlocale(::LC_ALL, "")); + vmime::charset ch(::nl_langinfo(::CODESET)); + ::setlocale(::LC_ALL, prevLocale.c_str()); + + return (ch); +} + + +const vmime::string posixHandler::getHostName() const +{ + std::vector hostnames; + char buffer[256]; + + // Try with 'gethostname' + ::gethostname(buffer, sizeof(buffer)); + buffer[sizeof(buffer) - 1] = '\0'; + + if (::strlen(buffer) == 0) + ::strcpy(buffer, "localhost"); + + hostnames.push_back(buffer); + + // And with 'gethostbyname' + struct hostent* he = ::gethostbyname(buffer); + + if (he != NULL) + { + if (::strlen(he->h_name) != 0) + hostnames.push_back(he->h_name); + + char** alias = he->h_aliases; + + while (alias && *alias) + { + if (::strlen(*alias) != 0) + hostnames.push_back(*alias); + + ++alias; + } + } + + // Find a Fully-Qualified Domain Name (FQDN) + for (unsigned int i = 0 ; i < hostnames.size() ; ++i) + { + if (hostnames[i].find_first_of(".") != vmime::string::npos) + return (hostnames[i]); + } + + return (hostnames[0]); +} + + +const unsigned int posixHandler::getProcessId() const +{ + return (::getpid()); +} + + +#if VMIME_HAVE_MESSAGING_FEATURES + +vmime::messaging::socketFactory* posixHandler::getSocketFactory + (const vmime::string& /* name */) const +{ + return (m_socketFactory); +} + + +vmime::messaging::timeoutHandlerFactory* posixHandler::getTimeoutHandlerFactory + (const vmime::string& /* name */) const +{ + // Not used by default + return (NULL); +} + +#endif + + +#if VMIME_HAVE_FILESYSTEM_FEATURES + +vmime::utility::fileSystemFactory* posixHandler::getFileSystemFactory() const +{ + return (m_fileSysFactory); +} + +#endif + + +void posixHandler::wait() const +{ + ::sleep(1); +} + + +} // posix +} // platforms +} // vmime diff --git a/src/platforms/posix/posixSocket.cpp b/src/platforms/posix/posixSocket.cpp new file mode 100644 index 00000000..09c8820e --- /dev/null +++ b/src/platforms/posix/posixSocket.cpp @@ -0,0 +1,191 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "vmime/platforms/posix/posixSocket.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "vmime/exception.hpp" + + +#if VMIME_HAVE_MESSAGING_FEATURES + + +namespace vmime { +namespace platforms { +namespace posix { + + +// +// posixSocket +// + +posixSocket::posixSocket() + : m_desc(-1) +{ +} + + +posixSocket::~posixSocket() +{ + if (m_desc != -1) + ::close(m_desc); +} + + +void posixSocket::connect(const vmime::string& address, const vmime::port_t port) +{ + // Close current connection, if any + if (m_desc != -1) + { + ::close(m_desc); + m_desc = -1; + } + + // Resolve address + ::sockaddr_in addr; + + memset(&addr, 0, sizeof(addr)); + + addr.sin_family = AF_INET; + addr.sin_port = htons((unsigned short) port); + addr.sin_addr.s_addr = ::inet_addr(address.c_str()); + + if (addr.sin_addr.s_addr == (::in_addr_t) -1) + { + ::hostent* hostInfo = (hostent*) ::gethostbyname(address.c_str()); + + if (hostInfo == NULL) + { + // Error: cannot resolve address + throw vmime::exceptions::connection_error(); + } + + bcopy(hostInfo->h_addr, (char*) &addr.sin_addr, hostInfo->h_length); + } + else + { + // Error: cannot resolve address + throw vmime::exceptions::connection_error(); + } + + // Get a new socket + m_desc = ::socket(AF_INET, SOCK_STREAM, 0); + + if (m_desc == -1) + throw vmime::exceptions::connection_error(); + + // Start connection + if (::connect(m_desc, (sockaddr*) &addr, sizeof(addr)) == -1) + { + ::close(m_desc); + m_desc = -1; + + // Error + throw vmime::exceptions::connection_error(); + } +} + + +const bool posixSocket::isConnected() const +{ + return (m_desc != -1); +} + + +void posixSocket::disconnect() +{ + if (m_desc != -1) + { + ::shutdown(m_desc, SHUT_RDWR); + ::close(m_desc); + + m_desc = -1; + } +} + + +void posixSocket::receive(vmime::string& buffer) +{ + ::ssize_t ret = ::recv(m_desc, m_buffer, sizeof(m_buffer), 0); + + if (ret == -1) + { + // Error or no data + return; + } + else if (ret > 0) + { + buffer = vmime::string(m_buffer, ret); + } +} + + +const int posixSocket::receiveRaw(char* buffer, const int count) +{ + ::ssize_t ret = ::recv(m_desc, buffer, count, 0); + + if (ret == -1) + { + // Error or no data + return (0); + } + else + { + return (ret); + } +} + + +void posixSocket::send(const vmime::string& buffer) +{ + ::send(m_desc, buffer.data(), buffer.length(), 0); +} + + +void posixSocket::sendRaw(const char* buffer, const int count) +{ + ::send(m_desc, buffer, count, 0); +} + + + + +// +// posixSocketFactory +// + +vmime::messaging::socket* posixSocketFactory::create() +{ + return new posixSocket(); +} + + +} // posix +} // platforms +} // vmime + + +#endif // VMIME_HAVE_MESSAGING_FEATURES diff --git a/src/platforms/posix/socket.cpp b/src/platforms/posix/socket.cpp deleted file mode 100644 index fe3150bf..00000000 --- a/src/platforms/posix/socket.cpp +++ /dev/null @@ -1,191 +0,0 @@ -// -// VMime library (http://vmime.sourceforge.net) -// Copyright (C) 2002-2004 Vincent Richard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -// - -#include "vmime/platforms/posix/socket.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#include "vmime/exception.hpp" - - -#if VMIME_HAVE_MESSAGING_FEATURES - - -namespace vmime { -namespace platforms { -namespace posix { - - -// -// posixSocket -// - -posixSocket::posixSocket() - : m_desc(-1) -{ -} - - -posixSocket::~posixSocket() -{ - if (m_desc != -1) - ::close(m_desc); -} - - -void posixSocket::connect(const vmime::string& address, const vmime::port_t port) -{ - // Close current connection, if any - if (m_desc != -1) - { - ::close(m_desc); - m_desc = -1; - } - - // Resolve address - ::sockaddr_in addr; - - memset(&addr, 0, sizeof(addr)); - - addr.sin_family = AF_INET; - addr.sin_port = htons((unsigned short) port); - addr.sin_addr.s_addr = ::inet_addr(address.c_str()); - - if (addr.sin_addr.s_addr == (::in_addr_t) -1) - { - ::hostent* hostInfo = (hostent*) ::gethostbyname(address.c_str()); - - if (hostInfo == NULL) - { - // Error: cannot resolve address - throw vmime::exceptions::connection_error(); - } - - bcopy(hostInfo->h_addr, (char*) &addr.sin_addr, hostInfo->h_length); - } - else - { - // Error: cannot resolve address - throw vmime::exceptions::connection_error(); - } - - // Get a new socket - m_desc = ::socket(AF_INET, SOCK_STREAM, 0); - - if (m_desc == -1) - throw vmime::exceptions::connection_error(); - - // Start connection - if (::connect(m_desc, (sockaddr*) &addr, sizeof(addr)) == -1) - { - ::close(m_desc); - m_desc = -1; - - // Error - throw vmime::exceptions::connection_error(); - } -} - - -const bool posixSocket::isConnected() const -{ - return (m_desc != -1); -} - - -void posixSocket::disconnect() -{ - if (m_desc != -1) - { - ::shutdown(m_desc, SHUT_RDWR); - ::close(m_desc); - - m_desc = -1; - } -} - - -void posixSocket::receive(vmime::string& buffer) -{ - ::ssize_t ret = ::recv(m_desc, m_buffer, sizeof(m_buffer), 0); - - if (ret == -1) - { - // Error or no data - return; - } - else if (ret > 0) - { - buffer = vmime::string(m_buffer, ret); - } -} - - -const int posixSocket::receiveRaw(char* buffer, const int count) -{ - ::ssize_t ret = ::recv(m_desc, buffer, count, 0); - - if (ret == -1) - { - // Error or no data - return (0); - } - else - { - return (ret); - } -} - - -void posixSocket::send(const vmime::string& buffer) -{ - ::send(m_desc, buffer.data(), buffer.length(), 0); -} - - -void posixSocket::sendRaw(const char* buffer, const int count) -{ - ::send(m_desc, buffer, count, 0); -} - - - - -// -// posixSocketFactory -// - -vmime::messaging::socket* posixSocketFactory::create() -{ - return new posixSocket(); -} - - -} // posix -} // platforms -} // vmime - - -#endif // VMIME_HAVE_MESSAGING_FEATURES -- cgit