From 4b7965413a170f808221a8407f1a63bb3a36cc65 Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Mon, 12 Aug 2024 21:07:26 +0545 Subject: [PATCH] localfs: reduce stats calls during info (#1659) --- fsspec/implementations/local.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fsspec/implementations/local.py b/fsspec/implementations/local.py index 9881606f1..f032d8aeb 100644 --- a/fsspec/implementations/local.py +++ b/fsspec/implementations/local.py @@ -79,6 +79,14 @@ def info(self, path, **kwargs): t = "file" else: t = "other" + + size = out.st_size + if link: + try: + out2 = path.stat(follow_symlinks=True) + size = out2.st_size + except OSError: + size = 0 path = self._strip_protocol(path.path) else: # str or path-like @@ -87,6 +95,7 @@ def info(self, path, **kwargs): link = stat.S_ISLNK(out.st_mode) if link: out = os.stat(path, follow_symlinks=True) + size = out.st_size if stat.S_ISDIR(out.st_mode): t = "directory" elif stat.S_ISREG(out.st_mode): @@ -95,20 +104,15 @@ def info(self, path, **kwargs): t = "other" result = { "name": path, - "size": out.st_size, + "size": size, "type": t, "created": out.st_ctime, "islink": link, } for field in ["mode", "uid", "gid", "mtime", "ino", "nlink"]: result[field] = getattr(out, f"st_{field}") - if result["islink"]: + if link: result["destination"] = os.readlink(path) - try: - out2 = os.stat(path, follow_symlinks=True) - result["size"] = out2.st_size - except OSError: - result["size"] = 0 return result def lexists(self, path, **kwargs):