-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsourcefile.cpp
151 lines (126 loc) · 3.74 KB
/
sourcefile.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "sourcefile.h"
#include "source.h"
#include "serialize.h"
#include "util/filelocker.h"
#include "util/pathtools.h"
#include <sys/stat.h>
#include <sys/file.h>
#include <utime.h>
#include <unistd.h>
#include <stdexcept>
#include <iostream>
using namespace std;
SourceFile::SourceFile(const Source* parent, const string& path)
: pathHashReady{false}, parent{parent}, path{path}
{
struct stat buf;
if (stat((parent->getPath()+'/'+path).c_str(), &buf) < 0)
throw runtime_error("SourceFile: Failed to stat "+path);
attrs.mtime = (uint64_t)buf.st_mtim.tv_sec;
attrs.userId = buf.st_uid;
attrs.groupId = buf.st_gid;
attrs.mode = buf.st_mode;
rawSize = buf.st_size;
}
SourceFile::SourceFile(const Source* parent, string &&path)
: pathHashReady{false}, parent{parent}, path{move(path)}
{
struct stat buf;
if (stat((parent->getPath()+'/'+this->path).c_str(), &buf) < 0)
throw runtime_error("SourceFile: Failed to stat "+this->path);
attrs.mtime = (uint64_t)buf.st_mtim.tv_sec;
attrs.userId = buf.st_uid;
attrs.groupId = buf.st_gid;
attrs.mode = buf.st_mode;
rawSize = buf.st_size;
}
SourceFile::SourceFile(const Source* parent, string &&path, const char* fullpath)
: pathHashReady{false}, parent{parent}, path{move(path)}
{
struct stat buf;
if (stat(fullpath, &buf) < 0)
throw runtime_error(string("SourceFile: Failed to stat ")+fullpath);
attrs.mtime = (uint64_t)buf.st_mtim.tv_sec;
attrs.userId = buf.st_uid;
attrs.groupId = buf.st_gid;
attrs.mode = buf.st_mode;
rawSize = buf.st_size;
}
SourceFile::SourceFile(const Source* parent, const std::vector<char> &metadata,
uint64_t mtime, const std::vector<char> &data)
: pathHashReady{false}, parent{parent}
{
auto mit = metadata.begin();
path = ::deserializeConsume<decltype(path)>(mit);
attrs.userId = ::deserializeConsume<decltype(attrs.userId)>(mit);
attrs.groupId = ::deserializeConsume<decltype(attrs.groupId)>(mit);
attrs.mode = ::deserializeConsume<decltype(attrs.mode)>(mit);
attrs.mtime = mtime;
rawSize = data.size();
string fullPath = parent->getPath()+'/'+path;
createPathTo("/", fullPath);
{
FileLocker file{fullPath};
file.overwrite(data);
}
applyAttrs();
}
uint64_t SourceFile::getRawSize() const
{
return rawSize;
}
FileAttr SourceFile::getAttrs() const
{
return attrs;
}
string SourceFile::getPath() const
{
return path;
}
PathHash SourceFile::getPathHash() const
{
if (!pathHashReady)
{
pathHash.rehash(path);
pathHashReady = true;
}
return pathHash;
}
std::vector<char> SourceFile::readAll() const
{
string fullpath = parent->getPath()+"/"+path;
int fd = open(fullpath.c_str(), O_RDONLY);
if (fd < 0)
throw runtime_error("SourceFile::readAll: Unabled to open "+fullpath);
size_t size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
vector<char> data(size);
auto r = read(fd, data.data(), size);
if (r<0)
data.clear();
close(fd);
return data;
}
std::vector<char> SourceFile::serializeMetadata() const
{
std::vector<char> data;
::serializeAppend(data, path);
::serializeAppend(data, attrs.userId);
::serializeAppend(data, attrs.groupId);
::serializeAppend(data, attrs.mode);
return data;
}
bool SourceFile::operator <(const SourceFile &other) const
{
return getPathHash() < other.getPathHash();
}
void SourceFile::applyAttrs()
{
string fullPath = parent->getPath()+'/'+path;
chmod(fullPath.c_str(), attrs.mode);
chown(fullPath.c_str(), attrs.userId, attrs.groupId);
utimbuf times;
times.actime = time(nullptr);
times.modtime = attrs.mtime;
utime(fullPath.c_str(), ×);
}