Skip to content

Commit 4b79654

Browse files
authored
localfs: reduce stats calls during info (#1659)
1 parent 32ce723 commit 4b79654

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

fsspec/implementations/local.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def info(self, path, **kwargs):
7979
t = "file"
8080
else:
8181
t = "other"
82+
83+
size = out.st_size
84+
if link:
85+
try:
86+
out2 = path.stat(follow_symlinks=True)
87+
size = out2.st_size
88+
except OSError:
89+
size = 0
8290
path = self._strip_protocol(path.path)
8391
else:
8492
# str or path-like
@@ -87,6 +95,7 @@ def info(self, path, **kwargs):
8795
link = stat.S_ISLNK(out.st_mode)
8896
if link:
8997
out = os.stat(path, follow_symlinks=True)
98+
size = out.st_size
9099
if stat.S_ISDIR(out.st_mode):
91100
t = "directory"
92101
elif stat.S_ISREG(out.st_mode):
@@ -95,20 +104,15 @@ def info(self, path, **kwargs):
95104
t = "other"
96105
result = {
97106
"name": path,
98-
"size": out.st_size,
107+
"size": size,
99108
"type": t,
100109
"created": out.st_ctime,
101110
"islink": link,
102111
}
103112
for field in ["mode", "uid", "gid", "mtime", "ino", "nlink"]:
104113
result[field] = getattr(out, f"st_{field}")
105-
if result["islink"]:
114+
if link:
106115
result["destination"] = os.readlink(path)
107-
try:
108-
out2 = os.stat(path, follow_symlinks=True)
109-
result["size"] = out2.st_size
110-
except OSError:
111-
result["size"] = 0
112116
return result
113117

114118
def lexists(self, path, **kwargs):

0 commit comments

Comments
 (0)