libdnf5::utils::fs
-
class File
- #include <file.hpp>
A wrapper for a
FILE *that handles opening and closing a file in RAII fashion. Errors are handled by raising instances oflibdnf5::FileSystemErrorso that there’s a single exception type being raised for all filesystem-related errors.Public Functions
-
File(const std::filesystem::path &path, const char *mode, bool use_solv_xfopen = false)
Creates an instance of
Fileand opens the file atpathusing modemode.- Parameters:
path – The path of the file.
mode – The mode for opening the file.
use_solv_xfopen – Use libsolv’s solv_xfopen to transparently work with compressed files (based on filename extension).
-
File(int fd, const std::filesystem::path &path, const char *mode, bool use_solv_xfopen_fd = false)
Creates an instance of
Fileby opening file descriptorfd. Thepathargument is only used for error reporting and for retrieving from the object viaget_path().- Parameters:
fd – The file descriptor to call fdopen() on.
path – The path of the file, for error reporting etc.
mode – The mode for opening the file.
use_solv_xfopen_fd – Use libsolv’s solv_xfopen_fd to transparently work with compressed files (based on filename extension).
-
~File()
-
void open(const std::filesystem::path &path, const char *mode, bool use_solv_xfopen = false)
Opens the file at
pathusing modemode. If this object already has an open file, closes it first before opening the new one.- Parameters:
path – The path of the file.
mode – The mode for opening the file.
use_solv_xfopen – Use libsolv’s solv_xfopen to transparently work with compressed files (based on filename extension).
-
void open(int fd, const std::filesystem::path &path, const char *mode, bool use_solv_xfopen_fd = false)
Opens the file by opening file descriptor
fd(viafdopen()). Thepathargument is only used for error reporting and for retrieving from the object viaget_path().- Parameters:
fd – The file descriptor to call
fdopen()on.path – The path of the file, for error reporting etc.
mode – The mode for opening the file.
use_solv_xfopen_fd – Use libsolv’s solv_xfopen_fd to transparently work with compressed files (based on filename extension).
-
void close()
Close the file.
-
std::FILE *release() noexcept
Releases the file, meaning it will no longer be closed on destruction.
-
std::size_t read(void *buffer, std::size_t count)
Reads at most
countchars intobuffer. If EOF is reached, returns a number smaller thancount.- Parameters:
buffer – The data buffer to read into.
count – The size of
buffer.
- Returns:
The number of chars read.
-
void write(const void *buffer, std::size_t count)
Writes
countchars from `buffer.- Parameters:
buffer – The data buffer to write.
count – The number of chars to write from
buffer.
-
bool getc(char &c)
Reads a single char from the file.
- Parameters:
c – The char variable to read to.
- Returns:
trueif char was read,falsein case EOF was reached.
-
void putc(char c)
Writes a single char to the file.
- Parameters:
c – The char to write.
-
void flush()
Flushes the data from the internal FILE stream buffer.
-
void seek(long offset, int whence)
Seeks to
offsetrelative towhence. The values forwhenceareSEEK_SET,SEEK_CURandSEEK_END, directly from the<cstdio>header.- Parameters:
offset – The offset to seek to.
whence – The relative position to seek from.
-
long tell() const
- Returns:
The current position indicator value of the FILE stream.
-
void rewind()
Rewinds the FILE stream to the beginning.
-
bool is_at_eof() const
- Returns:
Whether the current position indicator is at the end of file (EOF).
-
std::string read(std::size_t count = 0)
Reads the contents of the file from current position to the end or until
countchars are read.It will try to detect the number of characters in the file until the end. If the detection is successful, the required memory is allocated at once. Otherwise, the fallback solution reads the file block by block and reallocates memory.
- Parameters:
count – The maximum number of characters to read, 0 to read till the end.
- Returns:
The contents read from the file.
-
bool read_line(std::string &line)
Reads a single line of the file.
- Parameters:
line – The string variable to read the line into.
- Returns:
trueif line was read,falsein case EOF was reached.
-
void write(std::string_view data)
Writes
datainto the file.- Parameters:
data – The data to write.
-
explicit operator bool() const noexcept
- Returns:
Whether this object contains an open file.
-
const std::filesystem::path &get_path() const noexcept
Returns the associated file path.
- Returns:
The associated file path.
-
std::FILE *get() const noexcept
Returns the associated open stream or nullptr.
- Returns:
The associated open stream or nullptr.
-
int get_fd() const
Returns the associated open file descriptor. The operation requires an open file. Throws a
libdnf5::FileSystemErrorexception if an error occurs.- Returns:
The associated open file descriptor.
-
File(const std::filesystem::path &path, const char *mode, bool use_solv_xfopen = false)
-
class TempDir
- #include <temp.hpp>
Object that creates and holds a temp directory. The directory gets removed when the object is deleted.
Public Functions
-
explicit TempDir(const std::string &name_prefix)
Creates a temporary directory in the system temporary directory path.
-
TempDir(std::filesystem::path destdir, const std::string &name_prefix)
Creates a temporary directory in
destdir.
-
~TempDir()
-
void release() noexcept
Releases the temporary directory, meaning it will no longer be deleted on destruction.
-
inline const std::filesystem::path &get_path() const noexcept
-
explicit TempDir(const std::string &name_prefix)
-
class TempFile
- #include <temp.hpp>
A mkstemp wrapper that creates and owns a temporary file, which will be deleted in the destructor unless released. Throws instances of
libdnf5::FileSystemErroron any I/O failure.Public Functions
-
explicit TempFile(const std::string &name_prefix)
Creates a temporary file in the system temporary directory path.
- Parameters:
name_prefix – The prefix of the filename to which “.XXXXXX” will be appended.
-
TempFile(std::filesystem::path destdir, const std::string &name_prefix)
Creates a temporary file in
destdir.- Parameters:
destdir – The directory in which the file will be created.
name_prefix – The prefix of the filename to which “.XXXXXX” will be appended.
-
~TempFile()
-
File &open_as_file(const char *mode)
Open the TempFile as a File object.
- Parameters:
mode – The mode for the file, passed to
::fdopen().
-
void close()
If this TempFile has been opened as File (via
open_as_file()), unsets and destroys that File (automatically closing upon destruction). Otherwise closes the open file descriptor.
-
void release() noexcept
Releases the temporary file, meaning it will no longer be closed or deleted on destruction. If open as File (via
open_as_file()), releases the File by calling itsrelease()method and unsets and destroys the File.
-
inline const std::filesystem::path &get_path() const noexcept
-
inline int get_fd() const noexcept
-
explicit TempFile(const std::string &name_prefix)