1 // Copyright 2014 The Chromium Authors. 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 "components/storage_monitor/media_storage_util.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/check_op.h"
12 #include "base/files/file_util.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/task/post_task.h"
16 #include "base/task/task_traits.h"
17 #include "base/task/thread_pool.h"
18 #include "build/build_config.h"
19 #include "components/storage_monitor/removable_device_constants.h"
20 #include "components/storage_monitor/storage_monitor.h"
21 #include "content/public/browser/browser_thread.h"
22
23 namespace storage_monitor {
24
25 namespace {
26
27 #if !defined(OS_WIN)
28 const char kRootPath[] = "/";
29 #endif
30
31 typedef std::vector<StorageInfo> StorageInfoList;
32
FindRemovableStorageLocationById(const std::string & device_id)33 base::FilePath::StringType FindRemovableStorageLocationById(
34 const std::string& device_id) {
35 StorageInfoList devices =
36 StorageMonitor::GetInstance()->GetAllAvailableStorages();
37 for (StorageInfoList::const_iterator it = devices.begin();
38 it != devices.end(); ++it) {
39 if (it->device_id() == device_id
40 && StorageInfo::IsRemovableDevice(device_id))
41 return it->location();
42 }
43 return base::FilePath::StringType();
44 }
45
FilterAttachedDevicesOnBackgroundSequence(MediaStorageUtil::DeviceIdSet * devices)46 void FilterAttachedDevicesOnBackgroundSequence(
47 MediaStorageUtil::DeviceIdSet* devices) {
48 MediaStorageUtil::DeviceIdSet missing_devices;
49
50 for (auto it = devices->begin(); it != devices->end(); ++it) {
51 StorageInfo::Type type;
52 std::string unique_id;
53 if (!StorageInfo::CrackDeviceId(*it, &type, &unique_id)) {
54 missing_devices.insert(*it);
55 continue;
56 }
57
58 if (type == StorageInfo::FIXED_MASS_STORAGE) {
59 if (!base::PathExists(base::FilePath::FromUTF8Unsafe(unique_id)))
60 missing_devices.insert(*it);
61 continue;
62 }
63
64 if (!MediaStorageUtil::IsRemovableStorageAttached(*it))
65 missing_devices.insert(*it);
66 }
67
68 for (auto it = missing_devices.begin(); it != missing_devices.end(); ++it) {
69 devices->erase(*it);
70 }
71 }
72
73 } // namespace
74
75 // static
HasDcim(const base::FilePath & mount_point)76 bool MediaStorageUtil::HasDcim(const base::FilePath& mount_point) {
77 base::FilePath::StringType dcim_dir(kDCIMDirectoryName);
78 if (!base::DirectoryExists(mount_point.Append(dcim_dir))) {
79 // Check for lowercase 'dcim' as well.
80 base::FilePath dcim_path_lower(
81 mount_point.Append(base::ToLowerASCII(dcim_dir)));
82 if (!base::DirectoryExists(dcim_path_lower))
83 return false;
84 }
85 return true;
86 }
87
88 // static
CanCreateFileSystem(const std::string & device_id,const base::FilePath & path)89 bool MediaStorageUtil::CanCreateFileSystem(const std::string& device_id,
90 const base::FilePath& path) {
91 StorageInfo::Type type;
92 if (!StorageInfo::CrackDeviceId(device_id, &type, nullptr))
93 return false;
94
95 if (type == StorageInfo::MAC_IMAGE_CAPTURE)
96 return true;
97
98 return !path.empty() && path.IsAbsolute() && !path.ReferencesParent();
99 }
100
101 // static
FilterAttachedDevices(DeviceIdSet * devices,base::OnceClosure done)102 void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
103 base::OnceClosure done) {
104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
105 base::ThreadPool::PostTaskAndReply(
106 FROM_HERE,
107 {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
108 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
109 base::BindOnce(&FilterAttachedDevicesOnBackgroundSequence, devices),
110 std::move(done));
111 }
112
113 // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
114 // static
GetDeviceInfoFromPath(const base::FilePath & path,StorageInfo * device_info,base::FilePath * relative_path)115 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
116 StorageInfo* device_info,
117 base::FilePath* relative_path) {
118 DCHECK(device_info);
119 DCHECK(relative_path);
120
121 if (!path.IsAbsolute())
122 return false;
123
124 StorageInfo info;
125 StorageMonitor* monitor = StorageMonitor::GetInstance();
126 bool found_device = monitor->GetStorageInfoForPath(path, &info);
127
128 if (found_device && StorageInfo::IsRemovableDevice(info.device_id())) {
129 base::FilePath sub_folder_path;
130 base::FilePath device_path(info.location());
131 if (path != device_path) {
132 bool success = device_path.AppendRelativePath(path, &sub_folder_path);
133 DCHECK(success);
134 }
135
136 *device_info = info;
137 *relative_path = sub_folder_path;
138 return true;
139 }
140
141 // On Posix systems, there's one root so any absolute path could be valid.
142 // TODO(gbillock): Delete this stanza? Posix systems should have the root
143 // volume information. If not, we should move the below into the
144 // right GetStorageInfoForPath implementations.
145 #if !defined(OS_POSIX)
146 if (!found_device)
147 return false;
148 #endif
149
150 // Handle non-removable devices. Note: this is just overwriting
151 // good values from StorageMonitor.
152 // TODO(gbillock): Make sure return values from that class are definitive,
153 // and don't do this here.
154 info.set_device_id(
155 StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
156 path.AsUTF8Unsafe()));
157 *device_info = info;
158 *relative_path = base::FilePath();
159 return true;
160 }
161
162 // static
FindDevicePathById(const std::string & device_id)163 base::FilePath MediaStorageUtil::FindDevicePathById(
164 const std::string& device_id) {
165 StorageInfo::Type type;
166 std::string unique_id;
167 if (!StorageInfo::CrackDeviceId(device_id, &type, &unique_id))
168 return base::FilePath();
169
170 if (type == StorageInfo::FIXED_MASS_STORAGE) {
171 // For this type, the unique_id is the path.
172 return base::FilePath::FromUTF8Unsafe(unique_id);
173 }
174
175 // For ImageCapture, the synthetic filesystem will be rooted at a fake
176 // top-level directory which is the device_id.
177 if (type == StorageInfo::MAC_IMAGE_CAPTURE) {
178 #if !defined(OS_WIN)
179 return base::FilePath(kRootPath + device_id);
180 #endif
181 }
182
183 DCHECK(type == StorageInfo::MTP_OR_PTP ||
184 type == StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
185 type == StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM);
186 return base::FilePath(FindRemovableStorageLocationById(device_id));
187 }
188
189 // static
IsRemovableStorageAttached(const std::string & id)190 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string& id) {
191 StorageMonitor* monitor = StorageMonitor::GetInstance();
192 if (!monitor)
193 return false;
194
195 StorageInfoList devices = monitor->GetAllAvailableStorages();
196 for (const auto& device : devices) {
197 if (StorageInfo::IsRemovableDevice(id) && device.device_id() == id)
198 return true;
199 }
200 return false;
201 }
202
203 } // namespace storage_monitor
204