Merge branch 'feature/FileFailOnOverwrite' of github.com:KevDi/poco into feature/FileFailOnOverwrite

This commit is contained in:
KevDi 2019-11-30 13:21:34 +01:00
commit f6f8ef8681
11 changed files with 60 additions and 44 deletions

View File

@ -70,6 +70,12 @@ public:
LINK_SYMBOLIC = 1 /// symbolic link
};
enum Options
/// Options for File Copy/Movement
{
OPT_FAIL_ON_OVERWRITE = OPT_FAIL_ON_OVERWRITE_IMPL
};
File();
/// Creates the file.
@ -183,23 +189,23 @@ public:
///
/// 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.
/// The target path can be a directory.
///
/// A directory is copied recursively.
/// If failOnOverwrite is set the Method throws an FileExists Exception
/// If options is set to OPT_FAIL_ON_OVERWRITE the Method throws an FileExists Exception
/// 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
/// removes the original file. The target path can be a directory.
/// If failOnOverwrite is set the Method throws an FileExists Exception
/// If options is set to OPT_FAIL_ON_OVERWRITE the Method throws an FileExists Exception
/// 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.
/// If failOnOverwrite is set the Method throws an FileExists Exception
/// If options is set to OPT_FAIL_ON_OVERWRITE the Method throws an FileExists Exception
/// if the File already exists.
void linkTo(const std::string& path, LinkType type = LINK_SYMBOLIC) const;
@ -259,7 +265,7 @@ public:
/// exception for the last file-related error.
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().
};

View File

@ -27,6 +27,11 @@ namespace Poco {
class FileImpl
{
protected:
enum Options {
OPT_FAIL_ON_OVERWRITE_IMPL = 0x01
};
typedef UInt64 FileSizeImpl;
FileImpl();
@ -51,8 +56,8 @@ protected:
void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void copyToImpl(const std::string& path, int options = 0) const;
void renameToImpl(const std::string& path, int options = 0);
void linkToImpl(const std::string& path, int type) const;
void removeImpl();
bool createFileImpl();

View File

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

View File

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

View File

@ -28,6 +28,11 @@ namespace Poco {
class Foundation_API FileImpl
{
protected:
enum Options {
OPT_FAIL_ON_OVERWRITE_IMPL = 0x01
};
typedef UInt64 FileSizeImpl;
FileImpl();
@ -52,8 +57,8 @@ protected:
void setSizeImpl(FileSizeImpl size);
void setWriteableImpl(bool flag = true);
void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false);
void copyToImpl(const std::string& path, int options = 0) const;
void renameToImpl(const std::string& path, int options = 0);
void linkToImpl(const std::string& path, int type) const;
void removeImpl();
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 dest(path);
@ -219,13 +219,13 @@ void File::copyTo(const std::string& path, bool failOnOverwrite) const
dest.setFileName(src.getFileName());
}
if (isDirectory())
copyDirectory(dest.toString(), failOnOverwrite);
copyDirectory(dest.toString(), options);
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);
target.createDirectories();
@ -236,22 +236,22 @@ void File::copyDirectory(const std::string& path, bool failOnOverwrite) const
DirectoryIterator end;
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);
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);
}

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());
@ -341,7 +341,7 @@ void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
}
const long blockSize = st.st_blksize;
int dd;
if (failOnOverwrite) {
if (options & OPT_FAIL_ON_OVERWRITE_IMPL) {
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);
@ -380,13 +380,13 @@ 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());
struct stat st;
if (stat(path.c_str(), &st) == 0 && failOnOverwrite)
if (stat(path.c_str(), &st) == 0 && (options & OPT_FAIL_ON_OVERWRITE_IMPL))
throw FileExistsException(path, EEXIST);
if (rename(_path.c_str(), path.c_str()) != 0)

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());
@ -248,7 +248,7 @@ void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
const long blockSize = st.st_blksize;
int dd;
if (failOnOverwrite) {
if (options == 1) {
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);
@ -282,13 +282,13 @@ 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());
struct stat st;
if (stat(path.c_str(), &st) == 0 && failOnOverwrite)
if (stat(path.c_str(), &st) == 0 && options == 1)
throw FileExistsException(path, EEXIST);
if (rename(_path.c_str(), path.c_str()) != 0)

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

View File

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