-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesystem.cpp
100 lines (75 loc) · 2.9 KB
/
Filesystem.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
#include "Filesystem.hpp"
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);
string MimeType = MimeRelations.at(FileExtension);
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;
_FilesExtended.insert(
FileListExtendedPair_t(FileListKey, FileProps)
);
}
}
}
FileProperties_t Filesystem::getFilePropertiesByFile(string File)
{
return _FilesExtended.at(File);
}