susumu.yata
null+****@clear*****
Fri Feb 22 13:22:09 JST 2013
susumu.yata 2013-02-22 13:22:09 +0900 (Fri, 22 Feb 2013) New Revision: 9cd65ff957cfbd438e9aa38436aa8bda5246f4c9 https://github.com/groonga/grnxx/commit/9cd65ff957cfbd438e9aa38436aa8bda5246f4c9 Log: Update the interface of grnxx::io::FileInfo. Removed files: lib/io/file_info-impl.cpp lib/io/file_info-impl.hpp Modified files: lib/io/Makefile.am lib/io/file_info.cpp lib/io/file_info.hpp test/test_io_file_info.cpp Modified: lib/io/Makefile.am (+0 -2) =================================================================== --- lib/io/Makefile.am 2013-02-22 13:20:06 +0900 (25ab70d) +++ lib/io/Makefile.am 2013-02-22 13:22:09 +0900 (42745d4) @@ -9,7 +9,6 @@ libgrnxx_io_la_SOURCES = \ file-windows.cpp \ file.cpp \ file_info.cpp \ - file_info-impl.cpp \ path.cpp \ pool.cpp \ pool-impl.cpp \ @@ -25,7 +24,6 @@ libgrnxx_io_include_HEADERS = \ file-windows.hpp \ file.hpp \ file_info.hpp \ - file_info-impl.hpp \ path.hpp \ pool.hpp \ pool-impl.hpp \ Deleted: lib/io/file_info-impl.cpp (+0 -106) 100644 =================================================================== --- lib/io/file_info-impl.cpp 2013-02-22 13:20:06 +0900 (de90257) +++ /dev/null @@ -1,106 +0,0 @@ -/* - Copyright (C) 2012 Brazil, Inc. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#include "file_info-impl.hpp" - -#include <cerrno> - -#include "../error.hpp" -#include "../exception.hpp" -#include "../logger.hpp" - -namespace grnxx { -namespace io { - -std::unique_ptr<FileInfoImpl> FileInfoImpl::stat(const char *path) { - if (!path) { - GRNXX_ERROR() << "invalid argument: path = " << path; - GRNXX_THROW(); - } - - Stat stat; -#ifdef GRNXX_WINDOWS - if (::_stat(path, &stat) != 0) { - if (errno != ENOENT) { - GRNXX_WARNING() << "failed to get file information: path = <" << path - << ">: '::_stat' " << Error(errno); -#else // GRNXX_WINDOWS - if (::stat(path, &stat) != 0) { - if (errno != ENOENT) { - GRNXX_WARNING() << "failed to get file information: path = <" << path - << ">: '::stat' " << Error(errno); -#endif // GRNXX_WINDOWS - } - return nullptr; - } - - std::unique_ptr<FileInfoImpl> file_info( - new (std::nothrow) FileInfoImpl(stat)); - if (!file_info) { - GRNXX_ERROR() << "new grnxx::io::FileInfoImpl failed"; - GRNXX_THROW(); - } - return file_info; -} - -std::unique_ptr<FileInfoImpl> FileInfoImpl::stat(const File &file) { - Stat stat; -#ifdef GRNXX_WINDOWS - if (::_stat(file.path().c_str(), &stat) != 0) { - if (errno != ENOENT) { - GRNXX_WARNING() << "failed to get file information: file = " << file - << ": '::_stat' " << Error(errno); - } -#else // GRNXX_WINDOWS - if (::fstat(*static_cast<const int *>(file.handle()), &stat) != 0) { - GRNXX_WARNING() << "failed to get file information: file = " << file - << ": '::fstat' " << Error(errno); -#endif // GRNXX_WINDOWS - return nullptr; - } - - std::unique_ptr<FileInfoImpl> file_info( - new (std::nothrow) FileInfoImpl(stat)); - if (!file_info) { - GRNXX_ERROR() << "new grnxx::io::FileInfoImpl failed"; - GRNXX_THROW(); - } - return file_info; -} - -StringBuilder &FileInfoImpl::write_to(StringBuilder &builder) const { - if (!builder) { - return builder; - } - - return builder << "{ is_file = " << is_file() - << ", is_directory = " << is_directory() - << ", device_id = " << device_id() - << ", inode_id = " << inode_id() - << ", mode_flags = " << mode_flags() - << ", num_links = " << num_links() - << ", user_id = " << user_id() - << ", group_id = " << group_id() - << ", size = " << size() - << ", last_access_time = " << last_access_time() - << ", last_modification_time = " << last_modification_time() - << ", last_status_change_time = " << last_status_change_time() - << " }"; -} - -} // namespace io -} // namespace grnxx Deleted: lib/io/file_info-impl.hpp (+0 -102) 100644 =================================================================== --- lib/io/file_info-impl.hpp 2013-02-22 13:20:06 +0900 (195ea0a) +++ /dev/null @@ -1,102 +0,0 @@ -/* - Copyright (C) 2012 Brazil, Inc. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#ifndef GRNXX_IO_FILE_INFO_IMPL_HPP -#define GRNXX_IO_FILE_INFO_IMPL_HPP - -#include <sys/types.h> -#include <sys/stat.h> - -#include "../time.hpp" -#include "file.hpp" - -namespace grnxx { -namespace io { - -#ifdef GRNXX_WINDOWS -typedef struct _stat Stat; -#else // GRNXX_WINDOWS -typedef struct stat Stat; -#endif // GRNXX_WINDOWS - -class FileInfoImpl { - public: - static std::unique_ptr<FileInfoImpl> stat(const char *path); - static std::unique_ptr<FileInfoImpl> stat(const File &file); - - bool is_file() const { -#ifdef GRNXX_WINDOWS - return (stat_.st_mode & _S_IFREG) != 0; -#else // GRNXX_WINDOWS - return S_ISREG(stat_.st_mode) != 0; -#endif // GRNXX_WINDOWS - } - - bool is_directory() const { -#ifdef GRNXX_WINDOWS - return (stat_.st_mode & _S_IFDIR) != 0; -#else // GRNXX_WINDOWS - return S_ISDIR(stat_.st_mode) != 0; -#endif // GRNXX_WINDOWS - } - - int64_t device_id() const { - return stat_.st_dev; - } - int64_t inode_id() const { - return stat_.st_ino; - } - int64_t mode_flags() const { - return stat_.st_mode; - } - int64_t num_links() const { - return stat_.st_nlink; - } - int64_t user_id() const { - return stat_.st_uid; - } - int64_t group_id() const { - return stat_.st_gid; - } - uint64_t size() const { - return stat_.st_size; - } - Time last_access_time() const { - return Time(static_cast<int64_t>(stat_.st_atime) * 1000000000); - } - Time last_modification_time() const { - return Time(static_cast<int64_t>(stat_.st_mtime) * 1000000000); - } - Time last_status_change_time() const { - return Time(static_cast<int64_t>(stat_.st_ctime) * 1000000000); - } - - StringBuilder &write_to(StringBuilder &builder) const; - - private: - Stat stat_; - - FileInfoImpl(const Stat &stat) : stat_(stat) {} - - FileInfoImpl(const FileInfoImpl &); - FileInfoImpl &operator=(const FileInfoImpl &); -}; - -} // namespace io -} // namespace grnxx - -#endif // GRNXX_IO_FILE_INFO_IMPL_HPP Modified: lib/io/file_info.cpp (+150 -52) =================================================================== --- lib/io/file_info.cpp 2013-02-22 13:20:06 +0900 (b222fb8) +++ lib/io/file_info.cpp 2013-02-22 13:22:09 +0900 (edf8ee1) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,75 +16,173 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "file_info.hpp" -#include "file_info-impl.hpp" + +#include <sys/types.h> +#include <sys/stat.h> + +#include <cerrno> + +#include "../error.hpp" +#include "../exception.hpp" +#include "../logger.hpp" +#include "file.hpp" namespace grnxx { namespace io { +namespace { -FileInfo::FileInfo() : impl_() {} +#ifdef GRNXX_WINDOWS +typedef struct _stat Stat; +#else // GRNXX_WINDOWS +typedef struct stat Stat; +#endif // GRNXX_WINDOWS -FileInfo::FileInfo(const char *path) : impl_(FileInfoImpl::stat(path)) {} +class Impl : public FileInfo { + public: + static Impl *stat(const char *path); + static Impl *stat(const File &file); -FileInfo::FileInfo(const File &file) : impl_(FileInfoImpl::stat(file)) {} + bool is_file() const { +#ifdef GRNXX_WINDOWS + return (stat_.st_mode & _S_IFREG) != 0; +#else // GRNXX_WINDOWS + return S_ISREG(stat_.st_mode) != 0; +#endif // GRNXX_WINDOWS + } -FileInfo::FileInfo(const FileInfo &file_info) : impl_(file_info.impl_) {} + bool is_directory() const { +#ifdef GRNXX_WINDOWS + return (stat_.st_mode & _S_IFDIR) != 0; +#else // GRNXX_WINDOWS + return S_ISDIR(stat_.st_mode) != 0; +#endif // GRNXX_WINDOWS + } -FileInfo &FileInfo::operator=(const FileInfo &file_info) { - impl_ = file_info.impl_; - return *this; -} + int64_t device_id() const { + return stat_.st_dev; + } + int64_t inode_id() const { + return stat_.st_ino; + } + int64_t mode_flags() const { + return stat_.st_mode; + } + int64_t num_links() const { + return stat_.st_nlink; + } + int64_t user_id() const { + return stat_.st_uid; + } + int64_t group_id() const { + return stat_.st_gid; + } + uint64_t size() const { + return stat_.st_size; + } + Time last_access_time() const { + return Time(static_cast<int64_t>(stat_.st_atime) * 1000000000); + } + Time last_modification_time() const { + return Time(static_cast<int64_t>(stat_.st_mtime) * 1000000000); + } + Time last_status_change_time() const { + return Time(static_cast<int64_t>(stat_.st_ctime) * 1000000000); + } -FileInfo::FileInfo(FileInfo &&file_info) : impl_(std::move(file_info.impl_)) {} + StringBuilder &write_to(StringBuilder &builder) const; -FileInfo &FileInfo::operator=(FileInfo &&file_info) { - impl_ = std::move(file_info.impl_); - return *this; -} + private: + Stat stat_; -bool FileInfo::is_file() const { - return impl_ ? impl_->is_file() : false; -} -bool FileInfo::is_directory() const { - return impl_ ? impl_->is_directory() : false; -} -int64_t FileInfo::device_id() const { - return impl_ ? impl_->device_id() : 0; -} -int64_t FileInfo::inode_id() const { - return impl_ ? impl_->inode_id() : 0; -} -int64_t FileInfo::mode_flags() const { - return impl_ ? impl_->mode_flags() : 0; -} -int64_t FileInfo::num_links() const { - return impl_ ? impl_->num_links() : 0; -} -int64_t FileInfo::user_id() const { - return impl_ ? impl_->user_id() : 0; -} -int64_t FileInfo::group_id() const { - return impl_ ? impl_->group_id() : 0; -} -uint64_t FileInfo::size() const { - return impl_ ? impl_->size() : 0; -} -Time FileInfo::last_access_time() const { - return impl_ ? impl_->last_access_time() : Time(); + Impl(const Stat &stat) : stat_(stat) {} +}; + +Impl *Impl::stat(const char *path) { + if (!path) { + GRNXX_ERROR() << "invalid argument: path = " << path; + GRNXX_THROW(); + } + + Stat stat; +#ifdef GRNXX_WINDOWS + if (::_stat(path, &stat) != 0) { + if (errno != ENOENT) { + GRNXX_WARNING() << "failed to get file information: path = <" << path + << ">: '::_stat' " << Error(errno); +#else // GRNXX_WINDOWS + if (::stat(path, &stat) != 0) { + if (errno != ENOENT) { + GRNXX_WARNING() << "failed to get file information: path = <" << path + << ">: '::stat' " << Error(errno); +#endif // GRNXX_WINDOWS + } + return nullptr; + } + + std::unique_ptr<Impl> file_info(new (std::nothrow) Impl(stat)); + if (!file_info) { + GRNXX_ERROR() << "new grnxx::io::{anonymous_namespace}::Impl failed"; + GRNXX_THROW(); + } + return file_info.release(); } -Time FileInfo::last_modification_time() const { - return impl_ ? impl_->last_modification_time() : Time(); + +Impl *Impl::stat(const File &file) { + Stat stat; +#ifdef GRNXX_WINDOWS + if (::_stat(file.path().c_str(), &stat) != 0) { + if (errno != ENOENT) { + GRNXX_WARNING() << "failed to get file information: file = " << file + << ": '::_stat' " << Error(errno); + } +#else // GRNXX_WINDOWS + if (::fstat(*static_cast<const int *>(file.handle()), &stat) != 0) { + GRNXX_WARNING() << "failed to get file information: file = " << file + << ": '::fstat' " << Error(errno); +#endif // GRNXX_WINDOWS + return nullptr; + } + + std::unique_ptr<Impl> file_info(new (std::nothrow) Impl(stat)); + if (!file_info) { + GRNXX_ERROR() << "new grnxx::io::{anonymous_namespace}::Impl failed"; + GRNXX_THROW(); + } + return file_info.release(); } -Time FileInfo::last_status_change_time() const { - return impl_ ? impl_->last_status_change_time() : Time(); + +StringBuilder &Impl::write_to(StringBuilder &builder) const { + if (!builder) { + return builder; + } + + return builder << "{ is_file = " << is_file() + << ", is_directory = " << is_directory() + << ", device_id = " << device_id() + << ", inode_id = " << inode_id() + << ", mode_flags = " << mode_flags() + << ", num_links = " << num_links() + << ", user_id = " << user_id() + << ", group_id = " << group_id() + << ", size = " << size() + << ", last_access_time = " << last_access_time() + << ", last_modification_time = " << last_modification_time() + << ", last_status_change_time = " << last_status_change_time() + << " }"; } -void FileInfo::swap(FileInfo &rhs) { - impl_.swap(rhs.impl_); +} // namespace + +FileInfo *FileInfo::stat(const char *path) { + return Impl::stat(path); } -StringBuilder &FileInfo::write_to(StringBuilder &builder) const { - return impl_ ? impl_->write_to(builder) : (builder << "n/a"); +FileInfo *FileInfo::stat(const File &file) { + return Impl::stat(file); } +FileInfo::FileInfo() {} +FileInfo::~FileInfo() {} + } // namespace io } // namespace grnxx Modified: lib/io/file_info.hpp (+22 -41) =================================================================== --- lib/io/file_info.hpp 2013-02-22 13:20:06 +0900 (82dcc40) +++ lib/io/file_info.hpp 2013-02-22 13:22:09 +0900 (112680d) @@ -19,56 +19,37 @@ #define GRNXX_IO_FILE_INFO_HPP #include "../time.hpp" -#include "file.hpp" namespace grnxx { namespace io { -class FileInfoImpl; +class File; class FileInfo { public: FileInfo(); - explicit FileInfo(const char *path); - explicit FileInfo(const File &file); - - FileInfo(const FileInfo &file_info); - FileInfo &operator=(const FileInfo &file_info); - - FileInfo(FileInfo &&file_info); - FileInfo &operator=(FileInfo &&file_info); - - explicit operator bool() const { - return static_cast<bool>(impl_); - } - - bool is_file() const; - bool is_directory() const; - int64_t device_id() const; - int64_t inode_id() const; - int64_t mode_flags() const; - int64_t num_links() const; - int64_t user_id() const; - int64_t group_id() const; - uint64_t size() const; - Time last_access_time() const; - Time last_modification_time() const; - Time last_status_change_time() const; - - void swap(FileInfo &rhs); - - StringBuilder &write_to(StringBuilder &builder) const; - - private: - std::shared_ptr<FileInfoImpl> impl_; - - // Copyable. + virtual ~FileInfo(); + + // Return nullptr iff "path" is not a valid path. + static FileInfo *stat(const char *path); + static FileInfo *stat(const File &file); + + virtual bool is_file() const = 0; + virtual bool is_directory() const = 0; + virtual int64_t device_id() const = 0; + virtual int64_t inode_id() const = 0; + virtual int64_t mode_flags() const = 0; + virtual int64_t num_links() const = 0; + virtual int64_t user_id() const = 0; + virtual int64_t group_id() const = 0; + virtual uint64_t size() const = 0; + virtual Time last_access_time() const = 0; + virtual Time last_modification_time() const = 0; + virtual Time last_status_change_time() const = 0; + + virtual StringBuilder &write_to(StringBuilder &builder) const = 0; }; -inline void swap(FileInfo &lhs, FileInfo &rhs) { - lhs.swap(rhs); -} - inline StringBuilder &operator<<(StringBuilder &builder, const FileInfo &file_info) { return file_info.write_to(builder); @@ -77,4 +58,4 @@ inline StringBuilder &operator<<(StringBuilder &builder, } // namespace io } // namespace grnxx -#endif // GRNXX_IO_FILE_INFO_HPP +#endif // GRNXX_IO_FILE_INFO2_HPP Modified: test/test_io_file_info.cpp (+21 -50) =================================================================== --- test/test_io_file_info.cpp 2013-02-22 13:20:06 +0900 (4694f29) +++ test/test_io_file_info.cpp 2013-02-22 13:22:09 +0900 (6a93723) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -26,13 +26,9 @@ void test_non_existent_file() { grnxx::io::File::unlink_if_exists(FILE_PATH); - grnxx::io::FileInfo file_info(FILE_PATH); - - GRNXX_NOTICE() << "file_info (invalid) = " << file_info; - + std::unique_ptr<grnxx::io::FileInfo> file_info( + grnxx::io::FileInfo::stat(FILE_PATH)); assert(!file_info); - assert(!file_info.is_file()); - assert(!file_info.is_directory()); } void test_existent_file() { @@ -43,16 +39,20 @@ void test_existent_file() { grnxx::io::File file(grnxx::io::FILE_CREATE, FILE_PATH); file.resize(FILE_SIZE); - grnxx::io::FileInfo file_info(FILE_PATH); + std::unique_ptr<grnxx::io::FileInfo> file_info( + grnxx::io::FileInfo::stat(FILE_PATH)); + assert(file_info); + + GRNXX_NOTICE() << "file_info (regular) = " << *file_info; - GRNXX_NOTICE() << "file_info (regular) = " << file_info; + assert(file_info->is_file()); + assert(!file_info->is_directory()); + assert(file_info->size() == FILE_SIZE); + file_info.reset(grnxx::io::FileInfo::stat(file)); assert(file_info); - assert(file_info.is_file()); - assert(!file_info.is_directory()); - assert(file_info.size() == FILE_SIZE); - assert(grnxx::io::FileInfo(file)); + GRNXX_NOTICE() << "file_info (regular) = " << *file_info; file.close(); grnxx::io::File::unlink(FILE_PATH); @@ -61,23 +61,22 @@ void test_existent_file() { void test_non_existent_directory() { const char DIRECTORY_PATH[] = "no_such_directory/"; - grnxx::io::FileInfo file_info(DIRECTORY_PATH); - + std::unique_ptr<grnxx::io::FileInfo> file_info( + grnxx::io::FileInfo::stat(DIRECTORY_PATH)); assert(!file_info); - assert(!file_info.is_file()); - assert(!file_info.is_directory()); } void test_existent_directory() { const char DIRECTORY_PATH[] = "./"; - grnxx::io::FileInfo file_info(DIRECTORY_PATH); + std::unique_ptr<grnxx::io::FileInfo> file_info( + grnxx::io::FileInfo::stat(DIRECTORY_PATH)); + assert(file_info); - GRNXX_NOTICE() << "file_info (directory) = " << file_info; + GRNXX_NOTICE() << "file_info (directory) = " << *file_info; - assert(file_info); - assert(!file_info.is_file()); - assert(file_info.is_directory()); + assert(!file_info->is_file()); + assert(file_info->is_directory()); } int main() { @@ -90,33 +89,5 @@ int main() { test_non_existent_directory(); test_existent_directory(); - const char FILE_PATH[] = "temp.grn"; - - grnxx::io::File::unlink_if_exists(FILE_PATH); - - grnxx::io::FileInfo file_info(FILE_PATH); - - GRNXX_NOTICE() << "file_info = " << file_info; - - assert(!file_info); - - grnxx::io::File file(grnxx::io::FILE_TEMPORARY, FILE_PATH); - file.resize(12345); - - file_info = grnxx::io::FileInfo(file); - - file_info = grnxx::io::FileInfo(file); - - GRNXX_NOTICE() << "file_info = " << file_info; - - assert(file_info); - assert(file_info.is_file()); - assert(!file_info.is_directory()); - assert(file_info.size() == 12345); - - file.close(); - - assert(!grnxx::io::File::exists("temp.dat")); - return 0; } -------------- next part -------------- HTML����������������������������...Download