From f373783fccaa7394a40502f2d815f1410389971c Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Thu, 11 Jan 2024 12:53:34 +0900 Subject: [PATCH] fix wrong dst path for source path under '/' When a source file path is /FILE, its dest path would be dst/ILE. This commit fixes this issue (#8). --- src/path.c | 16 ++++++++++++---- test/test_e2e.py | 2 +- test/util.py | 4 +++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/path.c b/src/path.c index 9e8a7a3..e7837ac 100644 --- a/src/path.c +++ b/src/path.c @@ -106,10 +106,18 @@ static int resolve_dst_path(const char *src_file_path, char *dst_file_path, mscp_set_error("dirname: %s", strerrno()); return -1; } - if (strlen(prefix) == 1 && prefix[0] == '.') - offset = 0; - else - offset = strlen(prefix) + 1; + + offset = strlen(prefix) + 1; + if (strlen(prefix) == 1) { /* corner cases */ + switch (prefix[0]) { + case '.': + offset = 0; + break; + case '/': + offset = 1; + break; + } + } if (!a->src_path_is_dir && !a->dst_path_is_dir) { /* src path is file. dst path is (1) file, or (2) does not exist. diff --git a/test/test_e2e.py b/test/test_e2e.py index b6ed73d..a73d896 100644 --- a/test/test_e2e.py +++ b/test/test_e2e.py @@ -216,7 +216,7 @@ def test_copy_file_under_root_to_dir(mscp, src_prefix, dst_prefix): dst_prefix + os.path.dirname(dst.path)]) assert check_same_md5sum(src, dst) src.cleanup() - dst.cleanup() + dst.cleanup(preserve_dir = True) @pytest.mark.parametrize("src_prefix, dst_prefix", param_remote_prefix) diff --git a/test/util.py b/test/util.py index b3b83c5..2baf934 100644 --- a/test/util.py +++ b/test/util.py @@ -46,8 +46,10 @@ def make_content_random(self): with open(self.path, "wb") as f: f.write(os.urandom(self.size)) - def cleanup(self): + def cleanup(self, preserve_dir = False): os.remove(self.path) + if preserve_dir: + return tmp = os.path.dirname(self.path) while tmp and not tmp in [".", "/"]: if len(os.listdir(tmp)) == 0: