Added FailOnOverwrite to File Class

This commit is contained in:
Jan Kevin Dick
2019-11-18 13:06:43 +01:00
parent d658cc25e7
commit 4e734553b3
12 changed files with 70 additions and 43 deletions

View File

@@ -183,18 +183,24 @@ public:
/// ///
/// Does nothing on Windows. /// Does nothing on Windows.
void copyTo(const std::string& path) const; void copyTo(const std::string& path, bool failOnOverwrite = false) const;
/// Copies the file (or directory) to the given path. /// Copies the file (or directory) to the given path.
/// The target path can be a directory. /// The target path can be a directory.
/// ///
/// A directory is copied recursively. /// A directory is copied recursively.
/// If failOnOverwrite is set the Method throws an FileExists Exception
/// if the File already exists.
void moveTo(const std::string& path); void moveTo(const std::string& path, bool failOnOverwrite = false);
/// Copies the file (or directory) to the given path and /// Copies the file (or directory) to the given path and
/// removes the original file. The target path can be a directory. /// removes the original file. The target path can be a directory.
/// If failOnOverwrite is set the Method throws an FileExists Exception
/// if the File already exists.
void renameTo(const std::string& path); void renameTo(const std::string& path, bool failOnOverwrite = false);
/// Renames the file to the new name. /// Renames the file to the new name.
/// If failOnOverwrite is set the Method throws an FileExists Exception
/// if the File already exists.
void linkTo(const std::string& path, LinkType type = LINK_SYMBOLIC) const; void linkTo(const std::string& path, LinkType type = LINK_SYMBOLIC) const;
/// Creates a link (symbolic or hard, depending on type argument) /// Creates a link (symbolic or hard, depending on type argument)
@@ -253,7 +259,7 @@ public:
/// exception for the last file-related error. /// exception for the last file-related error.
protected: protected:
void copyDirectory(const std::string& path) const; void copyDirectory(const std::string& path, bool failOnOverwrite = false) const;
/// Copies a directory. Used internally by copyTo(). /// Copies a directory. Used internally by copyTo().
}; };

View File

@@ -51,8 +51,8 @@ protected:
void setSizeImpl(FileSizeImpl size); void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true); void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true); void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const; void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path); void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void linkToImpl(const std::string& path, int type) const; void linkToImpl(const std::string& path, int type) const;
void removeImpl(); void removeImpl();
bool createFileImpl(); bool createFileImpl();

View File

@@ -51,8 +51,8 @@ protected:
void setSizeImpl(FileSizeImpl size); void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true); void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true); void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const; void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path); void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void linkToImpl(const std::string& path, int type) const; void linkToImpl(const std::string& path, int type) const;
void removeImpl(); void removeImpl();
bool createFileImpl(); bool createFileImpl();

View File

@@ -52,8 +52,8 @@ protected:
void setSizeImpl(FileSizeImpl size); void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true); void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true); void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const; void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path); void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void linkToImpl(const std::string& path, int type) const; void linkToImpl(const std::string& path, int type) const;
void removeImpl(); void removeImpl();
bool createFileImpl(); bool createFileImpl();

View File

@@ -52,8 +52,8 @@ protected:
void setSizeImpl(FileSizeImpl size); void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true); void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true); void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const; void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path); void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void linkToImpl(const std::string& path, int type) const; void linkToImpl(const std::string& path, int type) const;
void removeImpl(); void removeImpl();
bool createFileImpl(); bool createFileImpl();

View File

@@ -52,8 +52,8 @@ protected:
void setSizeImpl(FileSizeImpl size); void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true); void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true); void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const; void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path); void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void linkToImpl(const std::string& path, int type) const; void linkToImpl(const std::string& path, int type) const;
void removeImpl(); void removeImpl();
bool createFileImpl(); bool createFileImpl();

View File

