Correction to file remove utility
[ Merge of http://go/wvgerrit/84989 ] FileUtils::Remove() when used with wildcards would ignore a prefix specified before the asterisk and delete all files with the same extension. Fix proposed by broadcom. Bug: 120039689 Test: WV unit/integration tests Change-Id: Iddc6c6b1983c41b501b21f34626f56c0b74af6c8
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#include "file_utils.h"
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "file_store.h"
|
||||
#include "file_utils.h"
|
||||
#include "test_vectors.h"
|
||||
|
||||
namespace wvcdm {
|
||||
@@ -15,8 +13,10 @@ namespace {
|
||||
const std::string kTestDirName = "test_dir";
|
||||
const std::string kTestFileName = "test.txt";
|
||||
const std::string kTestFileName2 = "test2.txt";
|
||||
const std::string kTestFileNameWithDifferentExt = "test.log";
|
||||
const std::string kTestFileNameWithDifferentPrefix = "file.txt";
|
||||
const std::string kTestFileNamePrefix = "test";
|
||||
const std::string kTestFileNameExt = ".txt";
|
||||
const std::string kWildcard = "*";
|
||||
} // namespace
|
||||
|
||||
class FileUtilsTest : public testing::Test {
|
||||
@@ -38,6 +38,16 @@ class FileUtilsTest : public testing::Test {
|
||||
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
||||
}
|
||||
|
||||
void CreateTestFile(const std::string file_path) {
|
||||
std::string write_data = GenerateRandomData(600);
|
||||
size_t write_data_size = write_data.size();
|
||||
std::unique_ptr<File> file =
|
||||
file_system.Open(file_path, FileSystem::kCreate);
|
||||
EXPECT_TRUE(file);
|
||||
EXPECT_EQ(file->Write(write_data.data(), write_data_size), write_data_size);
|
||||
EXPECT_TRUE(file_system.Exists(file_path));
|
||||
}
|
||||
|
||||
std::string GenerateRandomData(uint32_t len) {
|
||||
std::string data(len, 0);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
@@ -105,7 +115,7 @@ TEST_F(FileUtilsTest, CopyFile) {
|
||||
read_data.resize(file_system.FileSize(path_copy));
|
||||
size_t read_data_size = read_data.size();
|
||||
std::unique_ptr<File> rd_file =
|
||||
file_system.Open(path_copy, FileSystem::kReadOnly);
|
||||
file_system.Open(path_copy, FileSystem::kReadOnly);
|
||||
EXPECT_TRUE(rd_file);
|
||||
EXPECT_EQ(rd_file->Read(&read_data[0], read_data_size), read_data_size);
|
||||
EXPECT_EQ(write_data, read_data);
|
||||
@@ -148,4 +158,157 @@ TEST_F(FileUtilsTest, ListFiles) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveDirectory) {
|
||||
std::string path = test_vectors::kTestDir;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(path));
|
||||
EXPECT_TRUE(FileUtils::Exists(path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(path));
|
||||
EXPECT_FALSE(FileUtils::Exists(path));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveTopLevelDirectory) {
|
||||
std::string base_path = test_vectors::kTestDir;
|
||||
std::string sub_dir_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(sub_dir_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(sub_dir_path));
|
||||
|
||||
std::string file_path_1 = base_path + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = sub_dir_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(base_path));
|
||||
|
||||
EXPECT_FALSE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(sub_dir_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingWildcard) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path = base_path + kDirectoryDelimiter + kWildcard;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingWildcardAndExtension) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path =
|
||||
base_path + kDirectoryDelimiter + kWildcard + kTestFileNameExt;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingPrefixAndWildcard) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path =
|
||||
base_path + kDirectoryDelimiter + kTestFileNamePrefix + kWildcard;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingPrefixWildcardAndExtension) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path = base_path + kDirectoryDelimiter +
|
||||
kTestFileNamePrefix + kWildcard +
|
||||
kTestFileNameExt;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
@@ -18,6 +18,7 @@ bool IsCurrentOrParentDirectory(char* dir);
|
||||
class FileUtils {
|
||||
public:
|
||||
static bool Exists(const std::string& src);
|
||||
// The caller may only specifying a single wildcard
|
||||
static bool Remove(const std::string& src);
|
||||
static bool Copy(const std::string& src, const std::string& dest);
|
||||
static bool List(const std::string& path, std::vector<std::string>* files);
|
||||
|
||||
@@ -76,8 +76,11 @@ bool FileUtils::Remove(const std::string& path) {
|
||||
|
||||
DIR* dir;
|
||||
std::string dir_path = path.substr(0, delimiter_pos);
|
||||
std::string prepend =
|
||||
path.substr(delimiter_pos + 1, wildcard_pos - delimiter_pos - 1);
|
||||
if ((dir = opendir(dir_path.c_str())) == nullptr) {
|
||||
LOGW("File::Remove: directory open failed for wildcard");
|
||||
LOGW("File::Remove: directory open failed for wildcard: %d, %s", errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -88,7 +91,9 @@ bool FileUtils::Remove(const std::string& path) {
|
||||
size_t filename_len = strlen(entry->d_name);
|
||||
if (filename_len > ext.size()) {
|
||||
if (strcmp(entry->d_name + filename_len - ext.size(), ext.c_str()) ==
|
||||
0) {
|
||||
0 &&
|
||||
!IsCurrentOrParentDirectory(entry->d_name) &&
|
||||
strncmp(entry->d_name, prepend.c_str(), prepend.size()) == 0) {
|
||||
std::string file_path_to_remove =
|
||||
dir_path + kDirectoryDelimiter + entry->d_name;
|
||||
if (!Remove(file_path_to_remove)) {
|
||||
|
||||
Reference in New Issue
Block a user