-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystem.cpp
More file actions
143 lines (112 loc) · 4.21 KB
/
Filesystem.cpp
File metadata and controls
143 lines (112 loc) · 4.21 KB
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
#include "Filesystem.hpp"
#include <memory>
using namespace std;
Filesystem::Filesystem()
{
DBG(120, "Constructor");
}
Filesystem::~Filesystem()
{
DBG(120, "Destructor");
}
void Filesystem::initFiles()
{
_CompletePath = BasePath + Path + STATICFS_SUBDIR + "/";
DBG(120, "Filesystem Host:" << Hostname << " Path:" << _CompletePath);
for (const auto &Mimetype:Mimetypes) {
DBG(120, "Filesystem processing Mimetype:" << Mimetype);
}
for (const auto &Mimetype:Mimetypes) {
FilelistPlain_t TmpFiles;
FilesystemHelper::GetDirListingByFiletype(
TmpFiles,
_CompletePath,
"." + Mimetype
);
_Files.insert(
_Files.end(),
make_move_iterator(TmpFiles.begin()),
make_move_iterator(TmpFiles.end())
);
}
}
void Filesystem::processFileProperties()
{
for (const auto &File:_Files) {
const char* Filename = File.c_str();
struct stat Filestat;
int fd = open(Filename, O_RDONLY, 0440);
if (fd > 0) {
fstat(fd, &Filestat);
auto FileSize = Filestat.st_size;
if (FileSize > 300000) {
DBG(140, "Map File with MAP_HUGETLB set and madvise().");
auto FileMemPtr = mmap(NULL, FileSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, fd, 0);
madvise(FileMemPtr, FileSize, MADV_HUGEPAGE);
}
else {
mmap(NULL, FileSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, fd, 0);
}
size_t FindPos = File.rfind("/");
string FileName = File.substr(FindPos+1, File.length()-FindPos);
string FilePath = File.substr(0, FindPos);
FindPos = FileName.rfind(".");
string FileExtension = FileName.substr(FindPos+1, File.length()-FindPos);
//- safely get mime type
auto mimeIter = MimeRelations.find(FileExtension);
string MimeType = (mimeIter != MimeRelations.end()) ? mimeIter->second : "application/octet-stream";
string ReplacePath = BasePath + Path + "/static";
string RelPath = FilePath.substr(ReplacePath.length(), FilePath.length()-ReplacePath.length());
string FileListKey = RelPath + "/" + FileName;
DBG(210, "FilePath:" << FilePath << " ReplacePath:" << ReplacePath << " FileListKey:" << FileListKey);
DBG(210, "FileName:" << FileName << " FD:" << fd << " RelPath:" << RelPath << " Extension:" << FileExtension << " Mimetype:" << MimeType);
FileProperties_t FileProps;
FileProps.Filedescriptor = fd;
FileProps.FileSize = FileSize;
FileProps.FileName = FileName;
FileProps.FileExtension = FileExtension;
FileProps.MimeType = MimeType;
FileProps.ETag = this->getFileEtag(File);
_FilesExtended.emplace(
FileListKey, FileProps
);
}
}
}
bool Filesystem::checkFileExists(const string &File)
{
if (_FilesExtended.find(File) == _FilesExtended.end()) {
return false;
}
return true;
}
FileProperties_t Filesystem::getFilePropertiesByFile(const string &File)
{
auto iter = _FilesExtended.find(File);
if (iter != _FilesExtended.end()) {
return iter->second;
}
//- return empty FileProperties if not found (should not happen after checkFileExists)
ERR("File not found in _FilesExtended map: " << File);
return FileProperties_t{};
}
string Filesystem::getFileEtag(const string &File) {
streampos FileSize;
size_t FileHashInt = 0;
try {
std::ifstream FStream(File, ios::in | ios::binary | ios::ate);
FileSize = FStream.tellg();
auto FileBuffer = std::make_unique<char[]>(FileSize);
FStream.seekg (0, ios::beg);
FStream.read (FileBuffer.get(), FileSize);
FStream.close();
FileHashInt = hash<string>{}(string(FileBuffer.get(), FileSize));
}
catch(const std::exception& e) {
ERR("Etag generation error: " << e.what());
exit(1);
}
stringstream FileHash;
FileHash << std::hex << FileHashInt;
return FileHash.str();
}