-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ResolvePossibleSymlinkToHostPath to common directory
Signed-off-by: Dom Del Nano <[email protected]>
- Loading branch information
Showing
3 changed files
with
124 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2018- The Pixie Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "src/common/system/linux_headers_utils.h" | ||
|
||
#include <fstream> | ||
#include <limits> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include "src/common/base/file.h" | ||
#include "src/common/fs/fs_wrapper.h" | ||
#include "src/common/system/config.h" | ||
|
||
namespace px { | ||
namespace system { | ||
|
||
StatusOr<std::filesystem::path> ResolvePossibleSymlinkToHostPath(const std::filesystem::path p) { | ||
// Check if "p" is a symlink. | ||
std::error_code ec; | ||
const bool is_symlink = std::filesystem::is_symlink(p, ec); | ||
if (ec) { | ||
return error::NotFound(absl::Substitute("Did not find the host headers at path: $0, $1.", | ||
p.string(), ec.message())); | ||
} | ||
|
||
if (!is_symlink) { | ||
// Not a symlink, we are good now. | ||
return p; | ||
} | ||
|
||
// Resolve the symlink, and re-convert to a host path.. | ||
const std::filesystem::path resolved = std::filesystem::read_symlink(p, ec); | ||
if (ec) { | ||
return error::Internal(ec.message()); | ||
} | ||
|
||
// Relative paths containing "../" can result in an invalid host mount path when using | ||
// ToHostPath. Therefore, we need to treat the absolute and relative cases differently. | ||
std::filesystem::path resolved_host_path; | ||
if (resolved.is_absolute()) { | ||
resolved_host_path = system::Config::GetInstance().ToHostPath(resolved); | ||
VLOG(1) << absl::Substitute( | ||
"Symlink target is an absolute path. Converting that to host path: $0 -> $1.", | ||
resolved.string(), resolved_host_path.string()); | ||
} else { | ||
resolved_host_path = p.parent_path(); | ||
resolved_host_path /= resolved.string(); | ||
VLOG(1) << absl::Substitute( | ||
"Symlink target is a relative path. Concatenating it to parent directory: $0", | ||
resolved_host_path.string()); | ||
} | ||
|
||
// Downstream won't be ok unless the resolved host path exists; return an error if needed. | ||
if (!fs::Exists(resolved_host_path)) { | ||
return error::NotFound(absl::Substitute("Did not find host headers at resolved path: $0.", | ||
resolved_host_path.string())); | ||
} | ||
return resolved_host_path; | ||
} | ||
|
||
} // namespace system | ||
} // namespace px |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2018- The Pixie Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <filesystem> | ||
|
||
#include "src/common/base/base.h" | ||
|
||
namespace px { | ||
namespace system { | ||
|
||
/** | ||
* Resolves a possible symlink path to its corresponding host filesystem path. | ||
* | ||
* This function takes a filesystem path and checks if it is a symbolic link. If it is, | ||
* the symlink is resolved to its target path. Depending on whether the target is an absolute | ||
* or relative path, it is further processed to convert it into a valid host path (as in | ||
* Config::ToHostPath(...) path). | ||
* | ||
* If the input path is not a symlink, it is returned as-is. The function ensures that | ||
* the final resolved path exists in the host filesystem before returning it. Errors are | ||
* returned when the path does not exist, the resolution fails, or when there is an issue | ||
* accessing the filesystem. | ||
*/ | ||
StatusOr<std::filesystem::path> ResolvePossibleSymlinkToHostPath(const std::filesystem::path p); | ||
|
||
} // namespace system | ||
} // namespace px |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters