grfs/main.py

237 lines
7.2 KiB
Python
Executable File

#!/usr/bin/env python3
import requests
import random
import os
import sys
import errno
from fuse import FUSE, FuseOSError, Operations, fuse_get_context
releases = []
assets = {}
headers = {"Authorization":"token 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"}
repo = "iwisoftware"
files=[{"fd":0}]
def getReleases():
if not releases:
a=requests.get("https://api.github.com/repos/DomiOwO/" + repo + "/releases", headers=headers);
x=a.json()
for i in x:
releases.extend([{"name":i["tag_name"], "id":i["id"]}])
for j in i["assets"]:
assets[i["tag_name"]] = {} # change this later
for j in i["assets"]:
assets[i["tag_name"]][j["name"]]={"id": j["id"], "size": j["size"]}
print(assets)
def allocateFd(path):
global files
fd = files[-1]["fd"] + 1
files += [{"fd": fd, "path": path, "buffer": b""}]
return fd
def fileByFd(fd):
global files
for i in files:
if i["fd"] == fd:
return i
return -1
class Passthrough(Operations):
def __init__(self, root):
self.root = root
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
def access(self, path, mode):
print("-- access")
#full_path = self._full_path(path)
#if not os.access(full_path, mode):
# raise FuseOSError(errno.EACCES)
def chmod(self, path, mode):
print("-- chmod")
#full_path = self._full_path(path)
#return os.chmod(full_path, mode)
return 0
def chown(self, path, uid, gid):
print("-- chown")
#full_path = self._full_path(path)
#return os.chown(full_path, uid, gid)
return 0
def getattr(self, path, fh=None):
full_path = self._full_path(path)
print("-- getattr", full_path)
if full_path.split('/')[-2] in assets or len(full_path.split('/')) == 2:
if len(full_path.split('/')) < 3: # TODO: making new releases
st_mode = 0o40744
st_size = 1337
else:
st_mode = 0o100744
print(full_path.split('/'))
print(assets[full_path.split('/')[-2]])
if full_path.split('/')[-1] in assets[full_path.split('/')[-2]]:
st_size = assets[full_path.split('/')[-2]][full_path.split('/')[-1]]["size"]
else:
st_mode = 0o100000
st_size = 0
else:
st_mode = 0o100000
st_size = 0
print(st_mode, st_size)
return {'st_atime': 1,
'st_ctime': 1,
'st_gid': 1000,
'st_mode': int(st_mode),
'st_mtime': 1,
'st_nlink': 16,
'st_size': st_size,
'st_uid': 1000}
def readdir(self, path, fh):
print("-- readdir")
full_path = self._full_path(path)
dirents = ['.', '..']
print(full_path.split('/'))
if len(full_path.split('/')) == 2 and full_path.endswith('/'):
for i in releases:
print(i)
dirents.extend([i["name"]])
else:
if full_path.endswith('/'):
p=full_path.split('/')[-2]
else:
p=full_path.split('/')[-1]
if p in assets:
for i in assets[p]:
dirents.extend([i])
for r in dirents:
yield r
def readlink(self, path):
print("-- readlink")
pathname = os.readlink(self._full_path(path))
if pathname.startswith("/"):
# Path name is absolute, sanitize it.
return os.path.relpath(pathname, self.root)
else:
return pathname
def mknod(self, path, mode, dev):
print("-- mknod")
return os.mknod(self._full_path(path), mode, dev)
def rmdir(self, path):
print("-- rmdir")
full_path = self._full_path(path)
return os.rmdir(full_path)
def mkdir(self, path, mode):
print("-- mkdir")
return os.mkdir(self._full_path(path), mode)
def statfs(self, path):
full_path = self._full_path(path)
stv = os.statvfs(full_path)
print("-- statfs")
return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
'f_frsize', 'f_namemax'))
def unlink(self, path):
print("-- unlink")
return os.unlink(self._full_path(path))
def symlink(self, name, target):
print("-- symlink")
return os.symlink(target, self._full_path(name))
def rename(self, old, new):
print("-- rename")
return os.rename(self._full_path(old), self._full_path(new))
def link(self, target, name):
print("-- link")
return os.link(self._full_path(name), self._full_path(target))
def utimens(self, path, times=None):
print("-- utimens")
#return os.utime(self._full_path(path), times)
return 0
# File methods
# ============
def open(self, path, flags):
print("-- open")
return allocateFd(path)
def create(self, path, mode, fi=None):
print("-- create")
return allocateFd(path)
def read(self, path, length, offset, fh):
f = fileByFd(fh)
print("-- read: offset ", offset, " len ", length, " path ", path)
if offset + length > len(f["buffer"]):
print("buffer exhausted: ", len(f["buffer"]))
_h = headers
_h["Range"] = "bytes=" + str(len(f["buffer"])) + "-" + str(len(f["buffer"]) + 10485760) # 5MB
_h["Accept"] = "application/octet-stream"
a = requests.get("https://api.github.com/repos/DomiOwO/" + repo + "/releases/assets/" + str(assets[path.split("/")[-2]][path.split("/")[-1]]["id"]),
headers = _h);
f["buffer"] += a.content
return f["buffer"][offset:offset+length]
def write(self, path, buf, offset, fh):
print("-- write: offset ", offset, " buf ", buf, " path ", path)
_h = headers
_h["Content-Type"] = "application/octet-stream"
for i in releases:
if i["name"] == path.split('/')[-2]:
id = i["id"]
break
a = requests.post("https://uploads.github.com/repos/DomiOwO/" + repo + "/releases/" + str(id) + "/assets?name=" + path.split('/')[-1],
data = buf,
headers = _h)
print(a.content)
return 0
def truncate(self, path, length, fh=None):
print("-- truncate")
return 0
def flush(self, path, fh):
print("-- flush")
return 0
def release(self, path, fh):
print("-- release/close")
#del(files[fileByFd(fh)])
def fsync(self, path, fdatasync, fh):
print("-- fsync")
return 0
def main(mountpoint):
root='b'
getReleases()
FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True, allow_other=True)
if __name__ == '__main__':
main(sys.argv[1])