diff --git a/main.py b/main.py index ba5c673..12e032b 100755 --- a/main.py +++ b/main.py @@ -12,12 +12,14 @@ releases = [] assets = {} headers = {"Authorization": "token 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", "Accept": "application/json"} +user = "DomiOwO" repo = "iwisoftware" -files=[{"fd":0}] +files=[{}] +files_buffers=[b""] def getReleases(): if not releases: - a=requests.get("https://api.github.com/repos/DomiOwO/" + repo + "/releases", headers = headers); + a=requests.get("https://api.github.com/repos/"+ user +"/" + repo + "/releases", headers = headers); x=a.json() for i in x: releases.extend([{"name":i["tag_name"], "id":i["id"]}]) @@ -28,22 +30,14 @@ def getReleases(): print(assets) def allocateFd(path): - global files - fd = files[-1]["fd"] + 1 - files += [{"fd": fd, - "path": path, - "buffer": b"", + global files, files_buffers + fd = len(files) + files += [{"path": path, "state": None # 0 - read, 1 - write }] + files_buffers += [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 @@ -56,27 +50,20 @@ class Passthrough(Operations): 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 + if len(full_path.split('/')) < 3: if full_path == 'b/': st_mode = 0o40744 st_size = 1337 @@ -86,8 +73,6 @@ class Passthrough(Operations): st_size = 1337 else: raise FuseOSError(2) - st_mode = 0o700000 - st_size = 0 else: st_mode = 0o100744 print(full_path.split('/')) @@ -147,21 +132,30 @@ class Passthrough(Operations): def rmdir(self, path): print("-- rmdir") + _h = headers + _h["Accept"] = "application/json" full_path = self._full_path(path) - for r in releases: - if r["name"] == path.split("/")[1]: - a = requests.delete("https://api.github.com/repos/DomiOwO/" + repo + "/releases/" + str(r["id"]), - headers=headers) + for i in range(len(releases)-1): + print("asdf: ", i) + print("asdf: ", releases[i]) + if releases[i]["name"] == path.split("/")[1]: + a = requests.delete("https://api.github.com/repos/" + user + "/" + repo + "/releases/" + str(releases[i]["id"]), + headers = _h) + assets[releases[i]["name"]] = {} + del(releases[i]) print(a.content) return 0 def mkdir(self, path, mode): print("-- mkdir", path) - #return os.mkdir(self._full_path(path), mode) - a = requests.post("https://api.github.com/repos/DomiOwO/" + repo + "/releases", + global releases + a = requests.post("https://api.github.com/repos/" + user + "/" + repo + "/releases", headers = headers, data = '{"tag_name":"'+path.split("/")[1]+'"}') print(a.content) + x = a.json() + releases+=[{"name":path.split("/")[1], "id": x["id"]}] + assets[path.split('/')[-2]]={} return 0 def statfs(self, path): @@ -177,7 +171,7 @@ class Passthrough(Operations): global assets _h = headers _h["Accept"] = "application/json" - a = requests.delete("https://api.github.com/repos/DomiOwO/" + repo + "/releases/assets/" + str(assets[path.split("/")[-2]][path.split("/")[-1]]["id"]), + a = requests.delete("https://api.github.com/repos/" + user + "/" + repo + "/releases/assets/" + str(assets[path.split("/")[-2]][path.split("/")[-1]]["id"]), headers = _h) print(a.content) del(assets[path.split('/')[-2]][path.split('/')[-1]]) @@ -185,24 +179,24 @@ class Passthrough(Operations): def symlink(self, name, target): print("-- symlink") - return os.symlink(target, self._full_path(name)) + #return os.symlink(target, self._full_path(name)) + return -1 def rename(self, old, new): print("-- rename") - return os.rename(self._full_path(old), self._full_path(new)) + #return os.rename(self._full_path(old), self._full_path(new)) + return -1 def link(self, target, name): print("-- link") - return os.link(self._full_path(name), self._full_path(target)) + #return os.link(self._full_path(name), self._full_path(target)) + return -1 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) @@ -212,25 +206,29 @@ class Passthrough(Operations): return allocateFd(path) def read(self, path, length, offset, fh): - f = fileByFd(fh) + f = files[h] f["state"] = 0 print("-- read: offset ", offset, " len ", length, " path ", path) - if offset + length > len(f["buffer"]): - print("buffer exhausted: ", len(f["buffer"])) + if offset == 0: + fetch_len = 16384 # small buffer arrives like wroom + else: + fetch_len = 10485760 # big buffer goes whoosh + if offset + length > len(files_buffers[fh]): + print("buffer exhausted: ", len(files_buffers[fh])) _h = headers - _h["Range"] = "bytes=" + str(len(f["buffer"])) + "-" + str(len(f["buffer"]) + 10485760) # 5MB + _h["Range"] = "bytes=" + str(len(files_buffers[fh])) + "-" + str(len(files_buffers[fh]) + fetch_len) _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"]), + a = requests.get("https://api.github.com/repos/" + user + "/" + repo + "/releases/assets/" + str(assets[path.split("/")[-2]][path.split("/")[-1]]["id"]), headers = _h); - f["buffer"] += a.content - return f["buffer"][offset:offset+length] + files_buffers[fh] += a.content + return files_buffers[fh][offset:offset+length] def write(self, path, buf, offset, fh): - print("-- write: offset ", offset, " buf ", buf, " path ", path) - f = fileByFd(fh) + #print("-- write: offset ", offset, " path ", path) + f = files[fh] f["state"] = 1 - f["buffer"] += buf + files_buffers[fh] += buf return len(buf) def truncate(self, path, length, fh=None): @@ -243,7 +241,7 @@ class Passthrough(Operations): def release(self, path, fh): print("-- release/close") - f = fileByFd(fh) + f = files[fh] if(f["state"] == 1): _h = headers _h["Content-Type"] = "application/octet-stream" @@ -253,14 +251,18 @@ class Passthrough(Operations): break a = requests.post( - "https://uploads.github.com/repos/DomiOwO/" + repo + "/releases/" + str(id) + "/assets?name=" + + "https://uploads.github.com/repos/" + user + "/" + repo + "/releases/" + str(id) + "/assets?name=" + path.split('/')[-1], - data = f["buffer"], + data = files_buffers[fh], headers = _h) print(a.content) res = a.json() + print(assets[path.split('/')[-2]]) + if not assets[path.split('/')[-2]]: + assets[path.split('/')[-2]]={} assets[path.split('/')[-2]][path.split('/')[-1]] = {"id": res["id"], "size": res["size"]} - del (files[fileByFd(fh)["fd"]]) # idk, bad syntax? + del(files[fh]) # idk, bad syntax? + del(files_buffers[fh]) return 0 def fsync(self, path, fdatasync, fh):