Added the Options Enum advice.

This commit is contained in:
Jan Kevin Dick
2019-11-26 13:03:43 +01:00
parent 8b4ef5f706
commit d5a25f2053
11 changed files with 45 additions and 39 deletions

View File

@@ -70,6 +70,12 @@ public:
LINK_SYMBOLIC = 1 /// symbolic link LINK_SYMBOLIC = 1 /// symbolic link
}; };
enum Options
/// Options for File Copy/Movement
{
OPT_FAIL_ON_OVERWRITE = 0x01
};
File(); File();
/// Creates the file. /// Creates the file.
@@ -183,7 +189,7 @@ public:
/// ///
/// Does nothing on Windows. /// Does nothing on Windows.
void copyTo(const std::string& path, bool failOnOverwrite = false) const; void copyTo(const std::string& path, int options = 0) 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.
/// ///
@@ -191,13 +197,13 @@ public:
/// If failOnOverwrite is set the Method throws an FileExists Exception /// If failOnOverwrite is set the Method throws an FileExists Exception
/// if the File already exists. /// if the File already exists.
void moveTo(const std::string& path, bool failOnOverwrite = false); void moveTo(const std::string& path, int options = 0);
/// 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 failOnOverwrite is set the Method throws an FileExists Exception
/// if the File already exists. /// if the File already exists.
void renameTo(const std::string& path, bool failOnOverwrite = false); void renameTo(const std::string& path, int options = 0);
/// 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 failOnOverwrite is set the Method throws an FileExists Exception
/// if the File already exists. /// if the File already exists.
@@ -259,7 +265,7 @@ public:
/// exception for the last file-related error. /// exception for the last file-related error.
protected: protected:
void copyDirectory(const std::string& path, bool failOnOverwrite = false) const; void copyDirectory(const std::string& path, int options = 0) 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, bool failOnOverwrite = false) const; void copyToImpl(const std::string& path, int options = 0) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false); void renameToImpl(const std::string& path, int options = 0);
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, bool failOnOverwrite = false) const; void copyToImpl(const std::string& path, int options = 0) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false); void renameToImpl(const std::string& path, int options = 0);
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, bool failOnOverwrite = false) const; void copyToImpl(const std::string& path, int options = 0) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false); void renameToImpl(const std::string& path, int options = 0);
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, bool failOnOverwrite = false) const; void copyToImpl(const std::string& path, int options = 0) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false); void renameToImpl(const std::string& path, int options = 0);
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, bool failOnOverwrite) const void File::copyTo(const std::string& path, int options) const
{ {
Path src(getPathImpl()); Path src(getPathImpl());
Path dest(path); Path dest(path);
@@ -219,13 +219,13 @@ void File::copyTo(const std::string& path, bool failOnOverwrite) const
dest.setFileName(src.getFileName()); dest.setFileName(src.getFileName());
} }
if (isDirectory()) if (isDirectory())
copyDirectory(dest.toString(), failOnOverwrite); copyDirectory(dest.toString(), options);
else else
copyToImpl(dest.toString(), failOnOverwrite); copyToImpl(dest.toString(), options);
} }
void File::copyDirectory(const std::string& path, bool failOnOverwrite) const void File::copyDirectory(const std::string& path, int options) const
{ {
File target(path); File target(path);
target.createDirectories(); target.createDirectories();
@@ -236,22 +236,22 @@ void File::copyDirectory(const std::string& path, bool failOnOverwrite) const
DirectoryIterator end; DirectoryIterator end;
for (; it != end; ++it) for (; it != end; ++it)
{ {
it->copyTo(path, failOnOverwrite); it->copyTo(path, options);
} }
} }
void File::moveTo(const std::string& path, bool failOnOverwrite) void File::moveTo(const std::string& path, int options)
{ {
copyTo(path, failOnOverwrite); copyTo(path, options);
remove(true); remove(true);
setPathImpl(path); setPathImpl(path);
} }
void File::renameTo(const std::string& path, bool failOnOverwrite) void File::renameTo(const std::string& path, int options)
{ {
renameToImpl(path, failOnOverwrite); renameToImpl(path, options);
setPathImpl(path); setPathImpl(path);
} }

