-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsfs.cpp
More file actions
81 lines (66 loc) · 2.06 KB
/
httpsfs.cpp
File metadata and controls
81 lines (66 loc) · 2.06 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
#include <string>
#include <filesystem>
#include <fstream>
// #include <iostream>
#include <cassert>
#include "errno.h"
#define FUSE_USE_VERSION 35
#include <fuse3/fuse.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Infos.hpp>
namespace ivl::httpsfs {
std::string prefix;
void load_to_cache(const char* path) try {
auto cachepath = prefix + path;
if (std::filesystem::exists(cachepath))
return;
if (std::filesystem::path(cachepath).extension().empty()){
std::filesystem::create_directory(cachepath);
return;
}
cURLpp::Easy handle;
handle.setOpt(cURLpp::Options::Url(std::string("https:/") + path));
std::ofstream os(cachepath);
cURLpp::options::WriteStream ws(&os);
handle.setOpt(ws);
// handle.setOpt(cURLpp::Options::Verbose(true));
handle.perform();
os.close();
if (cURLpp::infos::ResponseCode::get(handle) != 200){
std::filesystem::remove(cachepath);
std::filesystem::create_directory(cachepath);
}
} catch(...){
auto cachepath = prefix + path;
if (std::filesystem::exists(cachepath)){
std::filesystem::remove(cachepath);
std::filesystem::create_directory(cachepath);
}
}
int getattr(const char *path, struct stat *st, struct fuse_file_info *fi){
load_to_cache(path);
auto realpath = prefix + path;
auto retvalue = ::stat(realpath.c_str(), st);
return retvalue;
}
int read(const char *path, char *buff, size_t size, off_t offset,
struct fuse_file_info *){
auto realpath = prefix + path;
std::ifstream is(realpath);
is.seekg(offset);
is.read(buff, size);
return size;
}
static fuse_operations ops = {
.getattr = getattr,
.read = read,
};
} // namespace ivl::httpsfs
int main(int argc, char* argv[]){
ivl::httpsfs::prefix = std::filesystem::canonical(argv[0]).parent_path() / "cache";
assert(std::filesystem::is_directory(ivl::httpsfs::prefix));
cURLpp::initialize();
return fuse_main(argc, argv, &ivl::httpsfs::ops, nullptr);
}