Remove deprecated VecStringToCharPtrArr

PiperOrigin-RevId: 563374332
Change-Id: I6873ca0d45fd5de6ad8eab1cbc395032130e9697
This commit is contained in:
Wiktor Garbacz 2023-09-07 03:08:44 -07:00 committed by Copybara-Service
parent b350a41a10
commit f614862e07
5 changed files with 23 additions and 52 deletions

View File

@ -19,7 +19,7 @@
#include "sandboxed_api/util/status_macros.h"
absl::Status CreateArchive(const char* initial_filename, int compress,
const char** argv, bool verbose) {
const std::vector<std::string>& argv, bool verbose) {
// We split the filename path into dirname and filename. To the filename we
// prepend "/output/"" so that it will work with the security policy.
std::string abs_path = MakeAbsolutePathAtCWD(std::string(initial_filename));
@ -28,9 +28,7 @@ absl::Status CreateArchive(const char* initial_filename, int compress,
std::string filename = sandbox2::file::JoinPath("/output/", filename_tmp);
std::vector<std::string> absolute_paths;
sandbox2::util::CharPtrArrToVecString(const_cast<char* const*>(argv),
&absolute_paths);
std::vector<std::string> absolute_paths = argv;
std::vector<std::string> relative_paths = absolute_paths;

View File

@ -101,7 +101,9 @@ int main(int unused_argc, char* argv[]) {
absl::Status status;
switch (mode) {
case 'c':
status = CreateArchive(filename, compress, argv, verbose);
status = CreateArchive(filename, compress,
sandbox2::util::CharArrPtr(argv).ToStringVector(),
verbose);
if (!status.ok()) {
LOG(ERROR) << "Archive creation failed with message: "
<< status.message();

View File

@ -23,7 +23,6 @@
namespace {
using ::sandbox2::util::VecStringToCharPtrArr;
using ::sapi::IsOk;
using ::sapi::file::JoinPath;
using ::sapi::file_util::fileops::Exists;
@ -164,8 +163,7 @@ std::string* MiniTarTest::init_wd_;
TEST_F(MiniTarTest, TestFileSimple) {
std::vector<std::string> v = {kFile1.data()};
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), 0, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -178,8 +176,7 @@ TEST_F(MiniTarTest, TestFileSimple) {
TEST_F(MiniTarTest, TestMultipleFiles) {
std::vector<std::string> v = {kFile1.data(), kFile2.data(), kFile3.data()};
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), 0, v, false), IsOk());
ASSERT_THAT(Exists(id_.data(), false), IsTrue())
<< "Archive file was not created";
@ -196,8 +193,7 @@ TEST_F(MiniTarTest, TestMultipleFiles) {
TEST_F(MiniTarTest, TestDirectorySimple) {
std::vector<std::string> v = {kDir2.data()};
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), 0, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -210,8 +206,7 @@ TEST_F(MiniTarTest, TestDirectorySimple) {
TEST_F(MiniTarTest, TestDirectoryNested) {
std::vector<std::string> v = {kDir1.data()};
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), 0, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -225,8 +220,7 @@ TEST_F(MiniTarTest, TestDirectoryNested) {
TEST_F(MiniTarTest, TestComplex) {
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), 0, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -242,9 +236,7 @@ TEST_F(MiniTarTest, TestComplex) {
TEST_F(MiniTarTest, TestCompress) {
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
int compress = 'Z';
ASSERT_THAT(
CreateArchive(id_.data(), compress, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), compress, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -259,9 +251,7 @@ TEST_F(MiniTarTest, TestCompress) {
TEST_F(MiniTarTest, TestGZIP) {
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
int compress = 'z';
ASSERT_THAT(
CreateArchive(id_.data(), compress, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), compress, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -276,9 +266,7 @@ TEST_F(MiniTarTest, TestGZIP) {
TEST_F(MiniTarTest, TestBZIP2) {
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
int compress = 'j';
ASSERT_THAT(
CreateArchive(id_.data(), compress, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), compress, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";
@ -294,8 +282,7 @@ TEST_F(MiniTarTest, TestPaths) {
// These should be equivalent to kFile1 and kDir1 after cleaning.
std::vector<std::string> v = {JoinPath("a/b/../../c/../", kFile1).data(),
JoinPath("d/../e/././///../", kDir1).data()};
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
IsOk());
ASSERT_THAT(CreateArchive(id_.data(), 0, v, false), IsOk());
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
<< "Could not chdir into test data directory";

View File

@ -96,20 +96,6 @@ void DumpCoverageData() {
#endif
}
void CharPtrArrToVecString(char* const* arr, std::vector<std::string>* vec) {
*vec = CharPtrArray(arr).ToStringVector();
}
const char** VecStringToCharPtrArr(const std::vector<std::string>& vec) {
const int vec_size = vec.size();
const char** arr = new const char*[vec_size + 1];
for (int i = 0; i < vec_size; ++i) {
arr[i] = vec[i].c_str();
}
arr[vec_size] = nullptr;
return arr;
}
CharPtrArray::CharPtrArray(char* const* arr) : content_(ConcatenateAll(arr)) {
for (auto it = content_.begin(); it != content_.end();
it += strlen(&*it) + 1) {

View File

@ -25,23 +25,13 @@
#include <vector>
#include "absl/base/attributes.h"
#include "absl/base/macros.h"
#include "absl/status/statusor.h"
namespace sandbox2::util {
void DumpCoverageData();
// Converts an array of char* (terminated by a nullptr, like argv, or environ
// arrays), to an std::vector<std::string>.
ABSL_DEPRECATED("Use CharPtrArray(arr).ToStringVector() instead")
void CharPtrArrToVecString(char* const* arr, std::vector<std::string>* vec);
// Converts a vector of strings to a newly allocated array. The array is limited
// by the terminating nullptr entry (like environ or argv). It must be freed by
// the caller.
ABSL_DEPRECATED("Use CharPtrArray class instead")
const char** VecStringToCharPtrArr(const std::vector<std::string>& vec);
// An char ptr array limited by the terminating nullptr entry (like environ
// or argv).
class CharPtrArray {
@ -62,6 +52,14 @@ class CharPtrArray {
std::vector<const char*> array_;
};
// Converts an array of char* (terminated by a nullptr, like argv, or environ
// arrays), to an std::vector<std::string>.
ABSL_DEPRECATED("Use CharPtrArray(arr).ToStringVector() instead")
inline void CharPtrArrToVecString(char* const* arr,
std::vector<std::string>* vec) {
*vec = sandbox2::util::CharPtrArray(arr).ToStringVector();
}
// Returns the program name (via /proc/self/comm) for a given PID.
std::string GetProgName(pid_t pid);