-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopySymlink.cpp
49 lines (38 loc) · 1.14 KB
/
CopySymlink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "CopySymlink.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include "messmer/fspp/fuse/FuseErrnoException.h"
#include "CopyDevice.h"
#include <messmer/cpp-utils/assert/assert.h>
//TODO Get rid of this in favor of exception hierarchy
using fspp::fuse::CHECK_RETVAL;
namespace bf = boost::filesystem;
using std::string;
using std::vector;
namespace copyfs {
CopySymlink::CopySymlink(CopyDevice *device, const bf::path &path)
:CopyNode(device, path) {
ASSERT(bf::is_symlink(base_path()), "Path isn't valid symlink");
}
CopySymlink::~CopySymlink() {
}
bf::path CopySymlink::target() const {
int readBytes;
struct stat st;
while(true) {
CHECK_RETVAL(::lstat(base_path().c_str(), &st));
char buffer[st.st_size+1];
readBytes = ::readlink(base_path().c_str(), buffer, st.st_size+1);
buffer[st.st_size] = '\0';
if (readBytes == st.st_size) { //If readBytes > st.st_size, then the size of the symlink changed between the ::lstat and the ::readlink call
return buffer;
}
}
}
void CopySymlink::remove() {
int retval = ::unlink(base_path().c_str());
CHECK_RETVAL(retval);
}
}