From 260770c28c282a031d05b5e9129a6afd3a22ebf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Wed, 7 Nov 2018 15:08:18 +0100 Subject: [PATCH] Delete rtc::Filesystem. Move needed functions to filerotatingstream.cc. Bug: webrtc:6424 Change-Id: I2a1f215cb4521f21c2f8defd03f0f28c1deae24a Reviewed-on: https://webrtc-review.googlesource.com/c/109003 Commit-Queue: Niels Moller Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#25568} --- rtc_base/BUILD.gn | 10 -- rtc_base/filerotatingstream.cc | 132 +++++++++++++++++------ rtc_base/filerotatingstream.h | 2 - rtc_base/filerotatingstream_unittest.cc | 5 +- rtc_base/fileutils.cc | 138 ------------------------ rtc_base/fileutils.h | 124 --------------------- rtc_base/unixfilesystem.cc | 94 ---------------- rtc_base/unixfilesystem.h | 43 -------- rtc_base/win32filesystem.cc | 72 ------------- rtc_base/win32filesystem.h | 39 ------- 10 files changed, 104 insertions(+), 555 deletions(-) delete mode 100644 rtc_base/fileutils.cc delete mode 100644 rtc_base/fileutils.h delete mode 100644 rtc_base/unixfilesystem.cc delete mode 100644 rtc_base/unixfilesystem.h delete mode 100644 rtc_base/win32filesystem.cc delete mode 100644 rtc_base/win32filesystem.h diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 076834d10a..f9702785d0 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -741,8 +741,6 @@ rtc_static_library("rtc_base") { "dscp.h", "filerotatingstream.cc", "filerotatingstream.h", - "fileutils.cc", - "fileutils.h", "gunit_prod.h", "helpers.cc", "helpers.h", @@ -894,18 +892,12 @@ rtc_static_library("rtc_base") { "macutils.cc", "macutils.h", ] - libs += [ - # For ProcessInformationCopyDictionary in unixfilesystem.cc. - "ApplicationServices.framework", - ] } if (is_win) { sources += [ "win32.cc", "win32.h", - "win32filesystem.cc", - "win32filesystem.h", "win32window.cc", "win32window.h", ] @@ -923,8 +915,6 @@ rtc_static_library("rtc_base") { sources += [ "ifaddrs_converter.cc", "ifaddrs_converter.h", - "unixfilesystem.cc", - "unixfilesystem.h", ] } diff --git a/rtc_base/filerotatingstream.cc b/rtc_base/filerotatingstream.cc index e9ac394244..d03ab3937f 100644 --- a/rtc_base/filerotatingstream.cc +++ b/rtc_base/filerotatingstream.cc @@ -19,12 +19,12 @@ #include #include "rtc_base/stringutils.h" #else +#include #include #endif // WEBRTC_WIN #include "absl/strings/match.h" #include "rtc_base/checks.h" -#include "rtc_base/fileutils.h" #include "rtc_base/logging.h" // Note: We use fprintf for logging in the write paths of this stream to avoid @@ -35,7 +35,16 @@ namespace rtc { namespace { std::string AddTrailingPathDelimiterIfNeeded(std::string directory); + +// |dir| must have a trailing delimiter. |prefix| must not include wild card +// characters. +std::vector GetFilesWithPrefix(const std::string& directory, + const std::string& prefix); +bool DeleteFile(const std::string& file); +bool MoveFile(const std::string& old_file, const std::string& new_file); +bool IsFile(const std::string& file); bool IsFolder(const std::string& file); +absl::optional GetFileSize(const std::string& file); #if defined(WEBRTC_WIN) @@ -46,6 +55,40 @@ std::string AddTrailingPathDelimiterIfNeeded(std::string directory) { return directory + "\\"; } +std::vector GetFilesWithPrefix(const std::string& directory, + const std::string& prefix) { + RTC_DCHECK(absl::EndsWith(directory, "\\")); + WIN32_FIND_DATA data; + HANDLE handle; + handle = ::FindFirstFile(ToUtf16(directory + prefix + '*').c_str(), &data); + if (handle == INVALID_HANDLE_VALUE) + return {}; + + std::vector file_list; + do { + file_list.emplace_back(directory + ToUtf8(data.cFileName)); + } while (::FindNextFile(handle, &data) == TRUE); + + ::FindClose(handle); + return file_list; +} + +bool DeleteFile(const std::string& file) { + return ::DeleteFile(ToUtf16(file).c_str()) != 0; +} + +bool MoveFile(const std::string& old_file, const std::string& new_file) { + return ::MoveFile(ToUtf16(old_file).c_str(), ToUtf16(new_file).c_str()) != 0; +} + +bool IsFile(const std::string& file) { + WIN32_FILE_ATTRIBUTE_DATA data = {0}; + if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard, + &data)) + return false; + return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; +} + bool IsFolder(const std::string& file) { WIN32_FILE_ATTRIBUTE_DATA data = {0}; if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard, @@ -55,6 +98,14 @@ bool IsFolder(const std::string& file) { FILE_ATTRIBUTE_DIRECTORY; } +absl::optional GetFileSize(const std::string& file) { + WIN32_FILE_ATTRIBUTE_DATA data = {0}; + if (::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard, + &data) == 0) + return absl::nullopt; + return data.nFileSizeLow; +} + #else // defined(WEBRTC_WIN) std::string AddTrailingPathDelimiterIfNeeded(std::string directory) { @@ -64,12 +115,52 @@ std::string AddTrailingPathDelimiterIfNeeded(std::string directory) { return directory + "/"; } +std::vector GetFilesWithPrefix(const std::string& directory, + const std::string& prefix) { + RTC_DCHECK(absl::EndsWith(directory, "/")); + DIR* dir = ::opendir(directory.c_str()); + if (dir == nullptr) + return {}; + std::vector file_list; + for (struct dirent* dirent = ::readdir(dir); dirent; + dirent = ::readdir(dir)) { + std::string name = dirent->d_name; + if (name.compare(0, prefix.size(), prefix) == 0) { + file_list.emplace_back(directory + name); + } + } + ::closedir(dir); + return file_list; +} + +bool DeleteFile(const std::string& file) { + return ::unlink(file.c_str()) == 0; +} + +bool MoveFile(const std::string& old_file, const std::string& new_file) { + return ::rename(old_file.c_str(), new_file.c_str()) == 0; +} + +bool IsFile(const std::string& file) { + struct stat st; + int res = ::stat(file.c_str(), &st); + // Treat symlinks, named pipes, etc. all as files. + return res == 0 && !S_ISDIR(st.st_mode); +} + bool IsFolder(const std::string& file) { struct stat st; int res = ::stat(file.c_str(), &st); return res == 0 && S_ISDIR(st.st_mode); } +absl::optional GetFileSize(const std::string& file) { + struct stat st; + if (::stat(file.c_str(), &st) != 0) + return absl::nullopt; + return st.st_size; +} + #endif } // namespace @@ -116,7 +207,7 @@ FileRotatingStream::FileRotatingStream(const std::string& dir_path, break; } case kRead: { - file_names_ = GetFilesWithPrefix(); + file_names_ = GetFilesWithPrefix(dir_path_, file_prefix_); std::sort(file_names_.begin(), file_names_.end()); if (file_names_.size() > 0) { // |file_names_| is sorted newest first, so read from the end. @@ -237,10 +328,7 @@ bool FileRotatingStream::GetSize(size_t* size) const { *size = 0; size_t total_size = 0; for (auto file_name : file_names_) { - size_t file_size = 0; - if (Filesystem::GetFileSize(file_name, &file_size)) { - total_size += file_size; - } + total_size += GetFileSize(file_name).value_or(0); } *size = total_size; return true; @@ -258,9 +346,10 @@ bool FileRotatingStream::Open() { return true; case kWrite: { // Delete existing files when opening for write. - std::vector matching_files = GetFilesWithPrefix(); - for (auto matching_file : matching_files) { - if (!Filesystem::DeleteFile(matching_file)) { + std::vector matching_files = + GetFilesWithPrefix(dir_path_, file_prefix_); + for (const auto& matching_file : matching_files) { + if (!DeleteFile(matching_file)) { std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str()); } } @@ -331,16 +420,16 @@ void FileRotatingStream::RotateFiles() { // See header file comments for example. RTC_DCHECK_LT(rotation_index_, file_names_.size()); std::string file_to_delete = file_names_[rotation_index_]; - if (Filesystem::IsFile(file_to_delete)) { - if (!Filesystem::DeleteFile(file_to_delete)) { + if (IsFile(file_to_delete)) { + if (!DeleteFile(file_to_delete)) { std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str()); } } for (auto i = rotation_index_; i > 0; --i) { std::string rotated_name = file_names_[i]; std::string unrotated_name = file_names_[i - 1]; - if (Filesystem::IsFile(unrotated_name)) { - if (!Filesystem::MoveFile(unrotated_name, rotated_name)) { + if (IsFile(unrotated_name)) { + if (!MoveFile(unrotated_name, rotated_name)) { std::fprintf(stderr, "Failed to move: %s to %s\n", unrotated_name.c_str(), rotated_name.c_str()); } @@ -351,23 +440,6 @@ void FileRotatingStream::RotateFiles() { OnRotation(); } -std::vector FileRotatingStream::GetFilesWithPrefix() const { - std::vector files; - // Iterate over the files in the directory. - DirectoryIterator it; - if (!it.Iterate(dir_path_)) { - return files; - } - do { - std::string current_name = it.Name(); - if (current_name.size() && !it.IsDirectory() && - current_name.compare(0, file_prefix_.size(), file_prefix_) == 0) { - files.push_back(it.PathName()); - } - } while (it.Next()); - return files; -} - std::string FileRotatingStream::GetFilePath(size_t index, size_t num_files) const { RTC_DCHECK_LT(index, num_files); diff --git a/rtc_base/filerotatingstream.h b/rtc_base/filerotatingstream.h index 1d3da92eea..c75ee153b6 100644 --- a/rtc_base/filerotatingstream.h +++ b/rtc_base/filerotatingstream.h @@ -102,8 +102,6 @@ class FileRotatingStream : public StreamInterface { // create new file_0 void RotateFiles(); - // Returns a list of file names in the directory beginning with the prefix. - std::vector GetFilesWithPrefix() const; // Private version of GetFilePath. std::string GetFilePath(size_t index, size_t num_files) const; diff --git a/rtc_base/filerotatingstream_unittest.cc b/rtc_base/filerotatingstream_unittest.cc index 7ca7e3ecc7..172be57eac 100644 --- a/rtc_base/filerotatingstream_unittest.cc +++ b/rtc_base/filerotatingstream_unittest.cc @@ -13,7 +13,6 @@ #include "rtc_base/arraysize.h" #include "rtc_base/checks.h" #include "rtc_base/filerotatingstream.h" -#include "rtc_base/fileutils.h" #include "rtc_base/gunit.h" #include "test/testsupport/fileutils.h" @@ -161,12 +160,12 @@ TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) { } // Check that exactly three files exist. for (size_t i = 0; i < arraysize(messages); ++i) { - EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); + EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i))); } std::string message("d"); WriteAndFlush(message.c_str(), message.size()); for (size_t i = 0; i < arraysize(messages); ++i) { - EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); + EXPECT_TRUE(webrtc::test::FileExists(stream_->GetFilePath(i))); } // TODO(tkchin): Maybe check all the files in the dir. diff --git a/rtc_base/fileutils.cc b/rtc_base/fileutils.cc deleted file mode 100644 index 1086f3c7f7..0000000000 --- a/rtc_base/fileutils.cc +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/fileutils.h" - -#include "rtc_base/checks.h" - -#if defined(WEBRTC_WIN) -#include "rtc_base/stringutils.h" // for ToUtf16 -#include "rtc_base/win32filesystem.h" -#else -#include "rtc_base/unixfilesystem.h" -#endif - -#if !defined(WEBRTC_WIN) -#define MAX_PATH 260 -#endif - -namespace rtc { - -////////////////////////// -// Directory Iterator // -////////////////////////// - -// A DirectoryIterator is created with a given directory. It originally points -// to the first file in the directory, and can be advanecd with Next(). This -// allows you to get information about each file. - -// Constructor -DirectoryIterator::DirectoryIterator() -#ifdef WEBRTC_WIN - : handle_(INVALID_HANDLE_VALUE) { -#else - : dir_(nullptr), - dirent_(nullptr){ -#endif -} - -// Destructor -DirectoryIterator::~DirectoryIterator() { -#if defined(WEBRTC_WIN) - if (handle_ != INVALID_HANDLE_VALUE) - ::FindClose(handle_); -#else - if (dir_) - closedir(dir_); -#endif -} - -// Starts traversing a directory. -// dir is the directory to traverse -// returns true if the directory exists and is valid -bool DirectoryIterator::Iterate(const std::string& dir) { - directory_ = dir; -#if defined(WEBRTC_WIN) - if (handle_ != INVALID_HANDLE_VALUE) - ::FindClose(handle_); - std::string d = dir + '*'; - handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_); - if (handle_ == INVALID_HANDLE_VALUE) - return false; -#else - if (dir_ != nullptr) - closedir(dir_); - dir_ = ::opendir(directory_.c_str()); - if (dir_ == nullptr) - return false; - dirent_ = readdir(dir_); - if (dirent_ == nullptr) - return false; - - if (::stat(PathName().c_str(), &stat_) != 0) - return false; -#endif - return true; -} - -// Advances to the next file -// returns true if there were more files in the directory. -bool DirectoryIterator::Next() { -#if defined(WEBRTC_WIN) - return ::FindNextFile(handle_, &data_) == TRUE; -#else - dirent_ = ::readdir(dir_); - if (dirent_ == nullptr) - return false; - - return ::stat(PathName().c_str(), &stat_) == 0; -#endif -} - -// returns true if the file currently pointed to is a directory -bool DirectoryIterator::IsDirectory() const { -#if defined(WEBRTC_WIN) - return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE; -#else - return S_ISDIR(stat_.st_mode); -#endif -} - -// returns the name of the file currently pointed to -std::string DirectoryIterator::Name() const { -#if defined(WEBRTC_WIN) - return ToUtf8(data_.cFileName); -#else - RTC_DCHECK(dirent_); - return std::string(dirent_->d_name); -#endif -} - -// returns the name of the file currently pointed to -std::string DirectoryIterator::PathName() const { -#if defined(WEBRTC_WIN) - return directory_ + "\\" + ToUtf8(data_.cFileName); -#else - RTC_DCHECK(dirent_); - return directory_ + "/" + dirent_->d_name; -#endif -} - -FilesystemInterface* Filesystem::GetFilesystem() { -#if defined(WEBRTC_WIN) - static FilesystemInterface* const filesystem = new Win32Filesystem(); -#else - static FilesystemInterface* const filesystem = new UnixFilesystem(); -#endif - - return filesystem; -} - -} // namespace rtc diff --git a/rtc_base/fileutils.h b/rtc_base/fileutils.h deleted file mode 100644 index d85b6b1787..0000000000 --- a/rtc_base/fileutils.h +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef RTC_BASE_FILEUTILS_H_ -#define RTC_BASE_FILEUTILS_H_ - -#include - -#if defined(WEBRTC_WIN) -#include -#else -#include -#include -#include -#endif // WEBRTC_WIN - -#include "rtc_base/constructormagic.h" - -namespace rtc { - -////////////////////////// -// Directory Iterator // -////////////////////////// - -// A DirectoryIterator is created with a given directory. It originally points -// to the first file in the directory, and can be advanecd with Next(). This -// allows you to get information about each file. - -class DirectoryIterator { - friend class Filesystem; - - public: - // Constructor - DirectoryIterator(); - // Destructor - virtual ~DirectoryIterator(); - - // Starts traversing a directory. - // |dir| is the directory to traverse. - // returns true if the directory exists and is valid. - // The iterator will point to the first entry in the directory. - virtual bool Iterate(const std::string& dir); - - // Advances to the next file - // returns true if there were more files in the directory. - virtual bool Next(); - - // Returns true if the file currently pointed to is a directory. - virtual bool IsDirectory() const; - - // Returns the name of the file currently pointed to. - virtual std::string Name() const; - - // Returns complete name of the file, including directory part. - virtual std::string PathName() const; - - private: - std::string directory_; -#if defined(WEBRTC_WIN) - WIN32_FIND_DATA data_; - HANDLE handle_; -#else - DIR* dir_; - struct dirent* dirent_; - struct stat stat_; -#endif -}; - -class FilesystemInterface { - public: - virtual ~FilesystemInterface() {} - - // This will attempt to delete the path located at filename. - // It DCHECKs and returns false if the path points to a folder or a - // non-existent file. - virtual bool DeleteFile(const std::string& filename) = 0; - - // This moves a file from old_path to new_path, where "old_path" is a - // plain file. This DCHECKs and returns false if old_path points to a - // directory, and returns true if the function succeeds. - virtual bool MoveFile(const std::string& old_path, - const std::string& new_path) = 0; - - // Returns true if pathname refers to a file - virtual bool IsFile(const std::string& pathname) = 0; - - // Determines the size of the file indicated by path. - virtual bool GetFileSize(const std::string& path, size_t* size) = 0; -}; - -class Filesystem { - public: - static bool DeleteFile(const std::string& filename) { - return GetFilesystem()->DeleteFile(filename); - } - - static bool MoveFile(const std::string& old_path, - const std::string& new_path) { - return GetFilesystem()->MoveFile(old_path, new_path); - } - - static bool IsFile(const std::string& pathname) { - return GetFilesystem()->IsFile(pathname); - } - - static bool GetFileSize(const std::string& path, size_t* size) { - return GetFilesystem()->GetFileSize(path, size); - } - - private: - static FilesystemInterface* GetFilesystem(); - RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem); -}; - -} // namespace rtc - -#endif // RTC_BASE_FILEUTILS_H_ diff --git a/rtc_base/unixfilesystem.cc b/rtc_base/unixfilesystem.cc deleted file mode 100644 index ee9e3f069a..0000000000 --- a/rtc_base/unixfilesystem.cc +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/unixfilesystem.h" - -#include -#include -#include - -#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) -#include -#include -#include - -#include "rtc_base/macutils.h" -#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) - -#if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) -#include -#if defined(WEBRTC_ANDROID) -#include -#elif !defined(__native_client__) -#include -#endif // !defined(__native_client__) -#include -#endif // WEBRTC_POSIX && !WEBRTC_MAC || WEBRTC_IOS - -#if defined(__native_client__) && !defined(__GLIBC__) -#include -#endif - -#include "rtc_base/checks.h" -#include "rtc_base/logging.h" - -namespace rtc { - -UnixFilesystem::UnixFilesystem() {} - -UnixFilesystem::~UnixFilesystem() {} - -bool UnixFilesystem::DeleteFile(const std::string& filename) { - RTC_LOG(LS_INFO) << "Deleting file:" << filename; - - if (!IsFile(filename)) { - RTC_DCHECK(IsFile(filename)); - return false; - } - return ::unlink(filename.c_str()) == 0; -} - -bool UnixFilesystem::MoveFile(const std::string& old_path, - const std::string& new_path) { - if (!IsFile(old_path)) { - RTC_DCHECK(IsFile(old_path)); - return false; - } - RTC_LOG(LS_VERBOSE) << "Moving " << old_path << " to " << new_path; - if (rename(old_path.c_str(), new_path.c_str()) != 0) { - return false; - } - return true; -} - -bool UnixFilesystem::IsFile(const std::string& pathname) { - struct stat st; - int res = ::stat(pathname.c_str(), &st); - // Treat symlinks, named pipes, etc. all as files. - return res == 0 && !S_ISDIR(st.st_mode); -} - -bool UnixFilesystem::GetFileSize(const std::string& pathname, size_t* size) { - struct stat st; - if (::stat(pathname.c_str(), &st) != 0) - return false; - *size = st.st_size; - return true; -} - -} // namespace rtc - -#if defined(__native_client__) -extern "C" int __attribute__((weak)) -link(const char* oldpath, const char* newpath) { - errno = EACCES; - return -1; -} -#endif diff --git a/rtc_base/unixfilesystem.h b/rtc_base/unixfilesystem.h deleted file mode 100644 index 32af9b510a..0000000000 --- a/rtc_base/unixfilesystem.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef RTC_BASE_UNIXFILESYSTEM_H_ -#define RTC_BASE_UNIXFILESYSTEM_H_ - -#include - -#include "rtc_base/fileutils.h" - -namespace rtc { - -class UnixFilesystem : public FilesystemInterface { - public: - UnixFilesystem(); - ~UnixFilesystem() override; - - // This will attempt to delete the file located at filename. - // It will fail with VERIY if you pass it a non-existant file, or a directory. - bool DeleteFile(const std::string& filename) override; - - // This moves a file from old_path to new_path, where "file" can be a plain - // file or directory, which will be moved recursively. - // Returns true if function succeeds. - bool MoveFile(const std::string& old_path, - const std::string& new_path) override; - - // Returns true of pathname represents an existing file - bool IsFile(const std::string& pathname) override; - - bool GetFileSize(const std::string& path, size_t* size) override; -}; - -} // namespace rtc - -#endif // RTC_BASE_UNIXFILESYSTEM_H_ diff --git a/rtc_base/win32filesystem.cc b/rtc_base/win32filesystem.cc deleted file mode 100644 index 5465a5cbb7..0000000000 --- a/rtc_base/win32filesystem.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/win32filesystem.h" - -#include -#include -#include -#include "rtc_base/win32.h" - -#include - -#include "rtc_base/arraysize.h" -#include "rtc_base/checks.h" -#include "rtc_base/fileutils.h" -#include "rtc_base/logging.h" -#include "rtc_base/stream.h" -#include "rtc_base/stringutils.h" - -// In several places in this file, we test the integrity level of the process -// before calling GetLongPathName. We do this because calling GetLongPathName -// when running under protected mode IE (a low integrity process) can result in -// a virtualized path being returned, which is wrong if you only plan to read. -// TODO: Waiting to hear back from IE team on whether this is the -// best approach; IEIsProtectedModeProcess is another possible solution. - -namespace rtc { - -bool Win32Filesystem::DeleteFile(const std::string& filename) { - RTC_LOG(LS_INFO) << "Deleting file " << filename; - if (!IsFile(filename)) { - RTC_DCHECK(IsFile(filename)); - return false; - } - return ::DeleteFile(ToUtf16(filename).c_str()) != 0; -} - -bool Win32Filesystem::MoveFile(const std::string& old_path, - const std::string& new_path) { - if (!IsFile(old_path)) { - RTC_DCHECK(IsFile(old_path)); - return false; - } - RTC_LOG(LS_INFO) << "Moving " << old_path << " to " << new_path; - return ::MoveFile(ToUtf16(old_path).c_str(), ToUtf16(new_path).c_str()) != 0; -} - -bool Win32Filesystem::IsFile(const std::string& path) { - WIN32_FILE_ATTRIBUTE_DATA data = {0}; - if (0 == ::GetFileAttributesEx(ToUtf16(path).c_str(), GetFileExInfoStandard, - &data)) - return false; - return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; -} - -bool Win32Filesystem::GetFileSize(const std::string& pathname, size_t* size) { - WIN32_FILE_ATTRIBUTE_DATA data = {0}; - if (::GetFileAttributesEx(ToUtf16(pathname).c_str(), GetFileExInfoStandard, - &data) == 0) - return false; - *size = data.nFileSizeLow; - return true; -} - -} // namespace rtc diff --git a/rtc_base/win32filesystem.h b/rtc_base/win32filesystem.h deleted file mode 100644 index 2c27f90575..0000000000 --- a/rtc_base/win32filesystem.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef RTC_BASE_WIN32FILESYSTEM_H_ -#define RTC_BASE_WIN32FILESYSTEM_H_ - -#include "fileutils.h" - -namespace rtc { - -class Win32Filesystem : public FilesystemInterface { - public: - // This will attempt to delete the path located at filename. - // If the path points to a folder, it will fail with VERIFY - bool DeleteFile(const std::string& filename) override; - - // This moves a file from old_path to new_path. If the new path is on a - // different volume than the old, it will attempt to copy and then delete - // the folder - // Returns true if the file is successfully moved - bool MoveFile(const std::string& old_path, - const std::string& new_path) override; - - // Returns true if a file exists at path - bool IsFile(const std::string& path) override; - - bool GetFileSize(const std::string& path, size_t* size) override; -}; - -} // namespace rtc - -#endif // RTC_BASE_WIN32FILESYSTEM_H_