From ff04b72f62bc46f41268215baf27e1b3466a35ca Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 24 Oct 2022 14:08:32 +0200 Subject: [PATCH] smb: add mkdir/copy/rename/delete --- README.md | 3 +-- copyparty/bos/bos.py | 2 +- copyparty/smbd.py | 30 +++++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a1212c52..b7c12204 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ try the **[read-only demo server](https://a.ocv.me/pub/demo/)** 👀 running fro * [qr-code](#qr-code) - print a qr-code [(screenshot)](https://user-images.githubusercontent.com/241032/194728533-6f00849b-c6ac-43c6-9359-83e454d11e00.png) for quick access * [ftp server](#ftp-server) - an FTP server can be started using `--ftp 3921` * [webdav server](#webdav-server) - with read-write support - * [smb server](#smb-server) - unsafe, not recommended for wan + * [smb server](#smb-server) - unsafe, slow, not recommended for wan * [file indexing](#file-indexing) - enables dedup and music search ++ * [exclude-patterns](#exclude-patterns) - to save some time * [filesystem guards](#filesystem-guards) - avoid traversing into other filesystems @@ -755,7 +755,6 @@ some big warnings specific to SMB/CIFS, in decreasing importance: * shadowing (hiding the contents in subfolders by creating overlapping volumes) probably works as expected but no guarantees and some minor issues, -* files are not [indexed](#file-indexing) when uploaded through smb; please [schedule rescans](#periodic-rescan) as a workaround * hot-reload of server config (`/?reload=cfg`) only works for volumes, not account passwords * listens on the first `-i` interface only (default = 0.0.0.0 = all) * login doesn't work on winxp, but anonymous access is ok -- remove all accounts from copyparty config for that to work diff --git a/copyparty/bos/bos.py b/copyparty/bos/bos.py index 52263652..25087b5d 100644 --- a/copyparty/bos/bos.py +++ b/copyparty/bos/bos.py @@ -38,7 +38,7 @@ def mkdir(p: str, mode: int = 0o755) -> None: return os.mkdir(fsenc(p), mode) -def open(p: str, *a, **ka) -> Any: +def open(p: str, *a, **ka) -> int: return os.open(fsenc(p), *a, **ka) diff --git a/copyparty/smbd.py b/copyparty/smbd.py index ef805a47..41781b6d 100644 --- a/copyparty/smbd.py +++ b/copyparty/smbd.py @@ -75,8 +75,13 @@ class SMB(object): pass fos.close = self._close fos.listdir = self._listdir + fos.mkdir = self._mkdir fos.open = self._open + fos.remove = self._unlink + fos.rename = self._rename fos.stat = self._stat + fos.unlink = self._unlink + fos.utime = self._utime smbserver.os = fos # ...and smbserver.os.path @@ -160,7 +165,7 @@ class SMB(object): if not readonly: now = time.time() nf = len(self.files) - if nf > 10: + if nf > 9000: oldest = min([x[0] for x in self.files.values()]) cutoff = oldest + (now - oldest) / 2 self.files = {k: v for k, v in self.files.items() if v[0] > cutoff} @@ -188,9 +193,28 @@ class SMB(object): time.time(), ) + def _rename(self, vp1: str, vp2: str) -> None: + vp1 = vp1.lstrip("/") + vp2 = vp2.lstrip("/") + ap2 = self._v2a("rename", vp2, vp1)[1] + self.hub.up2k.handle_mv(LEELOO_DALLAS, vp1, vp2) + bos.makedirs(ap2, exist_ok=True) + + def _mkdir(self, vpath: str) -> None: + return bos.mkdir(self._v2a("mkdir", vpath)[1]) + def _stat(self, vpath: str, *a: Any, **ka: Any) -> os.stat_result: return bos.stat(self._v2a("stat", vpath, *a)[1], *a, **ka) + def _unlink(self, vpath: str) -> None: + # return bos.unlink(self._v2a("stat", vpath, *a)[1]) + logging.info("delete %s", vpath) + vp = vpath.lstrip("/") + self.hub.up2k.handle_rm(LEELOO_DALLAS, "1.7.6.2", [vp], []) + + def _utime(self, vpath: str, times: tuple[float, float]) -> None: + return bos.utime(self._v2a("stat", vpath)[1], times) + def _p_exists(self, vpath: str) -> bool: try: bos.stat(self._v2a("p.exists", vpath)[1]) @@ -223,14 +247,10 @@ class SMB(object): smbserver.os.lchown = self._hook smbserver.os.link = self._hook smbserver.os.lstat = self._hook - smbserver.os.mkdir = self._hook - smbserver.os.remove = self._hook - smbserver.os.rename = self._hook smbserver.os.replace = self._hook smbserver.os.scandir = self._hook smbserver.os.symlink = self._hook smbserver.os.truncate = self._hook - smbserver.os.unlink = self._hook smbserver.os.walk = self._hook smbserver.os.path.abspath = self._hook