#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_PLATFORM_PLATFORM_LINUXFILEAPI_H #define NUCLEX_PLATFORM_PLATFORM_LINUXFILEAPI_H #include "Nuclex/Platform/Config.h" #if defined(NUCLEX_PLATFORM_LINUX) #include // std::string #include // std::uint8_t and std::size_t #include // for std::vector #include // ::fstat() and permission flags #include // struct ::dirent namespace Nuclex { namespace Platform { namespace Platform { // ------------------------------------------------------------------------------------------- // /// Wraps the Linux file system API /// /// /// This is just a small helper class that reduces the amount of boilerplate code /// required when calling the file system API functions, such as checking /// result codes over and over again. /// /// /// It is not intended to hide operating system details or make this API platform /// neutral (the File and Container classes do that), so depending on the amount /// of noise required by the file system APIs, only some methods will be wrapped here. /// /// class LinuxFileApi { /// Opens the specified file for shared reading /// Path of the file that will be opened /// The descriptor (numeric handle) of the opened file public: static int OpenFileForReading(const std::string &path); /// Creates or opens the specified file for exclusive writing /// Path of the file that will be opened /// The descriptor (numeric handle) of the opened file public: static int OpenFileForWriting(const std::string &path); /// Changes the position of the file cursor /// File handle whose file cursor will be moved /// Relative position, in bytes, to move the file cursor to /// Position to which the offset is relative /// The absolute position in bytes from the beginning of the file public: static std::size_t Seek(int fileDescriptor, ::off_t offset, int anchor); /// Reads data from the specified file /// Handle of the file from which data will be read /// Buffer into which the data will be put /// Number of bytes that will be read from the file /// The number of bytes that were actually read public: static std::size_t Read( int fileDescriptor, std::uint8_t *buffer, std::size_t count ); /// Writes data into the specified file /// Handle of the file into which data will be written /// Buffer containing the data that will be written /// Number of bytes that will be written into the file /// The number of bytes that were actually written public: static std::size_t Write( int fileDescriptor, const std::uint8_t *buffer, std::size_t count ); /// Truncates or pads the file to the specified length /// Handle of the file whose length will be set /// New length fo the file in bytes public: static void SetLength(int fileDescriptor, std::size_t byteCount); /// Flushes all buffered output to the hard drive /// /// File descriptor whose buffered output will be flushed /// public: static void Flush(int fileDescriptor); /// Closes the specified file /// Handle of the file that will be closed /// /// Whether to throw an exception if the file cannot be closed /// public: static void Close(int fileDescriptor, bool throwOnError = true); /// Reads the target file or directory pointed to by a symlink /// Path to the symlink whose target will be read /// String that will receive the link target path /// /// Will be filled with the errno value in case the method returns false /// /// /// True if the path was written into the target string, false if the link /// didn't exist or couldn't be accessed (permissions). Any other problem will /// still result in an exception being thrown. /// public: static bool TryReadLink( const std::string &path, std::string &target, int *causingErrorNumber = nullptr ); /// Loads a whole file into memory and returns its contents an std::vector /// Absolute path of the file that will be loaded /// A vector with the full file contents public: static std::vector ReadFileIntoMemory(const std::string &path); /// /// Attempts to read an entire file into a string with a single read() call /// /// Path of the file that will be read /// String that will receive the contents of the file /// True if the read suceeded, false otherwise /// /// If the read fails, the contents and length of the output string are undefined. /// This method makes an effort to read the whole file specified in one go into /// the provided output string. This is useful to minimize the chance of mixed-up /// file data when a file might be modified during the read. It's only used for /// the XDG settings file, a small text file. /// public: static bool TryReadFileInOneReadCall( const std::string &path, std::string &contents ) noexcept; /// Joins two paths together, inserted a forward slash when needed /// Base path, typically an absolute path or directory name /// Sub path, typically a filename with or without subdirectory /// The joined path public: static std::string JoinPaths(const std::string &base, const std::string &sub); }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Platform::Platform #endif // defined(NUCLEX_PLATFORM_LINUX) #endif // NUCLEX_PLATFORM_PLATFORM_LINUXFILEAPI_H