Delete unused Filesystem methods GetAppDataFolder and GetDiskFreeSpace.

BUG=webrtc:6424

Review-Url: https://codereview.webrtc.org/2699143002
Cr-Commit-Position: refs/heads/master@{#16709}
This commit is contained in:
nisse 2017-02-20 00:37:21 -08:00 committed by Commit bot
parent e29dfb7e36
commit bf25bbdc63
6 changed files with 0 additions and 236 deletions

View File

@ -155,19 +155,12 @@ class FilesystemInterface {
virtual bool GetFileTime(const Pathname& path, FileTimeType which,
time_t* time) = 0;
// Get a folder that is unique to the current application, which is suitable
// for sharing data between executions of the app. If the per_user arg is
// true, the folder is also specific to the current user.
virtual bool GetAppDataFolder(Pathname* path, bool per_user) = 0;
// Get a temporary folder that is unique to the current user and application.
// TODO: Re-evaluate the goals of this function. We probably just need any
// directory that won't collide with another existing directory, and which
// will be cleaned up when the program exits.
virtual bool GetAppTempFolder(Pathname* path) = 0;
virtual bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) = 0;
// Note: These might go into some shared config section later, but they're
// used by some methods in this interface, so we're leaving them here for now.
void SetOrganizationName(const std::string& organization) {
@ -276,18 +269,10 @@ class Filesystem {
return EnsureDefaultFilesystem()->GetFileTime(path, which, time);
}
static bool GetAppDataFolder(Pathname* path, bool per_user) {
return EnsureDefaultFilesystem()->GetAppDataFolder(path, per_user);
}
static bool GetAppTempFolder(Pathname* path) {
return EnsureDefaultFilesystem()->GetAppTempFolder(path);
}
static bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) {
return EnsureDefaultFilesystem()->GetDiskFreeSpace(path, freebytes);
}
static void SetOrganizationName(const std::string& organization) {
EnsureDefaultFilesystem()->SetOrganizationName(organization);
}

View File

@ -70,37 +70,4 @@ TEST(MAYBE_FilesystemTest, TestOpenBadFile) {
EXPECT_FALSE(fs != NULL);
}
// Test checking for free disk space.
TEST(MAYBE_FilesystemTest, TestGetDiskFreeSpace) {
// Note that we should avoid picking any file/folder which could be located
// at the remotely mounted drive/device.
Pathname path;
ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true));
int64_t free1 = 0;
EXPECT_TRUE(Filesystem::IsFolder(path));
EXPECT_FALSE(Filesystem::IsFile(path));
EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1));
EXPECT_GT(free1, 0);
int64_t free2 = 0;
path.AppendFolder("this_folder_doesnt_exist");
EXPECT_FALSE(Filesystem::IsFolder(path));
EXPECT_TRUE(Filesystem::IsAbsent(path));
EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free2));
// These should be the same disk, and disk free space should not have changed
// by more than 1% between the two calls.
EXPECT_LT(static_cast<int64_t>(free1 * .9), free2);
EXPECT_LT(free2, static_cast<int64_t>(free1 * 1.1));
int64_t free3 = 0;
path.clear();
EXPECT_TRUE(path.empty());
EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free3));
// Current working directory may not be where exe is.
// EXPECT_LT(static_cast<int64_t>(free1 * .9), free3);
// EXPECT_LT(free3, static_cast<int64_t>(free1 * 1.1));
EXPECT_GT(free3, 0);
}
} // namespace rtc

View File

