Commit 3f29f38b authored by Hugo Hörnquist's avatar Hugo Hörnquist

Reading now works.

parent c51ceff4
LIBS = $(shell pkg-config --libs libcurl fuse) LIBS = $(shell pkg-config --libs libcurl fuse)
CFLAGS = $(shell pkg-config --cflags libcurl fuse) -ggdb \ CFLAGS = $(shell pkg-config --cflags libcurl fuse) -ggdb \
-std=c++2a -Wall -Wextra -pedantic \ -std=c++2a -Wall -Wextra -pedantic
-D_FILE_OFFSET_BITS=64
all: CPP_FILES = $(wildcard *.cpp)
g++ $(LIBS) $(CFLAGS) -o mfs mediawikifs.cpp O_FILES = $(CPP_FILES:%.cpp=obj/%.o)
$(shell mkdir -p obj/)
obj/%.o: %.cpp
$(CXX) -c $(CFLAGS) -o $@ $<
mfs: $(O_FILES)
$(CXX) -o $@ $^ $(LIBS)
...@@ -18,15 +18,17 @@ static std::string str; ...@@ -18,15 +18,17 @@ static std::string str;
static CURL* curl; static CURL* curl;
static std::map<std::string, page> pages;
page* get_page(const char* path) { page* get_page(const char* path) {
static std::map<const char*, page> pages;
page* p = & pages[path]; page* p = & pages[path];
if (p->init) return p; if (p->init) return p;
str = ""; str = "";
CURLcode code; CURLcode code;
code = curl_easy_setopt (curl, CURLOPT_URL, url); std::string urlstring = url;
urlstring += path;
code = curl_easy_setopt (curl, CURLOPT_URL, urlstring.c_str());
if (code != CURLE_OK) ERR("Set url"); if (code != CURLE_OK) ERR("Set url");
code = curl_easy_perform (curl); code = curl_easy_perform (curl);
if (code != CURLE_OK) ERR("Perform %i", code); if (code != CURLE_OK) ERR("Perform %i", code);
...@@ -47,14 +49,18 @@ size_t handle_data (void* buffer, size_t, size_t nmemb, void*) { ...@@ -47,14 +49,18 @@ size_t handle_data (void* buffer, size_t, size_t nmemb, void*) {
int getattr_callback(const char* path, struct stat* st) { int getattr_callback(const char* path, struct stat* st) {
if (strcmp(path, "/") == 0) { if (strcmp(path, "/") == 0) {
// TODO get size of map
return 2; st->st_mode = S_IFDIR | 0555;
st->st_size = pages.size();
} else {
page* p = get_page(path + 1);
st->st_mode = S_IFREG | 0444;
st->st_size = p->size;
// st->st_mtime
} }
page* p = get_page(path);
st->st_mode = 0444;
st->st_size = p->size;
// st->st_mtime
return 0; return 0;
} }
...@@ -69,7 +75,7 @@ int read_callback ( ...@@ -69,7 +75,7 @@ int read_callback (
off_t offset, off_t offset,
struct fuse_file_info* /*f*/) struct fuse_file_info* /*f*/)
{ {
page* p = get_page (path); page* p = get_page (path + 1);
std::string* s = & p->data; std::string* s = & p->data;
ssize_t len = s->size(); ssize_t len = s->size();
...@@ -77,12 +83,13 @@ int read_callback ( ...@@ -77,12 +83,13 @@ int read_callback (
if (offset >= len) return 0; if (offset >= len) return 0;
if (offset + (off_t) size > len) { if (offset + (off_t) size > len) {
memcpy (buf, s->c_str() + offset, len - offset); size = len - offset;
return len - offset;
} }
memcpy (buf, s->c_str() + offset, size); *s = s->substr(offset, offset + size);
return size;
memcpy (buf, s->c_str(), size);
return s->size();
} }
int readdir_callback ( int readdir_callback (
...@@ -93,17 +100,24 @@ int readdir_callback ( ...@@ -93,17 +100,24 @@ int readdir_callback (
struct fuse_file_info* /*f*/) struct fuse_file_info* /*f*/)
{ {
if (strcmp(path, "/") != 0) { if (strcmp(path, "/") != 0) {
return 1; return -ENOMEM;
} }
filler(buf, ".", NULL, 0); filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0); filler(buf, "..", NULL, 0);
filler(buf, "Tottaly a file", NULL, 0); // filler(buf, "Tottaly a file", NULL, 0);
for (auto kv : pages) {
filler(buf, kv.first.c_str(), NULL, 0);
}
return 0; return 0;
} }
int main (int argc, char* argv[]) { /*
* Calls when file system gets mounted
*/
void* init_callback (struct fuse_conn_info* /*conn*/) {
CURLcode code; CURLcode code;
code = curl_global_init (CURL_GLOBAL_DEFAULT); code = curl_global_init (CURL_GLOBAL_DEFAULT);
if (code != CURLE_OK) ERR("Global init"); if (code != CURLE_OK) ERR("Global init");
...@@ -114,17 +128,25 @@ int main (int argc, char* argv[]) { ...@@ -114,17 +128,25 @@ int main (int argc, char* argv[]) {
code = curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, handle_data); code = curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, handle_data);
if (code != CURLE_OK) ERR("Write data"); if (code != CURLE_OK) ERR("Write data");
return NULL;
}
/*
* Called on filesystem exit
*/
void destroy_callback (void*) {
curl_easy_cleanup (curl);
}
struct fuse_operations fops; struct fuse_operations fops;
int main (int argc, char* argv[]) {
fops.getattr = getattr_callback; fops.getattr = getattr_callback;
fops.open = open_callback; fops.open = open_callback;
fops.read = read_callback; fops.read = read_callback;
fops.readdir = readdir_callback; fops.readdir = readdir_callback;
fops.init = init_callback;
// page* p = get_page("Bishibosh"); fops.destroy = destroy_callback;
// std::cout << p->data << std::endl;
return fuse_main (argc, argv, &fops, NULL); return fuse_main (argc, argv, &fops, NULL);
// puts(str.c_str());
// curl_easy_cleanup (curl);
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <fuse.h> #include <fuse.h>
#define url "https://datorhandbok.lysator.liu.se/index.php/Special:Exportera/Bishibosh" #define url "https://datorhandbok.lysator.liu.se/index.php/Special:Exportera/"
#define ERR(s, ...) fprintf(stderr, "\x1b[0;31mERR\x1b[m " s "\n", ## __VA_ARGS__) #define ERR(s, ...) fprintf(stderr, "\x1b[0;31mERR\x1b[m " s "\n", ## __VA_ARGS__)
......
...@@ -35,7 +35,7 @@ class Page: ...@@ -35,7 +35,7 @@ class Page:
self.timestamp = time.strptime(timestr, "%Y-%m-%dT%H:%M:%Sz") self.timestamp = time.strptime(timestr, "%Y-%m-%dT%H:%M:%Sz")
text = revision.find("mw:text", ns) text = revision.find("mw:text", ns)
self.text = text.text self.text = bytes(text.text, "utf-8")
# TODO get propper length, something with character encoding, posibly # TODO get propper length, something with character encoding, posibly
self.len = int(text.get("bytes") or len(self.text)) self.len = int(text.get("bytes") or len(self.text))
# TODO check hash # TODO check hash
...@@ -53,7 +53,7 @@ def get_page(name): ...@@ -53,7 +53,7 @@ def get_page(name):
info = request.info() info = request.info()
s = info.get("content-type") s = info.get("content-type")
charset = info.get_charset() or s[len("charset=") + s.find("charset="):] charset = info.get_charset() or s[len("charset=") + s.find("charset="):]
str = request.read().decode(charset) str = request.read()
pages[name] = p = Page(str) pages[name] = p = Page(str)
return p return p
...@@ -107,7 +107,7 @@ class MediawikiFS(fuse.Fuse): ...@@ -107,7 +107,7 @@ class MediawikiFS(fuse.Fuse):
buf = p.text[offset:offset+size] buf = p.text[offset:offset+size]
else: else:
buf = '' buf = ''
return buf return str(buf, "utf-8")
def main(): def main():
usage=""" usage="""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment