merge File::linkTo() from 1.8.1

This commit is contained in:
Günter Obiltschnig 2017-12-14 11:53:32 +01:00
parent b27daf2324
commit 472f57821d
10 changed files with 89 additions and 1 deletions

View File

@ -60,6 +60,13 @@ class Foundation_API File: private FileImpl
public:
typedef FileSizeImpl FileSize;
enum LinkType
/// Type of link for linkTo().
{
LINK_HARD = 0, /// hard link
LINK_SYMBOLIC = 1 /// symbolic link
};
File();
/// Creates the file.
@ -186,6 +193,14 @@ public:
void renameTo(const std::string& path);
/// Renames the file to the new name.
void linkTo(const std::string& path, LinkType type = LINK_SYMBOLIC) const;
/// Creates a link (symbolic or hard, depending on type argument)
/// at the given path to the file or directory.
///
/// May not be supported on all platforms.
/// Furthermore, some operating systems do not allow creating
/// hard links to directories.
void remove(bool recursive = false);
/// Deletes the file. If recursive is true and the
/// file is a directory, recursively deletes all

View File

@ -54,6 +54,7 @@ protected:
void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const;
void renameToImpl(const std::string& path);
void linkToImpl(const std::string& path, int type) const;
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();

View File

@ -53,6 +53,7 @@ protected:
void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const;
void renameToImpl(const std::string& path);
void linkToImpl(const std::string& path, int type) const;
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();

View File

@ -54,6 +54,7 @@ protected:
void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const;
void renameToImpl(const std::string& path);
void linkToImpl(const std::string& path, int type) const;
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();

View File

@ -54,6 +54,7 @@ protected:
void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const;
void renameToImpl(const std::string& path);
void linkToImpl(const std::string& path, int type) const;
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();

View File

@ -260,6 +260,12 @@ void File::renameTo(const std::string& rPath)
}
void File::linkTo(const std::string& rPath, LinkType type) const
{
linkToImpl(rPath, type);
}
void File::remove(bool recursive)
{
if (recursive && !isLink() && isDirectory())

View File

@ -414,6 +414,23 @@ void FileImpl::renameToImpl(const std::string& path)
}
void FileImpl::linkToImpl(const std::string& path, int type) const
{
poco_assert (!_path.empty());
if (type == 0)
{
if (link(_path.c_str(), path.c_str()) != 0)
handleLastErrorImpl(_path);
}
else
{
if (symlink(_path.c_str(), path.c_str()) != 0)
handleLastErrorImpl(_path);
}
}
void FileImpl::removeImpl()
{
poco_assert (!_path.empty());

View File

@ -285,6 +285,12 @@ void FileImpl::renameToImpl(const std::string& path)
}
void FileImpl::linkToImpl(const std::string& path, int type) const
{
throw Poco::NotImplementedException("File::linkTo() is not available on this platform");
}
void FileImpl::removeImpl()
{
poco_assert (!_path.empty());

View File

@ -167,7 +167,12 @@ bool FileImpl::isDirectoryImpl() const
bool FileImpl::isLinkImpl() const
{
return false;
poco_assert (!_path.empty());
DWORD attr = GetFileAttributesW(_upath.c_str());
if (attr == INVALID_FILE_ATTRIBUTES)
handleLastErrorImpl(_path);
return (attr & FILE_ATTRIBUTE_DIRECTORY) == 0 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
}
@ -308,6 +313,35 @@ void FileImpl::renameToImpl(const std::string& path)
}
void FileImpl::linkToImpl(const std::string& path, int type) const
{
poco_assert (!_path.empty());
std::wstring upath;
convertPath(path, upath);
if (type == 0)
{
if (CreateHardLinkW(upath.c_str(), _upath.c_str(), NULL) == 0)
handleLastErrorImpl(_path);
}
else
{
#if _WIN32_WINNT >= 0x0600 && defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
DWORD flags = 0;
if (isDirectoryImpl()) flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
#ifdef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
#endif
if (CreateSymbolicLinkW(upath.c_str(), _upath.c_str(), flags) == 0)
handleLastErrorImpl(_path);
#else
throw Poco::NotImplementedException("Symbolic link support not available in used version of the Windows SDK");
#endif
}
}
void FileImpl::removeImpl()
{
poco_assert (!_path.empty());

View File

@ -299,6 +299,12 @@ void FileImpl::renameToImpl(const std::string& path)
}
void FileImpl::linkToImpl(const std::string& path, int type) const
{
throw Poco::NotImplementedException("File::linkTo() is not available on this platform");
}
void FileImpl::removeImpl()
{
poco_assert (!_path.empty());