1 // Copyright (c) 2013 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.
5 // MtabWatcherLinux implementation.
7 #include "chrome/browser/storage_monitor/mtab_watcher_linux.h"
12 #include "base/bind.h"
13 #include "content/public/browser/browser_thread.h"
17 // List of file systems we care about.
18 const char* const kKnownFileSystems
[] = {
33 MtabWatcherLinux::MtabWatcherLinux(const base::FilePath
& mtab_path
,
34 base::WeakPtr
<Delegate
> delegate
)
35 : mtab_path_(mtab_path
),
37 weak_ptr_factory_(this) {
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
39 bool ret
= file_watcher_
.Watch(
41 base::Bind(&MtabWatcherLinux::OnFilePathChanged
,
42 weak_ptr_factory_
.GetWeakPtr()));
44 LOG(ERROR
) << "Adding watch for " << mtab_path_
.value() << " failed";
51 MtabWatcherLinux::~MtabWatcherLinux() {
52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
55 void MtabWatcherLinux::ReadMtab() const {
56 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
58 FILE* fp
= setmntent(mtab_path_
.value().c_str(), "r");
62 MountPointDeviceMap device_map
;
66 // We return the same device mounted to multiple locations, but hide
67 // devices that have been mounted over.
68 while (getmntent_r(fp
, &entry
, buf
, sizeof(buf
))) {
69 // We only care about real file systems.
70 for (size_t i
= 0; i
< arraysize(kKnownFileSystems
); ++i
) {
71 if (strcmp(kKnownFileSystems
[i
], entry
.mnt_type
) == 0) {
72 device_map
[base::FilePath(entry
.mnt_dir
)] =
73 base::FilePath(entry
.mnt_fsname
);
80 content::BrowserThread::PostTask(
81 content::BrowserThread::UI
, FROM_HERE
,
82 base::Bind(&Delegate::UpdateMtab
, delegate_
, device_map
));
85 void MtabWatcherLinux::OnFilePathChanged(
86 const base::FilePath
& path
, bool error
) {
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
89 if (path
!= mtab_path_
) {
90 // This cannot happen unless FilePathWatcher is buggy. Just ignore this
91 // notification and do nothing.
96 LOG(ERROR
) << "Error watching " << mtab_path_
.value();