@@ -208,7 +208,7 @@ File& File::setExecutable(bool flag)
} }
void File::copyTo(const std::string& path) const void File::copyTo(const std::string& path, bool failOnOverwrite) const
{ {
Path src(getPathImpl()); Path src(getPathImpl());
Path dest(path); Path dest(path);
@@ -219,13 +219,13 @@ void File::copyTo(const std::string& path) const
dest.setFileName(src.getFileName()); dest.setFileName(src.getFileName());
} }
if (isDirectory()) if (isDirectory())
copyDirectory(dest.toString()); copyDirectory(dest.toString(), failOnOverwrite);
else else
copyToImpl(dest.toString()); copyToImpl(dest.toString(), failOnOverwrite);
} }
void File::copyDirectory(const std::string& path) const void File::copyDirectory(const std::string& path, bool failOnOverwrite) const
{ {
File target(path); File target(path);
target.createDirectories(); target.createDirectories();
@@ -236,22 +236,22 @@ void File::copyDirectory(const std::string& path) const
DirectoryIterator end; DirectoryIterator end;
for (; it != end; ++it) for (; it != end; ++it)
{ {
it->copyTo(path); it->copyTo(path, failOnOverwrite);
} }
} }
void File::moveTo(const std::string& path) void File::moveTo(const std::string& path, bool failOnOverwrite)
{ {
copyTo(path); copyTo(path, failOnOverwrite);
remove(true); remove(true);
setPathImpl(path); setPathImpl(path);
} }
void File::renameTo(const std::string& path) void File::renameTo(const std::string& path, bool failOnOverwrite)
{ {
renameToImpl(path); renameToImpl(path, failOnOverwrite);
setPathImpl(path); setPathImpl(path);
} }

View File

@@ -326,7 +326,7 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path) const void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
@@ -340,8 +340,12 @@ void FileImpl::copyToImpl(const std::string& path) const
handleLastErrorImpl(_path); handleLastErrorImpl(_path);
} }
const long blockSize = st.st_blksize; const long blockSize = st.st_blksize;
int dd;
int dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode); if (failOnOverwrite) {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, st.st_mode);
} else {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode);
}
if (dd == -1) if (dd == -1)
{ {
close(sd); close(sd);
@@ -376,7 +380,7 @@ void FileImpl::copyToImpl(const std::string& path) const
} }
void FileImpl::renameToImpl(const std::string& path) void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite = false)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());

View File

@@ -232,7 +232,7 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path) const void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
@@ -247,7 +247,13 @@ void FileImpl::copyToImpl(const std::string& path) const
} }
const long blockSize = st.st_blksize; const long blockSize = st.st_blksize;
int dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode & S_IRWXU); int dd;
if (failOnOverwrite) {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, st.st_mode & S_IRWXU);
} else {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode & S_IRWXU);
}
if (dd == -1) if (dd == -1)
{ {
close(sd); close(sd);
@@ -276,7 +282,7 @@ void FileImpl::copyToImpl(const std::string& path) const
} }
void FileImpl::renameToImpl(const std::string& path) void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());

View File

@@ -288,21 +288,27 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path) const void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
if (CopyFileA(_path.c_str(), path.c_str(), FALSE) == 0) if (CopyFileA(_path.c_str(), path.c_str(), failOnOverwrite) == 0)
handleLastErrorImpl(_path); handleLastErrorImpl(_path);
} }
void FileImpl::renameToImpl(const std::string& path) void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
if (MoveFileExA(_path.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING) == 0) if (failOnOverwrite) {
handleLastErrorImpl(_path); if (MoveFileExA(_path.c_str(), path.c_str(), NULL) == 0)
handleLastErrorImpl(_path);
} else {
if (MoveFileExA(_path.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING) == 0)
handleLastErrorImpl(_path);
}
} }

View File

@@ -291,25 +291,30 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path) const void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
std::wstring upath; std::wstring upath;
convertPath(path, upath); convertPath(path, upath);
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) == 0) if (CopyFileW(_upath.c_str(), upath.c_str(), failOnOverwrite) == 0)
handleLastErrorImpl(_path); handleLastErrorImpl(_path);
} }
void FileImpl::renameToImpl(const std::string& path) void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
std::wstring upath; std::wstring upath;
convertPath(path, upath); convertPath(path, upath);
if (MoveFileExW(_upath.c_str(), upath.c_str(), MOVEFILE_REPLACE_EXISTING) == 0) if (failOnOverwrite) {
handleLastErrorImpl(_path); if (MoveFileExW(_upath.c_str(), upath.c_str(), NULL) == 0)
handleLastErrorImpl(_path);
} else {
if (MoveFileExW(_upath.c_str(), upath.c_str(), MOVEFILE_REPLACE_EXISTING) == 0)
handleLastErrorImpl(_path);
}
} }

View File

@@ -277,18 +277,18 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path) const void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
std::wstring upath; std::wstring upath;
convertPath(path, upath); convertPath(path, upath);
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) == 0) if (CopyFileW(_upath.c_str(), upath.c_str(), failOnOverwrite) == 0)
handleLastErrorImpl(_path); handleLastErrorImpl(_path);
} }
void FileImpl::renameToImpl(const std::string& path) void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());