Skip to content

Commit

Permalink
fuse.py prefers ?ls if available
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Feb 21, 2021
1 parent 2d2e8a3 commit 5e3775c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
39 changes: 34 additions & 5 deletions bin/copyparty-fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import os
import sys
import time
import json
import stat
import errno
import struct
Expand Down Expand Up @@ -323,7 +324,7 @@ def listdir(self, path):
if bad_good:
path = dewin(path)

web_path = self.quotep("/" + "/".join([self.web_root, path])) + "?dots"
web_path = self.quotep("/" + "/".join([self.web_root, path])) + "?dots&ls"
r = self.sendreq("GET", web_path)
if r.status != 200:
self.closeconn()
Expand All @@ -334,12 +335,17 @@ def listdir(self, path):
)
raise FuseOSError(errno.ENOENT)

if not r.getheader("Content-Type", "").startswith("text/html"):
ctype = r.getheader("Content-Type", "")
if ctype == "application/json":
parser = self.parse_jls
elif ctype.startswith("text/html"):
parser = self.parse_html
else:
log("listdir on file: {}".format(path))
raise FuseOSError(errno.ENOENT)

try:
return self.parse_html(r)
return parser(r)
except:
info(repr(path) + "\n" + traceback.format_exc())
raise
Expand Down Expand Up @@ -367,6 +373,29 @@ def download_file_range(self, path, ofs1, ofs2):

return r.read()

def parse_jls(self, datasrc):
rsp = b""
while True:
buf = datasrc.read(1024 * 32)
if not buf:
break

rsp += buf

rsp = json.loads(rsp.decode("utf-8"))
ret = []
for is_dir, nodes in [[True, rsp["dirs"]], [False, rsp["files"]]]:
for n in nodes:
fname = unquote(n["href"]).rstrip(b"/")
fname = fname.decode("wtf-8")
if bad_good:
fname = enwin(fname)

fun = self.stat_dir if is_dir else self.stat_file
ret.append([fname, fun(n["ts"], n["sz"]), 0])

return ret

def parse_html(self, datasrc):
ret = []
remainder = b""
Expand Down Expand Up @@ -818,9 +847,9 @@ def getattr(self, path, fh=None):
return cache_stat

fun = info
if MACOS and path.split('/')[-1].startswith('._'):
if MACOS and path.split("/")[-1].startswith("._"):
fun = dbg

fun("=ENOENT ({})".format(hexler(path)))
raise FuseOSError(errno.ENOENT)

Expand Down
4 changes: 2 additions & 2 deletions copyparty/httpcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ def tx_tree(self):

ret = self.gen_tree(top, dst)
ret = json.dumps(ret)
self.reply(ret.encode("utf-8"))
self.reply(ret.encode("utf-8"), mime="application/json")
return True

def gen_tree(self, top, target):
Expand Down Expand Up @@ -1270,7 +1270,7 @@ def tx_browser(self):
if is_ls:
[x.pop("name") for y in [dirs, files] for x in y]
ret = json.dumps({"dirs": dirs, "files": files, "srvinf": srv_info})
self.reply(ret.encode("utf-8", "replace"))
self.reply(ret.encode("utf-8", "replace"), mime="application/json")
return True

logues = [None, None]
Expand Down

0 comments on commit 5e3775c

Please sign in to comment.