From 6efaa600e7d4aa37397b0e0aa733c18c5a802bd1 Mon Sep 17 00:00:00 2001 From: Bartosz Date: Tue, 10 Jan 2023 13:51:14 +0100 Subject: [PATCH] [MOS-872] Add support for directiories in filesystem endpoint's listdir Added support for listing directiories with nested ones --- .../endpoints/filesystem/FS_Helper.cpp | 61 +++++++++++++------ .../endpoints/filesystem/FS_Helper.hpp | 3 +- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp index f37181238d12a39df9d12c61fba2e0b5b16e8617..30d6f19e675c8f7f198058cbe97f7f76f133833d 100644 --- a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp +++ b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp @@ -1,23 +1,52 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include -#include -#include #include -#include #include -#include #include -#include namespace sdesktop::endpoints { namespace { constexpr auto bytesInMebibyte = 1024LLU * 1024LLU; - } + + enum FileType + { + directory, + regularFile, + symlink, + other, + }; + + auto parseFileEntry(const std::filesystem::directory_entry &entry) -> json11::Json + { + int size = 0; + FileType type; + + if (entry.is_directory()) { + type = FileType::directory; + } + else { + size = static_cast(entry.file_size()); + } + + if (entry.is_regular_file()) { + type = FileType::regularFile; + } + else if (entry.is_symlink()) { + type = FileType::symlink; + } + else { + type = FileType::other; + } + + return json11::Json::object{ + {json::fs::path, entry.path().string()}, {json::fs::fileSize, size}, {json::fs::type, type}}; + } + } // namespace using sender::putToSendQueue; namespace fs = std::filesystem; @@ -98,7 +127,6 @@ namespace sdesktop::endpoints return {sent::no, ResponseContext{.status = code}}; } - auto FS_Helper::requestLogsFlush() const -> void { auto ownerService = dynamic_cast(owner); @@ -201,7 +229,7 @@ namespace sdesktop::endpoints auto FS_Helper::startSendFile(Context &context) const -> ResponseContext { const auto &body = context.getBody(); - std::filesystem::path filePath = body[json::fs::fileName].string_value(); + const auto filePath = body[json::fs::fileName].string_value(); const uint32_t fileSize = body[json::fs::fileSize].int_value(); const auto fileCrc32 = body[json::fs::fileCrc32].string_value(); auto code = http::Code::BadRequest; @@ -329,20 +357,13 @@ namespace sdesktop::endpoints return ResponseContext{.status = http::Code::NotFound}; } - std::vector> filesInDir; - for (const auto &entry : std::filesystem::directory_iterator{directory}) { - filesInDir.push_back(std::make_pair(entry.path(), entry.file_size())); - } - json11::Json::array jsonArr; - jsonArr.reserve(filesInDir.size()); - for (const auto &pathAndSize : filesInDir) { - jsonArr.push_back( - json11::Json::object{{json::fs::path, pathAndSize.first}, {json::fs::fileSize, pathAndSize.second}}); + for (const auto &entry : std::filesystem::directory_iterator{directory}) { + jsonArr.push_back(parseFileEntry(entry)); } - json11::Json::object response({{directory, jsonArr}}); + json11::Json::object const response({{directory, jsonArr}}); return ResponseContext{.status = http::Code::OK, .body = response}; } @@ -350,7 +371,7 @@ namespace sdesktop::endpoints { const auto userDiskPath = purefs::dir::getUserDiskPath(); - std::unique_ptr vfstat = std::make_unique(); + auto vfstat = std::make_unique(); if (statvfs(userDiskPath.c_str(), vfstat.get()) < 0) { return 0; } diff --git a/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp b/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp index 226d464a27c49645f3c4f95698a9a02363736dc7..6700065d8a4ad20787414f19c449f32901ab97fb 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -17,6 +17,7 @@ namespace sdesktop::endpoints inline constexpr auto path = "path"; inline constexpr auto fileName = "fileName"; inline constexpr auto fileSize = "fileSize"; + inline constexpr auto type = "type"; inline constexpr auto fileCrc32 = "fileCrc32"; inline constexpr auto chunkSize = "chunkSize"; inline constexpr auto chunkNo = "chunkNo";