Commit 250d12a4 authored by Joel Hockey's avatar Joel Hockey Committed by Commit Bot

Add better error checking in ConvertFileSystemURLToPathInsideCrostini

Change method API to take resulting FilePath as input pointer, and
return bool to indicate success or otherwise since it is not
guaranteed that input will always convert.

Bug: 878324
Change-Id: I17f99201c2c4b5105cceb48d6a3c0d57b80c5f7b
Reviewed-on: https://chromium-review.googlesource.com/c/1295634
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601903}
parent 379cc533
...@@ -759,13 +759,16 @@ FileManagerPrivateInternalGetLinuxPackageInfoFunction::Run() { ...@@ -759,13 +759,16 @@ FileManagerPrivateInternalGetLinuxPackageInfoFunction::Run() {
file_manager::util::GetFileSystemContextForRenderFrameHost( file_manager::util::GetFileSystemContextForRenderFrameHost(
profile, render_frame_host()); profile, render_frame_host());
std::string url = base::FilePath path;
file_manager::util::ConvertFileSystemURLToPathInsideCrostini( if (!file_manager::util::ConvertFileSystemURLToPathInsideCrostini(
profile, file_system_context->CrackURL(GURL(params->url))); profile, file_system_context->CrackURL(GURL(params->url)), &path)) {
return RespondNow(Error("Invalid url: " + params->url));
}
crostini::CrostiniPackageInstallerService::GetForProfile(profile) crostini::CrostiniPackageInstallerService::GetForProfile(profile)
->GetLinuxPackageInfo( ->GetLinuxPackageInfo(
crostini::kCrostiniDefaultVmName, crostini::kCrostiniDefaultVmName,
crostini::kCrostiniDefaultContainerName, url, crostini::kCrostiniDefaultContainerName, path.value(),
base::BindOnce( base::BindOnce(
&FileManagerPrivateInternalGetLinuxPackageInfoFunction:: &FileManagerPrivateInternalGetLinuxPackageInfoFunction::
OnGetLinuxPackageInfo, OnGetLinuxPackageInfo,
...@@ -804,13 +807,16 @@ FileManagerPrivateInternalInstallLinuxPackageFunction::Run() { ...@@ -804,13 +807,16 @@ FileManagerPrivateInternalInstallLinuxPackageFunction::Run() {
file_manager::util::GetFileSystemContextForRenderFrameHost( file_manager::util::GetFileSystemContextForRenderFrameHost(
profile, render_frame_host()); profile, render_frame_host());
std::string url = base::FilePath path;
file_manager::util::ConvertFileSystemURLToPathInsideCrostini( if (!file_manager::util::ConvertFileSystemURLToPathInsideCrostini(
profile, file_system_context->CrackURL(GURL(params->url))); profile, file_system_context->CrackURL(GURL(params->url)), &path)) {
return RespondNow(Error("Invalid url: " + params->url));
}
crostini::CrostiniPackageInstallerService::GetForProfile(profile) crostini::CrostiniPackageInstallerService::GetForProfile(profile)
->InstallLinuxPackage( ->InstallLinuxPackage(
crostini::kCrostiniDefaultVmName, crostini::kCrostiniDefaultVmName,
crostini::kCrostiniDefaultContainerName, url, crostini::kCrostiniDefaultContainerName, path.value(),
base::BindOnce( base::BindOnce(
&FileManagerPrivateInternalInstallLinuxPackageFunction:: &FileManagerPrivateInternalInstallLinuxPackageFunction::
OnInstallLinuxPackage, OnInstallLinuxPackage,
......
...@@ -153,8 +153,13 @@ void ExecuteCrostiniTask( ...@@ -153,8 +153,13 @@ void ExecuteCrostiniTask(
std::vector<std::string> files; std::vector<std::string> files;
for (const storage::FileSystemURL& file_system_url : file_system_urls) { for (const storage::FileSystemURL& file_system_url : file_system_urls) {
files.emplace_back(util::ConvertFileSystemURLToPathInsideCrostini( base::FilePath file;
profile, file_system_url)); if (!util::ConvertFileSystemURLToPathInsideCrostini(
profile, file_system_url, &file)) {
LOG(ERROR) << "Invalid file: " << file_system_url.DebugString();
return;
}
files.emplace_back(file.value());
} }
crostini::LaunchCrostiniApp(profile, task.app_id, display::kInvalidDisplayId, crostini::LaunchCrostiniApp(profile, task.app_id, display::kInvalidDisplayId,
......
...@@ -181,37 +181,31 @@ std::vector<std::string> GetCrostiniMountOptions( ...@@ -181,37 +181,31 @@ std::vector<std::string> GetCrostiniMountOptions(
return options; return options;
} }
std::string ConvertFileSystemURLToPathInsideCrostini( bool ConvertFileSystemURLToPathInsideCrostini(
Profile* profile, Profile* profile,
const storage::FileSystemURL& file_system_url) { const storage::FileSystemURL& file_system_url,
std::string id(file_system_url.mount_filesystem_id()); base::FilePath* inside) {
const std::string& id(file_system_url.mount_filesystem_id());
std::string mount_point_name_crostini = GetCrostiniMountPointName(profile); std::string mount_point_name_crostini = GetCrostiniMountPointName(profile);
std::string mount_point_name_downloads = GetDownloadsMountPointName(profile); std::string mount_point_name_downloads = GetDownloadsMountPointName(profile);
DCHECK(file_system_url.mount_type() == storage::kFileSystemTypeExternal);
DCHECK(file_system_url.type() == storage::kFileSystemTypeNativeLocal);
DCHECK(id == mount_point_name_crostini || id == mount_point_name_downloads);
// Reformat virtual_path() from: // Reformat virtual_path() from:
// <mount_label>/path/to/file // <mount_label>/path/to/file
// To either: // To either:
// /<home-directory>/path/to/file (path is already in crostini volume) // /<home-directory>/path/to/file (path is already in crostini volume)
// /ChromeOS/<volume_id>/path/to/file (path is shared with crostini) // /ChromeOS/<volume_id>/path/to/file (path is shared with crostini)
base::FilePath result;
base::FilePath folder; base::FilePath folder;
if (id == mount_point_name_crostini) { if (id == mount_point_name_crostini) {
folder = base::FilePath(mount_point_name_crostini); folder = base::FilePath(mount_point_name_crostini);
result = crostini::ContainerHomeDirectoryForProfile(profile); *inside = crostini::ContainerHomeDirectoryForProfile(profile);
} else if (id == mount_point_name_downloads) { } else if (id == mount_point_name_downloads) {
folder = base::FilePath(mount_point_name_downloads); folder = base::FilePath(mount_point_name_downloads);
result = *inside =
crostini::ContainerChromeOSBaseDirectory().Append(kDownloadsFolderName); crostini::ContainerChromeOSBaseDirectory().Append(kDownloadsFolderName);
} else { } else {
NOTREACHED(); return false;
} }
bool success = return folder.AppendRelativePath(file_system_url.virtual_path(), inside);
folder.AppendRelativePath(file_system_url.virtual_path(), &result);
DCHECK(success);
return result.AsUTF8Unsafe();
} }
bool ConvertPathToArcUrl(const base::FilePath& path, GURL* arc_url_out) { bool ConvertPathToArcUrl(const base::FilePath& path, GURL* arc_url_out) {
......
...@@ -63,9 +63,10 @@ std::vector<std::string> GetCrostiniMountOptions( ...@@ -63,9 +63,10 @@ std::vector<std::string> GetCrostiniMountOptions(
const std::string& container_public_key); const std::string& container_public_key);
// Convert a cracked url to a path inside the Crostini VM. // Convert a cracked url to a path inside the Crostini VM.
std::string ConvertFileSystemURLToPathInsideCrostini( bool ConvertFileSystemURLToPathInsideCrostini(
Profile* profile, Profile* profile,
const storage::FileSystemURL& file_system_url); const storage::FileSystemURL& file_system_url,
base::FilePath* inside);
// DEPRECATED. Use |ConvertToContentUrls| instead. // DEPRECATED. Use |ConvertToContentUrls| instead.
// While this function can convert paths under Downloads, /media/removable // While this function can convert paths under Downloads, /media/removable
......
...@@ -175,16 +175,27 @@ TEST(FileManagerPathUtilTest, ConvertFileSystemURLToPathInsideCrostini) { ...@@ -175,16 +175,27 @@ TEST(FileManagerPathUtilTest, ConvertFileSystemURLToPathInsideCrostini) {
GetDownloadsMountPointName(&profile), storage::kFileSystemTypeNativeLocal, GetDownloadsMountPointName(&profile), storage::kFileSystemTypeNativeLocal,
storage::FileSystemMountOption(), GetDownloadsFolderForProfile(&profile)); storage::FileSystemMountOption(), GetDownloadsFolderForProfile(&profile));
EXPECT_EQ("/home/testing_profile/path/in/crostini", base::FilePath inside;
ConvertFileSystemURLToPathInsideCrostini( EXPECT_TRUE(ConvertFileSystemURLToPathInsideCrostini(
&profile, mount_points->CreateExternalFileSystemURL( &profile,
GURL(), "crostini_test_termina_penguin", mount_points->CreateExternalFileSystemURL(
base::FilePath("path/in/crostini")))); GURL(), "crostini_test_termina_penguin",
EXPECT_EQ("/ChromeOS/Downloads/path/in/downloads", base::FilePath("path/in/crostini")),
ConvertFileSystemURLToPathInsideCrostini( &inside));
&profile, EXPECT_EQ("/home/testing_profile/path/in/crostini", inside.value());
mount_points->CreateExternalFileSystemURL(
GURL(), "Downloads", base::FilePath("path/in/downloads")))); EXPECT_TRUE(ConvertFileSystemURLToPathInsideCrostini(
&profile,
mount_points->CreateExternalFileSystemURL(
GURL(), "Downloads", base::FilePath("path/in/downloads")),
&inside));
EXPECT_EQ("/ChromeOS/Downloads/path/in/downloads", inside.value());
EXPECT_FALSE(ConvertFileSystemURLToPathInsideCrostini(
&profile,
mount_points->CreateExternalFileSystemURL(
GURL(), "unknown", base::FilePath("path/in/unknown")),
&inside));
} }
std::unique_ptr<KeyedService> CreateFileSystemOperationRunnerForTesting( std::unique_ptr<KeyedService> CreateFileSystemOperationRunnerForTesting(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment