susumu.yata
null+****@clear*****
Wed Jul 10 18:13:34 JST 2013
susumu.yata 2013-07-10 18:13:34 +0900 (Wed, 10 Jul 2013) New Revision: f492e0402b91485ce938ee752b62794199d2acf6 https://github.com/groonga/grnxx/commit/f492e0402b91485ce938ee752b62794199d2acf6 Message: Update grnxx::storage::File::unlink(). Throw grnxx::SystemError on failure. Modified files: lib/grnxx/storage/file-posix.cpp lib/grnxx/storage/file-posix.hpp lib/grnxx/storage/file-windows.cpp lib/grnxx/storage/file-windows.hpp lib/grnxx/storage/file.cpp lib/grnxx/storage/file.hpp lib/grnxx/storage/storage_impl.cpp test/test_storage.cpp Modified: lib/grnxx/storage/file-posix.cpp (+4 -8) =================================================================== --- lib/grnxx/storage/file-posix.cpp 2013-07-10 14:43:58 +0900 (e7c257d) +++ lib/grnxx/storage/file-posix.cpp 2013-07-10 18:13:34 +0900 (db3fb5e) @@ -121,21 +121,17 @@ bool FileImpl::exists(const char *path) { return S_ISREG(stat.st_mode); } -bool FileImpl::unlink(const char *path) { +void FileImpl::unlink(const char *path) { if (!path) { GRNXX_ERROR() << "invalid argument: path = nullptr"; throw LogicError(); } - if (!exists(path)) { - return false; - } if (::unlink(path) != 0) { Errno errno_copy(errno); - GRNXX_WARNING() << "failed to unlink file: path = " << path - << ", call = ::unlink, errno = " << errno_copy; - return false; + GRNXX_ERROR() << "failed to unlink file: path = " << path + << ", call = ::unlink, errno = " << errno_copy; + throw SystemError(errno_copy); } - return true; } bool FileImpl::lock(FileLockFlags lock_flags) { Modified: lib/grnxx/storage/file-posix.hpp (+1 -1) =================================================================== --- lib/grnxx/storage/file-posix.hpp 2013-07-10 14:43:58 +0900 (3cfc728) +++ lib/grnxx/storage/file-posix.hpp 2013-07-10 18:13:34 +0900 (72740e1) @@ -39,7 +39,7 @@ class FileImpl : public File { static FileImpl *open_or_create(const char *path, FileFlags flags); static bool exists(const char *path); - static bool unlink(const char *path); + static void unlink(const char *path); bool lock(FileLockFlags lock_flags); void unlock(); Modified: lib/grnxx/storage/file-windows.cpp (+4 -8) =================================================================== --- lib/grnxx/storage/file-windows.cpp 2013-07-10 14:43:58 +0900 (41317cc) +++ lib/grnxx/storage/file-windows.cpp 2013-07-10 18:13:34 +0900 (1ba3f77) @@ -120,21 +120,17 @@ bool FileImpl::exists(const char *path) { return stat.st_mode & _S_IFREG; } -bool FileImpl::unlink(const char *path) { +void FileImpl::unlink(const char *path) { if (!path) { GRNXX_ERROR() << "invalid argument: path = nullptr"; throw LogicError(); } - if (!exists(path)) { - return false; - } if (!::DeleteFile(path)) { Errno errno_copy(::GetLastError()); - GRNXX_WARNING() << "failed to unlink file: path = " << path - << ", call = ::DeleteFile, errno = " << errno_copy; - return false; + GRNXX_ERROR() << "failed to unlink file: path = " << path + << ", call = ::DeleteFile, errno = " << errno_copy; + throw SystemError(errno_copy); } - return true; } bool FileImpl::lock(FileLockMode mode) { Modified: lib/grnxx/storage/file-windows.hpp (+1 -1) =================================================================== --- lib/grnxx/storage/file-windows.hpp 2013-07-10 14:43:58 +0900 (a5efd8e) +++ lib/grnxx/storage/file-windows.hpp 2013-07-10 18:13:34 +0900 (9dc527e) @@ -46,7 +46,7 @@ class FileImpl : public File { static FileImpl *open_or_create(const char *path, FileFlags flags); static bool exists(const char *path); - static bool unlink(const char *path); + static void unlink(const char *path); bool lock(FileLockFlags lock_flags); void unlock(); Modified: lib/grnxx/storage/file.cpp (+2 -2) =================================================================== --- lib/grnxx/storage/file.cpp 2013-07-10 14:43:58 +0900 (601b114) +++ lib/grnxx/storage/file.cpp 2013-07-10 18:13:34 +0900 (8bb3370) @@ -74,8 +74,8 @@ bool File::exists(const char *path) { return FileImpl::exists(path); } -bool File::unlink(const char *path) { - return FileImpl::unlink(path); +void File::unlink(const char *path) { + FileImpl::unlink(path); } } // namespace storage Modified: lib/grnxx/storage/file.hpp (+2 -2) =================================================================== --- lib/grnxx/storage/file.hpp 2013-07-10 14:43:58 +0900 (33fd64a) +++ lib/grnxx/storage/file.hpp 2013-07-10 18:13:34 +0900 (5cabce4) @@ -75,8 +75,8 @@ class File { // Return true iff "path" refers to a regular file. static bool exists(const char *path); - // Unlink a file and return true on success. - static bool unlink(const char *path); + // Unlink a file. + static void unlink(const char *path); // Try to lock a file and return true on success. // Note that the file is accessible even if it is locked (advisory). Modified: lib/grnxx/storage/storage_impl.cpp (+3 -3) =================================================================== --- lib/grnxx/storage/storage_impl.cpp 2013-07-10 14:43:58 +0900 (48f7f72) +++ lib/grnxx/storage/storage_impl.cpp 2013-07-10 18:13:34 +0900 (5a2b648) @@ -481,13 +481,13 @@ bool StorageImpl::unlink_storage() { } const uint16_t max_file_id = static_cast<uint16_t>( header_->total_size / header_->max_file_size); - bool result = File::unlink(path_.get()); + File::unlink(path_.get()); for (uint16_t i = 1; i <= max_file_id; ++i) { // Component files may be left if path generation fails. std::unique_ptr<char[]> numbered_path(generate_path(i)); - result &= File::unlink(numbered_path.get()); + File::unlink(numbered_path.get()); } - return result; + return true; } void StorageImpl::prepare_pointers() { Modified: test/test_storage.cpp (+18 -13) =================================================================== --- test/test_storage.cpp 2013-07-10 14:43:58 +0900 (d2910fb) +++ test/test_storage.cpp 2013-07-10 18:13:34 +0900 (9f7690f) @@ -65,9 +65,15 @@ void test_unique_path() { GRNXX_NOTICE() << "unique_path = " << unique_path.get(); } +void unlink_file_if_exists(const char *path) { + if (grnxx::storage::File::exists(path)) { + grnxx::storage::File::unlink(path); + } +} + void test_file_create() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::create(FILE_PATH)); @@ -85,7 +91,7 @@ void test_file_create() { void test_file_open() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::create(FILE_PATH)); @@ -97,7 +103,7 @@ void test_file_open() { void test_file_open_or_create() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::open_or_create(FILE_PATH)); @@ -113,9 +119,8 @@ void test_file_exists_and_unlink() { grnxx::storage::File::open_or_create(FILE_PATH)); assert(grnxx::storage::File::exists(FILE_PATH)); - assert(grnxx::storage::File::unlink(FILE_PATH)); + grnxx::storage::File::unlink(FILE_PATH); assert(!grnxx::storage::File::exists(FILE_PATH)); - assert(!grnxx::storage::File::unlink(FILE_PATH)); } void test_file_lock_and_unlock() { @@ -172,7 +177,7 @@ void test_file_resize_and_size() { void test_file_path() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::create(FILE_PATH)); @@ -182,12 +187,12 @@ void test_file_path() { grnxx::storage::FILE_TEMPORARY)); assert(std::strcmp(file->path(), FILE_PATH) != 0); - assert(grnxx::storage::File::unlink(FILE_PATH)); + grnxx::storage::File::unlink(FILE_PATH); } void test_file_flags() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; file.reset(grnxx::storage::File::create(FILE_PATH)); @@ -201,7 +206,7 @@ void test_file_flags() { grnxx::storage::FILE_TEMPORARY)); assert(file->flags() == grnxx::storage::FILE_TEMPORARY); - assert(grnxx::storage::File::unlink(FILE_PATH)); + grnxx::storage::File::unlink(FILE_PATH); } void test_file_handle() { @@ -245,7 +250,7 @@ void test_chunk_sync() { void test_chunk_flags() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; std::unique_ptr<grnxx::storage::Chunk> chunk; @@ -262,12 +267,12 @@ void test_chunk_flags() { assert(chunk->flags() == grnxx::storage::CHUNK_READ_ONLY); file.reset(); - assert(grnxx::storage::File::unlink(FILE_PATH)); + grnxx::storage::File::unlink(FILE_PATH); } void test_chunk_address() { const char FILE_PATH[] = "temp.grn"; - grnxx::storage::File::unlink(FILE_PATH); + unlink_file_if_exists(FILE_PATH); std::unique_ptr<grnxx::storage::File> file; std::unique_ptr<grnxx::storage::Chunk> chunk; @@ -297,7 +302,7 @@ void test_chunk_address() { } file.reset(); - assert(grnxx::storage::File::unlink(FILE_PATH)); + grnxx::storage::File::unlink(FILE_PATH); } void test_chunk_size() { -------------- next part -------------- HTML����������������������������...Download