From 394d1ff6601411b931cb9bd3fadab3f87fec668f Mon Sep 17 00:00:00 2001 From: "p.kosko@samsung.com" Date: Tue, 18 Jul 2023 14:13:05 +0200 Subject: [PATCH] Fixed listDirectory() [KONA] DF230714-00592 Issue: names returned via API can be invalid. 1. create many files in dir, 1-100.txt files 2. tizen.filesystem.listDirectory("documents", function(files) {f = files; console.log(files) }, function(e) { console.log(e) } ) issued list contains empty names/names with strange characters. This fixes above problems. [Verification] With above code, problem doesn't occur. TCT passrate 100%. Change-Id: Ief9ec91b0a130d789c8777423f51dfa6b2f6de9d --- src/filesystem/filesystem_instance.cc | 14 ++++++++------ src/filesystem/filesystem_utils.cc | 8 +++++--- src/filesystem/filesystem_utils.h | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 9b844d8..4ab1ce3 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -1657,7 +1657,8 @@ void FilesystemInstance::FileSystemManagerRename(const picojson::value& args, ReportSuccess(out); } -void FilterResult(std::vector& names, std::vector& types, bool is_type, +void FilterResult(std::vector& names, + std::vector& types, bool is_type, unsigned char type) { int i = (int)names.size() - 1; @@ -1686,13 +1687,14 @@ void FilesystemInstance::FileSystemManagerListDirectory(const picojson::value& a obj["callbackId"] = picojson::value(callback_id); try { - std::vector names; + std::vector names; { std::vector types; - FilesystemUtils::ListDirectory(path, [&](const char* name, unsigned char type) { - names.push_back(name); - types.push_back(type); - }); + FilesystemUtils::ListDirectory( + path, [&](const std::string& name, unsigned char type) { + names.push_back(name); + types.push_back(type); + }); auto it = filter.find("isFile"); if (filter.end() != it) { diff --git a/src/filesystem/filesystem_utils.cc b/src/filesystem/filesystem_utils.cc index dccd81f..e58887d 100644 --- a/src/filesystem/filesystem_utils.cc +++ b/src/filesystem/filesystem_utils.cc @@ -161,7 +161,7 @@ void CopyFile(const std::string& src, const std::string& dest, bool overwrite) { void CopyDirectory(const std::string& src, const std::string& dest, bool overwrite) { ScopeLogger("From: %s, To %s", src.c_str(), dest.c_str()); - ListDirectory(src, [&](const char* name, unsigned char type) { + ListDirectory(src, [&](const std::string& name, unsigned char type) { if (DT_DIR == type) { std::string dest_dir = dest + '/' + name; struct stat buf {}; @@ -188,7 +188,9 @@ void CopyDirectory(const std::string& src, const std::string& dest, bool overwri }); } -void ListDirectory(const std::string& path, std::function next) { +void ListDirectory( + const std::string& path, + std::function next) { ScopeLogger("%s", path.c_str()); DIR* d = ::opendir(path.c_str()); if (nullptr == d) { @@ -218,7 +220,7 @@ void ListDirectory(const std::string& path, std::functiond_name, ".") || 0 == std::strcmp(entry->d_name, "..")) { continue; } - next(entry->d_name, entry->d_type); + next(std::string(entry->d_name), entry->d_type); } } diff --git a/src/filesystem/filesystem_utils.h b/src/filesystem/filesystem_utils.h index 5888284..3a5fc3c 100644 --- a/src/filesystem/filesystem_utils.h +++ b/src/filesystem/filesystem_utils.h @@ -92,7 +92,7 @@ void CopyDirectory(const std::string& src, const std::string& dest, bool overwri * @brief Calls 'next' function with name for every entry in given directory * @throw std::system_error */ -void ListDirectory(const std::string& path, std::function next); +void ListDirectory(const std::string& path, std::function next); /** * @brief Removes directory recursively pointed by path. -- 2.7.4