1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/storage_monitor/media_storage_util.h"
9 #include "base/check_op.h"
10 #include "base/files/file_util.h"
11 #include "base/functional/bind.h"
12 #include "base/functional/callback.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/task/task_traits.h"
16 #include "base/task/thread_pool.h"
17 #include "build/build_config.h"
18 #include "components/storage_monitor/removable_device_constants.h"
19 #include "components/storage_monitor/storage_monitor.h"
20 #include "content/public/browser/browser_thread.h"
22 namespace storage_monitor {
26 #if !BUILDFLAG(IS_WIN)
27 const char kRootPath[] = "/";
30 typedef std::vector<StorageInfo> StorageInfoList;
32 base::FilePath::StringType FindRemovableStorageLocationById(
33 const std::string& device_id) {
34 StorageInfoList devices =
35 StorageMonitor::GetInstance()->GetAllAvailableStorages();
36 for (StorageInfoList::const_iterator it = devices.begin();
37 it != devices.end(); ++it) {
38 if (it->device_id() == device_id
39 && StorageInfo::IsRemovableDevice(device_id))
40 return it->location();
42 return base::FilePath::StringType();
45 void FilterAttachedDevicesOnBackgroundSequence(
46 MediaStorageUtil::DeviceIdSet* devices) {
47 MediaStorageUtil::DeviceIdSet missing_devices;
49 for (auto it = devices->begin(); it != devices->end(); ++it) {
50 StorageInfo::Type type;
51 std::string unique_id;
52 if (!StorageInfo::CrackDeviceId(*it, &type, &unique_id)) {
53 missing_devices.insert(*it);
57 if (type == StorageInfo::FIXED_MASS_STORAGE) {
58 if (!base::PathExists(base::FilePath::FromUTF8Unsafe(unique_id)))
59 missing_devices.insert(*it);
63 if (!MediaStorageUtil::IsRemovableStorageAttached(*it))
64 missing_devices.insert(*it);
67 for (auto it = missing_devices.begin(); it != missing_devices.end(); ++it) {
75 bool MediaStorageUtil::HasDcim(const base::FilePath& mount_point) {
76 base::FilePath::StringType dcim_dir(kDCIMDirectoryName);
77 if (!base::DirectoryExists(mount_point.Append(dcim_dir))) {
78 // Check for lowercase 'dcim' as well.
79 base::FilePath dcim_path_lower(
80 mount_point.Append(base::ToLowerASCII(dcim_dir)));
81 if (!base::DirectoryExists(dcim_path_lower))
88 bool MediaStorageUtil::CanCreateFileSystem(const std::string& device_id,
89 const base::FilePath& path) {
90 StorageInfo::Type type;
91 if (!StorageInfo::CrackDeviceId(device_id, &type, nullptr))
94 if (type == StorageInfo::MAC_IMAGE_CAPTURE)
97 return !path.empty() && path.IsAbsolute() && !path.ReferencesParent();
101 void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
102 base::OnceClosure done) {
103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
104 base::ThreadPool::PostTaskAndReply(
106 {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
107 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
108 base::BindOnce(&FilterAttachedDevicesOnBackgroundSequence, devices),
112 // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
114 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
115 StorageInfo* device_info,
116 base::FilePath* relative_path) {
118 DCHECK(relative_path);
120 if (!path.IsAbsolute())
124 StorageMonitor* monitor = StorageMonitor::GetInstance();
125 bool found_device = monitor->GetStorageInfoForPath(path, &info);
127 if (found_device && StorageInfo::IsRemovableDevice(info.device_id())) {
128 base::FilePath sub_folder_path;
129 base::FilePath device_path(info.location());
130 if (path != device_path) {
131 bool success = device_path.AppendRelativePath(path, &sub_folder_path);
136 *relative_path = sub_folder_path;
140 // On Posix systems, there's one root so any absolute path could be valid.
141 // TODO(gbillock): Delete this stanza? Posix systems should have the root
142 // volume information. If not, we should move the below into the
143 // right GetStorageInfoForPath implementations.
144 #if !BUILDFLAG(IS_POSIX)
149 // Handle non-removable devices. Note: this is just overwriting
150 // good values from StorageMonitor.
151 // TODO(gbillock): Make sure return values from that class are definitive,
152 // and don't do this here.
154 StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
155 path.AsUTF8Unsafe()));
157 *relative_path = base::FilePath();
162 base::FilePath MediaStorageUtil::FindDevicePathById(
163 const std::string& device_id) {
164 StorageInfo::Type type;
165 std::string unique_id;
166 if (!StorageInfo::CrackDeviceId(device_id, &type, &unique_id))
167 return base::FilePath();
169 if (type == StorageInfo::FIXED_MASS_STORAGE) {
170 // For this type, the unique_id is the path.
171 return base::FilePath::FromUTF8Unsafe(unique_id);
174 // For ImageCapture, the synthetic filesystem will be rooted at a fake
175 // top-level directory which is the device_id.
176 if (type == StorageInfo::MAC_IMAGE_CAPTURE) {
177 #if !BUILDFLAG(IS_WIN)
178 return base::FilePath(kRootPath + device_id);
182 DCHECK(type == StorageInfo::MTP_OR_PTP ||
183 type == StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
184 type == StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM);
185 return base::FilePath(FindRemovableStorageLocationById(device_id));
189 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string& id) {
190 StorageMonitor* monitor = StorageMonitor::GetInstance();
194 StorageInfoList devices = monitor->GetAllAvailableStorages();
195 for (const auto& device : devices) {
196 if (StorageInfo::IsRemovableDevice(id) && device.device_id() == id)
202 } // namespace storage_monitor