Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / tizen / xwalk_rds_delta_parser.cc
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.
4
5 #include "xwalk/application/tools/tizen/xwalk_rds_delta_parser.h"
6
7 #include <list>
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "base/files/file_util.h"
14 #include "base/logging.h"
15 #include "base/strings/string_split.h"
16
17 namespace {
18
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};
26
27 }  // namespace
28
29 RDSDeltaParser::RDSDeltaParser(const base::FilePath& app_path,
30     const std::string& pkgid) : app_dir_(app_path) {
31   rds_dir_ = kRDSTempDir.Append(pkgid);
32 }
33
34 bool RDSDeltaParser::Parse() {
35   base::FilePath rds_file = rds_dir_.AppendASCII(kRDSDeltaFile);
36
37   if (!base::PathExists(rds_file)) {
38     LOG(ERROR) << "RDS delta file " << rds_file.value()
39                << " does not exists!";
40     return false;
41   }
42
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!";
47     return false;
48   }
49
50   std::vector<std::string> lines;
51   base::SplitString(file_buffer, '\n', &lines);
52
53   if (lines.empty()) {
54     LOG(ERROR) << "RDS delta file " << rds_file.value() << " is empty!";
55     return false;
56   }
57
58   std::string key;
59   parsed_data_.clear();
60   for (const auto& line : lines) {
61     for (const auto& it : key_list) {
62       if (line == it) {
63         key = line;
64         break;
65       }
66     }
67     if (key == line || line.empty() || line == "\n") {
68       continue;
69     }
70     parsed_data_.insert(std::pair<std::string, std::string>(key,
71         line.substr(kRDSWidgetPath.length())));
72   }
73   return true;
74 }
75
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);
84   }
85   return true;
86 }
87
88 bool RDSDeltaParser::AddFile(const std::string& file_name) {
89   base::FilePath src_path = rds_dir_.Append(kRDSWidgetPath).Append(file_name);
90
91   if (!base::PathExists(src_path)) {
92     LOG(ERROR) << "File " << src_path.value() << " does not exists!";
93     return false;
94   }
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();
99     return false;
100   }
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();
104     return false;
105   }
106   return true;
107 }
108
109 bool RDSDeltaParser::DeleteFile(const std::string& file_name) {
110   base::FilePath dst_file = app_dir_.Append(file_name);
111
112   if (!base::DeleteFile(dst_file, true)) {
113     LOG(ERROR) << "Error when deleting a file " << dst_file.value();
114     return false;
115   }
116   return true;
117 }
118
119 bool RDSDeltaParser::ModifyFile(const std::string& file_name) {
120   base::FilePath src_file = rds_dir_.Append(kRDSWidgetPath).Append(file_name);
121
122   if (!base::PathExists(src_file)) {
123     LOG(ERROR) << "File " << src_file.value() << " does not exists!";
124     return false;
125   }
126
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();
131     return false;
132   }
133   return true;
134 }