@ -313,81 +313,6 @@ bool UnixFilesystem::GetFileTime(const Pathname& path, FileTimeType which,
return true;
}
bool UnixFilesystem::GetAppDataFolder(Pathname* path, bool per_user) {
// On macOS and iOS, there is no requirement that the path contains the
// organization.
#if !defined(WEBRTC_MAC)
RTC_DCHECK(!organization_name_.empty());
#endif
RTC_DCHECK(!application_name_.empty());
// First get the base directory for app data.
#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
RTC_DCHECK(provided_app_data_folder_ != NULL);
path->SetPathname(provided_app_data_folder_, "");
#elif defined(WEBRTC_LINUX) // && !WEBRTC_MAC && !WEBRTC_ANDROID
if (per_user) {
// We follow the recommendations in
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
// It specifies separate directories for data and config files, but
// GetAppDataFolder() does not distinguish. We just return the config dir
// path.
const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
if (xdg_config_home) {
path->SetPathname(xdg_config_home, "");
} else {
// XDG says to default to $HOME/.config. We also support falling back to
// other synonyms for HOME if for some reason it is not defined.
const char* homedir;
if (const char* home = getenv("HOME")) {
homedir = home;
} else if (const char* dotdir = getenv("DOTDIR")) {
homedir = dotdir;
} else if (passwd* pw = getpwuid(geteuid())) {
homedir = pw->pw_dir;
} else {
return false;
}
path->SetPathname(homedir, "");
path->AppendFolder(".config");
}
} else {
// XDG does not define a standard directory for writable global data. Let's
// just use this.
path->SetPathname("/var/cache/", "");
}
#endif // !WEBRTC_MAC && !WEBRTC_LINUX
// Now add on a sub-path for our app.
#if defined(WEBRTC_MAC) || defined(WEBRTC_ANDROID)
path->AppendFolder(organization_name_);
path->AppendFolder(application_name_);
#elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
// XDG says to use a single directory level, so we concatenate the org and app
// name with a hyphen. We also do the Linuxy thing and convert to all
// lowercase with no spaces.
std::string subdir(organization_name_);
subdir.append("-");
subdir.append(application_name_);
replace_substrs(" ", 1, "", 0, &subdir);
std::transform(subdir.begin(), subdir.end(), subdir.begin(), ::tolower);
path->AppendFolder(subdir);
#endif
if (!CreateFolder(*path, 0700)) {
return false;
}
#if !defined(__native_client__)
// If the folder already exists, it may have the wrong mode or be owned by
// someone else, both of which are security problems. Setting the mode
// avoids both issues since it will fail if the path is not owned by us.
if (0 != ::chmod(path->pathname().c_str(), 0700)) {
LOG_ERR(LS_ERROR) << "Can't set mode on " << path;
return false;
}
#endif
return true;
}
bool UnixFilesystem::GetAppTempFolder(Pathname* path) {
#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
RTC_DCHECK(provided_app_temp_folder_ != NULL);
@ -418,40 +343,6 @@ bool UnixFilesystem::GetAppTempFolder(Pathname* path) {
#endif
}
bool UnixFilesystem::GetDiskFreeSpace(const Pathname& path,
int64_t* freebytes) {
#ifdef __native_client__
return false;
#else // __native_client__
RTC_DCHECK(NULL != freebytes);
// TODO: Consider making relative paths absolute using cwd.
// TODO: When popping off a symlink, push back on the components of the
// symlink, so we don't jump out of the target disk inadvertently.
Pathname existing_path(path.folder(), "");
while (!existing_path.folder().empty() && IsAbsent(existing_path)) {
existing_path.SetFolder(existing_path.parent_folder());
}
#if defined(WEBRTC_ANDROID)
struct statfs vfs;
memset(&vfs, 0, sizeof(vfs));
if (0 != statfs(existing_path.pathname().c_str(), &vfs))
return false;
#else
struct statvfs vfs;
memset(&vfs, 0, sizeof(vfs));
if (0 != statvfs(existing_path.pathname().c_str(), &vfs))
return false;
#endif // WEBRTC_ANDROID
#if defined(WEBRTC_LINUX)
*freebytes = static_cast<int64_t>(vfs.f_bsize) * vfs.f_bavail;
#elif defined(WEBRTC_MAC)
*freebytes = static_cast<int64_t>(vfs.f_frsize) * vfs.f_bavail;
#endif
return true;
#endif // !__native_client__
}
char* UnixFilesystem::CopyString(const std::string& str) {
size_t size = str.length() + 1;

View File

@ -94,13 +94,9 @@ class UnixFilesystem : public FilesystemInterface {
FileTimeType which,
time_t* time) override;
bool GetAppDataFolder(Pathname* path, bool per_user) override;
// Get a temporary folder that is unique to the current user and application.
bool GetAppTempFolder(Pathname* path) override;
bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) override;
private:
#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
static char* provided_app_data_folder_;

View File

@ -216,34 +216,6 @@ bool Win32Filesystem::GetAppPathname(Pathname* path) {
return true;
}
bool Win32Filesystem::GetAppDataFolder(Pathname* path, bool per_user) {
RTC_DCHECK(!organization_name_.empty());
RTC_DCHECK(!application_name_.empty());
TCHAR buffer[MAX_PATH + 1];
int csidl = per_user ? CSIDL_LOCAL_APPDATA : CSIDL_COMMON_APPDATA;
if (!::SHGetSpecialFolderPath(NULL, buffer, csidl, TRUE))
return false;
if (!IsCurrentProcessLowIntegrity() &&
!::GetLongPathName(buffer, buffer, arraysize(buffer)))
return false;
size_t len = strcatn(buffer, arraysize(buffer), __T("\\"));
len += strcpyn(buffer + len, arraysize(buffer) - len,
ToUtf16(organization_name_).c_str());
if ((len > 0) && (buffer[len-1] != __T('\\'))) {
len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\"));
}
len += strcpyn(buffer + len, arraysize(buffer) - len,
ToUtf16(application_name_).c_str());
if ((len > 0) && (buffer[len-1] != __T('\\'))) {
len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\"));
}
if (len >= arraysize(buffer) - 1)
return false;
path->clear();
path->SetFolder(ToUtf8(buffer));
return CreateFolder(*path);
}
bool Win32Filesystem::GetAppTempFolder(Pathname* path) {
if (!GetAppPathname(path))
return false;
@ -251,47 +223,4 @@ bool Win32Filesystem::GetAppTempFolder(Pathname* path) {
return GetTemporaryFolder(*path, true, &filename);
}
bool Win32Filesystem::GetDiskFreeSpace(const Pathname& path,
int64_t* free_bytes) {
if (!free_bytes) {
return false;
}
char drive[4];
std::wstring drive16;
const wchar_t* target_drive = NULL;
if (path.GetDrive(drive, sizeof(drive))) {
drive16 = ToUtf16(drive);
target_drive = drive16.c_str();
} else if (path.folder().substr(0, 2) == "\\\\") {
// UNC path, fail.
// TODO: Handle UNC paths.
return false;
} else {
// The path is probably relative. GetDriveType and GetDiskFreeSpaceEx
// use the current drive if NULL is passed as the drive name.
// TODO: Add method to Pathname to determine if the path is relative.
// TODO: Add method to Pathname to convert a path to absolute.
}
UINT drive_type = ::GetDriveType(target_drive);
if ((drive_type == DRIVE_REMOTE) || (drive_type == DRIVE_UNKNOWN)) {
LOG(LS_VERBOSE) << "Remote or unknown drive: " << drive;
return false;
}
int64_t total_number_of_bytes; // receives the number of bytes on disk
int64_t total_number_of_free_bytes; // receives the free bytes on disk
// make sure things won't change in 64 bit machine
// TODO replace with compile time assert
RTC_DCHECK(sizeof(ULARGE_INTEGER) == sizeof(uint64_t)); // NOLINT
if (::GetDiskFreeSpaceEx(target_drive,
(PULARGE_INTEGER)free_bytes,
(PULARGE_INTEGER)&total_number_of_bytes,
(PULARGE_INTEGER)&total_number_of_free_bytes)) {
return true;
} else {
LOG(LS_VERBOSE) << "GetDiskFreeSpaceEx returns error.";
return false;
}
}
} // namespace rtc

View File

@ -74,13 +74,9 @@ class Win32Filesystem : public FilesystemInterface {
virtual bool GetTemporaryFolder(Pathname &path, bool create,
const std::string *append);
virtual bool GetAppDataFolder(Pathname* path, bool per_user);
// Get a temporary folder that is unique to the current user and application.
virtual bool GetAppTempFolder(Pathname* path);
virtual bool GetDiskFreeSpace(const Pathname& path, int64_t* free_bytes);
private:
// Returns the path to the running application.
bool GetAppPathname(Pathname* path);