2025-04-23 14:27:22 -04:00
|
|
|
// Copyright (C) 2021-2025 Internet Systems Consortium, Inc. ("ISC")
|
2024-03-04 11:48:36 +02:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
#ifndef KEA_UTIL_FILESYSTEM_H
|
|
|
|
#define KEA_UTIL_FILESYSTEM_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace isc {
|
|
|
|
namespace util {
|
|
|
|
namespace file {
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Get the content of a regular file.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @param file_name The file name.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return The content of the file.
|
|
|
|
/// @throw BadValue when the file can't be opened or is not a regular one.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string
|
|
|
|
getContent(const std::string& file_name);
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Check if there is a file or directory at the given path.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @param path The path being checked.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return True if the path points to a file or a directory, false otherwise.
|
2024-03-04 11:48:36 +02:00
|
|
|
bool
|
|
|
|
exists(const std::string& path);
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Check if there is a directory at the given path.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @param path The path being checked.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return True if the path points to a directory, false otherwise including
|
2024-03-04 11:48:36 +02:00
|
|
|
/// if the pointed location does not exist.
|
|
|
|
bool
|
|
|
|
isDir(const std::string& path);
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Check if there is a file at the given path.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @param path The path being checked.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return True if the path points to a file, false otherwise including
|
2024-03-04 11:48:36 +02:00
|
|
|
/// if the pointed location does not exist.
|
|
|
|
bool
|
|
|
|
isFile(const std::string& path);
|
|
|
|
|
2025-04-29 11:36:52 +02:00
|
|
|
/// @brief Check if there is a socket at the given path.
|
|
|
|
///
|
|
|
|
/// @param path The path being checked.
|
|
|
|
///
|
|
|
|
/// @return True if the path points to a socket, false otherwise including
|
|
|
|
/// if the pointed location does not exist.
|
2024-10-14 10:05:31 +03:00
|
|
|
bool
|
|
|
|
isSocket(const std::string& path);
|
|
|
|
|
2025-04-29 11:36:52 +02:00
|
|
|
/// @brief Set umask (at least 0027 i.e. no group write and no other access).
|
|
|
|
void
|
|
|
|
setUmask();
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Paths on a filesystem
|
2024-03-04 11:48:36 +02:00
|
|
|
struct Path {
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Constructor
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
|
|
|
/// Splits the full name into components.
|
|
|
|
Path(std::string const& path);
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Get the path in textual format.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
|
|
|
/// Counterpart for std::filesystem::path::string.
|
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return stored filename.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string str() const;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Get the parent path.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
|
|
|
/// Counterpart for std::filesystem::path::parent_path.
|
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return parent path of current path.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string parentPath() const;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Get the base name of the file without the extension.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
|
|
|
/// Counterpart for std::filesystem::path::stem.
|
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return the base name of current path without the extension.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string stem() const;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Get the extension of the file.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
|
|
|
/// Counterpart for std::filesystem::path::extension.
|
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return extension of current path.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string extension() const;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Get the name of the file, extension included.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2024-03-21 10:48:18 +02:00
|
|
|
/// Counterpart for std::filesystem::path::filename.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return name + extension of current path.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string filename() const;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Identifies the extension in {replacement}, trims it, and
|
2024-03-04 11:48:36 +02:00
|
|
|
/// replaces this instance's extension with it.
|
|
|
|
///
|
|
|
|
/// Counterpart for std::filesystem::path::replace_extension.
|
|
|
|
///
|
|
|
|
/// The change is done in the members and {this} is returned to allow call
|
|
|
|
/// chaining.
|
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @param replacement The extension to replace with.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return The current instance after the replacement was done.
|
2024-03-04 11:48:36 +02:00
|
|
|
Path& replaceExtension(std::string const& replacement = std::string());
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Trims {replacement} and replaces this instance's parent path with
|
2024-03-04 11:48:36 +02:00
|
|
|
/// it.
|
|
|
|
///
|
|
|
|
/// The change is done in the members and {this} is returned to allow call
|
|
|
|
/// chaining.
|
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @param replacement The parent path to replace with.
|
2024-03-04 11:48:36 +02:00
|
|
|
///
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @return The current instance after the replacement was done.
|
2024-03-04 11:48:36 +02:00
|
|
|
Path& replaceParentPath(std::string const& replacement = std::string());
|
|
|
|
|
|
|
|
private:
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Parent path.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string parent_path_;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief Stem.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string stem_;
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
/// @brief File name extension.
|
2024-03-04 11:48:36 +02:00
|
|
|
std::string extension_;
|
|
|
|
};
|
|
|
|
|
2024-10-14 10:05:31 +03:00
|
|
|
struct TemporaryDirectory {
|
|
|
|
TemporaryDirectory();
|
|
|
|
~TemporaryDirectory();
|
|
|
|
std::string dirName();
|
|
|
|
private:
|
|
|
|
std::string dir_name_;
|
|
|
|
};
|
|
|
|
|
2025-04-23 14:27:22 -04:00
|
|
|
/// @brief Class that provides basic file related tasks.
|
|
|
|
class FileManager {
|
|
|
|
public:
|
|
|
|
/// @brief Validates a file path against a supported path.
|
|
|
|
///
|
|
|
|
/// If the input path specifies a parent path and file name, the parent path
|
|
|
|
/// is validated against the supported path. If they match, the function returns
|
|
|
|
/// the validated path. If the input path contains only a file name the function
|
|
|
|
/// returns valid path using the supported path and the input path name.
|
|
|
|
///
|
|
|
|
/// @param supported_path_str absolute path specifying the supported path
|
|
|
|
/// of the file against which the input path is validated.
|
|
|
|
/// @param input_path_str file path to validate.
|
|
|
|
/// @param enforce_path enables validation against the supported path. If false
|
|
|
|
/// verifies only that the path contains a file name.
|
|
|
|
///
|
|
|
|
/// @return validated path as a string (supported path + input file name)
|
|
|
|
///
|
|
|
|
/// @throw BadValue if the input path does not include a file name or if the
|
|
|
|
/// it the parent path does not path the supported path.
|
|
|
|
static std::string validatePath(const std::string supported_path_str,
|
|
|
|
const std::string input_path_str,
|
|
|
|
bool enforce_path = true);
|
|
|
|
};
|
|
|
|
|
2024-03-04 11:48:36 +02:00
|
|
|
} // namespace file
|
|
|
|
} // namespace util
|
|
|
|
} // namespace isc
|
|
|
|
|
|
|
|
#endif // KEA_UTIL_FILESYSTEM_H
|