| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2005 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This program is free software; you can redistribute it and/or modify * |
| 9 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 10 |
* as published by the Free Software Foundation; either version 2 of * |
| 11 |
* the License, or (at your option) any later version. * |
| 12 |
* for detail see the LICENCE text file. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 17 |
* GNU Library General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Library General Public * |
| 20 |
* License along with FreeCAD; if not, write to the Free Software * |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 22 |
* USA * |
| 23 |
* * |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
#include <FCConfig.h> |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <codecvt> |
| 30 |
#include <cstring> |
| 31 |
#include <fstream> |
| 32 |
#include <iostream> |
| 33 |
#include <system_error> |
| 34 |
#ifdef FC_OS_WIN32 |
| 35 |
# include <Windows.h> |
| 36 |
#else |
| 37 |
# include <unistd.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "FileInfo.h" |
| 41 |
#include "Exception.h" |
| 42 |
#include "TimeInfo.h" |
| 43 |
|
| 44 |
using namespace Base; |
| 45 |
namespace fs = std::filesystem; |
| 46 |
|
| 47 |
//********************************************************************************** |
| 48 |
// helper |
| 49 |
|
| 50 |
#ifdef FC_OS_WIN32 |
| 51 |
std::string ConvertFromWideString(const std::wstring& string) |
| 52 |
{ |
| 53 |
int neededSize = WideCharToMultiByte(CP_UTF8, 0, string.c_str(), -1, 0, 0, 0, 0); |
| 54 |
char* CharString = new char[static_cast<size_t>(neededSize)]; |
| 55 |
WideCharToMultiByte(CP_UTF8, 0, string.c_str(), -1, CharString, neededSize, 0, 0); |
| 56 |
std::string String(CharString); |
| 57 |
delete[] CharString; |
| 58 |
CharString = NULL; |
| 59 |
return String; |
| 60 |
} |
| 61 |
|
| 62 |
std::wstring ConvertToWideString(const std::string& string) |
| 63 |
{ |
| 64 |
int neededSize = MultiByteToWideChar(CP_UTF8, 0, string.c_str(), -1, 0, 0); |
| 65 |
wchar_t* wideCharString = new wchar_t[static_cast<size_t>(neededSize)]; |
| 66 |
MultiByteToWideChar(CP_UTF8, 0, string.c_str(), -1, wideCharString, neededSize); |
| 67 |
std::wstring wideString(wideCharString); |
| 68 |
delete[] wideCharString; |
| 69 |
wideCharString = NULL; |
| 70 |
return wideString; |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
//********************************************************************************** |
| 76 |
// FileInfo |
| 77 |
|
| 78 |
|
| 79 |
FileInfo::FileInfo(const char* fileName) |
| 80 |
{ |
| 81 |
setFile(fileName); |
| 82 |
} |
| 83 |
|
| 84 |
FileInfo::FileInfo(const std::string& fileName) |
| 85 |
{ |
| 86 |
setFile(fileName.c_str()); |
| 87 |
} |
| 88 |
|
| 89 |
const std::string& FileInfo::getTempPath() |
| 90 |
{ |
| 91 |
static std::string tempPath; |
| 92 |
|
| 93 |
if (tempPath.empty()) { |
| 94 |
fs::path tmp = fs::temp_directory_path(); |
| 95 |
tmp += fs::path::preferred_separator; |
| 96 |
tempPath = pathToString(tmp); |
| 97 |
} |
| 98 |
|
| 99 |
return tempPath; |
| 100 |
} |
| 101 |
|
| 102 |
std::string FileInfo::getTempFileName(const char* FileName, const char* Path) |
| 103 |
{ |
| 104 |
// FIXME: To avoid race conditions we should rather return a file pointer |
| 105 |
// than a file name. |
| 106 |
#ifdef FC_OS_WIN32 |
| 107 |
wchar_t buf[MAX_PATH + 2]; |
| 108 |
|
| 109 |
// Path where the file is located |
| 110 |
std::wstring path; |
| 111 |
if (Path) { |
| 112 |
path = ConvertToWideString(std::string(Path)); |
| 113 |
} |
| 114 |
else { |
| 115 |
path = ConvertToWideString(getTempPath()); |
| 116 |
} |
| 117 |
|
| 118 |
// File name in the path |
| 119 |
std::wstring file; |
| 120 |
if (FileName) { |
| 121 |
file = ConvertToWideString(std::string(FileName)); |
| 122 |
} |
| 123 |
else { |
| 124 |
file = L"FCTempFile"; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
// this already creates the file |
| 129 |
GetTempFileNameW(path.c_str(), file.c_str(), 0, buf); |
| 130 |
DeleteFileW(buf); |
| 131 |
|
| 132 |
return std::string(ConvertFromWideString(std::wstring(buf))); |
| 133 |
#else |
| 134 |
std::string buf; |
| 135 |
|
| 136 |
// Path where the file is located |
| 137 |
if (Path) { |
| 138 |
buf = Path; |
| 139 |
} |
| 140 |
else { |
| 141 |
buf = getTempPath(); |
| 142 |
} |
| 143 |
|
| 144 |
// File name in the path |
| 145 |
if (FileName) { |
| 146 |
buf += "/"; |
| 147 |
buf += FileName; |
| 148 |
buf += "XXXXXX"; |
| 149 |
} |
| 150 |
else { |
| 151 |
buf += "/fileXXXXXX"; |
| 152 |
} |
| 153 |
|
| 154 |
std::vector<char> vec; |
| 155 |
std::copy(buf.begin(), buf.end(), std::back_inserter(vec)); |
| 156 |
vec.push_back('\0'); |
| 157 |
|
| 158 |
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */ |
| 159 |
int id = mkstemp(vec.data()); |
| 160 |
if (id > -1) { |
| 161 |
FILE* file = fdopen(id, "w"); |
| 162 |
fclose(file); |
| 163 |
vec.pop_back(); // remove '\0' |
| 164 |
std::string str(vec.begin(), vec.end()); |
| 165 |
buf.swap(str); |
| 166 |
unlink(buf.c_str()); |
| 167 |
} |
| 168 |
return buf; |
| 169 |
#endif |
| 170 |
} |
| 171 |
|
| 172 |
fs::path FileInfo::stringToPath(const std::string& str) |
| 173 |
{ |
| 174 |
#ifdef FC_OS_WIN32 |
| 175 |
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; |
| 176 |
fs::path path(converter.from_bytes(str)); |
| 177 |
#else |
| 178 |
fs::path path(str); |
| 179 |
#endif |
| 180 |
return path; |
| 181 |
} |
| 182 |
|
| 183 |
std::string FileInfo::pathToString(const fs::path& path) |
| 184 |
{ |
| 185 |
#ifdef FC_OS_WIN32 |
| 186 |
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; |
| 187 |
return converter.to_bytes(path.wstring()); |
| 188 |
#else |
| 189 |
return path.string(); |
| 190 |
#endif |
| 191 |
} |
| 192 |
|
| 193 |
void FileInfo::setFile(const char* name) |
| 194 |
{ |
| 195 |
if (!name) { |
| 196 |
FileName.clear(); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
FileName = name; |
| 201 |
|
| 202 |
// keep the UNC paths intact |
| 203 |
if (FileName.substr(0, 2) == std::string("\\\\")) { |
| 204 |
std::replace(FileName.begin() + 2, FileName.end(), '\\', '/'); |
| 205 |
} |
| 206 |
else { |
| 207 |
std::replace(FileName.begin(), FileName.end(), '\\', '/'); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
std::string FileInfo::filePath() const |
| 212 |
{ |
| 213 |
return FileName; |
| 214 |
} |
| 215 |
|
| 216 |
std::string FileInfo::fileName() const |
| 217 |
{ |
| 218 |
return FileName.substr(FileName.find_last_of('/') + 1); |
| 219 |
} |
| 220 |
|
| 221 |
std::string FileInfo::dirPath() const |
| 222 |
{ |
| 223 |
std::size_t last_pos {}; |
| 224 |
std::string retval; |
| 225 |
last_pos = FileName.find_last_of('/'); |
| 226 |
if (last_pos != std::string::npos) { |
| 227 |
retval = FileName.substr(0, last_pos); |
| 228 |
} |
| 229 |
else { |
| 230 |
retval = pathToString(fs::current_path()); |
| 231 |
} |
| 232 |
return retval; |
| 233 |
} |
| 234 |
|
| 235 |
std::string FileInfo::fileNamePure() const |
| 236 |
{ |
| 237 |
std::string temp = fileName(); |
| 238 |
std::string::size_type pos = temp.find_last_of('.'); |
| 239 |
|
| 240 |
if (pos != std::string::npos) { |
| 241 |
return temp.substr(0, pos); |
| 242 |
} |
| 243 |
|
| 244 |
return temp; |
| 245 |
} |
| 246 |
|
| 247 |
std::wstring FileInfo::toStdWString() const |
| 248 |
{ |
| 249 |
// As FileName is UTF-8 is encoded we have to convert it |
| 250 |
// for Windows because the path names are UTF-16 encoded. |
| 251 |
#ifdef FC_OS_WIN32 |
| 252 |
return ConvertToWideString(FileName); |
| 253 |
#else |
| 254 |
// On other platforms it's discouraged to use wchar_t for file names |
| 255 |
throw Base::FileException("Cannot use FileInfo::toStdWString() on this platform"); |
| 256 |
#endif |
| 257 |
} |
| 258 |
|
| 259 |
std::string FileInfo::extension() const |
| 260 |
{ |
| 261 |
std::string::size_type pos = FileName.find_last_of('.'); |
| 262 |
if (pos == std::string::npos) { |
| 263 |
return {}; |
| 264 |
} |
| 265 |
return FileName.substr(pos + 1); |
| 266 |
} |
| 267 |
|
| 268 |
std::string FileInfo::completeExtension() const |
| 269 |
{ |
| 270 |
std::string::size_type pos = FileName.find_first_of('.'); |
| 271 |
if (pos == std::string::npos) { |
| 272 |
return {}; |
| 273 |
} |
| 274 |
return FileName.substr(pos + 1); |
| 275 |
} |
| 276 |
|
| 277 |
bool FileInfo::hasExtension(const char* Ext) const |
| 278 |
{ |
| 279 |
#ifdef FC_OS_WIN32 |
| 280 |
return _stricmp(Ext, extension().c_str()) == 0; |
| 281 |
#else |
| 282 |
return strcasecmp(Ext, extension().c_str()) == 0; |
| 283 |
#endif |
| 284 |
} |
| 285 |
|
| 286 |
bool FileInfo::hasExtension(std::initializer_list<const char*> Exts) const |
| 287 |
{ |
| 288 |
return std::ranges::any_of(Exts, [this](const char* ext) { return hasExtension(ext); }); |
| 289 |
} |
| 290 |
|
| 291 |
bool FileInfo::exists() const |
| 292 |
{ |
| 293 |
fs::path path(stringToPath(FileName)); |
| 294 |
return fs::exists(path); |
| 295 |
} |
| 296 |
|
| 297 |
bool FileInfo::isReadable() const |
| 298 |
{ |
| 299 |
fs::path path = stringToPath(FileName); |
| 300 |
if (!fs::exists(path)) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
fs::file_status stat = fs::status(path); |
| 304 |
fs::perms perms = stat.permissions(); |
| 305 |
return (perms & fs::perms::owner_read) == fs::perms::owner_read; |
| 306 |
} |
| 307 |
|
| 308 |
bool directoryIsWritable(const fs::path& dir) |
| 309 |
{ |
| 310 |
try { |
| 311 |
if (!fs::exists(dir) || !fs::is_directory(dir)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
fs::path test_file = dir / (".fs_perm_test_" + std::to_string(std::rand()) + ".tmp"); |
| 315 |
{ |
| 316 |
std::ofstream ofs(test_file); |
| 317 |
if (!ofs) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
} |
| 321 |
std::error_code ec; |
| 322 |
fs::remove(test_file, ec); |
| 323 |
return true; |
| 324 |
} |
| 325 |
catch (...) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
bool FileInfo::isWritable() const |
| 331 |
{ |
| 332 |
fs::path path = stringToPath(FileName); |
| 333 |
if (!fs::exists(path)) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
if (fs::is_directory(path)) { |
| 337 |
return directoryIsWritable(path); |
| 338 |
} |
| 339 |
#ifdef FC_OS_WIN32 |
| 340 |
// convert filename from UTF-8 to windows WSTRING |
| 341 |
std::wstring fileNameWstring = toStdWString(); |
| 342 |
// requires import of <windows.h> |
| 343 |
DWORD attributes = GetFileAttributes(fileNameWstring.c_str()); |
| 344 |
if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 345 |
// Log the error? |
| 346 |
std::clog << "GetFileAttributes failed for file: " << FileName << '\n'; |
| 347 |
// usually indicates some kind of network file issue, so the file is probably not writable |
| 348 |
return false; |
| 349 |
} |
| 350 |
if ((attributes & FILE_ATTRIBUTE_READONLY) != 0) { |
| 351 |
return false; |
| 352 |
} |
| 353 |
// TEST if file is truly writable, because windows ACL does not map well to POSIX perms, |
| 354 |
// and there are other potential blockers (app or shared file locks, etc) |
| 355 |
HANDLE hFile = CreateFileW( |
| 356 |
fileNameWstring.c_str(), |
| 357 |
GENERIC_WRITE, |
| 358 |
0, // ----> no sharing: fail if anyone else has it open |
| 359 |
nullptr, |
| 360 |
OPEN_EXISTING, |
| 361 |
FILE_ATTRIBUTE_NORMAL, |
| 362 |
nullptr |
| 363 |
); |
| 364 |
|
| 365 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 366 |
DWORD err = GetLastError(); |
| 367 |
if (err == ERROR_SHARING_VIOLATION || err == ERROR_LOCK_VIOLATION) { |
| 368 |
return false; |
| 369 |
} |
| 370 |
return false; |
| 371 |
} |
| 372 |
if (!CloseHandle(hFile)) { |
| 373 |
std::clog << "CloseHandle failed for file: " << FileName |
| 374 |
<< " while checking for write access." << '\n'; |
| 375 |
} |
| 376 |
#endif |
| 377 |
fs::file_status stat = fs::status(path); |
| 378 |
fs::perms perms = stat.permissions(); |
| 379 |
return (perms & fs::perms::owner_write) == fs::perms::owner_write; |
| 380 |
} |
| 381 |
|
| 382 |
bool FileInfo::setPermissions(Permissions perms) |
| 383 |
{ |
| 384 |
fs::perms mode = fs::perms::none; |
| 385 |
|
| 386 |
if (perms & FileInfo::ReadOnly) { |
| 387 |
mode |= fs::perms::owner_read; |
| 388 |
} |
| 389 |
if (perms & FileInfo::WriteOnly) { |
| 390 |
mode |= fs::perms::owner_write; |
| 391 |
} |
| 392 |
|
| 393 |
if (mode == fs::perms::none) { // bad argument |
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
fs::path file_path = stringToPath(FileName); |
| 398 |
if (!fs::exists(file_path)) { |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
fs::permissions(file_path, mode); |
| 403 |
fs::file_status stat = fs::status(file_path); |
| 404 |
return stat.permissions() == mode; |
| 405 |
} |
| 406 |
|
| 407 |
bool FileInfo::isFile() const |
| 408 |
{ |
| 409 |
fs::path path = stringToPath(FileName); |
| 410 |
if (fs::exists(path)) { |
| 411 |
return fs::is_regular_file(path); |
| 412 |
} |
| 413 |
|
| 414 |
// TODO: Check for valid file name |
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
bool FileInfo::isDir() const |
| 419 |
{ |
| 420 |
fs::path path = stringToPath(FileName); |
| 421 |
if (fs::exists(path)) { |
| 422 |
return fs::is_directory(path); |
| 423 |
} |
| 424 |
|
| 425 |
return false; |
| 426 |
} |
| 427 |
|
| 428 |
bool FileInfo::isSymlink() const |
| 429 |
{ |
| 430 |
fs::path path = stringToPath(FileName); |
| 431 |
if (fs::exists(path)) { |
| 432 |
return fs::is_symlink(path); |
| 433 |
} |
| 434 |
|
| 435 |
return false; |
| 436 |
} |
| 437 |
|
| 438 |
unsigned int FileInfo::size() const |
| 439 |
{ |
| 440 |
unsigned int bytes {}; |
| 441 |
fs::path path = stringToPath(FileName); |
| 442 |
if (fs::exists(path)) { |
| 443 |
bytes = fs::file_size(path); |
| 444 |
} |
| 445 |
|
| 446 |
return bytes; |
| 447 |
} |
| 448 |
|
| 449 |
template<typename TP> |
| 450 |
std::time_t to_time_t(TP tp) |
| 451 |
{ |
| 452 |
using namespace std::chrono; |
| 453 |
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now()); |
| 454 |
return system_clock::to_time_t(sctp); |
| 455 |
} |
| 456 |
|
| 457 |
TimeInfo FileInfo::lastModified() const |
| 458 |
{ |
| 459 |
TimeInfo ti = TimeInfo::null(); |
| 460 |
|
| 461 |
if (exists()) { |
| 462 |
fs::path path = stringToPath(FileName); |
| 463 |
ti.setTime_t(to_time_t(fs::last_write_time(path))); |
| 464 |
} |
| 465 |
|
| 466 |
return ti; |
| 467 |
} |
| 468 |
|
| 469 |
bool FileInfo::deleteFile() const |
| 470 |
{ |
| 471 |
try { |
| 472 |
fs::path path = stringToPath(FileName); |
| 473 |
return fs::remove(path); |
| 474 |
} |
| 475 |
catch (const fs::filesystem_error& e) { |
| 476 |
std::clog << e.what() << '\n'; |
| 477 |
return false; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
bool FileInfo::renameFile(const char* NewName) |
| 482 |
{ |
| 483 |
try { |
| 484 |
fs::path old_path = stringToPath(FileName); |
| 485 |
fs::path new_path = stringToPath(NewName); |
| 486 |
fs::rename(old_path, new_path); |
| 487 |
FileName = NewName; |
| 488 |
return true; |
| 489 |
} |
| 490 |
catch (const fs::filesystem_error& e) { |
| 491 |
std::clog << "Error in renameFile: " << e.what() << '\n'; |
| 492 |
return false; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
bool FileInfo::copyTo(const char* NewName) const |
| 497 |
{ |
| 498 |
try { |
| 499 |
fs::path old_path = stringToPath(FileName); |
| 500 |
fs::path new_path = stringToPath(NewName); |
| 501 |
fs::copy(old_path, new_path); |
| 502 |
return true; |
| 503 |
} |
| 504 |
catch (const fs::filesystem_error&) { |
| 505 |
return false; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
bool FileInfo::createDirectory() const |
| 510 |
{ |
| 511 |
try { |
| 512 |
fs::path path(stringToPath(FileName)); |
| 513 |
return fs::create_directory(path); |
| 514 |
} |
| 515 |
catch (const fs::filesystem_error&) { |
| 516 |
return false; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
bool FileInfo::createDirectories() const |
| 521 |
{ |
| 522 |
try { |
| 523 |
fs::path path(stringToPath(FileName)); |
| 524 |
if (fs::exists(path)) { |
| 525 |
return true; |
| 526 |
} |
| 527 |
|
| 528 |
return fs::create_directories(path); |
| 529 |
} |
| 530 |
catch (const fs::filesystem_error&) { |
| 531 |
return false; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
bool FileInfo::deleteDirectory() const |
| 536 |
{ |
| 537 |
if (!isDir()) { |
| 538 |
return false; |
| 539 |
} |
| 540 |
|
| 541 |
try { |
| 542 |
fs::path path = stringToPath(FileName); |
| 543 |
return fs::remove(path); |
| 544 |
} |
| 545 |
catch (const fs::filesystem_error& e) { |
| 546 |
std::clog << e.what() << '\n'; |
| 547 |
return false; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
bool FileInfo::deleteDirectoryRecursive() const |
| 552 |
{ |
| 553 |
if (!isDir()) { |
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
try { |
| 558 |
fs::path path = stringToPath(FileName); |
| 559 |
return fs::remove_all(path) > 0; |
| 560 |
} |
| 561 |
catch (const fs::filesystem_error& e) { |
| 562 |
std::clog << e.what() << '\n'; |
| 563 |
return false; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
std::vector<Base::FileInfo> FileInfo::getDirectoryContent() const |
| 568 |
{ |
| 569 |
std::error_code ec; |
| 570 |
std::vector<Base::FileInfo> list; |
| 571 |
fs::path path = stringToPath(FileName); |
| 572 |
|
| 573 |
for (const fs::directory_entry& f : fs::directory_iterator {path, ec}) { |
| 574 |
list.emplace_back(pathToString(f.path())); |
| 575 |
} |
| 576 |
|
| 577 |
return list; |
| 578 |
} |
| 579 |
|
| 580 |
std::optional<std::string> FileInfo::getSymlinkTarget() |
| 581 |
{ |
| 582 |
fs::path path = stringToPath(FileName); |
| 583 |
if (isSymlink()) { |
| 584 |
return pathToString(fs::read_symlink(path)); |
| 585 |
} |
| 586 |
return std::nullopt; |
| 587 |
} |
| 588 |
|
| 589 |
std::optional<std::string> FileInfo::getCannonicalPath() |
| 590 |
{ |
| 591 |
try { |
| 592 |
fs::path path = stringToPath(FileName); |
| 593 |
return pathToString(fs::canonical(path)); |
| 594 |
} |
| 595 |
catch (const fs::filesystem_error& e) { |
| 596 |
std::clog << e.what() << '\n'; |
| 597 |
return std::nullopt; |
| 598 |
} |
| 599 |
} |