mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-31 16:04:27 +02:00
added Poco::File::linkTo()
This commit is contained in:
parent
9472b163b4
commit
d29972ef24
@ -63,6 +63,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.
|
||||
|
||||
@ -189,6 +196,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
|
||||
|
@ -49,18 +49,19 @@ protected:
|
||||
void setLastModifiedImpl(const Timestamp& ts);
|
||||
FileSizeImpl getSizeImpl() const;
|
||||
void setSizeImpl(FileSizeImpl size);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
void setExecutableImpl(bool flag = true);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
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();
|
||||
static void handleLastErrorImpl(const std::string& path);
|
||||
|
||||
|
||||
private:
|
||||
std::string _path;
|
||||
|
||||
|
||||
friend class DirectoryIteratorImpl;
|
||||
friend class LinuxDirectoryWatcherStrategy;
|
||||
friend class BSDDirectoryWatcherStrategy;
|
||||
|
@ -49,18 +49,19 @@ protected:
|
||||
void setLastModifiedImpl(const Timestamp& ts);
|
||||
FileSizeImpl getSizeImpl() const;
|
||||
void setSizeImpl(FileSizeImpl size);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
void setExecutableImpl(bool flag = true);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
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();
|
||||
static void handleLastErrorImpl(const std::string& path);
|
||||
|
||||
|
||||
private:
|
||||
std::string _path;
|
||||
|
||||
|
||||
friend class DirectoryIteratorImpl;
|
||||
};
|
||||
|
||||
|
@ -50,18 +50,19 @@ protected:
|
||||
void setLastModifiedImpl(const Timestamp& ts);
|
||||
FileSizeImpl getSizeImpl() const;
|
||||
void setSizeImpl(FileSizeImpl size);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
void setExecutableImpl(bool flag = true);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
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();
|
||||
static void handleLastErrorImpl(const std::string& path);
|
||||
|
||||
|
||||
private:
|
||||
std::string _path;
|
||||
|
||||
|
||||
friend class FileHandle;
|
||||
friend class DirectoryIteratorImpl;
|
||||
friend class WindowsDirectoryWatcherStrategy;
|
||||
|
@ -50,20 +50,21 @@ protected:
|
||||
void setLastModifiedImpl(const Timestamp& ts);
|
||||
FileSizeImpl getSizeImpl() const;
|
||||
void setSizeImpl(FileSizeImpl size);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
void setExecutableImpl(bool flag = true);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
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();
|
||||
static void handleLastErrorImpl(const std::string& path);
|
||||
static void convertPath(const std::string& utf8Path, std::wstring& utf16Path);
|
||||
|
||||
|
||||
private:
|
||||
std::string _path;
|
||||
std::wstring _upath;
|
||||
|
||||
|
||||
friend class FileHandle;
|
||||
friend class DirectoryIteratorImpl;
|
||||
friend class WindowsDirectoryWatcherStrategy;
|
||||
|
@ -50,20 +50,21 @@ protected:
|
||||
void setLastModifiedImpl(const Timestamp& ts);
|
||||
FileSizeImpl getSizeImpl() const;
|
||||
void setSizeImpl(FileSizeImpl size);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
void setExecutableImpl(bool flag = true);
|
||||
void setWriteableImpl(bool flag = true);
|
||||
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();
|
||||
static void handleLastErrorImpl(const std::string& path);
|
||||
static void convertPath(const std::string& utf8Path, std::wstring& utf16Path);
|
||||
|
||||
|
||||
private:
|
||||
std::string _path;
|
||||
std::wstring _upath;
|
||||
|
||||
|
||||
friend class FileHandle;
|
||||
friend class DirectoryIteratorImpl;
|
||||
friend class FileStreamBuf;
|
||||
|
@ -256,6 +256,12 @@ void File::renameTo(const std::string& path)
|
||||
}
|
||||
|
||||
|
||||
void File::linkTo(const std::string& path, LinkType type) const
|
||||
{
|
||||
linkToImpl(path, type);
|
||||
}
|
||||
|
||||
|
||||
void File::remove(bool recursive)
|
||||
{
|
||||
if (recursive && !isLink() && isDirectory())
|
||||
|
@ -368,6 +368,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());
|
||||
|
@ -240,7 +240,7 @@ void FileImpl::copyToImpl(const std::string& path) const
|
||||
if (sd == -1) handleLastErrorImpl(_path);
|
||||
|
||||
struct stat st;
|
||||
if (fstat(sd, &st) != 0)
|
||||
if (fstat(sd, &st) != 0)
|
||||
{
|
||||
close(sd);
|
||||
handleLastErrorImpl(_path);
|
||||
@ -259,7 +259,7 @@ void FileImpl::copyToImpl(const std::string& path) const
|
||||
int n;
|
||||
while ((n = read(sd, buffer.begin(), blockSize)) > 0)
|
||||
{
|
||||
if (write(dd, buffer.begin(), n) != n)
|
||||
if (write(dd, buffer.begin(), n) != n)
|
||||
handleLastErrorImpl(path);
|
||||
}
|
||||
if (n < 0)
|
||||
@ -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());
|
||||
@ -301,7 +307,7 @@ void FileImpl::removeImpl()
|
||||
bool FileImpl::createFileImpl()
|
||||
{
|
||||
poco_assert (!_path.empty());
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
@ -322,7 +328,7 @@ bool FileImpl::createDirectoryImpl()
|
||||
|
||||
if (existsImpl() && isDirectoryImpl())
|
||||
return false;
|
||||
if (mkdir(_path.c_str()) != 0)
|
||||
if (mkdir(_path.c_str()) != 0)
|
||||
handleLastErrorImpl(_path);
|
||||
return true;
|
||||
}
|
||||
|
@ -163,10 +163,16 @@ bool FileImpl::isDirectoryImpl() const
|
||||
|
||||
bool FileImpl::isLinkImpl() const
|
||||
{
|
||||
return false;
|
||||
poco_assert (!_path.empty());
|
||||
|
||||
DWORD attr = GetFileAttributes(_upath.c_str());
|
||||
if (attr == INVALID_FILE_ATTRIBUTES)
|
||||
handleLastErrorImpl(_path);
|
||||
return (attr & FILE_ATTRIBUTE_DIRECTORY) == 0 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool FileImpl::isDeviceImpl() const
|
||||
{
|
||||
return
|
||||
@ -300,6 +306,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 (CreateHardLinkA(path.c_str(), _path.c_str(), NULL) == 0)
|
||||
handleLastErrorImpl(_path);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (CreateSymbolicLinkA(path.c_str(), _path.c_str(), (isDirectoryImpl() ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0) | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) == 0)
|
||||
handleLastErrorImpl(_path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FileImpl::removeImpl()
|
||||
{
|
||||
poco_assert (!_path.empty());
|
||||
|
@ -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,26 @@ 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 (CreateSymbolicLinkW(upath.c_str(), _upath.c_str(), (isDirectoryImpl() ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0) | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) == 0)
|
||||
handleLastErrorImpl(_path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FileImpl::removeImpl()
|
||||
{
|
||||
poco_assert (!_path.empty());
|
||||
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user