1 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/application/tools/tizen/xwalk_rds_delta_parser.h"
13 #include "base/files/file_util.h"
14 #include "base/logging.h"
15 #include "base/strings/string_split.h"
19 const char kRDSDeltaFile[] = ".rds_delta";
20 const std::string kRDSWidgetPath = "res/wgt/";
21 const std::string kKeyAdd = "#add";
22 const std::string kKeyDelete = "#delete";
23 const std::string kKeyModify = "#modify";
24 const base::FilePath kRDSTempDir = base::FilePath("/opt/usr/apps/tmp/");
25 const std::list<std::string> key_list = {kKeyDelete, kKeyAdd, kKeyModify};
29 RDSDeltaParser::RDSDeltaParser(const base::FilePath& app_path,
30 const std::string& pkgid) : app_dir_(app_path) {
31 rds_dir_ = kRDSTempDir.Append(pkgid);
34 bool RDSDeltaParser::Parse() {
35 base::FilePath rds_file = rds_dir_.AppendASCII(kRDSDeltaFile);
37 if (!base::PathExists(rds_file)) {
38 LOG(ERROR) << "RDS delta file " << rds_file.value()
39 << " does not exists!";
43 std::string file_buffer;
44 if (!base::ReadFileToString(rds_file, &file_buffer)) {
45 LOG(ERROR) << "RDS delta file " << rds_file.value()
46 << " cannot be read!";
50 std::vector<std::string> lines;
51 base::SplitString(file_buffer, '\n', &lines);
54 LOG(ERROR) << "RDS delta file " << rds_file.value() << " is empty!";
60 for (const auto& line : lines) {
61 for (const auto& it : key_list) {
67 if (key == line || line.empty() || line == "\n") {
70 parsed_data_.insert(std::pair<std::string, std::string>(key,
71 line.substr(kRDSWidgetPath.length())));
76 bool RDSDeltaParser::ApplyParsedData() {
77 for (const auto& it : parsed_data_) {
78 if (it.first == kKeyDelete)
79 return DeleteFile(it.second);
80 if (it.first == kKeyAdd)
81 return AddFile(it.second);
82 if (it.first == kKeyModify)
83 return ModifyFile(it.second);
88 bool RDSDeltaParser::AddFile(const std::string& file_name) {
89 base::FilePath src_path = rds_dir_.Append(kRDSWidgetPath).Append(file_name);
91 if (!base::PathExists(src_path)) {
92 LOG(ERROR) << "File " << src_path.value() << " does not exists!";
95 base::FilePath dst_file = app_dir_.Append(file_name);
96 if (!base::DirectoryExists(dst_file.DirName()) &&
97 !base::CreateDirectory(dst_file.DirName())) {
98 LOG(ERROR) << "Can't create directory " << dst_file.DirName().value();
101 if (!base::CopyFile(src_path, dst_file)) {
102 LOG(ERROR) << "Error when adding a file " << src_path.BaseName().value()
103 << " to " << dst_file.DirName().value();
109 bool RDSDeltaParser::DeleteFile(const std::string& file_name) {
110 base::FilePath dst_file = app_dir_.Append(file_name);
112 if (!base::DeleteFile(dst_file, true)) {
113 LOG(ERROR) << "Error when deleting a file " << dst_file.value();
119 bool RDSDeltaParser::ModifyFile(const std::string& file_name) {
120 base::FilePath src_file = rds_dir_.Append(kRDSWidgetPath).Append(file_name);
122 if (!base::PathExists(src_file)) {
123 LOG(ERROR) << "File " << src_file.value() << " does not exists!";
127 base::FilePath dst_file = app_dir_.Append(file_name);
128 if (!base::CopyFile(src_file, dst_file)) {
129 LOG(ERROR) << "Error when copying a file " << src_file.value()
130 << " to " << dst_file.DirName().value();