Skip to content

Commit 690c568

Browse files
committed
refac
1 parent 98bd1a5 commit 690c568

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

open_terminal/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ async def list_files(
510510
if not await fs.isdir(target):
511511
raise HTTPException(status_code=404, detail="Directory not found")
512512
entries = await fs.listdir(target)
513-
return {"dir": target, "entries": entries}
513+
return {"dir": target, "writable": await fs.is_writable(target), "entries": entries}
514514

515515

516516
@app.get(

open_terminal/utils/fs.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,28 @@ async def stat(self, path: str) -> dict:
179179
"size": s.st_size,
180180
"modified": s.st_mtime,
181181
"type": "directory" if os.path.isdir(path) else "file",
182+
"writable": self._is_writable_sync(path),
182183
}
183184

185+
def _is_writable_sync(self, path: str) -> bool:
186+
if not self.is_path_allowed(path):
187+
return False
188+
try:
189+
if os.statvfs(path).f_flag & getattr(os, "ST_RDONLY", 0):
190+
return False
191+
except (AttributeError, OSError):
192+
pass
193+
try:
194+
return os.access(path, os.W_OK, effective_ids=True)
195+
except TypeError:
196+
return os.access(path, os.W_OK)
197+
198+
async def is_writable(self, path: str) -> bool:
199+
"""Return whether the current process can write to *path*."""
200+
return await asyncio.to_thread(self._is_writable_sync, path)
201+
184202
async def listdir(self, path: str) -> list[dict]:
185-
"""List directory contents with type, size, and mtime."""
203+
"""List directory contents with type, size, mtime, and writability."""
186204
self._check_path(path)
187205
def _list_sync():
188206
entries = []
@@ -197,6 +215,7 @@ def _list_sync():
197215
"type": "directory" if os.path.isdir(full) else "file",
198216
"size": s.st_size,
199217
"modified": s.st_mtime,
218+
"writable": self._is_writable_sync(full),
200219
})
201220
except OSError:
202221
continue

0 commit comments

Comments
 (0)