Skip to content

Commit b830d12

Browse files
committed
feat: implement get_directories_by_id
1 parent e1ca5c0 commit b830d12

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

db.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,35 @@ def get_owner(self, path: str) -> int:
391391
info = self.search_path(path)
392392
return info.uid
393393

394+
def get_directories_by_id(
395+
self,
396+
uids: Iterable[int],
397+
gids: Iterable[int],
398+
) -> list[tuple[str, int]]:
399+
"""Return (path, mode) of matching directories.
400+
401+
Directories are matched based on `uid` or `gid`."""
402+
res = self.cur.execute(
403+
f'''WITH RECURSIVE child AS
404+
(
405+
SELECT rowid AS original, rowid, parent, name, type, mode
406+
FROM fs
407+
WHERE type = {stat.S_IFDIR}
408+
AND (uid IN ({",".join(str(x) for x in uids)})
409+
OR gid IN ({",".join(str(x) for x in gids)}))
410+
411+
UNION ALL
412+
413+
SELECT original, fs.rowid, fs.parent, fs.name || '/' || child.name, child.type, child.mode
414+
FROM fs, child
415+
WHERE child.parent = fs.rowid
416+
)
417+
SELECT name, mode
418+
From child
419+
WHERE rowid = 1'''
420+
)
421+
return res.fetchall()
422+
394423

395424
class DatabaseWriter(DatabaseCommon):
396425
"""Database with read-write support."""

0 commit comments

Comments
 (0)