summaryrefslogtreecommitdiff
path: root/xwalk/common/virtual_fs.cc
blob: 93984a0b249bedaae3a0e701004e7521c5f438c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright (c) 2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "common/virtual_fs.h"

#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <pkgmgr-info.h>
#include <tzplatform_config.h>

#include <cassert>
#include <algorithm>
#include <sstream>
#include <string>

#include "common/extension.h"

namespace {

const char kInternalStorage[] = "internal";
const char kRemovableStorage[] = "removable";

const char kStorageTypeInternal[] = "INTERNAL";
const char kStorageTypeExternal[] = "EXTERNAL";
const char kStorageStateMounted[] = "MOUNTED";
const char kStorageStateRemoved[] = "REMOVED";
const char kStorageStateUnmountable[] = "UNMOUNTABLE";

}  // namespace

namespace vfs_const {

const unsigned kDefaultFileMode = 0755;
const char kLocationCamera[] = "camera";
const char kLocationMusic[] = "music";
const char kLocationImages[] = "images";
const char kLocationVideos[] = "videos";
const char kLocationDownloads[] = "downloads";
const char kLocationDocuments[] = "documents";
const char kLocationRingtones[] = "ringtones";
const char kLocationWgtPackage[] = "wgt-package";
const char kLocationWgtPrivate[] = "wgt-private";
const char kLocationWgtPrivateTmp[] = "wgt-private-tmp";

}  // namespace vfs_const

VirtualFS::VirtualFS() {
  std::string app_path = GetApplicationPath();
  if (!app_path.empty()) {
    AddInternalStorage(vfs_const::kLocationWgtPackage, app_path);
    AddInternalStorage(vfs_const::kLocationWgtPrivate, JoinPath(app_path, "private"));
    AddInternalStorage(vfs_const::kLocationWgtPrivateTmp, JoinPath(app_path, "tmp"));
  }

  AddInternalStorage(vfs_const::kLocationCamera, tzplatform_getenv(TZ_USER_CAMERA));
  AddInternalStorage(vfs_const::kLocationMusic, tzplatform_getenv(TZ_USER_SOUNDS));
  AddInternalStorage(vfs_const::kLocationImages, tzplatform_getenv(TZ_USER_IMAGES));
  AddInternalStorage(vfs_const::kLocationVideos, tzplatform_getenv(TZ_USER_VIDEOS));
  AddInternalStorage(vfs_const::kLocationDownloads, tzplatform_getenv(TZ_USER_DOWNLOADS));
  AddInternalStorage(vfs_const::kLocationDocuments, tzplatform_getenv(TZ_USER_DOCUMENTS));
  AddInternalStorage(vfs_const::kLocationRingtones,
      tzplatform_mkpath(TZ_USER_SHARE, "settings/Ringtones"));
  storage_changed_cb_ = NULL;
  cb_user_data_ = NULL;
}

VirtualFS::~VirtualFS() {
}

std::string VirtualFS::JoinPath(const std::string& one,
    const std::string& another) {
  return one + '/' + another;
}

bool VirtualFS::MakePath(const std::string& path, int mode) {
  // Path should start with '/' and contain at least 1 character after '/'.
  if (path.empty() || path[0] != '/' || path.length() < 2)
    return false;

  struct stat st;
  std::string dir = path;
  if (stat(dir.c_str(), &st) == 0)
    return true;

  // Add trailing '/' if missing, so we can iterate till the end of the path.
  if (dir[dir.size() - 1] != '/')
    dir += '/';

  for (std::string::iterator iter = dir.begin(); iter != dir.end();) {
    std::string::iterator cur_iter = std::find(iter, dir.end(), '/');

    // If '/' is found at the beginning of the string, iterate to the next '/'.
    if (cur_iter == iter) {
      ++iter;
      cur_iter = std::find(iter, dir.end(), '/');
    }

    std::string new_path = std::string(dir.begin(), cur_iter);

    // If path doesn't exist, try to create one and continue iteration.
    // In case of error, stop iteration and return.
    if (stat(new_path.c_str(), &st) != 0) {
      if (mkdir(new_path.c_str(), mode) != 0 && errno != EEXIST )
          return false;
    // If path exists and it is not a directory, stop iteration and return.
    } else if (!S_ISDIR(st.st_mode)) {
      return false;
    }

    // Advance iterator and create next parent folder.
    iter = cur_iter;
    if (cur_iter != dir.end())
      ++iter;
  }
  return true;
}

int VirtualFS::GetDirEntryCount(const char* path) {
  int count = 0;
  DIR* dir = opendir(path);
  if (!dir)
    return count;

  struct dirent entry;
  struct dirent *result;
  int ret = readdir_r(dir, &entry, &result);

  for (; ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)) {
    if (entry.d_type == DT_REG || entry.d_type == DT_DIR)
      count++;
  }

  closedir(dir);
  return count;
}

std::string VirtualFS::GetAppId(const std::string& package_id) {
  char* appid = NULL;
  pkgmgrinfo_pkginfo_h pkginfo_handle;
  int ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id.c_str(), &pkginfo_handle);
  if (ret != PMINFO_R_OK)
    return std::string();
  ret = pkgmgrinfo_pkginfo_get_mainappid(pkginfo_handle, &appid);
  if (ret != PMINFO_R_OK) {
    pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_handle);
    return std::string();
  }

  std::string retval(appid);
  pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_handle);
  return retval;
}

