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 <nisse@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25568}
This commit is contained in:
@ -741,8 +741,6 @@ rtc_static_library("rtc_base") {
|
|||||||
"dscp.h",
|
"dscp.h",
|
||||||
"filerotatingstream.cc",
|
"filerotatingstream.cc",
|
||||||
"filerotatingstream.h",
|
"filerotatingstream.h",
|
||||||
"fileutils.cc",
|
|
||||||
"fileutils.h",
|
|
||||||
"gunit_prod.h",
|
"gunit_prod.h",
|
||||||
"helpers.cc",
|
"helpers.cc",
|
||||||
"helpers.h",
|
"helpers.h",
|
||||||
@ -894,18 +892,12 @@ rtc_static_library("rtc_base") {
|
|||||||
"macutils.cc",
|
"macutils.cc",
|
||||||
"macutils.h",
|
"macutils.h",
|
||||||
]
|
]
|
||||||
libs += [
|
|
||||||
# For ProcessInformationCopyDictionary in unixfilesystem.cc.
|
|
||||||
"ApplicationServices.framework",
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_win) {
|
if (is_win) {
|
||||||
sources += [
|
sources += [
|
||||||
"win32.cc",
|
"win32.cc",
|
||||||
"win32.h",
|
"win32.h",
|
||||||
"win32filesystem.cc",
|
|
||||||
"win32filesystem.h",
|
|
||||||
"win32window.cc",
|
"win32window.cc",
|
||||||
"win32window.h",
|
"win32window.h",
|
||||||
]
|
]
|
||||||
@ -923,8 +915,6 @@ rtc_static_library("rtc_base") {
|
|||||||
sources += [
|
sources += [
|
||||||
"ifaddrs_converter.cc",
|
"ifaddrs_converter.cc",
|
||||||
"ifaddrs_converter.h",
|
"ifaddrs_converter.h",
|
||||||
"unixfilesystem.cc",
|
|
||||||
"unixfilesystem.h",
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,12 +19,12 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include "rtc_base/stringutils.h"
|
#include "rtc_base/stringutils.h"
|
||||||
#else
|
#else
|
||||||
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#endif // WEBRTC_WIN
|
#endif // WEBRTC_WIN
|
||||||
|
|
||||||
#include "absl/strings/match.h"
|
#include "absl/strings/match.h"
|
||||||
#include "rtc_base/checks.h"
|
#include "rtc_base/checks.h"
|
||||||
#include "rtc_base/fileutils.h"
|
|
||||||
#include "rtc_base/logging.h"
|
#include "rtc_base/logging.h"
|
||||||
|
|
||||||
// Note: We use fprintf for logging in the write paths of this stream to avoid
|
// Note: We use fprintf for logging in the write paths of this stream to avoid
|
||||||
@ -35,7 +35,16 @@ namespace rtc {
|
|||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
std::string AddTrailingPathDelimiterIfNeeded(std::string directory);
|
std::string AddTrailingPathDelimiterIfNeeded(std::string directory);
|
||||||
|
|
||||||
|
// |dir| must have a trailing delimiter. |prefix| must not include wild card
|
||||||
|
// characters.
|
||||||
|
std::vector<std::string> 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);
|
bool IsFolder(const std::string& file);
|
||||||
|
absl::optional<size_t> GetFileSize(const std::string& file);
|
||||||
|
|
||||||
#if defined(WEBRTC_WIN)
|
#if defined(WEBRTC_WIN)
|
||||||
|
|
||||||
@ -46,6 +55,40 @@ std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
|
|||||||
return directory + "\\";
|
return directory + "\\";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> 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<std::string> 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) {
|
bool IsFolder(const std::string& file) {
|
||||||
WIN32_FILE_ATTRIBUTE_DATA data = {0};
|
WIN32_FILE_ATTRIBUTE_DATA data = {0};
|
||||||
if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
|
if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
|
||||||
@ -55,6 +98,14 @@ bool IsFolder(const std::string& file) {
|
|||||||
FILE_ATTRIBUTE_DIRECTORY;
|
FILE_ATTRIBUTE_DIRECTORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::optional<size_t> 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)
|
#else // defined(WEBRTC_WIN)
|
||||||
|
|
||||||
std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
|
std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
|
||||||
@ -64,12 +115,52 @@ std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
|
|||||||
return directory + "/";
|
return directory + "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> 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<std::string> 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) {
|
bool IsFolder(const std::string& file) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int res = ::stat(file.c_str(), &st);
|
int res = ::stat(file.c_str(), &st);
|
||||||
return res == 0 && S_ISDIR(st.st_mode);
|
return res == 0 && S_ISDIR(st.st_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::optional<size_t> GetFileSize(const std::string& file) {
|
||||||
|
struct stat st;
|
||||||
|
if (::stat(file.c_str(), &st) != 0)
|
||||||
|
return absl::nullopt;
|
||||||
|
return st.st_size;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@ -116,7 +207,7 @@ FileRotatingStream::FileRotatingStream(const std::string& dir_path,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case kRead: {
|
case kRead: {
|
||||||
file_names_ = GetFilesWithPrefix();
|
file_names_ = GetFilesWithPrefix(dir_path_, file_prefix_);
|
||||||
std::sort(file_names_.begin(), file_names_.end());
|
std::sort(file_names_.begin(), file_names_.end());
|
||||||
if (file_names_.size() > 0) {
|
if (file_names_.size() > 0) {
|
||||||
// |file_names_| is sorted newest first, so read from the end.
|
// |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 = 0;
|
||||||
size_t total_size = 0;
|
size_t total_size = 0;
|
||||||
for (auto file_name : file_names_) {
|
for (auto file_name : file_names_) {
|
||||||
size_t file_size = 0;
|
total_size += GetFileSize(file_name).value_or(0);
|
||||||
if (Filesystem::GetFileSize(file_name, &file_size)) {
|
|
||||||
total_size += file_size;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*size = total_size;
|
*size = total_size;
|
||||||
return true;
|
return true;
|
||||||
@ -258,9 +346,10 @@ bool FileRotatingStream::Open() {
|
|||||||
return true;
|
return true;
|
||||||
case kWrite: {
|
case kWrite: {
|
||||||
// Delete existing files when opening for write.
|
// Delete existing files when opening for write.
|
||||||
std::vector<std::string> matching_files = GetFilesWithPrefix();
|
std::vector<std::string> matching_files =
|
||||||
for (auto matching_file : matching_files) {
|
GetFilesWithPrefix(dir_path_, file_prefix_);
|
||||||
if (!Filesystem::DeleteFile(matching_file)) {
|
for (const auto& matching_file : matching_files) {
|
||||||
|
if (!DeleteFile(matching_file)) {
|
||||||
std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str());
|
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.
|
// See header file comments for example.
|
||||||
RTC_DCHECK_LT(rotation_index_, file_names_.size());
|
RTC_DCHECK_LT(rotation_index_, file_names_.size());
|
||||||
std::string file_to_delete = file_names_[rotation_index_];
|
std::string file_to_delete = file_names_[rotation_index_];
|
||||||
if (Filesystem::IsFile(file_to_delete)) {
|
if (IsFile(file_to_delete)) {
|
||||||
if (!Filesystem::DeleteFile(file_to_delete)) {
|
if (!DeleteFile(file_to_delete)) {
|
||||||
std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
|
std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto i = rotation_index_; i > 0; --i) {
|
for (auto i = rotation_index_; i > 0; --i) {
|
||||||
std::string rotated_name = file_names_[i];
|
std::string rotated_name = file_names_[i];
|
||||||
std::string unrotated_name = file_names_[i - 1];
|
std::string unrotated_name = file_names_[i - 1];
|
||||||
if (Filesystem::IsFile(unrotated_name)) {
|
if (IsFile(unrotated_name)) {
|
||||||
if (!Filesystem::MoveFile(unrotated_name, rotated_name)) {
|
if (!MoveFile(unrotated_name, rotated_name)) {
|
||||||
std::fprintf(stderr, "Failed to move: %s to %s\n",
|
std::fprintf(stderr, "Failed to move: %s to %s\n",
|
||||||
unrotated_name.c_str(), rotated_name.c_str());
|
unrotated_name.c_str(), rotated_name.c_str());
|
||||||
}
|
}
|
||||||
@ -351,23 +440,6 @@ void FileRotatingStream::RotateFiles() {
|
|||||||
OnRotation();
|
OnRotation();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> FileRotatingStream::GetFilesWithPrefix() const {
|
|
||||||
std::vector<std::string> 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,
|
std::string FileRotatingStream::GetFilePath(size_t index,
|
||||||
size_t num_files) const {
|
size_t num_files) const {
|
||||||
RTC_DCHECK_LT(index, num_files);
|
RTC_DCHECK_LT(index, num_files);
|
||||||
|
@ -102,8 +102,6 @@ class FileRotatingStream : public StreamInterface {
|
|||||||
// create new file_0
|
// create new file_0
|
||||||
void RotateFiles();
|
void RotateFiles();
|
||||||
|
|
||||||
// Returns a list of file names in the directory beginning with the prefix.
|
|
||||||
std::vector<std::string> GetFilesWithPrefix() const;
|
|
||||||
// Private version of GetFilePath.
|
// Private version of GetFilePath.
|
||||||
std::string GetFilePath(size_t index, size_t num_files) const;
|
std::string GetFilePath(size_t index, size_t num_files) const;
|
||||||
|
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
#include "rtc_base/arraysize.h"
|
#include "rtc_base/arraysize.h"
|
||||||
#include "rtc_base/checks.h"
|
#include "rtc_base/checks.h"
|
||||||
#include "rtc_base/filerotatingstream.h"
|
#include "rtc_base/filerotatingstream.h"
|
||||||
#include "rtc_base/fileutils.h"
|
|
||||||
#include "rtc_base/gunit.h"
|
#include "rtc_base/gunit.h"
|
||||||
#include "test/testsupport/fileutils.h"
|
#include "test/testsupport/fileutils.h"
|
||||||
|
|
||||||
@ -161,12 +160,12 @@ TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) {
|
|||||||
}
|
}
|
||||||
// Check that exactly three files exist.
|
// Check that exactly three files exist.
|
||||||
for (size_t i = 0; i < arraysize(messages); ++i) {
|
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");
|
std::string message("d");
|
||||||
WriteAndFlush(message.c_str(), message.size());
|
WriteAndFlush(message.c_str(), message.size());
|
||||||
for (size_t i = 0; i < arraysize(messages); ++i) {
|
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.
|
// TODO(tkchin): Maybe check all the files in the dir.
|
||||||
|
|
||||||
|
@ -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
|
|
@ -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 <string>
|
|
||||||
|
|
||||||
#if defined(WEBRTC_WIN)
|
|
||||||
#include <windows.h>
|
|
||||||
#else
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#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_
|
|
@ -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 <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
|
||||||
#include <CoreServices/CoreServices.h>
|
|
||||||
#include <IOKit/IOCFBundle.h>
|
|
||||||
#include <sys/statvfs.h>
|
|
||||||
|
|
||||||
#include "rtc_base/macutils.h"
|
|
||||||
#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
|
|
||||||
|
|
||||||
#if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
|
|
||||||
#include <sys/types.h>
|
|
||||||
#if defined(WEBRTC_ANDROID)
|
|
||||||
#include <sys/statfs.h>
|
|
||||||
#elif !defined(__native_client__)
|
|
||||||
#include <sys/statvfs.h>
|
|
||||||
#endif // !defined(__native_client__)
|
|
||||||
#include <stdio.h>
|
|
||||||
#endif // WEBRTC_POSIX && !WEBRTC_MAC || WEBRTC_IOS
|
|
||||||
|
|
||||||
#if defined(__native_client__) && !defined(__GLIBC__)
|
|
||||||
#include <sys/syslimits.h>
|
|
||||||
#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
|
|
@ -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 <stddef.h>
|
|
||||||
|
|
||||||
#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_
|
|
@ -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 <shellapi.h>
|
|
||||||
#include <shlobj.h>
|
|
||||||
#include <tchar.h>
|
|
||||||
#include "rtc_base/win32.h"
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#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
|
|
@ -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_
|
|
Reference in New Issue
Block a user