15namespace fs = std::filesystem;
18 fs::path ToFsPath(
const DotNetDupe::System::String& sPath) {
33 return fs::exists(ToFsPath(sPath));
38 std::ifstream fileStream(ToFsPath(sPath), std::ios::binary);
39 if (!fileStream.is_open()) {
44 std::stringstream buffer;
45 buffer << fileStream.rdbuf();
46 std::string charContent = buffer.str();
49 for (
int iIdx = 0; iIdx < (int)charContent.size(); iIdx++) bytes[iIdx] = charContent[iIdx];
56 std::ofstream fileStream(ToFsPath(sPath), std::ios::binary);
57 if (!fileStream.is_open()) {
64 fs::copy_options options = fs::copy_options::none;
65 if (bOverwrite) options |= fs::copy_options::overwrite_existing;
68 fs::copy(ToFsPath(sSourceFileName), ToFsPath(sDestFileName), options);
69 }
catch (
const std::exception& ex) {
76 fs::rename(ToFsPath(sSourceFileName), ToFsPath(sDestFileName));
77 }
catch (
const std::exception& ex) {
84 fs::remove(ToFsPath(sPath));
85 }
catch (
const std::exception& ex) {
92 std::ofstream fileStream(ToFsPath(sPath), std::ios::binary | std::ios_base::app);
93 if (!fileStream.is_open()) {
100 std::ofstream fileStream(ToFsPath(sPath), std::ios::binary | std::ios_base::app);
101 if (!fileStream.is_open()) {
104 for (
int iIdx = 0; iIdx < sContents.
GetLength(); iIdx++) {
105 const auto& line = sContents[iIdx];
113 std::vector<String> tempLines;
114 std::ifstream fileStream(ToFsPath(sPath), std::ios::binary);
115 if (!fileStream.is_open()) {
119 while (std::getline(fileStream, line)) {
121 for (
int iIdx = 0; iIdx < (int)line.size(); iIdx++) lineBytes[iIdx] = line[iIdx];
126 for (
int iIdx = 0; iIdx < (int)tempLines.size(); iIdx++) lines[iIdx] = tempLines[iIdx];
131 std::ofstream fileStream(ToFsPath(sPath), std::ios::binary);
132 if (!fileStream.is_open()) {
135 for (
int iIdx = 0; iIdx < sContents.
GetLength(); iIdx++) {
136 const auto& line = sContents[iIdx];
144 std::ofstream fileStream(ToFsPath(sPath));
145 if (!fileStream.is_open()) {
151 static int ComputeLinuxFileAttributes(
const fs::path& pPath,
const fs::file_status& statusInfo) {
153 if ((statusInfo.permissions() & fs::perms::owner_write) == fs::perms::none) iAttrs |=
static_cast<int>(
FileAttributes::ReadOnly);
155 if (!pPath.filename().empty() && pPath.filename().string()[0] ==
'.') iAttrs |=
static_cast<int>(
FileAttributes::Hidden);
159 static fs::perms ComputeLinuxFilePermissions(
FileAttributes eAttributes, fs::perms permsCurrent) {
161 return permsCurrent & ~(fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write);
163 return permsCurrent | fs::perms::owner_write;
170 DWORD iAttrs = GetFileAttributesW(sWPath.c_str());
171 if (iAttrs == INVALID_FILE_ATTRIBUTES)
return false;
175 fs::path pPath = ToFsPath(sPath);
177 auto statusInfo = fs::status(pPath, ec);
178 if (ec)
return false;
179 eAttributes =
static_cast<FileAttributes>(ComputeLinuxFileAttributes(pPath, statusInfo));
187 return ::SetFileAttributesW(sWPath.c_str(),
static_cast<DWORD
>(eAttributes)) != FALSE;
189 fs::path pPath = ToFsPath(sPath);
191 auto statusInfo = fs::status(pPath, ec);
192 if (ec)
return false;
193 fs::permissions(pPath, ComputeLinuxFilePermissions(eAttributes, statusInfo.permissions()), fs::perm_options::replace, ec);
Provides static methods for the creation, copying, deletion, moving, and opening of a single file.
The exception that is thrown when an I/O error occurs.
Represents character encodings and code page conversions.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
T * GetData()
Gets a pointer to the contiguous internal element buffer.
static bool GetAttributes(const String &sPath, FileAttributes &attrAttributes)
static void Copy(const String &sSourceFileName, const String &sDestFileName, bool bOverwrite)
Copies an existing file to a new file, allowing overwriting of an existing file.
static bool SetAttributes(const String &sPath, FileAttributes attrAttributes)
static void WriteAllText(const String &sPath, const String &sContents)
Creates a new file, writes the specified string to the file, and then closes the file.
static bool Exists(const String &sPath)
Determines whether the specified file exists.
static void AppendAllText(const String &sPath, const String &sContents)
static String ReadAllText(const String &sPath)
Opens a text file, reads all the text in the file, and then closes the file.
static void AppendAllLines(const String &sPath, const Array< String > &sContents)
static void WriteAllLines(const String &sPath, const Array< String > &sContents)
static void Delete(const String &sPath)
Deletes the specified file.
static void Create(const String &sPath)
static void Move(const String &sSourceFileName, const String &sDestFileName)
Moves a specified file to a new location.
static Array< String > ReadAllLines(const String &sPath)
The exception that is thrown when an I/O error occurs.
static std::wstring Utf8ToWChar(const char *pUtf8Str)
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
const char * GetRawString() const
static EncodingPtr UTF8()
Gets an encoding for the UTF-8 format.
FileAttributes
Provides attributes for files and directories.
std::wstring Utf8ToWChar(const char *pUtf8Str)