[version] 2.15
[platform/core/api/webapi-plugins.git] / src / common / filesystem / filesystem_storage.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "common/filesystem/filesystem_storage.h"
18 #include <string>
19 #include "common/logger.h"
20
21 namespace common {
22
23 VirtualRoot::VirtualRoot(std::string const& name, std::string const& path, StorageType type,
24                          StorageState state)
25     : name_(name), path_(path), type_(type), state_(state) {
26   ScopeLogger();
27 }
28
29 Storage::Storage(int id, StorageType type, StorageState state, std::string const& path,
30                  std::string const& name)
31     : VirtualRoot(name, path, type, state), id_(id) {
32   ScopeLogger();
33   if (name_ == "") {
34     switch (type) {
35       case StorageType::kInternal:
36         name_ = "internal";
37         break;
38       case StorageType::kUsbDevice:
39       case StorageType::kUsbHost:
40       case StorageType::kMmc:
41         name_ = "removable";
42         break;
43       default:
44         name_ = "unknown";
45         LoggerE("Unknown storage type: %d", type);
46         break;
47     }
48     name_ += std::to_string(id);
49   }
50 }
51
52 picojson::value VirtualRoot::ToJson() const {
53   ScopeLogger();
54   picojson::value v{picojson::object{}};
55   picojson::object& obj = v.get<picojson::object>();
56
57   obj["type"] = picojson::value(ToString(type_));
58   obj["state"] = picojson::value(ToString(state_));
59   obj["path"] = picojson::value(path_);
60   obj["name"] = picojson::value(name_);
61
62   return v;
63 }
64
65 picojson::value Storage::ToJson() const {
66   ScopeLogger();
67   picojson::value value = VirtualRoot::ToJson();
68   picojson::object& obj = value.get<picojson::object>();
69   obj["storage_id"] = picojson::value(static_cast<double>(id_));
70   return value;
71 }
72
73 Storage::Storage(Storage const& other) : VirtualRoot(other) {
74   ScopeLogger();
75   this->id_ = other.id_;
76 }
77
78 VirtualRoot::VirtualRoot(VirtualRoot const& other) {
79   ScopeLogger();
80   this->path_ = other.path_;
81   this->name_ = other.name_;
82   this->state_ = other.state_;
83   this->type_ = other.type_;
84 }
85
86 std::string VirtualRoot::ToString(StorageType type) {
87   ScopeLogger();
88   switch (type) {
89     case StorageType::kInternal:
90       return "INTERNAL";
91     case StorageType::kUsbDevice:
92     case StorageType::kUsbHost:
93     case StorageType::kMmc:
94       return "EXTERNAL";
95     default:
96       LoggerE("Unknown storage type: %d", type);
97       return "UNKNOWN";
98   }
99 }
100
101 std::string VirtualRoot::ToString(StorageState state) {
102   ScopeLogger();
103   switch (state) {
104     case StorageState::kUnmounted:
105       return "REMOVED";
106     case StorageState::kUnmountable:
107       return "UNMOUNTABLE";
108     case StorageState::kMounted:
109       return "MOUNTED";
110     default:
111       LoggerE("Unknown storage state: %d", state);
112       return "UNKNOWN";
113   }
114 }
115
116 }  // namespace common