std::string VirtualFS::GetExecPath(const std::string& app_id) {
  char* exec_path = NULL;
  pkgmgrinfo_appinfo_h appinfo_handle;
  int ret = pkgmgrinfo_appinfo_get_appinfo(app_id.c_str(), &appinfo_handle);
  if (ret != PMINFO_R_OK)
    return std::string();
  ret = pkgmgrinfo_appinfo_get_exec(appinfo_handle, &exec_path);
  if (ret != PMINFO_R_OK) {
    pkgmgrinfo_appinfo_destroy_appinfo(appinfo_handle);
    return std::string();
  }

  std::string retval(exec_path);
  pkgmgrinfo_appinfo_destroy_appinfo(appinfo_handle);
  return retval;
}

bool VirtualFS::GetStorageByLabel(const std::string& label, Storage& storage) {
  storage_foreach_device_supported(OnStorageDeviceSupported, this);
  Storages::const_iterator it = storages_.find(label);

  if (it == storages_.end()) {
    return false;
  }
  storage = it->second;
  return true;
}

Storages::iterator VirtualFS::begin() {
  storage_foreach_device_supported(OnStorageDeviceSupported, this);
  return storages_.begin();
}

Storages::const_iterator VirtualFS::end() const {
  return storages_.end();
}

std::string VirtualFS::GetApplicationPath() {
  std::string id_str = common::Extension::GetRuntimeVariable("app_id", 64);
  std::string pkg_id = id_str.substr(1, id_str.rfind('"') - 1);
  if (pkg_id.empty())
    return std::string();
  std::string app_id = GetAppId(pkg_id);
  if (app_id.empty())
    return std::string();
  std::string exec_path = GetExecPath(app_id);
  if (exec_path.empty())
    return std::string();

  size_t index = exec_path.find(pkg_id);
  if (index != std::string::npos)
    return exec_path.substr(0, index + pkg_id.length());
  return std::string();
}

std::string VirtualFS::GetRealPath(const std::string& fullPath) const {
  std::size_t pos = fullPath.find_first_of('/');
  Storages::const_iterator it = storages_.find(fullPath.substr(0, pos));

  if (it == storages_.end())
    return std::string();

  if (pos != std::string::npos)
    return it->second.GetFullPath() + fullPath.substr(pos);

  return it->second.GetFullPath();
}

void VirtualFS::AddInternalStorage(
    const std::string& label, const std::string& path) {
  if (MakePath(path, vfs_const::kDefaultFileMode))
    storages_.insert(StorageLabelPair(label,
                                      Storage(-1,
                                      Storage::STORAGE_TYPE_INTERNAL,
                                      Storage::STORAGE_STATE_MOUNTED,
                                      path)));
}

void VirtualFS::AddStorage(int id,
                           storage_type_e type,
                           storage_state_e state,
                           const std::string& path) {
  std::string label;
  if (type == STORAGE_TYPE_INTERNAL)
    label = kInternalStorage + std::to_string(id);
  else if (type == STORAGE_TYPE_EXTERNAL)
    label = kRemovableStorage + std::to_string(id);

  storages_.insert(StorageLabelPair(label,
                                    Storage(id,
                                    type,
                                    state,
                                    path)));
  if (std::find(watched_storages_.begin(),
                watched_storages_.end(), id) != watched_storages_.end()) {
    watched_storages_.push_back(id);
    storage_set_state_changed_cb(id, OnStorageStateChanged, this);
  }
}

void VirtualFS::SetOnStorageChangedCb(CallBackFunctionPtr cb, void* user_data) {
  storage_changed_cb_ = cb;
  cb_user_data_ = user_data;
}

void VirtualFS::NotifyStorageStateChanged(int id, storage_state_e state) {
  for (Storages::iterator it = storages_.begin(); it != storages_.end(); ++it) {
    if (it->second.GetId() == id) {
      it->second.SetState(state);
      if (storage_changed_cb_) {
        storage_changed_cb_(it->first, it->second, cb_user_data_);
      }
      break;
    }
  }
}

bool VirtualFS::OnStorageDeviceSupported(
    int id, storage_type_e type, storage_state_e state,
    const char* path, void* user_data) {
  reinterpret_cast<VirtualFS*>(user_data)->AddStorage(
      id, type, state, path);
  return true;
}

void VirtualFS::OnStorageStateChanged(
    int id, storage_state_e state, void* user_data) {
  reinterpret_cast<VirtualFS*>(user_data)->NotifyStorageStateChanged(
      id, state);
}

/*
 * Storage Class
 */

Storage::Storage(
    int id, int type, int state, const std::string& fullpath)
    : id_(id),
      type_(type),
      state_(state),
      full_path_(fullpath) { }

std::string Storage::GetType() const {
  return (type_ == Storage::STORAGE_TYPE_INTERNAL) ? kStorageTypeInternal :
      kStorageTypeExternal;
}

std::string Storage::GetState() const {
  switch (state_) {
    case Storage::STORAGE_STATE_MOUNTED:
    case Storage::STORAGE_STATE_MOUNTED_READONLY:
      return kStorageStateMounted;
    case Storage::STORAGE_STATE_REMOVED:
      return kStorageStateRemoved;
    case Storage::STORAGE_STATE_UNMOUNTABLE:
      return kStorageStateUnmountable;
    default:
      assert(!"Not reached");
  }
  return std::string();
}