-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharchivefile.cpp
90 lines (76 loc) · 2.53 KB
/
archivefile.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
#include "archivefile.h"
#include "archive.h"
#include "util/filelocker.h"
#include "util/pathtools.h"
#include "serialize.h"
using namespace std;
ArchiveFile::ArchiveFile(const Archive *parent, PathHash pathHash, uint64_t mtime, const std::vector<char> &data)
: pathHash{pathHash}, mtime{mtime}, actualSize{data.size()}, parent{parent}
{
overwrite(mtime, data);
}
ArchiveFile::ArchiveFile(const Archive *parent, vector<char>::const_iterator &serializedData)
: parent{parent}
{
pathHash = ::deserializeConsume<decltype(pathHash)>(serializedData);
mtime = ::deserializeConsume<decltype(mtime)>(serializedData);
actualSize = ::deserializeConsume<decltype(actualSize)>(serializedData);
}
PathHash ArchiveFile::getPathHash() const
{
return pathHash;
}
uint64_t ArchiveFile::getMtime() const
{
return mtime;
}
uint64_t ArchiveFile::getActualSize() const
{
return actualSize;
}
std::vector<char> ArchiveFile::read(uint64_t startPos, uint64_t size) const
{
string pathHashStr = pathHash.toBase64();
string fullPath = parent->getFolderDataPath()+'/'+pathHashStr.substr(0,2)+'/'+pathHashStr.substr(2);
FileLocker file{fullPath};
return file.read(startPos, size);
}
std::vector<char> ArchiveFile::readMetadata() const
{
std::vector<char> meta;
std::vector<char> msizeData = read(0, sizeof(size_t));
if (msizeData.size() < sizeof(size_t))
return meta;
auto mit = msizeData.cbegin();
size_t msize = ::dataToVUint(mit);
size_t msizeSize = ::getVUint32Size(msizeData);
meta = read(msizeSize, msize);
return meta;
}
std::vector<char> ArchiveFile::readAll() const
{
string pathHashStr = pathHash.toBase64();
string fullPath = parent->getFolderDataPath()+'/'+pathHashStr.substr(0,2)+'/'+pathHashStr.substr(2);
FileLocker file{fullPath};
return file.readAll();
}
void ArchiveFile::overwrite(uint64_t _mtime, const std::vector<char> &data)
{
mtime = _mtime;
actualSize = data.size();
string pathHashStr = pathHash.toBase64();
string fullPath = parent->getFolderDataPath()+'/'+pathHashStr.substr(0,2)+'/'+pathHashStr.substr(2);
createPathTo(parent->getFolderDataPath(), pathHashStr.substr(0,2)+'/'+pathHashStr.substr(2));
FileLocker file{fullPath};
file.overwrite(data);
}
void ArchiveFile::serializeInto(std::vector<char> &dest) const
{
pathHash.serializeInto(dest);
::uint64ToData(dest, mtime);
::uint64ToData(dest, actualSize);
}
string ArchiveFile::deserializePath(std::vector<char>::const_iterator& meta)
{
return ::dataToString(meta);
}