From 7b58bc10e7d7eb06e47c200ca023b4bcb0b35796 Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Thu, 29 May 2025 10:50:58 +0200 Subject: [PATCH] Poco::File create path (#4873) (#4959) * #4202 Checked if the path exists, create one if not * Addressed comments * fix(Poco::File): resolve compile errors related to createfile. --------- Co-authored-by: capak07 <69026837+capak07@users.noreply.github.com> --- Foundation/include/Poco/File_UNIX.h | 2 +- Foundation/include/Poco/File_VX.h | 2 +- Foundation/include/Poco/File_WIN32U.h | 2 +- Foundation/src/File_UNIX.cpp | 7 ++++++- Foundation/src/File_VX.cpp | 6 +++++- Foundation/src/File_WIN32U.cpp | 7 ++++++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Foundation/include/Poco/File_UNIX.h b/Foundation/include/Poco/File_UNIX.h index 8c3c35207..44a96b36e 100644 --- a/Foundation/include/Poco/File_UNIX.h +++ b/Foundation/include/Poco/File_UNIX.h @@ -62,7 +62,7 @@ protected: void renameToImpl(const std::string& path, int options = 0); void linkToImpl(const std::string& path, int type) const; void removeImpl(); - bool createFileImpl(); + bool createFileImpl(bool createDirectories = false); bool createDirectoryImpl(); FileSizeImpl totalSpaceImpl() const; FileSizeImpl usableSpaceImpl() const; diff --git a/Foundation/include/Poco/File_VX.h b/Foundation/include/Poco/File_VX.h index ac99930e2..eeed2289e 100644 --- a/Foundation/include/Poco/File_VX.h +++ b/Foundation/include/Poco/File_VX.h @@ -61,7 +61,7 @@ protected: void renameToImpl(const std::string& path, int options = 0); void linkToImpl(const std::string& path, int type) const; void removeImpl(); - bool createFileImpl(); + bool createFileImpl(bool createDirectories = false); bool createDirectoryImpl(); FileSizeImpl totalSpaceImpl() const; FileSizeImpl usableSpaceImpl() const; diff --git a/Foundation/include/Poco/File_WIN32U.h b/Foundation/include/Poco/File_WIN32U.h index a88d8334c..4c7c6f1c2 100644 --- a/Foundation/include/Poco/File_WIN32U.h +++ b/Foundation/include/Poco/File_WIN32U.h @@ -62,7 +62,7 @@ protected: void renameToImpl(const std::string& path, int options = 0); void linkToImpl(const std::string& path, int type) const; void removeImpl(); - bool createFileImpl(); + bool createFileImpl(bool createDirectories = false); bool createDirectoryImpl(); FileSizeImpl totalSpaceImpl() const; FileSizeImpl usableSpaceImpl() const; diff --git a/Foundation/src/File_UNIX.cpp b/Foundation/src/File_UNIX.cpp index 9261a9a36..5d2a706c1 100644 --- a/Foundation/src/File_UNIX.cpp +++ b/Foundation/src/File_UNIX.cpp @@ -459,10 +459,15 @@ void FileImpl::removeImpl() } -bool FileImpl::createFileImpl() +bool FileImpl::createFileImpl(bool createDirectories) { poco_assert (!_path.empty()); + if(createDirectories) { + Path p(_path); + p.makeDirectory(); + } + int n = open(_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (n != -1) { diff --git a/Foundation/src/File_VX.cpp b/Foundation/src/File_VX.cpp index ce2399377..6eff933db 100644 --- a/Foundation/src/File_VX.cpp +++ b/Foundation/src/File_VX.cpp @@ -321,10 +321,14 @@ void FileImpl::removeImpl() } -bool FileImpl::createFileImpl() +bool FileImpl::createFileImpl(bool createDirectories) { poco_assert (!_path.empty()); + if(createDirectories) { + Path p(_path); + p.makeDirectory(); + } int n = open(_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (n != -1) { diff --git a/Foundation/src/File_WIN32U.cpp b/Foundation/src/File_WIN32U.cpp index f4cab98d6..02e736950 100644 --- a/Foundation/src/File_WIN32U.cpp +++ b/Foundation/src/File_WIN32U.cpp @@ -377,10 +377,15 @@ void FileImpl::removeImpl() } -bool FileImpl::createFileImpl() +bool FileImpl::createFileImpl(bool createDirectories) { poco_assert (!_path.empty()); + if(createDirectories) { + Path p(_path); + p.makeDirectory(); + } + HANDLE hFile = CreateFileW(_upath.c_str(), GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0); if (hFile != INVALID_HANDLE_VALUE) {