View File

@@ -326,7 +326,7 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const void FileImpl::copyToImpl(const std::string& path, int options) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
@@ -341,7 +341,7 @@ void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
} }
const long blockSize = st.st_blksize; const long blockSize = st.st_blksize;
int dd; int dd;
if (failOnOverwrite) { if (options) {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, st.st_mode); dd = open(path.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, st.st_mode);
} else { } else {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode); dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode);
@@ -380,7 +380,7 @@ void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
} }
void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite) void FileImpl::renameToImpl(const std::string& path, int options)
{ {
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, bool failOnOverwrite) const void FileImpl::copyToImpl(const std::string& path, int options) const
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
@@ -248,7 +248,7 @@ void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
const long blockSize = st.st_blksize; const long blockSize = st.st_blksize;
int dd; int dd;
if (failOnOverwrite) { if (options) {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, st.st_mode & S_IRWXU); dd = open(path.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, st.st_mode & S_IRWXU);
} else { } else {
dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode & S_IRWXU); dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode & S_IRWXU);
@@ -282,7 +282,7 @@ void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
} }
void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite) void FileImpl::renameToImpl(const std::string& path, int options)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());

View File

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

View File

@@ -291,24 +291,24 @@ void FileImpl::setExecutableImpl(bool flag)
} }
void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const void FileImpl::copyToImpl(const std::string& path, int options) 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(), failOnOverwrite) == 0) if (CopyFileW(_upath.c_str(), upath.c_str(), options) == 0)
handleLastErrorImpl(_path); handleLastErrorImpl(_path);
} }
void FileImpl::renameToImpl(const std::string& path, bool failOnOverwrite) void FileImpl::renameToImpl(const std::string& path, int options)
{ {
poco_assert (!_path.empty()); poco_assert (!_path.empty());
std::wstring upath; std::wstring upath;
convertPath(path, upath); convertPath(path, upath);
if (failOnOverwrite) { if (options) {
if (MoveFileExW(_upath.c_str(), upath.c_str(), NULL) == 0) if (MoveFileExW(_upath.c_str(), upath.c_str(), NULL) == 0)
handleLastErrorImpl(_path); handleLastErrorImpl(_path);
} else { } else {

View File

@@ -390,7 +390,7 @@ void FileTest::testCopyFailIfDestinationFileExists()
TemporaryFile f2; TemporaryFile f2;
f2.createFile(); f2.createFile();
try { try {
f1.setReadOnly().copyTo(f2.path(), true); f1.setReadOnly().copyTo(f2.path(), File::OPT_FAIL_ON_OVERWRITE);
failmsg("file exist - must throw exception"); failmsg("file exist - must throw exception");
} catch (Exception&) { } catch (Exception&) {
} }
@@ -423,7 +423,7 @@ void FileTest::testMoveFailIfDestinationFileExists() {
TemporaryFile f2; TemporaryFile f2;
f2.createFile(); f2.createFile();
try { try {
f1.moveTo(f2.path(), true); f1.moveTo(f2.path(), File::OPT_FAIL_ON_OVERWRITE);
failmsg("file exist - must throw exception"); failmsg("file exist - must throw exception");
} catch (Exception&) { } catch (Exception&) {
} }
@@ -529,7 +529,7 @@ void FileTest::testCopyDirectoryFailIfExists()
fd3.createDirectories(); fd3.createDirectories();
try { try {
fd1.copyTo("testdir", true); fd1.copyTo("testdir", File::OPT_FAIL_ON_OVERWRITE);
failmsg("Destination Directory exists - must throw exception"); failmsg("Destination Directory exists - must throw exception");
} catch (Exception&) { } catch (Exception&) {
} }
@@ -565,7 +565,7 @@ void FileTest::testRenameFailIfExists() {
f2.createFile(); f2.createFile();
try { try {
f1.renameTo(f2.path(), true); f1.renameTo(f2.path(), File::OPT_FAIL_ON_OVERWRITE);
failmsg("file exists - must throw exception"); failmsg("file exists - must throw exception");
} catch (Exception&) { } catch (Exception&) {
} }