From b474e650e69bfeca1347ac2bc30b4770c76e76c6 Mon Sep 17 00:00:00 2001 From: matan Date: Sun, 13 Feb 2022 09:11:34 +0200 Subject: [PATCH] Fs: Fix walk. --- src/rpcclient/rpcclient/fs.py | 12 +++---- src/rpcclient/tests/test_fs.py | 62 +++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/rpcclient/rpcclient/fs.py b/src/rpcclient/rpcclient/fs.py index c8a918f7..6f3100d8 100644 --- a/src/rpcclient/rpcclient/fs.py +++ b/src/rpcclient/rpcclient/fs.py @@ -293,15 +293,11 @@ def walk(self, top: str): """ provides the same results as os.walk(top) """ dirs = [] files = [] - for file in self.listdir(top): - filename = file.d_name - if filename in ('.', '..'): - continue - infos = self.stat(posixpath.join(top, filename)) - if infos.st_mode & S_IFMT == infos.st_mode & S_IFDIR: - dirs.append(filename) + for entry in self.scandir(top): + if entry.is_dir(): + dirs.append(entry.name) else: - files.append(filename) + files.append(entry.name) yield top, dirs, files diff --git a/src/rpcclient/tests/test_fs.py b/src/rpcclient/tests/test_fs.py index 9e9d4bb3..b65fea6c 100644 --- a/src/rpcclient/tests/test_fs.py +++ b/src/rpcclient/tests/test_fs.py @@ -51,26 +51,16 @@ def test_symlink(client, tmp_path): assert os.readlink(symlink) == str(file) -def test_stat_sanity(client, tmp_path): - file = (tmp_path / 'temp.txt') - file.write_text('h' * 0x10000) - client_stat = client.fs.stat(file) - path_stat = file.stat() - assert path_stat.st_dev == client_stat.st_dev - assert path_stat.st_ino == client_stat.st_ino - assert path_stat.st_mode == client_stat.st_mode - assert path_stat.st_nlink == client_stat.st_nlink - assert path_stat.st_uid == client_stat.st_uid - assert path_stat.st_gid == client_stat.st_gid - assert path_stat.st_rdev == client_stat.st_rdev - assert path_stat.st_atime == client_stat.st_atime - assert path_stat.st_mtime == client_stat.st_mtime - assert path_stat.st_ctime == client_stat.st_ctime - assert path_stat.st_size == client_stat.st_size - assert path_stat.st_blocks == client_stat.st_blocks - assert path_stat.st_blksize == client_stat.st_blksize - assert path_stat.st_flags == client_stat.st_flags - assert path_stat.st_gen == client_stat.st_gen +def test_link(client, tmp_path): + (tmp_path / 'temp.txt').write_text('hello') + client.fs.link((tmp_path / 'temp.txt'), (tmp_path / 'temp1.txt')) + assert (tmp_path / 'temp1.txt').read_text() == 'hello' + + +def test_listdir(client, tmp_path): + assert not client.fs.listdir(tmp_path) + (tmp_path / 'temp.txt').touch() + assert client.fs.listdir(tmp_path) == ['temp.txt'] def test_scandir_sanity(client, tmp_path): @@ -99,3 +89,35 @@ def test_scandir_context_manager(client, tmp_path): assert entries[0].is_file() assert not entries[0].is_dir() assert not entries[0].is_symlink() + + +def test_stat_sanity(client, tmp_path): + file = (tmp_path / 'temp.txt') + file.write_text('h' * 0x10000) + client_stat = client.fs.stat(file) + path_stat = file.stat() + assert path_stat.st_dev == client_stat.st_dev + assert path_stat.st_ino == client_stat.st_ino + assert path_stat.st_mode == client_stat.st_mode + assert path_stat.st_nlink == client_stat.st_nlink + assert path_stat.st_uid == client_stat.st_uid + assert path_stat.st_gid == client_stat.st_gid + assert path_stat.st_rdev == client_stat.st_rdev + assert path_stat.st_atime == client_stat.st_atime + assert path_stat.st_mtime == client_stat.st_mtime + assert path_stat.st_ctime == client_stat.st_ctime + assert path_stat.st_size == client_stat.st_size + assert path_stat.st_blocks == client_stat.st_blocks + assert path_stat.st_blksize == client_stat.st_blksize + assert path_stat.st_flags == client_stat.st_flags + assert path_stat.st_gen == client_stat.st_gen + + +def test_walk(client, tmp_path): + (tmp_path / 'dir_a').mkdir() + (tmp_path / 'dir_a' / 'a1.txt').touch() + (tmp_path / 'dir_a' / 'a2.txt').touch() + (tmp_path / 'dir_b').mkdir() + (tmp_path / 'dir_b' / 'b1.txt').touch() + (tmp_path / 'dir_b' / 'b2.txt').touch() + assert list(os.walk(tmp_path)) == list(client.fs.walk(tmp_path))