"res" is more common word for representing "resource" than "rsc".
Change-Id: I32c71d86a056a1c5127282e9fe0d69db8bc6e627
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
SET(TARGET_PKG_CLEARDATA "pkg_cleardata")
SET(TARGET_INSTALL_PRELOAD_PKG "install_preload_pkg")
SET(TARGET_PKG_UPGRADE "pkg_upgrade")
-SET(TARGET_RSC_COPY "rsc-copy")
+SET(TARGET_RES_COPY "res-copy")
SET(TARGET_RSC_SLICE "rsc-slice")
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
%{_bindir}/pkg_cleardata
%{_bindir}/pkginfo
%{_bindir}/rsc-slice
-%{_bindir}/rsc-copy
+%{_bindir}/res-copy
%{_bindir}/pkg_upgrade
%attr(0755,root,root) %{_bindir}/install_preload_pkg
%{_datadir}/mime/packages/mime.wac.xml
ADD_SUBDIRECTORY(pkg_upgrade)
ADD_SUBDIRECTORY(pkgcmd)
ADD_SUBDIRECTORY(pkginfo)
-ADD_SUBDIRECTORY(rsc-copy)
+ADD_SUBDIRECTORY(res-copy)
ADD_SUBDIRECTORY(rsc-slice)
--- /dev/null
+# Target - sources
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SRCS)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+
+# Target - definition
+ADD_EXECUTABLE(${TARGET_RES_COPY} ${SRCS})
+
+# Dependency
+APPLY_PKG_CONFIG(${TARGET_RES_COPY} PUBLIC
+ AUL_DEPS
+ GLIB_DEPS
+ BUNDLE_DEPS
+ Boost
+ PKGMGR_INFO_DEPS
+ TZPLATFORM_DEPS
+)
+
+# Install
+INSTALL(TARGETS ${TARGET_RES_COPY} DESTINATION bin)
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ABSTRACT_REQUEST_HANDLER_HH_
+#define ABSTRACT_REQUEST_HANDLER_HH_
+
+#include <list>
+#include <string>
+
+#include "include/error_type.hh"
+#include "include/res_path_info.hh"
+
+namespace res_handler {
+
+class AbstractRequestHandler {
+ public:
+ AbstractRequestHandler(
+ std::string pkgid, std::string root_path, std::list<ResPathInfo> path_list) :
+ pkgid_(pkgid), root_path_(root_path), path_list_(path_list) {};
+
+ virtual ErrorType Execute() = 0;
+ virtual const std::string GetRequestHandlerType() const = 0;
+
+ protected:
+ std::string GetRootPath();
+ const std::string GetPkgID() const;
+ const std::list<ResPathInfo> GetPathList() const;
+
+ private:
+ std::string pkgid_;
+ std::string root_path_;
+ std::list<ResPathInfo> path_list_;
+};
+
+} // namespace res_handler
+
+#endif // ABSTRACT_REQUEST_HANDLER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef CONDITION_VALIDATOR_HH_
+#define CONDITION_VALIDATOR_HH_
+
+#include <list>
+#include <string>
+
+#include "include/error_type.hh"
+#include "include/res_path_info.hh"
+#include "include/request_type.hh"
+
+namespace res_handler {
+
+class ConditionValidator {
+ public:
+ ConditionValidator(std::string pkgid, uid_t uid);
+ ErrorType ValidateCondition(ReqType req_type,
+ std::list<ResPathInfo> path_list);
+
+ private:
+ std::string pkgid_;
+ std::string root_path_;
+ uid_t uid_;
+
+ ErrorType CheckCopyRequest(std::list<ResPathInfo> path_list);
+ ErrorType CheckRemoveRequest(std::list<ResPathInfo> path_list);
+};
+
+} // namespace res_handler
+
+#endif // CONDITION_VALIDATOR_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef COPY_REQUEST_HANDLER_HH_
+#define COPY_REQUEST_HANDLER_HH_
+
+#include <list>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/res_path_info.hh"
+
+namespace res_handler {
+
+class CopyRequestHandler : public AbstractRequestHandler {
+ public:
+ CopyRequestHandler(std::string pkgid, std::string root_path,
+ std::list<ResPathInfo> path_list);
+
+ ErrorType Execute() override;
+ const std::string GetRequestHandlerType() const override;
+};
+
+} // namespace res_handler
+
+#endif // COPY_REQUEST_HANDLER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ERROR_TYPE_HH_
+#define ERROR_TYPE_HH_
+
+namespace res_handler {
+
+enum ErrorType {
+ ERROR_NONE = 0,
+ ERROR_INVALID_PARAMETER,
+ ERROR_PKG_NOT_FOUND,
+ ERROR_PERMISSION_DENIED,
+ ERROR_SYSTEM_ERROR,
+ ERROR_RES_NOT_FOUND,
+ ERROR_OUT_OF_SPACE,
+ ERROR_OUT_OF_MEMORY
+};
+
+} // namespace res_handler
+
+#endif // ERROR_TYPE_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef EVENT_SIGNAL_SENDER_HH_
+#define EVENT_SIGNAL_SENDER_HH_
+
+#include <string>
+
+#include "include/error_type.hh"
+#include "include/request_type.hh"
+
+namespace res_handler {
+
+class EventSignalSender {
+ public:
+ EventSignalSender() : pkgid_(""), req_type_(ReqType::REQ_TYPE_UNKNOWN) {};
+ bool SendStart();
+ bool SendOK();
+ bool SendFail(ErrorType error);
+ void SetPkgID(std::string pkgid);
+ void SetReqType(ReqType req_type);
+
+ private:
+ std::string pkgid_;
+ ReqType req_type_;
+};
+
+} // res_handler
+
+#endif // PARAM_CHECKER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FILE_UTIL_HH_
+#define FILE_UTIL_HH_
+
+#include <boost/filesystem.hpp>
+#include <boost/filesystem/path.hpp>
+
+namespace res_handler {
+
+// TODO(jungh.yeon) : is this necessary?
+enum FSFlag : int {
+ FS_NONE = 0,
+ FS_MERGE_SKIP = (1 << 0),
+ FS_MERGE_OVERWRITE = (1 << 1),
+ FS_COMMIT_COPY_FILE = (1 << 2),
+ FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
+};
+
+FSFlag operator|(FSFlag a, FSFlag b);
+
+bool CreateDir(const boost::filesystem::path& path);
+
+bool CopyDir(const boost::filesystem::path& src,
+ const boost::filesystem::path& dst,
+ FSFlag flags = FS_NONE, bool skip_symlink = false);
+
+bool CopyFile(const boost::filesystem::path& src,
+ const boost::filesystem::path& dst);
+
+bool RemoveAll(const boost::filesystem::path& path);
+
+} // namespace res_handler
+
+#endif // FILE_UTIL_HH_
--- /dev/null
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef LOGGING_HH_
+#define LOGGING_HH_
+
+#include <dlog.h>
+
+#ifndef PROJECT_TAG
+#define PROJECT_TAG "PKGMGR_TOOL"
+#endif
+
+#ifdef LOG
+#undef LOG
+#endif
+
+#include <cassert>
+#include <climits>
+#include <cstdio>
+#include <cstring>
+#include <iomanip>
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <string>
+#include <vector>
+
+#ifndef __FILENAME__
+#define __FILENAME__ \
+ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
+#endif
+
+namespace utils {
+
+enum class LogLevel {
+ LOG_ERROR,
+ LOG_WARNING,
+ LOG_INFO,
+ LOG_DEBUG,
+};
+
+log_priority LogLevelToPriority(LogLevel level);
+
+template<LogLevel> struct LogTag;
+template<> struct LogTag<LogLevel::LOG_ERROR> {
+ static constexpr const char* value = "\033[1;31m| ERROR |\033[0m";
+};
+template<> struct LogTag<LogLevel::LOG_WARNING> {
+ static constexpr const char* value = "\033[1;33m| WARNING |\033[0m";
+};
+template<> struct LogTag<LogLevel::LOG_INFO> {
+ static constexpr const char* value = "\033[1;32m| INFO |\033[0m";
+};
+template<> struct LogTag<LogLevel::LOG_DEBUG> {
+ static constexpr const char* value = "\033[0m| DEBUG |\033[0m";
+};
+
+template <class charT, class traits = std::char_traits<charT>>
+class StringStream : private std::basic_ostringstream<charT, traits> {
+ public:
+ using std::basic_ostringstream<charT, traits>::str;
+
+ template <class T>
+ StringStream& operator<<(const T& value) {
+ static_cast<std::basic_ostringstream<charT, traits> &>(*this) << value;
+ return *this;
+ }
+};
+
+// Interface class for logging backends. The custom LogBackend which wants
+// log using LOG() macro should be implement following interface.
+class ILogBackend {
+ public:
+ virtual void WriteLog(LogLevel level, const std::string& tag,
+ const std::string& logstr) = 0;
+};
+
+class DLogBackend : public ILogBackend {
+ public:
+ void WriteLog(LogLevel level, const std::string& tag,
+ const std::string& logstr) override {
+ dlog_print(LogLevelToPriority(level), tag.c_str(), "%s",
+ Escape(logstr).c_str());
+ }
+
+ private:
+ // Since LogCatcher passes input to dlog_print(), the input which contains
+ // format string(such as %d, %n) can cause unexpected result.
+ // This is simple function to escape '%'.
+ // NOTE: Is there any gorgeous way instead of this?
+ std::string Escape(const std::string& str) const {
+ std::string escaped = std::string(str);
+ size_t start_pos = 0;
+ std::string from = "%";
+ std::string to = "%%";
+ while ((start_pos = escaped.find(from, start_pos)) != std::string::npos) {
+ escaped.replace(start_pos, from.length(), to);
+ start_pos += to.length();
+ }
+ return escaped;
+ }
+};
+
+class LogCore {
+ public:
+ // Do not call this function at destructor of global object
+ static LogCore& GetCore() {
+ static LogCore core;
+ return core;
+ }
+
+ void AddLogBackend(std::shared_ptr<ILogBackend> backend) {
+ backend_list_.emplace_back(backend);
+ }
+
+ void Log(LogLevel level, const std::string& tag, const std::string& log) {
+ for (auto& backend : backend_list_)
+ backend->WriteLog(level, tag, log);
+ }
+
+ private:
+ LogCore() {
+ // add default dlog backend
+ AddLogBackend(std::shared_ptr<ILogBackend>(new DLogBackend()));
+ }
+ ~LogCore() = default;
+ LogCore(const LogCore&) = delete;
+ LogCore& operator=(const LogCore&) = delete;
+
+ std::vector<std::shared_ptr<ILogBackend>> backend_list_;
+};
+
+class LogCatcher {
+ public:
+ LogCatcher(LogLevel level, const char* tag)
+ : level_(level), tag_(tag) { }
+
+ void operator&(const StringStream<char>& str) const {
+ LogCore::GetCore().Log(level_, tag_, str.str());
+ }
+
+ private:
+ LogLevel level_;
+ std::string tag_;
+};
+
+} // namespace utils
+
+
+inline static const constexpr char* __tag_for_logging() {
+ return "";
+}
+
+inline static const constexpr char* __tag_for_project() {
+ return PROJECT_TAG;
+}
+
+// To be defined in class namespace if user want different log tag for given
+// scope
+#define SCOPE_LOG_TAG(TAG) \
+ inline static const constexpr char* __tag_for_logging() { \
+ return #TAG; \
+ } \
+
+// Simple logging macro of following usage:
+// LOG(LEVEL) << object_1 << object_2 << object_n;
+// where:
+// LEVEL = ERROR | WARNING | INFO | DEBUG
+#define LOG(LEVEL) \
+ ::utils::LogCatcher( \
+ ::utils::LogLevel::LOG_ ## LEVEL, __tag_for_project()) \
+ & ::utils::StringStream<char>() \
+ << std::string(::utils::LogTag<::utils::LogLevel::LOG_ ## LEVEL>::value) \
+ << " " << std::setw(25) << std::left << __tag_for_logging() \
+ << " : " << std::setw(36) \
+ << (std::string(__FILENAME__) + ":" + std::to_string(__LINE__)).c_str() \
+ << std::setw(0) << " : " \
+
+#endif // LOGGING_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PARAM_CHECKER_HH_
+#define PARAM_CHECKER_HH_
+
+#include <list>
+#include <string>
+
+#include "include/request_type.hh"
+#include "include/res_path_info.hh"
+
+namespace res_handler {
+
+class ParamChecker {
+ public:
+ ParamChecker(int argc, char* argv[]);
+ ReqType GetRequestType();
+ std::string GetPkgID() const;
+ uid_t GetUID() const;
+ const std::list<ResPathInfo>& GetPathList() const;
+ bool Validate();
+
+ private:
+ std::list<ResPathInfo> path_info_list_;
+ std::string pkgid_;
+ std::string session_id_;
+ uid_t uid_ = 0;
+ ReqType req_type_ = ReqType::REQ_TYPE_UNKNOWN;
+
+ void SetRequestType(std::string key);
+ bool ValidatePkgID();
+};
+
+} // res_handler
+
+#endif // PARAM_CHECKER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef REMOVE_REQUEST_HANDLER_HH_
+#define REMOVE_REQUEST_HANDLER_HH_
+
+#include <list>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/res_path_info.hh"
+
+namespace res_handler {
+
+class RemoveRequestHandler : public AbstractRequestHandler {
+ public:
+ RemoveRequestHandler(
+ std::string pkgid, std::string root_path,
+ std::list<ResPathInfo> path_list);
+
+ ErrorType Execute() override;
+ const std::string GetRequestHandlerType() const override;
+};
+
+} // namespace res_handler
+
+#endif // REMOVE_REQUEST_HANDLER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef REQUEST_HANDLER_INVOKER_HH_
+#define REQUEST_HANDLER_INVOKER_HH_
+
+#include <memory>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/event_signal_sender.hh"
+#include "include/param_checker.hh"
+
+namespace res_handler {
+
+class RequestHandlerInvoker {
+ public:
+ RequestHandlerInvoker(ParamChecker option, EventSignalSender signal);
+
+ bool Validate();
+ bool Execute();
+ const std::string GetHandlerType() const;
+
+ private:
+ std::unique_ptr<AbstractRequestHandler> handler_;
+ ParamChecker option_;
+ EventSignalSender signal_;
+
+ void SetReqHandler();
+};
+
+} // namespace res_handler
+
+#endif // REQUEST_HANDLER_INVOKER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef REQUEST_TYPE_HH_
+#define REQUEST_TYPE_HH_
+
+namespace res_handler {
+
+enum ReqType {
+ REQ_TYPE_NEW = 0,
+ REQ_TYPE_REMOVE = 1,
+ REQ_TYPE_UNINSTALL,
+ REQ_TYPE_UNKNOWN
+};
+
+} // namespace res_handler
+
+#endif // REQUEST_TYPE_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RES_HANDLER_HH_
+#define RES_HANDLER_HH_
+
+#include <memory>
+
+#include "include/request_handler_invoker.hh"
+
+namespace res_handler {
+
+class ResHandler {
+ public:
+ ResHandler() {};
+ bool Init(int argc, char* argv[]);
+ bool Run();
+
+ private:
+ std::unique_ptr<res_handler::RequestHandlerInvoker> handler_;
+};
+
+} // namespace res_handler
+
+#endif // RES_HANDLER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RES_PATH_INFO_HH_
+#define RES_PATH_INFO_HH_
+
+#include <string>
+
+namespace res_handler {
+
+class ResPathInfo {
+ public:
+ ResPathInfo(std::string src, std::string dst);
+
+ const std::string& GetSrcPath() const;
+ const std::string& GetDstPath() const;
+
+ private:
+ std::string src_;
+ std::string dst_;
+};
+
+} // namespace res_handler
+
+#endif // RES_PATH_INFO_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UNINSTALL_REQUEST_HANDLER_HH_
+#define UNINSTALL_REQUEST_HANDLER_HH_
+
+#include <list>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/res_path_info.hh"
+
+namespace res_handler {
+
+class UninstallRequestHandler : public AbstractRequestHandler {
+ public:
+ UninstallRequestHandler(
+ std::string pkgid, std::string root_path, std::list<ResPathInfo> path_list);
+
+ ErrorType Execute() override;
+ const std::string GetRequestHandlerType() const override;
+};
+
+} // namespace res_handler
+
+#endif // UNINSTALL_REQUEST_HANDLER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/abstract_request_handler.hh"
+
+#include <iostream>
+
+#include "include/request_type.hh"
+#include "include/res_path_info.hh"
+
+namespace res_handler {
+
+std::string AbstractRequestHandler::GetRootPath() {
+ return root_path_;
+}
+
+const std::string AbstractRequestHandler::GetPkgID() const {
+ return pkgid_;
+}
+
+const std::list<ResPathInfo> AbstractRequestHandler::GetPathList() const {
+ return path_list_;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/condition_validator.hh"
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/system/error_code.hpp>
+#include <tzplatform_config.h>
+
+#include <sys/stat.h>
+
+#include <iostream>
+#include <list>
+#include <string>
+
+#include "include/logging.hh"
+#include "include/request_type.hh"
+#include "include/res_path_info.hh"
+
+#include <pkgmgr-info.h>
+
+namespace bf = boost::filesystem;
+
+namespace {
+
+std::string GetRootPath(uid_t uid) {
+ tzplatform_set_user(uid);
+ const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
+ tzplatform_reset_user();
+ return rootpath;
+}
+
+bool IsPackageExists(std::string pkgid, uid_t uid) {
+ pkgmgrinfo_pkginfo_h handle;
+ if (pkgmgrinfo_pkginfo_get_usr_pkginfo(
+ pkgid.c_str(), uid, &handle) != PMINFO_R_OK) {
+ LOG(ERROR) << "package not exists : " << pkgid;
+ return false;
+ }
+ pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+
+ return true;
+}
+
+bool CheckFreeSpaceAtPath(int64_t required_size,
+ const bf::path& target_location) {
+ boost::system::error_code error;
+ bf::path root = target_location;
+
+ while (!bf::exists(root) && root != root.root_path())
+ root = root.parent_path();
+
+ if (!bf::exists(root)) {
+ LOG(ERROR) << "No mount point for path: " << target_location;
+ return false;
+ }
+
+ bf::space_info space_info = bf::space(root, error);
+ if (error) {
+ LOG(ERROR) << "Failed to get space_info: " << error.message();
+ return false;
+ }
+
+ return (space_info.free >= static_cast<uint64_t>(required_size));
+}
+
+int64_t GetBlockSizeForPath(const bf::path& path_in_partition) {
+ struct stat stats;
+ if (stat(path_in_partition.string().c_str(), &stats)) {
+ LOG(ERROR) << "stat(" << path_in_partition.string()
+ << ") failed - error code: " << errno;
+ return -1;
+ }
+ return stats.st_blksize;
+}
+
+int64_t RoundUpToBlockSizeOf(int64_t size, int64_t block_size) {
+ return ((size + block_size - 1) / block_size) * block_size;
+}
+
+int64_t GetDirectorySize(const bf::path& path) {
+ int64_t block_size = GetBlockSizeForPath(path);
+
+ if (block_size == -1)
+ return -1;
+
+ int64_t size = 0;
+ for (bf::recursive_directory_iterator iter(path);
+ iter != bf::recursive_directory_iterator(); ++iter) {
+ struct stat buf;
+ if (lstat(iter->path().c_str(), &buf) == -1) {
+ LOG(ERROR) << "lstat() failed for: " << iter->path();
+ return -1;
+ }
+ size += RoundUpToBlockSizeOf(buf.st_size, block_size);
+ }
+
+ return size;
+}
+
+} // namespace
+
+namespace res_handler {
+
+ConditionValidator::ConditionValidator(std::string pkgid, uid_t uid) :
+ pkgid_(pkgid), uid_(uid) {
+ root_path_ = GetRootPath(uid_);
+}
+
+ErrorType ConditionValidator::ValidateCondition(ReqType req_type,
+ std::list<ResPathInfo> path_list) {
+ if (!IsPackageExists(pkgid_, uid_))
+ return ErrorType::ERROR_PKG_NOT_FOUND;
+
+ if (req_type == ReqType::REQ_TYPE_NEW)
+ return CheckCopyRequest(path_list);
+ else if (req_type == ReqType::REQ_TYPE_REMOVE)
+ return CheckRemoveRequest(path_list);
+
+ return ErrorType::ERROR_NONE;
+}
+
+ErrorType ConditionValidator::CheckCopyRequest(
+ std::list<ResPathInfo> path_list) {
+ boost::filesystem::path src_root_path(root_path_);
+ uintmax_t res_size = 0;
+
+ src_root_path = src_root_path / "apps_rw" / pkgid_;
+ for (auto& path_info : path_list) {
+ boost::filesystem::path res_path(src_root_path);
+ res_path = res_path / path_info.GetSrcPath();
+ if (!boost::filesystem::exists(res_path)) {
+ LOG(ERROR) << "Resource not exists : " << res_path;
+ return ErrorType::ERROR_RES_NOT_FOUND;
+ }
+
+ if (boost::filesystem::is_directory(res_path))
+ res_size += GetDirectorySize(res_path);
+ else
+ res_size += boost::filesystem::file_size(res_path);
+
+ }
+ LOG(INFO) << "Required size for resource: " << res_size;
+
+ if (!CheckFreeSpaceAtPath(static_cast<int64_t>(res_size), root_path_)) {
+ LOG(ERROR) << "Not enough space for resource";
+ return ErrorType::ERROR_OUT_OF_SPACE;
+ }
+
+ return ErrorType::ERROR_NONE;
+}
+
+ErrorType ConditionValidator::CheckRemoveRequest(
+ std::list<ResPathInfo> path_list) {
+ for (auto& path_info : path_list) {
+ boost::filesystem::path dst_path(root_path_);
+ dst_path = dst_path / "shared_res"/ pkgid_ / path_info.GetDstPath();
+ if (!boost::filesystem::exists(dst_path)) {
+ LOG(ERROR) << "Resource not exists : " << dst_path;
+ return ErrorType::ERROR_RES_NOT_FOUND;
+ }
+ }
+
+ return ErrorType::ERROR_NONE;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/copy_request_handler.hh"
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+#include <iostream>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/file_util.hh"
+#include "include/logging.hh"
+#include "include/res_path_info.hh"
+
+namespace bf = boost::filesystem;
+
+namespace {
+
+constexpr char kCopyReqHandlerType[] = "copy";
+
+} // namespace
+
+namespace res_handler {
+
+CopyRequestHandler::CopyRequestHandler(
+ std::string pkgid, std::string root_path,
+ std::list<ResPathInfo> path_list) :
+ AbstractRequestHandler(pkgid, root_path, path_list) {}
+
+ErrorType CopyRequestHandler::Execute() {
+ bf::path root_path(GetRootPath());
+ bf::path src_root_path = root_path / "apps_rw" / GetPkgID();
+ bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
+
+ if (!CreateDir(dst_root_path))
+ return ErrorType::ERROR_SYSTEM_ERROR;
+
+ for (auto& path_info : GetPathList()) {
+ bf::path src_path = src_root_path / path_info.GetSrcPath();
+ if (!bf::exists(src_path)) {
+ LOG(ERROR) << "Path not exists :" << src_path;
+ return ErrorType::ERROR_RES_NOT_FOUND;
+ }
+
+ bf::path dst_path = dst_root_path / path_info.GetDstPath();
+
+ if (bf::is_directory(src_path)) {
+ if (!CopyDir(src_path, dst_path,
+ FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS, true)) {
+ LOG(ERROR) << "Failed to copy directory " << src_path;
+ return ErrorType::ERROR_SYSTEM_ERROR;
+ }
+ } else {
+ if (bf::is_directory(dst_path))
+ dst_path /= src_path.filename();
+
+ if (!CopyFile(src_path, dst_path)) {
+ LOG(ERROR) << "Failed to copy directory " << src_path;
+ return ErrorType::ERROR_SYSTEM_ERROR;
+ }
+ }
+ }
+
+ return ErrorType::ERROR_NONE;
+}
+
+const std::string CopyRequestHandler::GetRequestHandlerType() const {
+ return kCopyReqHandlerType;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/event_signal_sender.hh"
+
+#include <iostream>
+#include <string>
+
+#include "include/error_type.hh"
+#include "include/request_type.hh"
+
+namespace res_handler {
+
+bool EventSignalSender::SendStart() {
+ std::cout << "EventSignalSender::SendStart" << std::endl;
+
+ return true;
+}
+
+bool EventSignalSender::SendOK() {
+ std::cout << "EventSignalSender::SendOK" << std::endl;
+
+ return true;
+}
+
+bool EventSignalSender::SendFail(ErrorType error) {
+ std::cout << "EventSignalSender::SendFail" << std::endl;
+
+ return true;
+}
+
+void EventSignalSender::SetPkgID(std::string pkgid) {
+ pkgid_ = pkgid;
+}
+
+void EventSignalSender::SetReqType(ReqType req_type) {
+ req_type_ = req_type;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/file_util.hh"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/system/error_code.hpp>
+
+#include "include/logging.hh"
+
+namespace bs = boost::system;
+namespace bf = boost::filesystem;
+
+namespace res_handler {
+
+FSFlag operator|(FSFlag a, FSFlag b) {
+ return static_cast<FSFlag>(static_cast<int>(a) | static_cast<int>(b));
+}
+
+bool SetDirPermissions(const boost::filesystem::path& path,
+ boost::filesystem::perms permissions) {
+ boost::system::error_code error;
+ bf::permissions(path, permissions, error);
+
+ if (error) {
+ LOG(ERROR) << "Failed to set permissions for directory: " << path
+ << boost::system::system_error(error).what();
+ return false;
+ }
+ return true;
+}
+
+bool SetOwnership(const bf::path& path, uid_t uid, gid_t gid) {
+ int ret = lchown(path.c_str(), uid, gid);
+ if (ret != 0) {
+ LOG(ERROR) << "Failed to change owner of: " << path;
+ return false;
+ }
+ return true;
+}
+
+bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
+ boost::filesystem::perms permissions, uid_t uid,
+ gid_t gid) {
+ if (!SetOwnership(path, uid, gid)) {
+ LOG(ERROR) << "Failed to change owner: " << path
+ << "(" << uid << ", " << gid << ")";
+ return false;
+ }
+ if (!SetDirPermissions(path, permissions)) {
+ LOG(ERROR) << "Failed to change permission: " << path
+ << std::oct << permissions;
+ return false;
+ }
+
+ return true;
+}
+
+bool CopyOwnershipAndPermissions(const boost::filesystem::path& src,
+ const boost::filesystem::path& dst) {
+ if (!bf::exists(src)) {
+ LOG(ERROR) << "Failed to copy ownership and permissions"
+ << " from " << src << " to " << dst;
+ return false;
+ }
+ bf::perms permissions = bf::status(src).permissions();
+ struct stat stats;
+ if (stat(src.c_str(), &stats) != 0)
+ return false;
+ if (!SetDirOwnershipAndPermissions(dst, permissions, stats.st_uid,
+ stats.st_gid)) {
+ LOG(ERROR) << "Failed to copy ownership and permissions"
+ << " from " << src << " to " << dst;
+ return false;
+ }
+ return true;
+}
+
+bool RemoveAll(const bf::path& path) {
+ if (!exists(path) && !bf::is_symlink(bf::symlink_status(path)))
+ return true;
+
+ bs::error_code error;
+ bf::remove_all(path, error);
+
+ if (error) {
+ LOG(ERROR) << "Cannot remove: " << path << ", " << error.message();
+ return false;
+ }
+
+ return true;
+}
+
+bool CopyDir(const boost::filesystem::path& src,
+ const boost::filesystem::path& dst,
+ FSFlag flags, bool skip_symlink) {
+ try {
+ // Check whether the function call is valid
+ if (!bf::exists(src) || !bf::is_directory(src)) {
+ LOG(ERROR) << "Source directory " << src
+ << " does not exist or is not a directory.";
+ return false;
+ }
+ if (!bf::exists(dst)) {
+ // Create the destination directory
+ if (!CreateDir(dst)) {
+ LOG(ERROR) << "Unable to create destination directory" << dst;
+ return false;
+ }
+ if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
+ CopyOwnershipAndPermissions(src, dst);
+ } else {
+ if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
+ LOG(ERROR) << "Destination directory " << dst.string()
+ << " already exists.";
+ return false;
+ }
+ if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS))
+ CopyOwnershipAndPermissions(src, dst);
+ }
+ } catch (const bf::filesystem_error& error) {
+ LOG(ERROR) << "Failed to copy directory: " << error.what();
+ return false;
+ }
+
+ // Iterate through the source directory
+ for (bf::directory_iterator file(src);
+ file != bf::directory_iterator();
+ ++file) {
+ try {
+ bf::path current(file->path());
+ bf::path target = dst / current.filename();
+
+ if (bf::is_symlink(symlink_status(current))) {
+ if (skip_symlink)
+ continue;
+ if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) &&
+ bf::exists(target))
+ continue;
+ bs::error_code error;
+ bf::copy_symlink(current, target, error);
+ if (error) {
+ LOG(ERROR) << "Failed to copy symlink: " << current << ", "
+ << error.message();
+ return false;
+ }
+ } else if (bf::is_directory(current)) {
+ // Found directory: Recursion
+ if (!CopyDir(current, target, flags, skip_symlink)) {
+ return false;
+ }
+ } else {
+ if ((flags & FS_MERGE_SKIP) && bf::exists(target))
+ continue;
+ bf::path destination = target;
+
+ if (flags & FS_COMMIT_COPY_FILE)
+ destination =
+ bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
+
+ if (flags & FS_MERGE_OVERWRITE)
+ bf::copy_file(current, destination,
+ bf::copy_option::overwrite_if_exists);
+ else
+ bf::copy_file(current, destination);
+
+ if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
+ CopyOwnershipAndPermissions(current, destination);
+
+ if (flags & FS_COMMIT_COPY_FILE) {
+ if (flags & FS_MERGE_OVERWRITE)
+ bf::remove(target);
+ bf::rename(destination, target);
+ }
+ }
+ } catch (const bf::filesystem_error& error) {
+ LOG(ERROR) << "Failed to copy directory: " << error.what();
+ return false;
+ }
+ }
+ return true;
+}
+
+bool CreateDir(const bf::path& path) {
+ if (bf::exists(path))
+ return true;
+
+ boost::system::error_code error;
+ bf::create_directories(path, error);
+
+ if (error) {
+ LOG(ERROR) << "Failed to create directory: "
+ << boost::system::system_error(error).what();
+ return false;
+ }
+ return true;
+}
+
+bool CopyFile(const bf::path& src, const bf::path& dst) {
+ bs::error_code error;
+
+ bf::copy_file(src, dst, bf::copy_option::overwrite_if_exists, error);
+ if (error) {
+ LOG(WARNING) << "copy file " << src << " due to error ["
+ << error.message() << "]";
+ return false;
+ }
+ return true;
+}
+
+} // namespace res_handler
--- /dev/null
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include "include/logging.hh"
+
+namespace utils {
+
+log_priority LogLevelToPriority(LogLevel level) {
+ switch (level) {
+ case LogLevel::LOG_ERROR:
+ return log_priority::DLOG_ERROR;
+ case LogLevel::LOG_WARNING:
+ return log_priority::DLOG_WARN;
+ case LogLevel::LOG_INFO:
+ return log_priority::DLOG_INFO;
+ case LogLevel::LOG_DEBUG:
+ return log_priority::DLOG_DEBUG;
+ default:
+ return log_priority::DLOG_UNKNOWN;
+ }
+}
+
+} // namespace utils
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/param_checker.hh"
+
+#include <boost/exception/diagnostic_information.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/program_options.hpp>
+#include <boost/system/error_code.hpp>
+
+#include "include/logging.hh"
+#include "include/request_type.hh"
+#include "include/res_path_info.hh"
+
+#include <pkgmgr-info.h>
+
+#include <string>
+#include <iostream>
+
+namespace bs = boost::system;
+namespace bpo = boost::program_options;
+
+namespace res_handler {
+
+ParamChecker::ParamChecker(int argc, char* argv[]) {
+ bpo::options_description options;
+
+ options.add_options()
+ ("uid,u", bpo::value<int>()->default_value(0), "user id")
+ ("session-id,k", bpo::value<std::string>(), "session id")
+ ("path,p", bpo::value<std::vector<std::string>>()->multitoken(),
+ "source-destination path")
+ ("remove,r", bpo::value<std::string>(), "remove shared resource")
+ ("delete,d", bpo::value<std::string>(),
+ "delete shared resource for package")
+ ("copy,c", bpo::value<std::string>(), "copy resource")
+ ("help,h", "Show this message");
+
+ bpo::parsed_options parsed_options = bpo::command_line_parser(argc, argv)
+ .options(options)
+ .run();
+
+ for (const bpo::option& o : parsed_options.options) {
+ if (o.string_key == "uid") {
+ uid_ = static_cast<uid_t>(std::stoi(o.value.front()));
+ } else if (o.string_key == "session-id") {
+ session_id_ = o.value.front();
+ } else if (o.string_key == "path") {
+ if (o.value.front() == o.value.back())
+ path_info_list_.emplace_back(o.value.front(), "");
+ else
+ path_info_list_.emplace_back(o.value.front(), o.value.back());
+ } else if (o.string_key == "copy" ||
+ o.string_key == "delete" ||
+ o.string_key == "remove") {
+ pkgid_ = o.value.front();
+ SetRequestType(o.string_key);
+ } else if (o.string_key == "help") {
+ std::cout << options;
+ } else {
+ std::cout << "Invalid option : " << o.string_key << std::endl;
+ }
+ }
+}
+
+std::string ParamChecker::GetPkgID() const {
+ return pkgid_;
+}
+
+uid_t ParamChecker::GetUID() const {
+ return uid_;
+}
+
+ReqType ParamChecker::GetRequestType() {
+ return req_type_;
+}
+
+const std::list<ResPathInfo>& ParamChecker::GetPathList() const {
+ return path_info_list_;
+}
+
+bool ParamChecker::Validate() {
+ if (uid_ == 0) {
+ LOG(ERROR) << "Invalid uid: " << uid_;
+ return false;
+ }
+
+ if (req_type_ == ReqType::REQ_TYPE_UNKNOWN) {
+ LOG(ERROR) << "Invalid request type";
+ return false;
+ }
+
+ if (!ValidatePkgID())
+ return false;
+
+ if (req_type_ != ReqType::REQ_TYPE_UNINSTALL) {
+ if (path_info_list_.size() == 0) {
+ LOG(ERROR) << "Path is not given";
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void ParamChecker::SetRequestType(std::string key) {
+ if (key == "copy")
+ req_type_ = ReqType::REQ_TYPE_NEW;
+ else if (key == "remove")
+ req_type_ = ReqType::REQ_TYPE_REMOVE;
+ else if (key == "delete")
+ req_type_ = ReqType::REQ_TYPE_UNINSTALL;
+}
+
+bool ParamChecker::ValidatePkgID() {
+ if (pkgid_.size() == 0) {
+ LOG(ERROR) << "pkgid is empty";
+ return false;
+ }
+
+ return true;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/remove_request_handler.hh"
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+#include <iostream>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/file_util.hh"
+#include "include/logging.hh"
+#include "include/res_path_info.hh"
+
+namespace bf = boost::filesystem;
+
+namespace {
+
+constexpr char kRemoveReqHandlerType[] = "remove";
+
+} // namespace
+
+namespace res_handler {
+
+RemoveRequestHandler::RemoveRequestHandler(
+ std::string pkgid, std::string root_path,
+ std::list<ResPathInfo> path_list) :
+ AbstractRequestHandler(pkgid, root_path, path_list) {}
+
+ErrorType RemoveRequestHandler::Execute() {
+ bf::path root_path(GetRootPath());
+ bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
+
+ if (!bf::exists(dst_root_path)) {
+ LOG(ERROR) << "root path not exists";
+ return ErrorType::ERROR_RES_NOT_FOUND;
+ }
+
+ for (auto& path_info : GetPathList()) {
+ bf::path dst_path = dst_root_path / path_info.GetSrcPath();
+ if (!bf::exists(dst_path)) {
+ LOG(ERROR) << "Path not exists: " << dst_path;
+ continue;
+ }
+
+ if (!RemoveAll(dst_path))
+ return ErrorType::ERROR_SYSTEM_ERROR;
+ }
+
+ return ErrorType::ERROR_NONE;
+}
+
+const std::string RemoveRequestHandler::GetRequestHandlerType() const {
+ return kRemoveReqHandlerType;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/request_handler_invoker.hh"
+
+#include <iostream>
+
+#include <tzplatform_config.h>
+
+#include "include/abstract_request_handler.hh"
+#include "include/condition_validator.hh"
+#include "include/copy_request_handler.hh"
+#include "include/event_signal_sender.hh"
+#include "include/logging.hh"
+#include "include/param_checker.hh"
+#include "include/remove_request_handler.hh"
+#include "include/request_type.hh"
+#include "include/uninstall_request_handler.hh"
+
+namespace {
+
+std::string GetRootPathForUid(uid_t uid) {
+ tzplatform_set_user(uid);
+ const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
+ tzplatform_reset_user();
+ return rootpath;
+}
+
+} // namespace
+
+namespace res_handler {
+
+RequestHandlerInvoker::RequestHandlerInvoker(
+ ParamChecker option, EventSignalSender signal) :
+ option_(option), signal_(signal) {
+ SetReqHandler();
+}
+
+bool RequestHandlerInvoker::Validate() {
+ if (handler_ == nullptr) {
+ LOG(ERROR) << "Failed to initialize handler";
+ return false;
+ }
+
+ ConditionValidator validator(option_.GetPkgID(), option_.GetUID());
+ if (validator.ValidateCondition(
+ option_.GetRequestType(), option_.GetPathList())
+ != ErrorType::ERROR_NONE) {
+ LOG(ERROR) << "Validation failed";
+ return false;
+ }
+
+ return true;
+}
+
+bool RequestHandlerInvoker::Execute() {
+ if (handler_ == nullptr)
+ return false;
+
+ if (handler_->Execute() != ErrorType::ERROR_NONE)
+ return false;
+
+ return true;
+}
+
+const std::string RequestHandlerInvoker::GetHandlerType() const {
+ if (handler_ == nullptr) {
+ LOG(ERROR) << "handler has not initialized";
+ return {};
+ }
+
+ return handler_->GetRequestHandlerType();
+}
+
+void RequestHandlerInvoker::SetReqHandler() {
+ switch (option_.GetRequestType()) {
+ case ReqType::REQ_TYPE_NEW:
+ handler_.reset(
+ new CopyRequestHandler(
+ option_.GetPkgID(),
+ GetRootPathForUid(option_.GetUID()),
+ option_.GetPathList()));
+ break;
+ case ReqType::REQ_TYPE_REMOVE:
+
+ handler_.reset(
+ new RemoveRequestHandler(
+ option_.GetPkgID(),
+ GetRootPathForUid(option_.GetUID()),
+ option_.GetPathList()));
+ break;
+ case ReqType::REQ_TYPE_UNINSTALL:
+ handler_.reset(
+ new UninstallRequestHandler(
+ option_.GetPkgID(),
+ GetRootPathForUid(option_.GetUID()),
+ option_.GetPathList()));
+ break;
+ default:
+ LOG(ERROR) << "Invalid request type";
+ break;
+ }
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+
+#include "include/res_handler.hh"
+#include "include/logging.hh"
+
+int main(int argc, char *argv[]) {
+ try {
+ res_handler::ResHandler handler;
+ if (!handler.Init(argc, argv)) {
+ LOG(ERROR) << "Failed to initliaze handler";
+ return -1;
+ }
+
+ if (!handler.Run()) {
+ LOG(ERROR) << "Failed to handle request";
+ return -1;
+ }
+ } catch (...) {
+ LOG(ERROR) << "Exception occured";
+ return -1;
+ }
+
+ return 0;
+}
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/res_handler.hh"
+
+#include <iostream>
+
+#include "include/event_signal_sender.hh"
+#include "include/logging.hh"
+#include "include/param_checker.hh"
+#include "include/request_handler_invoker.hh"
+
+namespace res_handler {
+
+bool ResHandler::Init(int argc, char* argv[]) {
+ res_handler::ParamChecker option(argc, argv);
+ if (!option.Validate()) {
+ LOG(ERROR) << "Invalid argument has given";
+ return false;
+ }
+
+ res_handler::EventSignalSender signal;
+ signal.SetPkgID(option.GetPkgID());
+ signal.SetReqType(option.GetRequestType());
+
+ handler_.reset(new res_handler::RequestHandlerInvoker(option, signal));
+ if (!handler_->Validate()) {
+ LOG(ERROR) << "Failed to initialize request handler";
+ return false;
+ }
+
+ return true;
+}
+
+bool ResHandler::Run() {
+ return handler_->Execute();
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/res_path_info.hh"
+
+#include <iostream>
+#include <string>
+
+namespace res_handler {
+
+ResPathInfo::ResPathInfo(std::string src, std::string dst) :
+ src_(src), dst_(dst) {}
+
+const std::string& ResPathInfo::GetSrcPath() const {
+ return src_;
+}
+
+const std::string& ResPathInfo::GetDstPath() const {
+ return dst_;
+}
+
+} // namespace res_handler
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/uninstall_request_handler.hh"
+
+#include <iostream>
+#include <string>
+
+#include "include/abstract_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/file_util.hh"
+#include "include/logging.hh"
+#include "include/res_path_info.hh"
+
+namespace bf = boost::filesystem;
+
+namespace {
+
+constexpr char kUninstallReqHandlerType[] = "delete";
+
+} // namespace
+
+namespace res_handler {
+
+UninstallRequestHandler::UninstallRequestHandler(
+ std::string pkgid, std::string root_path,
+ std::list<ResPathInfo> path_list) :
+ AbstractRequestHandler(pkgid, root_path, path_list) {}
+
+ErrorType UninstallRequestHandler::Execute() {
+ if (GetPkgID().length() == 0) {
+ LOG(ERROR) << "Invalid argument";
+ return ErrorType::ERROR_INVALID_PARAMETER;
+ }
+
+ bf::path root_path(GetRootPath());
+ root_path = root_path / "shared_res" / GetPkgID();
+
+ if (!bf::exists(root_path)) {
+ LOG(WARNING) << "path not exists : " << root_path;
+ return ErrorType::ERROR_NONE;
+ }
+
+ if (!RemoveAll(root_path))
+ return ErrorType::ERROR_SYSTEM_ERROR;
+
+ return ErrorType::ERROR_NONE;
+}
+
+const std::string UninstallRequestHandler::GetRequestHandlerType() const {
+ return kUninstallReqHandlerType;
+}
+
+} // namespace res_handler
+++ /dev/null
-# Target - sources
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SRCS)
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
-
-# Target - definition
-ADD_EXECUTABLE(${TARGET_RSC_COPY} ${SRCS})
-
-# Dependency
-APPLY_PKG_CONFIG(${TARGET_RSC_COPY} PUBLIC
- AUL_DEPS
- GLIB_DEPS
- BUNDLE_DEPS
- Boost
- PKGMGR_INFO_DEPS
- TZPLATFORM_DEPS
-)
-
-# Install
-INSTALL(TARGETS ${TARGET_RSC_COPY} DESTINATION bin)
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ABSTRACT_REQUEST_HANDLER_HH_
-#define ABSTRACT_REQUEST_HANDLER_HH_
-
-#include <list>
-#include <string>
-
-#include "include/error_type.hh"
-#include "include/rsc_path_info.hh"
-
-namespace rsc_handler {
-
-class AbstractRequestHandler {
- public:
- AbstractRequestHandler(
- std::string pkgid, std::string root_path, std::list<RscPathInfo> path_list) :
- pkgid_(pkgid), root_path_(root_path), path_list_(path_list) {};
-
- virtual ErrorType Execute() = 0;
- virtual const std::string GetRequestHandlerType() const = 0;
-
- protected:
- std::string GetRootPath();
- const std::string GetPkgID() const;
- const std::list<RscPathInfo> GetPathList() const;
-
- private:
- std::string pkgid_;
- std::string root_path_;
- std::list<RscPathInfo> path_list_;
-};
-
-} // rsc_handler
-
-#endif // ABSTRACT_REQUEST_HANDLER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef CONDITION_VALIDATOR_HH_
-#define CONDITION_VALIDATOR_HH_
-
-#include <list>
-#include <string>
-
-#include "include/error_type.hh"
-#include "include/rsc_path_info.hh"
-#include "include/request_type.hh"
-
-namespace rsc_handler {
-
-class ConditionValidator {
- public:
- ConditionValidator(std::string pkgid, uid_t uid);
- ErrorType ValidateCondition(ReqType req_type,
- std::list<RscPathInfo> path_list);
-
- private:
- std::string pkgid_;
- std::string root_path_;
- uid_t uid_;
-
- ErrorType CheckCopyRequest(std::list<RscPathInfo> path_list);
- ErrorType CheckRemoveRequest(std::list<RscPathInfo> path_list);
-};
-
-} // namespace rsc_handler
-
-#endif // CONDITION_VALIDATOR_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef COPY_REQUEST_HANDLER_HH_
-#define COPY_REQUEST_HANDLER_HH_
-
-#include <list>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/rsc_path_info.hh"
-
-namespace rsc_handler {
-
-class CopyRequestHandler : public AbstractRequestHandler {
- public:
- CopyRequestHandler(std::string pkgid, std::string root_path,
- std::list<RscPathInfo> path_list);
-
- ErrorType Execute() override;
- const std::string GetRequestHandlerType() const override;
-};
-
-} // namespace rsc_handler
-
-#endif // COPY_REQUEST_HANDLER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ERROR_TYPE_HH_
-#define ERROR_TYPE_HH_
-
-namespace rsc_handler {
-
-enum ErrorType {
- ERROR_NONE = 0,
- ERROR_INVALID_PARAMETER,
- ERROR_PKG_NOT_FOUND,
- ERROR_PERMISSION_DENIED,
- ERROR_SYSTEM_ERROR,
- ERROR_RES_NOT_FOUND,
- ERROR_OUT_OF_SPACE,
- ERROR_OUT_OF_MEMORY
-};
-
-} // namespace rsc_handler
-
-#endif // ERROR_TYPE_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef EVENT_SIGNAL_SENDER_HH_
-#define EVENT_SIGNAL_SENDER_HH_
-
-#include <string>
-
-#include "include/error_type.hh"
-#include "include/request_type.hh"
-
-namespace rsc_handler {
-
-class EventSignalSender {
- public:
- EventSignalSender() : pkgid_(""), req_type_(ReqType::REQ_TYPE_UNKNOWN) {};
- bool SendStart();
- bool SendOK();
- bool SendFail(ErrorType error);
- void SetPkgID(std::string pkgid);
- void SetReqType(ReqType req_type);
-
- private:
- std::string pkgid_;
- ReqType req_type_;
-};
-
-} // rsc_handler
-
-#endif // PARAM_CHECKER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef FILE_UTIL_HH_
-#define FILE_UTIL_HH_
-
-#include <boost/filesystem.hpp>
-#include <boost/filesystem/path.hpp>
-
-namespace rsc_handler {
-
-// TODO(jungh.yeon) : is this necessary?
-enum FSFlag : int {
- FS_NONE = 0,
- FS_MERGE_SKIP = (1 << 0),
- FS_MERGE_OVERWRITE = (1 << 1),
- FS_COMMIT_COPY_FILE = (1 << 2),
- FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
-};
-
-FSFlag operator|(FSFlag a, FSFlag b);
-
-bool CreateDir(const boost::filesystem::path& path);
-
-bool CopyDir(const boost::filesystem::path& src,
- const boost::filesystem::path& dst,
- FSFlag flags = FS_NONE, bool skip_symlink = false);
-
-bool CopyFile(const boost::filesystem::path& src,
- const boost::filesystem::path& dst);
-
-bool RemoveAll(const boost::filesystem::path& path);
-
-} // namespace rsc_handler
-
-#endif // FILE_UTIL_HH_
+++ /dev/null
-// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by a apache 2.0 license that can be
-// found in the LICENSE file.
-
-#ifndef LOGGING_HH_
-#define LOGGING_HH_
-
-#include <dlog.h>
-
-#ifndef PROJECT_TAG
-#define PROJECT_TAG "PKGMGR_TOOL"
-#endif
-
-#ifdef LOG
-#undef LOG
-#endif
-
-#include <cassert>
-#include <climits>
-#include <cstdio>
-#include <cstring>
-#include <iomanip>
-#include <iostream>
-#include <memory>
-#include <sstream>
-#include <string>
-#include <vector>
-
-#ifndef __FILENAME__
-#define __FILENAME__ \
- (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
-#endif
-
-namespace utils {
-
-enum class LogLevel {
- LOG_ERROR,
- LOG_WARNING,
- LOG_INFO,
- LOG_DEBUG,
-};
-
-log_priority LogLevelToPriority(LogLevel level);
-
-template<LogLevel> struct LogTag;
-template<> struct LogTag<LogLevel::LOG_ERROR> {
- static constexpr const char* value = "\033[1;31m| ERROR |\033[0m";
-};
-template<> struct LogTag<LogLevel::LOG_WARNING> {
- static constexpr const char* value = "\033[1;33m| WARNING |\033[0m";
-};
-template<> struct LogTag<LogLevel::LOG_INFO> {
- static constexpr const char* value = "\033[1;32m| INFO |\033[0m";
-};
-template<> struct LogTag<LogLevel::LOG_DEBUG> {
- static constexpr const char* value = "\033[0m| DEBUG |\033[0m";
-};
-
-template <class charT, class traits = std::char_traits<charT>>
-class StringStream : private std::basic_ostringstream<charT, traits> {
- public:
- using std::basic_ostringstream<charT, traits>::str;
-
- template <class T>
- StringStream& operator<<(const T& value) {
- static_cast<std::basic_ostringstream<charT, traits> &>(*this) << value;
- return *this;
- }
-};
-
-// Interface class for logging backends. The custom LogBackend which wants
-// log using LOG() macro should be implement following interface.
-class ILogBackend {
- public:
- virtual void WriteLog(LogLevel level, const std::string& tag,
- const std::string& logstr) = 0;
-};
-
-class DLogBackend : public ILogBackend {
- public:
- void WriteLog(LogLevel level, const std::string& tag,
- const std::string& logstr) override {
- dlog_print(LogLevelToPriority(level), tag.c_str(), "%s",
- Escape(logstr).c_str());
- }
-
- private:
- // Since LogCatcher passes input to dlog_print(), the input which contains
- // format string(such as %d, %n) can cause unexpected result.
- // This is simple function to escape '%'.
- // NOTE: Is there any gorgeous way instead of this?
- std::string Escape(const std::string& str) const {
- std::string escaped = std::string(str);
- size_t start_pos = 0;
- std::string from = "%";
- std::string to = "%%";
- while ((start_pos = escaped.find(from, start_pos)) != std::string::npos) {
- escaped.replace(start_pos, from.length(), to);
- start_pos += to.length();
- }
- return escaped;
- }
-};
-
-class LogCore {
- public:
- // Do not call this function at destructor of global object
- static LogCore& GetCore() {
- static LogCore core;
- return core;
- }
-
- void AddLogBackend(std::shared_ptr<ILogBackend> backend) {
- backend_list_.emplace_back(backend);
- }
-
- void Log(LogLevel level, const std::string& tag, const std::string& log) {
- for (auto& backend : backend_list_)
- backend->WriteLog(level, tag, log);
- }
-
- private:
- LogCore() {
- // add default dlog backend
- AddLogBackend(std::shared_ptr<ILogBackend>(new DLogBackend()));
- }
- ~LogCore() = default;
- LogCore(const LogCore&) = delete;
- LogCore& operator=(const LogCore&) = delete;
-
- std::vector<std::shared_ptr<ILogBackend>> backend_list_;
-};
-
-class LogCatcher {
- public:
- LogCatcher(LogLevel level, const char* tag)
- : level_(level), tag_(tag) { }
-
- void operator&(const StringStream<char>& str) const {
- LogCore::GetCore().Log(level_, tag_, str.str());
- }
-
- private:
- LogLevel level_;
- std::string tag_;
-};
-
-} // namespace utils
-
-
-inline static const constexpr char* __tag_for_logging() {
- return "";
-}
-
-inline static const constexpr char* __tag_for_project() {
- return PROJECT_TAG;
-}
-
-// To be defined in class namespace if user want different log tag for given
-// scope
-#define SCOPE_LOG_TAG(TAG) \
- inline static const constexpr char* __tag_for_logging() { \
- return #TAG; \
- } \
-
-// Simple logging macro of following usage:
-// LOG(LEVEL) << object_1 << object_2 << object_n;
-// where:
-// LEVEL = ERROR | WARNING | INFO | DEBUG
-#define LOG(LEVEL) \
- ::utils::LogCatcher( \
- ::utils::LogLevel::LOG_ ## LEVEL, __tag_for_project()) \
- & ::utils::StringStream<char>() \
- << std::string(::utils::LogTag<::utils::LogLevel::LOG_ ## LEVEL>::value) \
- << " " << std::setw(25) << std::left << __tag_for_logging() \
- << " : " << std::setw(36) \
- << (std::string(__FILENAME__) + ":" + std::to_string(__LINE__)).c_str() \
- << std::setw(0) << " : " \
-
-#endif // LOGGING_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef PARAM_CHECKER_HH_
-#define PARAM_CHECKER_HH_
-
-#include <list>
-#include <string>
-
-#include "include/request_type.hh"
-#include "include/rsc_path_info.hh"
-
-namespace rsc_handler {
-
-class ParamChecker {
- public:
- ParamChecker(int argc, char* argv[]);
- ReqType GetRequestType();
- std::string GetPkgID() const;
- uid_t GetUID() const;
- const std::list<RscPathInfo>& GetPathList() const;
- bool Validate();
-
- private:
- std::list<RscPathInfo> path_info_list_;
- std::string pkgid_;
- std::string session_id_;
- uid_t uid_ = 0;
- ReqType req_type_ = ReqType::REQ_TYPE_UNKNOWN;
-
- void SetRequestType(std::string key);
- bool ValidatePkgID();
-};
-
-} // rsc_handler
-
-#endif // PARAM_CHECKER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef REMOVE_REQUEST_HANDLER_HH_
-#define REMOVE_REQUEST_HANDLER_HH_
-
-#include <list>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/rsc_path_info.hh"
-
-namespace rsc_handler {
-
-class RemoveRequestHandler : public AbstractRequestHandler {
- public:
- RemoveRequestHandler(
- std::string pkgid, std::string root_path,
- std::list<RscPathInfo> path_list);
-
- ErrorType Execute() override;
- const std::string GetRequestHandlerType() const override;
-};
-
-} // namespace rsc_handler
-
-#endif // REMOVE_REQUEST_HANDLER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef REQUEST_HANDLER_INVOKER_HH_
-#define REQUEST_HANDLER_INVOKER_HH_
-
-#include <memory>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/event_signal_sender.hh"
-#include "include/param_checker.hh"
-
-namespace rsc_handler {
-
-class RequestHandlerInvoker {
- public:
- RequestHandlerInvoker(ParamChecker option, EventSignalSender signal);
-
- bool Validate();
- bool Execute();
- const std::string GetHandlerType() const;
-
- private:
- std::unique_ptr<AbstractRequestHandler> handler_;
- ParamChecker option_;
- EventSignalSender signal_;
-
- void SetReqHandler();
-};
-
-} // rsc_handler
-
-#endif // REQUEST_HANDLER_INVOKER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef REQUEST_TYPE_HH_
-#define REQUEST_TYPE_HH_
-
-namespace rsc_handler {
-
-enum ReqType {
- REQ_TYPE_NEW = 0,
- REQ_TYPE_REMOVE = 1,
- REQ_TYPE_UNINSTALL,
- REQ_TYPE_UNKNOWN
-};
-
-} // namespace rsc_handler
-
-#endif // REQUEST_TYPE_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef RSC_HANDLER_HH_
-#define RSC_HANDLER_HH_
-
-#include <memory>
-
-#include "include/request_handler_invoker.hh"
-
-namespace rsc_handler {
-
-class RscHandler {
- public:
- RscHandler() {};
- //~RscHandler();
- bool Init(int argc, char* argv[]);
- bool Run();
-
- private:
- std::unique_ptr<rsc_handler::RequestHandlerInvoker> handler_;
-};
-
-} // rsc_handler
-
-#endif // RSC_HANDLER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef RSC_PATH_INFO_HH_
-#define RSC_PATH_INFO_HH_
-
-#include <string>
-
-namespace rsc_handler {
-
-class RscPathInfo {
- public:
- RscPathInfo(std::string src, std::string dst);
-
- const std::string& GetSrcPath() const;
- const std::string& GetDstPath() const;
-
- private:
- std::string src_;
- std::string dst_;
-};
-
-} // rsc_handler
-
-#endif // RSC_PATH_INFO_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef UNINSTALL_REQUEST_HANDLER_HH_
-#define UNINSTALL_REQUEST_HANDLER_HH_
-
-#include <list>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/rsc_path_info.hh"
-
-namespace rsc_handler {
-
-class UninstallRequestHandler : public AbstractRequestHandler {
- public:
- UninstallRequestHandler(
- std::string pkgid, std::string root_path, std::list<RscPathInfo> path_list);
-
- ErrorType Execute() override;
- const std::string GetRequestHandlerType() const override;
-};
-
-} // namespace rsc_handler
-
-#endif // UNINSTALL_REQUEST_HANDLER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/abstract_request_handler.hh"
-
-#include <iostream>
-
-#include "include/request_type.hh"
-#include "include/rsc_path_info.hh"
-
-namespace rsc_handler {
-
-std::string AbstractRequestHandler::GetRootPath() {
- return root_path_;
-}
-
-const std::string AbstractRequestHandler::GetPkgID() const {
- return pkgid_;
-}
-
-const std::list<RscPathInfo> AbstractRequestHandler::GetPathList() const {
- return path_list_;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/condition_validator.hh"
-
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-#include <boost/system/error_code.hpp>
-#include <tzplatform_config.h>
-
-#include <sys/stat.h>
-
-#include <iostream>
-#include <list>
-#include <string>
-
-#include "include/logging.hh"
-#include "include/request_type.hh"
-#include "include/rsc_path_info.hh"
-
-#include <pkgmgr-info.h>
-
-namespace bf = boost::filesystem;
-
-namespace {
-
-std::string GetRootPath(uid_t uid) {
- tzplatform_set_user(uid);
- const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
- tzplatform_reset_user();
- return rootpath;
-}
-
-bool IsPackageExists(std::string pkgid, uid_t uid) {
- pkgmgrinfo_pkginfo_h handle;
- if (pkgmgrinfo_pkginfo_get_usr_pkginfo(
- pkgid.c_str(), uid, &handle) != PMINFO_R_OK) {
- LOG(ERROR) << "package not exists : " << pkgid;
- return false;
- }
- pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-
- return true;
-}
-
-bool CheckFreeSpaceAtPath(int64_t required_size,
- const bf::path& target_location) {
- boost::system::error_code error;
- bf::path root = target_location;
-
- while (!bf::exists(root) && root != root.root_path())
- root = root.parent_path();
-
- if (!bf::exists(root)) {
- LOG(ERROR) << "No mount point for path: " << target_location;
- return false;
- }
-
- bf::space_info space_info = bf::space(root, error);
- if (error) {
- LOG(ERROR) << "Failed to get space_info: " << error.message();
- return false;
- }
-
- return (space_info.free >= static_cast<uint64_t>(required_size));
-}
-
-int64_t GetBlockSizeForPath(const bf::path& path_in_partition) {
- struct stat stats;
- if (stat(path_in_partition.string().c_str(), &stats)) {
- LOG(ERROR) << "stat(" << path_in_partition.string()
- << ") failed - error code: " << errno;
- return -1;
- }
- return stats.st_blksize;
-}
-
-int64_t RoundUpToBlockSizeOf(int64_t size, int64_t block_size) {
- return ((size + block_size - 1) / block_size) * block_size;
-}
-
-int64_t GetDirectorySize(const bf::path& path) {
- int64_t block_size = GetBlockSizeForPath(path);
-
- if (block_size == -1)
- return -1;
-
- int64_t size = 0;
- for (bf::recursive_directory_iterator iter(path);
- iter != bf::recursive_directory_iterator(); ++iter) {
- struct stat buf;
- if (lstat(iter->path().c_str(), &buf) == -1) {
- LOG(ERROR) << "lstat() failed for: " << iter->path();
- return -1;
- }
- size += RoundUpToBlockSizeOf(buf.st_size, block_size);
- }
-
- return size;
-}
-
-} // namespace
-
-namespace rsc_handler {
-
-ConditionValidator::ConditionValidator(std::string pkgid, uid_t uid) :
- pkgid_(pkgid), uid_(uid) {
- root_path_ = GetRootPath(uid_);
-}
-
-ErrorType ConditionValidator::ValidateCondition(ReqType req_type,
- std::list<RscPathInfo> path_list) {
- if (!IsPackageExists(pkgid_, uid_))
- return ErrorType::ERROR_PKG_NOT_FOUND;
-
- if (req_type == ReqType::REQ_TYPE_NEW)
- return CheckCopyRequest(path_list);
- else if (req_type == ReqType::REQ_TYPE_REMOVE)
- return CheckRemoveRequest(path_list);
-
- return ErrorType::ERROR_NONE;
-}
-
-ErrorType ConditionValidator::CheckCopyRequest(
- std::list<RscPathInfo> path_list) {
- boost::filesystem::path src_root_path(root_path_);
- uintmax_t rsc_size = 0;
-
- src_root_path = src_root_path / "apps_rw" / pkgid_;
- for (auto& path_info : path_list) {
- boost::filesystem::path rsc_path(src_root_path);
- rsc_path = rsc_path / path_info.GetSrcPath();
- if (!boost::filesystem::exists(rsc_path)) {
- LOG(ERROR) << "Resource not exists : " << rsc_path;
- return ErrorType::ERROR_RES_NOT_FOUND;
- }
-
- if (boost::filesystem::is_directory(rsc_path))
- rsc_size += GetDirectorySize(rsc_path);
- else
- rsc_size += boost::filesystem::file_size(rsc_path);
- }
- LOG(INFO) << "Required size for resource: " << rsc_size;
-
- if (!CheckFreeSpaceAtPath(static_cast<int64_t>(rsc_size), root_path_)) {
- LOG(ERROR) << "Not enough space for resource";
- return ErrorType::ERROR_OUT_OF_SPACE;
- }
-
- return ErrorType::ERROR_NONE;
-}
-
-ErrorType ConditionValidator::CheckRemoveRequest(
- std::list<RscPathInfo> path_list) {
- for (auto& path_info : path_list) {
- boost::filesystem::path dst_path(root_path_);
- dst_path = dst_path / "shared_res"/ pkgid_ / path_info.GetDstPath();
- if (!boost::filesystem::exists(dst_path)) {
- LOG(ERROR) << "Resource not exists : " << dst_path;
- return ErrorType::ERROR_RES_NOT_FOUND;
- }
- }
-
- return ErrorType::ERROR_NONE;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/copy_request_handler.hh"
-
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-
-#include <iostream>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/file_util.hh"
-#include "include/logging.hh"
-#include "include/rsc_path_info.hh"
-
-namespace bf = boost::filesystem;
-
-namespace {
-
-constexpr char kCopyReqHandlerType[] = "copy";
-
-} // namespace
-
-namespace rsc_handler {
-
-CopyRequestHandler::CopyRequestHandler(
- std::string pkgid, std::string root_path,
- std::list<RscPathInfo> path_list) :
- AbstractRequestHandler(pkgid, root_path, path_list) {}
-
-ErrorType CopyRequestHandler::Execute() {
- bf::path root_path(GetRootPath());
- bf::path src_root_path = root_path / "apps_rw" / GetPkgID();
- bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
-
- if (!CreateDir(dst_root_path))
- return ErrorType::ERROR_SYSTEM_ERROR;
-
- for (auto& path_info : GetPathList()) {
- bf::path src_path = src_root_path / path_info.GetSrcPath();
- if (!bf::exists(src_path)) {
- LOG(ERROR) << "Path not exists :" << src_path;
- return ErrorType::ERROR_RES_NOT_FOUND;
- }
-
- bf::path dst_path = dst_root_path / path_info.GetDstPath();
-
- if (bf::is_directory(src_path)) {
- if (!CopyDir(src_path, dst_path,
- FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS, true)) {
- LOG(ERROR) << "Failed to copy directory " << src_path;
- return ErrorType::ERROR_SYSTEM_ERROR;
- }
- } else {
- if (bf::is_directory(dst_path))
- dst_path /= src_path.filename();
-
- if (!CopyFile(src_path, dst_path)) {
- LOG(ERROR) << "Failed to copy directory " << src_path;
- return ErrorType::ERROR_SYSTEM_ERROR;
- }
- }
- }
-
- return ErrorType::ERROR_NONE;
-}
-
-const std::string CopyRequestHandler::GetRequestHandlerType() const {
- return kCopyReqHandlerType;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/event_signal_sender.hh"
-
-#include <iostream>
-#include <string>
-
-#include "include/error_type.hh"
-#include "include/request_type.hh"
-
-namespace rsc_handler {
-
-bool EventSignalSender::SendStart() {
- std::cout << "EventSignalSender::SendStart" << std::endl;
-
- return true;
-}
-
-bool EventSignalSender::SendOK() {
- std::cout << "EventSignalSender::SendOK" << std::endl;
-
- return true;
-}
-
-bool EventSignalSender::SendFail(ErrorType error) {
- std::cout << "EventSignalSender::SendFail" << std::endl;
-
- return true;
-}
-
-void EventSignalSender::SetPkgID(std::string pkgid) {
- pkgid_ = pkgid;
-}
-
-void EventSignalSender::SetReqType(ReqType req_type) {
- req_type_ = req_type;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/file_util.hh"
-
-#include <fcntl.h>
-#include <sys/stat.h>
-
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-#include <boost/system/error_code.hpp>
-
-#include "include/logging.hh"
-
-namespace bs = boost::system;
-namespace bf = boost::filesystem;
-
-namespace rsc_handler {
-
-FSFlag operator|(FSFlag a, FSFlag b) {
- return static_cast<FSFlag>(static_cast<int>(a) | static_cast<int>(b));
-}
-
-bool SetDirPermissions(const boost::filesystem::path& path,
- boost::filesystem::perms permissions) {
- boost::system::error_code error;
- bf::permissions(path, permissions, error);
-
- if (error) {
- LOG(ERROR) << "Failed to set permissions for directory: " << path
- << boost::system::system_error(error).what();
- return false;
- }
- return true;
-}
-
-bool SetOwnership(const bf::path& path, uid_t uid, gid_t gid) {
- int ret = lchown(path.c_str(), uid, gid);
- if (ret != 0) {
- LOG(ERROR) << "Failed to change owner of: " << path;
- return false;
- }
- return true;
-}
-
-bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
- boost::filesystem::perms permissions, uid_t uid,
- gid_t gid) {
- if (!SetOwnership(path, uid, gid)) {
- LOG(ERROR) << "Failed to change owner: " << path
- << "(" << uid << ", " << gid << ")";
- return false;
- }
- if (!SetDirPermissions(path, permissions)) {
- LOG(ERROR) << "Failed to change permission: " << path
- << std::oct << permissions;
- return false;
- }
-
- return true;
-}
-
-bool CopyOwnershipAndPermissions(const boost::filesystem::path& src,
- const boost::filesystem::path& dst) {
- if (!bf::exists(src)) {
- LOG(ERROR) << "Failed to copy ownership and permissions"
- << " from " << src << " to " << dst;
- return false;
- }
- bf::perms permissions = bf::status(src).permissions();
- struct stat stats;
- if (stat(src.c_str(), &stats) != 0)
- return false;
- if (!SetDirOwnershipAndPermissions(dst, permissions, stats.st_uid,
- stats.st_gid)) {
- LOG(ERROR) << "Failed to copy ownership and permissions"
- << " from " << src << " to " << dst;
- return false;
- }
- return true;
-}
-
-bool RemoveAll(const bf::path& path) {
- if (!exists(path) && !bf::is_symlink(bf::symlink_status(path)))
- return true;
-
- bs::error_code error;
- bf::remove_all(path, error);
-
- if (error) {
- LOG(ERROR) << "Cannot remove: " << path << ", " << error.message();
- return false;
- }
-
- return true;
-}
-
-bool CopyDir(const boost::filesystem::path& src,
- const boost::filesystem::path& dst,
- FSFlag flags, bool skip_symlink) {
- try {
- // Check whether the function call is valid
- if (!bf::exists(src) || !bf::is_directory(src)) {
- LOG(ERROR) << "Source directory " << src
- << " does not exist or is not a directory.";
- return false;
- }
- if (!bf::exists(dst)) {
- // Create the destination directory
- if (!CreateDir(dst)) {
- LOG(ERROR) << "Unable to create destination directory" << dst;
- return false;
- }
- if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
- CopyOwnershipAndPermissions(src, dst);
- } else {
- if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
- LOG(ERROR) << "Destination directory " << dst.string()
- << " already exists.";
- return false;
- }
- if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS))
- CopyOwnershipAndPermissions(src, dst);
- }
- } catch (const bf::filesystem_error& error) {
- LOG(ERROR) << "Failed to copy directory: " << error.what();
- return false;
- }
-
- // Iterate through the source directory
- for (bf::directory_iterator file(src);
- file != bf::directory_iterator();
- ++file) {
- try {
- bf::path current(file->path());
- bf::path target = dst / current.filename();
-
- if (bf::is_symlink(symlink_status(current))) {
- if (skip_symlink)
- continue;
- if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) &&
- bf::exists(target))
- continue;
- bs::error_code error;
- bf::copy_symlink(current, target, error);
- if (error) {
- LOG(ERROR) << "Failed to copy symlink: " << current << ", "
- << error.message();
- return false;
- }
- } else if (bf::is_directory(current)) {
- // Found directory: Recursion
- if (!CopyDir(current, target, flags, skip_symlink)) {
- return false;
- }
- } else {
- if ((flags & FS_MERGE_SKIP) && bf::exists(target))
- continue;
- bf::path destination = target;
-
- if (flags & FS_COMMIT_COPY_FILE)
- destination =
- bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
-
- if (flags & FS_MERGE_OVERWRITE)
- bf::copy_file(current, destination,
- bf::copy_option::overwrite_if_exists);
- else
- bf::copy_file(current, destination);
-
- if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
- CopyOwnershipAndPermissions(current, destination);
-
- if (flags & FS_COMMIT_COPY_FILE) {
- if (flags & FS_MERGE_OVERWRITE)
- bf::remove(target);
- bf::rename(destination, target);
- }
- }
- } catch (const bf::filesystem_error& error) {
- LOG(ERROR) << "Failed to copy directory: " << error.what();
- return false;
- }
- }
- return true;
-}
-
-bool CreateDir(const bf::path& path) {
- if (bf::exists(path))
- return true;
-
- boost::system::error_code error;
- bf::create_directories(path, error);
-
- if (error) {
- LOG(ERROR) << "Failed to create directory: "
- << boost::system::system_error(error).what();
- return false;
- }
- return true;
-}
-
-bool CopyFile(const bf::path& src, const bf::path& dst) {
- bs::error_code error;
-
- bf::copy_file(src, dst, bf::copy_option::overwrite_if_exists, error);
- if (error) {
- LOG(WARNING) << "copy file " << src << " due to error ["
- << error.message() << "]";
- return false;
- }
- return true;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by a apache 2.0 license that can be
-// found in the LICENSE file.
-
-#include "logging.hh"
-
-namespace utils {
-
-log_priority LogLevelToPriority(LogLevel level) {
- switch (level) {
- case LogLevel::LOG_ERROR:
- return log_priority::DLOG_ERROR;
- case LogLevel::LOG_WARNING:
- return log_priority::DLOG_WARN;
- case LogLevel::LOG_INFO:
- return log_priority::DLOG_INFO;
- case LogLevel::LOG_DEBUG:
- return log_priority::DLOG_DEBUG;
- default:
- return log_priority::DLOG_UNKNOWN;
- }
-}
-
-} // namespace utils
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/param_checker.hh"
-
-#include <boost/exception/diagnostic_information.hpp>
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-#include <boost/program_options.hpp>
-#include <boost/system/error_code.hpp>
-
-#include "include/logging.hh"
-#include "include/request_type.hh"
-#include "include/rsc_path_info.hh"
-
-#include <pkgmgr-info.h>
-
-#include <string>
-#include <iostream>
-
-namespace bs = boost::system;
-namespace bpo = boost::program_options;
-
-namespace rsc_handler {
-
-ParamChecker::ParamChecker(int argc, char* argv[]) {
- bpo::options_description options;
-
- options.add_options()
- ("uid,u", bpo::value<int>()->default_value(0), "user id")
- ("session-id,k", bpo::value<std::string>(), "session id")
- ("path,p", bpo::value<std::vector<std::string>>()->multitoken(),
- "source-destination path")
- ("remove,r", bpo::value<std::string>(), "remove shared resource")
- ("delete,d", bpo::value<std::string>(),
- "delete shared resource for package")
- ("copy,c", bpo::value<std::string>(), "copy resource")
- ("help,h", "Show this message");
-
- bpo::parsed_options parsed_options = bpo::command_line_parser(argc, argv)
- .options(options)
- .run();
-
- for (const bpo::option& o : parsed_options.options) {
- if (o.string_key == "uid") {
- uid_ = static_cast<uid_t>(std::stoi(o.value.front()));
- } else if (o.string_key == "session-id") {
- session_id_ = o.value.front();
- } else if (o.string_key == "path") {
- if (o.value.front() == o.value.back())
- path_info_list_.emplace_back(o.value.front(), "");
- else
- path_info_list_.emplace_back(o.value.front(), o.value.back());
- } else if (o.string_key == "copy" ||
- o.string_key == "delete" ||
- o.string_key == "remove") {
- pkgid_ = o.value.front();
- SetRequestType(o.string_key);
- } else if (o.string_key == "help") {
- std::cout << options;
- } else {
- std::cout << "Invalid option : " << o.string_key << std::endl;
- }
- }
-}
-
-std::string ParamChecker::GetPkgID() const {
- return pkgid_;
-}
-
-uid_t ParamChecker::GetUID() const {
- return uid_;
-}
-
-ReqType ParamChecker::GetRequestType() {
- return req_type_;
-}
-
-const std::list<RscPathInfo>& ParamChecker::GetPathList() const {
- return path_info_list_;
-}
-
-bool ParamChecker::Validate() {
- if (uid_ == 0) {
- LOG(ERROR) << "Invalid uid: " << uid_;
- return false;
- }
-
- if (req_type_ == ReqType::REQ_TYPE_UNKNOWN) {
- LOG(ERROR) << "Invalid request type";
- return false;
- }
-
- if (!ValidatePkgID())
- return false;
-
- if (req_type_ != ReqType::REQ_TYPE_UNINSTALL) {
- if (path_info_list_.size() == 0) {
- LOG(ERROR) << "Path is not given";
- return false;
- }
- }
-
- return true;
-}
-
-void ParamChecker::SetRequestType(std::string key) {
- if (key == "copy")
- req_type_ = ReqType::REQ_TYPE_NEW;
- else if (key == "remove")
- req_type_ = ReqType::REQ_TYPE_REMOVE;
- else if (key == "delete")
- req_type_ = ReqType::REQ_TYPE_UNINSTALL;
-}
-
-bool ParamChecker::ValidatePkgID() {
- if (pkgid_.size() == 0) {
- LOG(ERROR) << "pkgid is empty";
- return false;
- }
-
- return true;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/remove_request_handler.hh"
-
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-
-#include <iostream>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/file_util.hh"
-#include "include/logging.hh"
-#include "include/rsc_path_info.hh"
-
-namespace bf = boost::filesystem;
-
-namespace {
-
-constexpr char kRemoveReqHandlerType[] = "remove";
-
-} // namespace
-
-namespace rsc_handler {
-
-RemoveRequestHandler::RemoveRequestHandler(
- std::string pkgid, std::string root_path,
- std::list<RscPathInfo> path_list) :
- AbstractRequestHandler(pkgid, root_path, path_list) {}
-
-ErrorType RemoveRequestHandler::Execute() {
- bf::path root_path(GetRootPath());
- bf::path dst_root_path = root_path / "shared_res" / GetPkgID();
-
- if (!bf::exists(dst_root_path)) {
- LOG(ERROR) << "root path not exists";
- return ErrorType::ERROR_RES_NOT_FOUND;
- }
-
- for (auto& path_info : GetPathList()) {
- bf::path dst_path = dst_root_path / path_info.GetSrcPath();
- if (!bf::exists(dst_path)) {
- LOG(ERROR) << "Path not exists: " << dst_path;
- continue;
- }
-
- if (!RemoveAll(dst_path))
- return ErrorType::ERROR_SYSTEM_ERROR;
- }
-
- return ErrorType::ERROR_NONE;
-}
-
-const std::string RemoveRequestHandler::GetRequestHandlerType() const {
- return kRemoveReqHandlerType;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/request_handler_invoker.hh"
-
-#include <iostream>
-
-#include <tzplatform_config.h>
-
-#include "include/abstract_request_handler.hh"
-#include "include/condition_validator.hh"
-#include "include/copy_request_handler.hh"
-#include "include/event_signal_sender.hh"
-#include "include/logging.hh"
-#include "include/param_checker.hh"
-#include "include/remove_request_handler.hh"
-#include "include/request_type.hh"
-#include "include/uninstall_request_handler.hh"
-
-namespace {
-
-std::string GetRootPathForUid(uid_t uid) {
- tzplatform_set_user(uid);
- const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
- tzplatform_reset_user();
- return rootpath;
-}
-
-} // namespace
-
-namespace rsc_handler {
-
-RequestHandlerInvoker::RequestHandlerInvoker(
- ParamChecker option, EventSignalSender signal) :
- option_(option), signal_(signal) {
- SetReqHandler();
-}
-
-bool RequestHandlerInvoker::Validate() {
- if (handler_ == nullptr) {
- LOG(ERROR) << "Failed to initialize handler";
- return false;
- }
-
- ConditionValidator validator(option_.GetPkgID(), option_.GetUID());
- if (validator.ValidateCondition(
- option_.GetRequestType(), option_.GetPathList())
- != ErrorType::ERROR_NONE) {
- LOG(ERROR) << "Validation failed";
- return false;
- }
-
- return true;
-}
-
-bool RequestHandlerInvoker::Execute() {
- if (handler_ == nullptr)
- return false;
-
- if (handler_->Execute() != ErrorType::ERROR_NONE)
- return false;
-
- return true;
-}
-
-const std::string RequestHandlerInvoker::GetHandlerType() const {
- if (handler_ == nullptr) {
- LOG(ERROR) << "handler has not initialized";
- return {};
- }
-
- return handler_->GetRequestHandlerType();
-}
-
-void RequestHandlerInvoker::SetReqHandler() {
- switch (option_.GetRequestType()) {
- case ReqType::REQ_TYPE_NEW:
- handler_.reset(
- new CopyRequestHandler(
- option_.GetPkgID(),
- GetRootPathForUid(option_.GetUID()),
- option_.GetPathList()));
- break;
- case ReqType::REQ_TYPE_REMOVE:
-
- handler_.reset(
- new RemoveRequestHandler(
- option_.GetPkgID(),
- GetRootPathForUid(option_.GetUID()),
- option_.GetPathList()));
- break;
- case ReqType::REQ_TYPE_UNINSTALL:
- handler_.reset(
- new UninstallRequestHandler(
- option_.GetPkgID(),
- GetRootPathForUid(option_.GetUID()),
- option_.GetPathList()));
- break;
- default:
- LOG(ERROR) << "Invalid request type";
- break;
- }
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <iostream>
-
-#include "include/rsc_handler.hh"
-#include "include/logging.hh"
-
-int main(int argc, char *argv[]) {
- try {
- rsc_handler::RscHandler handler;
- if (!handler.Init(argc, argv)) {
- LOG(ERROR) << "Failed to initliaze handler";
- return -1;
- }
-
- if (!handler.Run()) {
- LOG(ERROR) << "Failed to handle request";
- return -1;
- }
- } catch (...) {
- LOG(ERROR) << "Exception occured";
- return -1;
- }
-
- return 0;
-}
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/rsc_handler.hh"
-
-#include <iostream>
-
-#include "include/event_signal_sender.hh"
-#include "include/logging.hh"
-#include "include/param_checker.hh"
-#include "include/request_handler_invoker.hh"
-
-namespace rsc_handler {
-
-bool RscHandler::Init(int argc, char* argv[]) {
- rsc_handler::ParamChecker option(argc, argv);
- if (!option.Validate()) {
- LOG(ERROR) << "Invalid argument has given";
- return false;
- }
-
- rsc_handler::EventSignalSender signal;
- signal.SetPkgID(option.GetPkgID());
- signal.SetReqType(option.GetRequestType());
-
- handler_.reset(new rsc_handler::RequestHandlerInvoker(option, signal));
- if (!handler_->Validate()) {
- LOG(ERROR) << "Failed to initialize request handler";
- return false;
- }
-
- return true;
-}
-
-bool RscHandler::Run() {
- return handler_->Execute();
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/rsc_path_info.hh"
-
-#include <iostream>
-#include <string>
-
-namespace rsc_handler {
-
-RscPathInfo::RscPathInfo(std::string src, std::string dst) :
- src_(src), dst_(dst) {}
-
-const std::string& RscPathInfo::GetSrcPath() const {
- return src_;
-}
-
-const std::string& RscPathInfo::GetDstPath() const {
- return dst_;
-}
-
-} // namespace rsc_handler
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/uninstall_request_handler.hh"
-
-#include <iostream>
-#include <string>
-
-#include "include/abstract_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/file_util.hh"
-#include "include/logging.hh"
-#include "include/rsc_path_info.hh"
-
-namespace bf = boost::filesystem;
-
-namespace {
-
-constexpr char kUninstallReqHandlerType[] = "delete";
-
-} // namespace
-
-namespace rsc_handler {
-
-UninstallRequestHandler::UninstallRequestHandler(
- std::string pkgid, std::string root_path,
- std::list<RscPathInfo> path_list) :
- AbstractRequestHandler(pkgid, root_path, path_list) {}
-
-ErrorType UninstallRequestHandler::Execute() {
- if (GetPkgID().length() == 0) {
- LOG(ERROR) << "Invalid argument";
- return ErrorType::ERROR_INVALID_PARAMETER;
- }
-
- bf::path root_path(GetRootPath());
- root_path = root_path / "shared_res" / GetPkgID();
-
- if (!bf::exists(root_path)) {
- LOG(WARNING) << "path not exists : " << root_path;
- return ErrorType::ERROR_NONE;
- }
-
- if (!RemoveAll(root_path))
- return ErrorType::ERROR_SYSTEM_ERROR;
-
- return ErrorType::ERROR_NONE;
-}
-
-const std::string UninstallRequestHandler::GetRequestHandlerType() const {
- return kUninstallReqHandlerType;
-}
-
-} // namespace rsc_handler
ADD_DEFINITIONS("-DDB_PATH=\"${DB_PATH}\"")
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkg_upgrade/include)
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/rsc-copy/)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../src/res-copy/)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
#pkg_upgrade
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkg_upgrade/src PKG_UPGRADE_LIB_SOURCES)
LIST(REMOVE_ITEM PKG_UPGRADE_LIB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/pkg_upgrade/src/main.cc)
-#rsc-copy
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/rsc-copy/src RSC_COPY_SRCS)
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/rsc-copy/src RSC_COPY_LIB_SOURCES)
-LIST(REMOVE_ITEM RSC_COPY_LIB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/rsc-copy/src/rsc_copy_main.cc)
-LIST(REMOVE_ITEM RSC_COPY_LIB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/rsc-copy/src/logging.cc)
+#res-copy
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/res-copy/src RES_COPY_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/res-copy/src RES_COPY_LIB_SOURCES)
+LIST(REMOVE_ITEM RES_COPY_LIB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/res-copy/src/res_copy_main.cc)
+LIST(REMOVE_ITEM RES_COPY_LIB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../src/res-copy/src/logging.cc)
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES)
ADD_EXECUTABLE(${PROJECT_NAME}
${PKG_UPGRADE_SRCS}
- ${RSC_COPY_SRCS}
+ ${RES_COPY_SRCS}
${MOCK_SOURCES}
${PKG_UPGRADE_LIB_SOURCES}
- ${RSC_COPY_LIB_SOURCES}
+ ${RES_COPY_LIB_SOURCES}
test_main.cc
)
--- /dev/null
+this is second resource file
--- /dev/null
+this is 3rd resource file
--- /dev/null
+this is resource file to being copied
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+
+#include <memory>
+
+#include "mock/os_mock.h"
+#include "mock/pkgmgr_info_mock.h"
+#include "mock/test_fixture.h"
+
+#include "include/condition_validator.hh"
+#include "include/param_checker.hh"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::InvokeArgument;
+using ::testing::SaveArg;
+
+using res_handler::ConditionValidator;
+using res_handler::ParamChecker;
+
+class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
+ public ::testing::NiceMock<OsMock> {};
+
+class ConditionValidatorTest : public TestFixture {
+ public:
+ ConditionValidatorTest() : TestFixture(std::make_unique<Mocks>()) {}
+ virtual ~ConditionValidatorTest() {}
+
+ virtual void SetUp() {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_destroy_pkginfo(_))
+ .WillRepeatedly(Return(0)) ;
+ }
+ virtual void TearDown() {}
+};
+
+TEST_F(ConditionValidatorTest, PkgNotExist) {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(-1));
+
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--copy",
+ "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+ ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
+ res_handler::ErrorType ret = validator.ValidateCondition(
+ checker.GetRequestType(), checker.GetPathList());
+
+ EXPECT_EQ(ret, res_handler::ErrorType::ERROR_PKG_NOT_FOUND);
+}
+
+TEST_F(ConditionValidatorTest, CopyPkgNotExist) {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(-1));
+
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--copy",
+ "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+ ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
+ res_handler::ErrorType ret = validator.ValidateCondition(
+ checker.GetRequestType(), checker.GetPathList());
+
+ EXPECT_EQ(ret, res_handler::ErrorType::ERROR_PKG_NOT_FOUND);
+}
+
+TEST_F(ConditionValidatorTest, CopySrcNotExist) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--copy",
+ "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+ ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
+ res_handler::ErrorType ret = validator.ValidateCondition(
+ checker.GetRequestType(), checker.GetPathList());
+
+ EXPECT_EQ(ret, res_handler::ErrorType::ERROR_RES_NOT_FOUND);
+}
+
+TEST_F(ConditionValidatorTest, RemovePkgNotExist) {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(-1));
+
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--remove",
+ "org.test.targetpkgid", "-p", "", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+ ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
+ res_handler::ErrorType ret = validator.ValidateCondition(
+ checker.GetRequestType(), checker.GetPathList());
+
+ EXPECT_EQ(ret, res_handler::ErrorType::ERROR_PKG_NOT_FOUND);
+}
+
+TEST_F(ConditionValidatorTest, RemoveSrcNotExist) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--remove",
+ "org.test.targetpkgid", "-p", "", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+ ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
+ res_handler::ErrorType ret = validator.ValidateCondition(
+ checker.GetRequestType(), checker.GetPathList());
+
+ EXPECT_EQ(ret, res_handler::ErrorType::ERROR_RES_NOT_FOUND);
+}
+
+TEST_F(ConditionValidatorTest, DeletePkgNotExist) {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(-1));
+
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--delete",
+ "org.test.targetpkgid", nullptr};
+ ParamChecker checker(5, (char**)argv);
+ ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
+ res_handler::ErrorType ret = validator.ValidateCondition(
+ checker.GetRequestType(), checker.GetPathList());
+
+ EXPECT_EQ(ret, res_handler::ErrorType::ERROR_PKG_NOT_FOUND);
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+
+#include <memory>
+
+#include "mock/os_mock.h"
+#include "mock/pkgmgr_info_mock.h"
+#include "mock/test_fixture.h"
+#include "include/param_checker.hh"
+#include "include/request_type.hh"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::InvokeArgument;
+using ::testing::SaveArg;
+
+using res_handler::ParamChecker;
+
+class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
+ public ::testing::NiceMock<OsMock> {};
+
+class ParamCheckerTest : public TestFixture {
+ public:
+ ParamCheckerTest() : TestFixture(std::make_unique<Mocks>()) {}
+ virtual ~ParamCheckerTest() {}
+
+ virtual void SetUp() {
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
+ .WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_destroy_pkginfo(_))
+ .WillRepeatedly(Return(0)) ;
+ }
+ virtual void TearDown() {}
+};
+
+TEST_F(ParamCheckerTest, InvalidUIDTest) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "0", "--copy",
+ "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, PkgIDNotGiven) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001",
+ "-p", "srcpath", "dstpath", "--copy", "", nullptr};
+ ParamChecker checker(8, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, CopyRes) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "-p", "srcpath", "dstpath",
+ "--copy", "org.test.targetpkgid", nullptr};
+
+ ParamChecker checker(8, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), true);
+ EXPECT_EQ(checker.GetRequestType(), res_handler::ReqType::REQ_TYPE_NEW);
+ EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
+ EXPECT_EQ(checker.GetPathList().size(), 1);
+ EXPECT_EQ(checker.GetUID(), 5001);
+}
+
+TEST_F(ParamCheckerTest, RemoveRes) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--remove",
+ "org.test.targetpkgid", "-p", "dstpath", nullptr};
+
+ ParamChecker checker(7, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), true);
+ EXPECT_EQ(checker.GetRequestType(), res_handler::ReqType::REQ_TYPE_REMOVE);
+ EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
+ EXPECT_EQ(checker.GetPathList().size(), 1);
+ EXPECT_EQ(checker.GetUID(), 5001);
+}
+
+TEST_F(ParamCheckerTest, DeleteRes) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--delete",
+ "org.test.targetpkgid", nullptr};
+
+ ParamChecker checker(5, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), true);
+ EXPECT_EQ(checker.GetRequestType(), res_handler::ReqType::REQ_TYPE_UNINSTALL);
+ EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
+ EXPECT_EQ(checker.GetPathList().size(), 0);
+ EXPECT_EQ(checker.GetUID(), 5001);
+}
+
+TEST_F(ParamCheckerTest, EmptyPkgID) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--copy",
+ "", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8,(char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, UnknownReqType) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "-p",
+ "srcpath", "dstpath", nullptr};
+ ParamChecker checker(6, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
+
+TEST_F(ParamCheckerTest, PathNotGiven) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001",
+ "-c", "org.tizen.pathnotgiven", nullptr};
+ ParamChecker checker(5, (char**)argv);
+
+ EXPECT_EQ(checker.Validate(), false);
+}
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/system/error_code.hpp>
+
+#include <list>
+#include <memory>
+#include <string>
+
+#include "mock/os_mock.h"
+#include "mock/pkgmgr_info_mock.h"
+#include "mock/test_fixture.h"
+
+#include "include/abstract_request_handler.hh"
+#include "include/copy_request_handler.hh"
+#include "include/error_type.hh"
+#include "include/param_checker.hh"
+#include "include/remove_request_handler.hh"
+#include "include/request_type.hh"
+#include "include/uninstall_request_handler.hh"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::InvokeArgument;
+using ::testing::SaveArg;
+
+using res_handler::CopyRequestHandler;
+using res_handler::RemoveRequestHandler;
+using res_handler::ResPathInfo;
+using res_handler::UninstallRequestHandler;
+
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+
+class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
+ public ::testing::NiceMock<OsMock> {};
+
+class CopyRequestHandlerTest : public TestFixture {
+ public:
+ CopyRequestHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
+ virtual ~CopyRequestHandlerTest() {}
+
+ virtual void SetUp() {
+ bf::path rootpath(root_path);
+ bf::create_directories(
+ rootpath / "shared_res/test_pkg/dest_dir");
+ }
+ virtual void TearDown() {
+ bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+ }
+
+ std::string root_path = "./tests/unit_tests/res-copy/data";
+};
+
+class RemoveRequestHandlerTest : public TestFixture {
+ public:
+ RemoveRequestHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
+ virtual ~RemoveRequestHandlerTest() {}
+
+ virtual void SetUp() {
+ bf::path rootpath(root_path);
+ bf::create_directories(
+ rootpath / "shared_res/test_pkg/new_dir/new_dir2");
+
+ bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
+ rootpath / "shared_res/test_pkg/resource_file.txt");
+
+ bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
+ rootpath / "shared_res/test_pkg/new_dir/resource_file.txt");
+
+ bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
+ rootpath / "shared_res/test_pkg/new_dir/new_dir2/resource_file.txt");
+ }
+ virtual void TearDown() {
+ bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+ }
+
+ std::string root_path = "./tests/unit_tests/res-copy/data";
+};
+
+class UninstallRequestHandlerTest : public TestFixture {
+ public:
+ UninstallRequestHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
+ virtual ~UninstallRequestHandlerTest() {}
+
+ virtual void SetUp() {
+ bf::path rootpath(root_path);
+ bf::create_directories(
+ rootpath / "shared_res/test_pkg");
+ }
+ virtual void TearDown() {
+ bf::remove_all("./tests/unit_tests/res-copy/data/shared_res/test_pkg");
+ }
+
+ std::string root_path = "./tests/unit_tests/res-copy/data";
+};
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_file.txt", "");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot_ChangeFileName) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_file.txt", "another_name.txt");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "another_name.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_file.txt", "dest_dir");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "dest_dir";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory_ChangeFileName) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_file.txt", "dest_dir/newname.txt");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "dest_dir";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "newname.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1/resource_file3.txt", "");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file3.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot_ChangeFileName) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back(
+ "data/resource_dir1/resource_file3.txt", "newname.txt");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "newname.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToDirectory) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1/resource_file3.txt", "dest_dir");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "dest_dir";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file3.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot_ChangeFileName) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back(
+ "data/resource_dir1/resource_file3.txt", "dest_dir/newname.txt");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "dest_dir";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "newname.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1", "");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file3.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path = check_path.parent_path();
+ check_path /= "resource_dir2";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file2.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1", "dest_dir");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "dest_dir";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file3.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path = check_path.parent_path();
+ check_path /= "resource_dir2";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file2.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory2) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1", "resource_dir1");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_dir1";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file3.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path = check_path.parent_path();
+ check_path /= "resource_dir2";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file2.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToRoot) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1/resource_dir2", "");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file2.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToDirectory) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_dir1/resource_dir2", "dest_dir");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg");
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "dest_dir";
+ EXPECT_TRUE(bf::exists(check_path));
+
+ check_path /= "resource_file2.txt";
+ EXPECT_TRUE(bf::exists(check_path));
+}
+
+TEST_F(CopyRequestHandlerTest, ResNotexists) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/not_existed_res", "new_dir");
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_RES_NOT_FOUND);
+}
+
+TEST_F(CopyRequestHandlerTest, ReplaceRes) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("data/resource_file.txt", "new_dir");
+
+ CopyRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ CopyRequestHandler replace_handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+}
+
+TEST_F(RemoveRequestHandlerTest, RemoveFileAtRoot) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("resource_file.txt", "");
+
+ RemoveRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg/resource_file.txt");
+ EXPECT_FALSE(bf::exists(check_path));
+}
+
+TEST_F(RemoveRequestHandlerTest, RemoveFileAtDirectory) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("new_dir/resource_file.txt", "");
+
+ RemoveRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(
+ root_path + "/shared_res/test_pkg/new_dir/resource_file.txt");
+ EXPECT_FALSE(bf::exists(check_path));
+}
+
+TEST_F(RemoveRequestHandlerTest, RemoveDirectoryAtRoot) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("new_dir", "");
+
+ RemoveRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg/new_dir");
+ EXPECT_FALSE(bf::exists(check_path));
+}
+
+TEST_F(RemoveRequestHandlerTest, RemoveDirectoryAtDirectory) {
+ std::list<ResPathInfo> path_list;
+ path_list.emplace_back("new_dir/new_dir2", "");
+
+ RemoveRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg/new_dir/new_dir2");
+ EXPECT_FALSE(bf::exists(check_path));
+}
+
+TEST_F(UninstallRequestHandlerTest, RemoveRootPath) {
+ std::list<ResPathInfo> path_list;
+
+ UninstallRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+
+ bf::path check_path(root_path + "/shared_res/test_pkg/");
+ EXPECT_FALSE(bf::exists(check_path));
+}
+
+TEST_F(UninstallRequestHandlerTest, RootNotExists) {
+ std::list<ResPathInfo> path_list;
+ bf::path check_path(root_path + "/shared_res/test_pkg/");
+ bf::remove_all(check_path);
+
+ UninstallRequestHandler handler("test_pkg", root_path, path_list);
+ EXPECT_EQ(handler.Execute(), res_handler::ErrorType::ERROR_NONE);
+}
+
+TEST_F(UninstallRequestHandlerTest, EmptyPkgID) {
+ std::list<ResPathInfo> path_list;
+
+ UninstallRequestHandler handler("", root_path, path_list);
+ EXPECT_EQ(handler.Execute(),
+ res_handler::ErrorType::ERROR_INVALID_PARAMETER);
+}
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+
+#include <memory>
+
+#include "mock/os_mock.h"
+#include "mock/pkgmgr_info_mock.h"
+#include "mock/test_fixture.h"
+#include "include/event_signal_sender.hh"
+#include "include/param_checker.hh"
+#include "include/request_handler_invoker.hh"
+#include "include/request_type.hh"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::InvokeArgument;
+using ::testing::SaveArg;
+
+using res_handler::ParamChecker;
+using res_handler::RequestHandlerInvoker;
+
+class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
+ public ::testing::NiceMock<OsMock> {};
+
+class RequestHandlerInvokerTest : public TestFixture {
+ public:
+ RequestHandlerInvokerTest() : TestFixture(std::make_unique<Mocks>()) {
+ signal_sender_.SetPkgID("org.tizen.targepkgid");
+ signal_sender_.SetReqType(res_handler::ReqType::REQ_TYPE_UNKNOWN);
+ }
+ virtual ~RequestHandlerInvokerTest() {}
+
+ virtual void SetUp() {}
+ virtual void TearDown() {}
+
+ res_handler::EventSignalSender signal_sender_;
+};
+
+TEST_F(RequestHandlerInvokerTest, CopyType) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--copy",
+ "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+
+ RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
+ EXPECT_EQ(request_handler_invoker.GetHandlerType(), "copy");
+
+}
+
+TEST_F(RequestHandlerInvokerTest, RemoveType) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--remove",
+ "org.test.targetpkgid", "-p", "", "dstpath", nullptr};
+ ParamChecker checker(8, (char**)argv);
+
+ RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
+ EXPECT_EQ(request_handler_invoker.GetHandlerType(), "remove");
+}
+
+TEST_F(RequestHandlerInvokerTest, UninstallType) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", "--delete",
+ "org.test.targetpkgid", nullptr};
+ ParamChecker checker(5, (char**)argv);
+
+ RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
+ EXPECT_EQ(request_handler_invoker.GetHandlerType(), "delete");
+}
+
+TEST_F(RequestHandlerInvokerTest, InvalidType) {
+ const char *argv[] = { "/bin/res-copy", "--uid", "5001", nullptr};
+ ParamChecker checker(3, (char**)argv);
+
+ RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
+ EXPECT_EQ(request_handler_invoker.GetHandlerType(), "");
+ EXPECT_EQ(request_handler_invoker.Execute(), false);
+}
+++ /dev/null
-this is second resource file
+++ /dev/null
-this is 3rd resource file
+++ /dev/null
-this is resource file to being copied
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-#include <gtest/gtest.h>
-#include <stdio.h>
-
-#include <memory>
-
-#include "mock/os_mock.h"
-#include "mock/pkgmgr_info_mock.h"
-#include "mock/test_fixture.h"
-
-#include "include/condition_validator.hh"
-#include "include/param_checker.hh"
-
-using ::testing::_;
-using ::testing::DoAll;
-using ::testing::Invoke;
-using ::testing::Return;
-using ::testing::SetArgPointee;
-using ::testing::InvokeArgument;
-using ::testing::SaveArg;
-
-using rsc_handler::ConditionValidator;
-using rsc_handler::ParamChecker;
-
-class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
- public ::testing::NiceMock<OsMock> {};
-
-class ConditionValidatorTest : public TestFixture {
- public:
- ConditionValidatorTest() : TestFixture(std::make_unique<Mocks>()) {}
- virtual ~ConditionValidatorTest() {}
-
- virtual void SetUp() {
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
- .WillRepeatedly(Return(0));
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_destroy_pkginfo(_))
- .WillRepeatedly(Return(0)) ;
- }
- virtual void TearDown() {}
-};
-
-TEST_F(ConditionValidatorTest, PkgNotExist) {
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
- .WillRepeatedly(Return(-1));
-
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--copy",
- "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
- ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
- rsc_handler::ErrorType ret = validator.ValidateCondition(
- checker.GetRequestType(), checker.GetPathList());
-
- EXPECT_EQ(ret, rsc_handler::ErrorType::ERROR_PKG_NOT_FOUND);
-}
-
-TEST_F(ConditionValidatorTest, CopyPkgNotExist) {
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
- .WillRepeatedly(Return(-1));
-
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--copy",
- "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
- ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
- rsc_handler::ErrorType ret = validator.ValidateCondition(
- checker.GetRequestType(), checker.GetPathList());
-
- EXPECT_EQ(ret, rsc_handler::ErrorType::ERROR_PKG_NOT_FOUND);
-}
-
-TEST_F(ConditionValidatorTest, CopySrcNotExist) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--copy",
- "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
- ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
- rsc_handler::ErrorType ret = validator.ValidateCondition(
- checker.GetRequestType(), checker.GetPathList());
-
- EXPECT_EQ(ret, rsc_handler::ErrorType::ERROR_RES_NOT_FOUND);
-}
-
-TEST_F(ConditionValidatorTest, RemovePkgNotExist) {
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
- .WillRepeatedly(Return(-1));
-
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--remove",
- "org.test.targetpkgid", "-p", "", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
- ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
- rsc_handler::ErrorType ret = validator.ValidateCondition(
- checker.GetRequestType(), checker.GetPathList());
-
- EXPECT_EQ(ret, rsc_handler::ErrorType::ERROR_PKG_NOT_FOUND);
-}
-
-TEST_F(ConditionValidatorTest, RemoveSrcNotExist) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--remove",
- "org.test.targetpkgid", "-p", "", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
- ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
- rsc_handler::ErrorType ret = validator.ValidateCondition(
- checker.GetRequestType(), checker.GetPathList());
-
- EXPECT_EQ(ret, rsc_handler::ErrorType::ERROR_RES_NOT_FOUND);
-}
-
-TEST_F(ConditionValidatorTest, DeletePkgNotExist) {
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
- .WillRepeatedly(Return(-1));
-
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--delete",
- "org.test.targetpkgid", nullptr};
- ParamChecker checker(5, (char**)argv);
- ConditionValidator validator(checker.GetPkgID(), checker.GetUID());
- rsc_handler::ErrorType ret = validator.ValidateCondition(
- checker.GetRequestType(), checker.GetPathList());
-
- EXPECT_EQ(ret, rsc_handler::ErrorType::ERROR_PKG_NOT_FOUND);
-}
-
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-#include <gtest/gtest.h>
-#include <stdio.h>
-
-#include <memory>
-
-#include "mock/os_mock.h"
-#include "mock/pkgmgr_info_mock.h"
-#include "mock/test_fixture.h"
-#include "include/param_checker.hh"
-#include "include/request_type.hh"
-
-using ::testing::_;
-using ::testing::DoAll;
-using ::testing::Invoke;
-using ::testing::Return;
-using ::testing::SetArgPointee;
-using ::testing::InvokeArgument;
-using ::testing::SaveArg;
-
-using rsc_handler::ParamChecker;
-
-class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
- public ::testing::NiceMock<OsMock> {};
-
-class ParamCheckerTest : public TestFixture {
- public:
- ParamCheckerTest() : TestFixture(std::make_unique<Mocks>()) {}
- virtual ~ParamCheckerTest() {}
-
- virtual void SetUp() {
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _))
- .WillRepeatedly(Return(0));
- EXPECT_CALL(GetMock<PkgMgrInfoMock>(), pkgmgrinfo_pkginfo_destroy_pkginfo(_))
- .WillRepeatedly(Return(0)) ;
- }
- virtual void TearDown() {}
-};
-
-TEST_F(ParamCheckerTest, InvalidUIDTest) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "0", "--copy",
- "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), false);
-}
-
-TEST_F(ParamCheckerTest, PkgIDNotGiven) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001",
- "-p", "srcpath", "dstpath", "--copy", "", nullptr};
- ParamChecker checker(8, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), false);
-}
-
-TEST_F(ParamCheckerTest, CopyRsc) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "-p", "srcpath", "dstpath",
- "--copy", "org.test.targetpkgid", nullptr};
-
- ParamChecker checker(8, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), true);
- EXPECT_EQ(checker.GetRequestType(), rsc_handler::ReqType::REQ_TYPE_NEW);
- EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
- EXPECT_EQ(checker.GetPathList().size(), 1);
- EXPECT_EQ(checker.GetUID(), 5001);
-}
-
-TEST_F(ParamCheckerTest, RemoveRsc) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--remove",
- "org.test.targetpkgid", "-p", "dstpath", nullptr};
-
- ParamChecker checker(7, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), true);
- EXPECT_EQ(checker.GetRequestType(), rsc_handler::ReqType::REQ_TYPE_REMOVE);
- EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
- EXPECT_EQ(checker.GetPathList().size(), 1);
- EXPECT_EQ(checker.GetUID(), 5001);
-}
-
-TEST_F(ParamCheckerTest, DeleteRsc) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--delete",
- "org.test.targetpkgid", nullptr};
-
- ParamChecker checker(5, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), true);
- EXPECT_EQ(checker.GetRequestType(), rsc_handler::ReqType::REQ_TYPE_UNINSTALL);
- EXPECT_EQ(checker.GetPkgID(), "org.test.targetpkgid");
- EXPECT_EQ(checker.GetPathList().size(), 0);
- EXPECT_EQ(checker.GetUID(), 5001);
-}
-
-TEST_F(ParamCheckerTest, EmptyPkgID) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--copy",
- "", "-p", "srcpath", "dstpath", nullptr};
- ParamChecker checker(8,(char**)argv);
-
- EXPECT_EQ(checker.Validate(), false);
-}
-
-TEST_F(ParamCheckerTest, UnknownReqType) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "-p",
- "srcpath", "dstpath", nullptr};
- ParamChecker checker(6, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), false);
-}
-
-TEST_F(ParamCheckerTest, PathNotGiven) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001",
- "-c", "org.tizen.pathnotgiven", nullptr};
- ParamChecker checker(5, (char**)argv);
-
- EXPECT_EQ(checker.Validate(), false);
-}
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-#include <gtest/gtest.h>
-#include <stdio.h>
-
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-#include <boost/system/error_code.hpp>
-
-#include <list>
-#include <memory>
-#include <string>
-
-#include "mock/os_mock.h"
-#include "mock/pkgmgr_info_mock.h"
-#include "mock/test_fixture.h"
-
-#include "include/abstract_request_handler.hh"
-#include "include/copy_request_handler.hh"
-#include "include/error_type.hh"
-#include "include/param_checker.hh"
-#include "include/remove_request_handler.hh"
-#include "include/request_type.hh"
-#include "include/uninstall_request_handler.hh"
-
-using ::testing::_;
-using ::testing::DoAll;
-using ::testing::Invoke;
-using ::testing::Return;
-using ::testing::SetArgPointee;
-using ::testing::InvokeArgument;
-using ::testing::SaveArg;
-
-using rsc_handler::CopyRequestHandler;
-using rsc_handler::RemoveRequestHandler;
-using rsc_handler::RscPathInfo;
-using rsc_handler::UninstallRequestHandler;
-
-namespace bf = boost::filesystem;
-namespace bs = boost::system;
-
-class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
- public ::testing::NiceMock<OsMock> {};
-
-class CopyRequestHandlerTest : public TestFixture {
- public:
- CopyRequestHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
- virtual ~CopyRequestHandlerTest() {}
-
- virtual void SetUp() {
- bf::path rootpath(root_path);
- bf::create_directories(
- rootpath / "shared_res/test_pkg/dest_dir");
- }
- virtual void TearDown() {
- bf::remove_all("./tests/unit_tests/rsc-copy/data/shared_res/test_pkg");
- }
-
- std::string root_path = "./tests/unit_tests/rsc-copy/data";
-};
-
-class RemoveRequestHandlerTest : public TestFixture {
- public:
- RemoveRequestHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
- virtual ~RemoveRequestHandlerTest() {}
-
- virtual void SetUp() {
- bf::path rootpath(root_path);
- bf::create_directories(
- rootpath / "shared_res/test_pkg/new_dir/new_dir2");
-
- bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
- rootpath / "shared_res/test_pkg/resource_file.txt");
-
- bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
- rootpath / "shared_res/test_pkg/new_dir/resource_file.txt");
-
- bf::copy_file(rootpath / "apps_rw/test_pkg/data/resource_file.txt",
- rootpath / "shared_res/test_pkg/new_dir/new_dir2/resource_file.txt");
- }
- virtual void TearDown() {
- bf::remove_all("./tests/unit_tests/rsc-copy/data/shared_res/test_pkg");
- }
-
- std::string root_path = "./tests/unit_tests/rsc-copy/data";
-};
-
-class UninstallRequestHandlerTest : public TestFixture {
- public:
- UninstallRequestHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
- virtual ~UninstallRequestHandlerTest() {}
-
- virtual void SetUp() {
- bf::path rootpath(root_path);
- bf::create_directories(
- rootpath / "shared_res/test_pkg");
- }
- virtual void TearDown() {
- bf::remove_all("./tests/unit_tests/rsc-copy/data/shared_res/test_pkg");
- }
-
- std::string root_path = "./tests/unit_tests/rsc-copy/data";
-};
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_file.txt", "");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtRootToRoot_ChangeFileName) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_file.txt", "another_name.txt");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "another_name.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_file.txt", "dest_dir");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "dest_dir";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtRootToDirectory_ChangeFileName) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_file.txt", "dest_dir/newname.txt");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "dest_dir";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "newname.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1/resource_file3.txt", "");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file3.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToRoot_ChangeFileName) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back(
- "data/resource_dir1/resource_file3.txt", "newname.txt");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "newname.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToDirectory) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1/resource_file3.txt", "dest_dir");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "dest_dir";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file3.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyFileAtDirectoryToDirectory_ChangeFileName) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back(
- "data/resource_dir1/resource_file3.txt", "dest_dir/newname.txt");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "dest_dir";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "newname.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToRoot) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1", "");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file3.txt";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path = check_path.parent_path();
- check_path /= "resource_dir2";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file2.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1", "dest_dir");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "dest_dir";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file3.txt";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path = check_path.parent_path();
- check_path /= "resource_dir2";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file2.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyDirectoryAtRootToDirectory2) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1", "resource_dir1");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_dir1";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file3.txt";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path = check_path.parent_path();
- check_path /= "resource_dir2";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file2.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToRoot) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1/resource_dir2", "");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file2.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, CopyDirectoryAtDirectoryToDirectory) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_dir1/resource_dir2", "dest_dir");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg");
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "dest_dir";
- EXPECT_TRUE(bf::exists(check_path));
-
- check_path /= "resource_file2.txt";
- EXPECT_TRUE(bf::exists(check_path));
-}
-
-TEST_F(CopyRequestHandlerTest, RscNotexists) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/not_existed_rsc", "new_dir");
- CopyRequestHandler handler("test_pkg", root_path, path_list);
-
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_RES_NOT_FOUND);
-}
-
-TEST_F(CopyRequestHandlerTest, ReplaceRsc) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("data/resource_file.txt", "new_dir");
-
- CopyRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- CopyRequestHandler replace_handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-}
-
-TEST_F(RemoveRequestHandlerTest, RemoveFileAtRoot) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("resource_file.txt", "");
-
- RemoveRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg/resource_file.txt");
- EXPECT_FALSE(bf::exists(check_path));
-}
-
-TEST_F(RemoveRequestHandlerTest, RemoveFileAtDirectory) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("new_dir/resource_file.txt", "");
-
- RemoveRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(
- root_path + "/shared_res/test_pkg/new_dir/resource_file.txt");
- EXPECT_FALSE(bf::exists(check_path));
-}
-
-TEST_F(RemoveRequestHandlerTest, RemoveDirectoryAtRoot) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("new_dir", "");
-
- RemoveRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg/new_dir");
- EXPECT_FALSE(bf::exists(check_path));
-}
-
-TEST_F(RemoveRequestHandlerTest, RemoveDirectoryAtDirectory) {
- std::list<RscPathInfo> path_list;
- path_list.emplace_back("new_dir/new_dir2", "");
-
- RemoveRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg/new_dir/new_dir2");
- EXPECT_FALSE(bf::exists(check_path));
-}
-
-TEST_F(UninstallRequestHandlerTest, RemoveRootPath) {
- std::list<RscPathInfo> path_list;
-
- UninstallRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-
- bf::path check_path(root_path + "/shared_res/test_pkg/");
- EXPECT_FALSE(bf::exists(check_path));
-}
-
-TEST_F(UninstallRequestHandlerTest, RootNotExists) {
- std::list<RscPathInfo> path_list;
- bf::path check_path(root_path + "/shared_res/test_pkg/");
- bf::remove_all(check_path);
-
- UninstallRequestHandler handler("test_pkg", root_path, path_list);
- EXPECT_EQ(handler.Execute(), rsc_handler::ErrorType::ERROR_NONE);
-}
-
-TEST_F(UninstallRequestHandlerTest, EmptyPkgID) {
- std::list<RscPathInfo> path_list;
-
- UninstallRequestHandler handler("", root_path, path_list);
- EXPECT_EQ(handler.Execute(),
- rsc_handler::ErrorType::ERROR_INVALID_PARAMETER);
-}
+++ /dev/null
-/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-#include <gtest/gtest.h>
-#include <stdio.h>
-
-#include <memory>
-
-#include "mock/os_mock.h"
-#include "mock/pkgmgr_info_mock.h"
-#include "mock/test_fixture.h"
-#include "include/event_signal_sender.hh"
-#include "include/param_checker.hh"
-#include "include/request_handler_invoker.hh"
-#include "include/request_type.hh"
-
-using ::testing::_;
-using ::testing::DoAll;
-using ::testing::Invoke;
-using ::testing::Return;
-using ::testing::SetArgPointee;
-using ::testing::InvokeArgument;
-using ::testing::SaveArg;
-
-using rsc_handler::ParamChecker;
-using rsc_handler::RequestHandlerInvoker;
-
-class Mocks : public ::testing::NiceMock<PkgMgrInfoMock>,
- public ::testing::NiceMock<OsMock> {};
-
-class RequestHandlerInvokerTest : public TestFixture {
- public:
- RequestHandlerInvokerTest() : TestFixture(std::make_unique<Mocks>()) {
- signal_sender_.SetPkgID("org.tizen.targepkgid");
- signal_sender_.SetReqType(rsc_handler::ReqType::REQ_TYPE_UNKNOWN);
- }
- virtual ~RequestHandlerInvokerTest() {}
-
- virtual void SetUp() {}
- virtual void TearDown() {}
-
- rsc_handler::EventSignalSender signal_sender_;
-};
-
-TEST_F(RequestHandlerInvokerTest, CopyType) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--copy",
- "org.test.targetpkgid", "-p", "srcpath", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
-
- RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
- EXPECT_EQ(request_handler_invoker.GetHandlerType(), "copy");
-
-}
-
-TEST_F(RequestHandlerInvokerTest, RemoveType) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--remove",
- "org.test.targetpkgid", "-p", "", "dstpath", nullptr};
- ParamChecker checker(8, (char**)argv);
-
- RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
- EXPECT_EQ(request_handler_invoker.GetHandlerType(), "remove");
-}
-
-TEST_F(RequestHandlerInvokerTest, UninstallType) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", "--delete",
- "org.test.targetpkgid", nullptr};
- ParamChecker checker(5, (char**)argv);
-
- RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
- EXPECT_EQ(request_handler_invoker.GetHandlerType(), "delete");
-}
-
-TEST_F(RequestHandlerInvokerTest, InvalidType) {
- const char *argv[] = { "/bin/rsc-copy", "--uid", "5001", nullptr};
- ParamChecker checker(3, (char**)argv);
-
- RequestHandlerInvoker request_handler_invoker(checker, signal_sender_);
- EXPECT_EQ(request_handler_invoker.GetHandlerType(), "");
- EXPECT_EQ(request_handler_invoker.Execute(), false);
-}