| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/event.h> | 8 #include <sys/event.h> |
| 9 #include <sys/param.h> | 9 #include <sys/param.h> |
| 10 | 10 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 int FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) { | 188 int FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) { |
| 189 return HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY)); | 189 return HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY)); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void FilePathWatcherImpl::CloseFileDescriptor(int *fd) { | 192 void FilePathWatcherImpl::CloseFileDescriptor(int *fd) { |
| 193 if (*fd == -1) { | 193 if (*fd == -1) { |
| 194 return; | 194 return; |
| 195 } | 195 } |
| 196 | 196 |
| 197 if (HANDLE_EINTR(close(*fd)) != 0) { | 197 if (HANDLE_EINTR(close(*fd)) != 0) { |
| 198 PLOG(ERROR) << "close"; | 198 DPLOG(ERROR) << "close"; |
| 199 } | 199 } |
| 200 *fd = -1; | 200 *fd = -1; |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents, | 203 bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents, |
| 204 int count) { | 204 int count) { |
| 205 if (count < 0) { | 205 if (count < 0) { |
| 206 PLOG(ERROR) << "kevent"; | 206 DPLOG(ERROR) << "kevent"; |
| 207 return false; | 207 return false; |
| 208 } | 208 } |
| 209 bool valid = true; | 209 bool valid = true; |
| 210 for (int i = 0; i < count; ++i) { | 210 for (int i = 0; i < count; ++i) { |
| 211 if (kevents[i].flags & EV_ERROR && kevents[i].data) { | 211 if (kevents[i].flags & EV_ERROR && kevents[i].data) { |
| 212 // Find the kevent in |events_| that matches the kevent with the error. | 212 // Find the kevent in |events_| that matches the kevent with the error. |
| 213 EventVector::iterator event = events_.begin(); | 213 EventVector::iterator event = events_.begin(); |
| 214 for (; event != events_.end(); ++event) { | 214 for (; event != events_.end(); ++event) { |
| 215 if (event->ident == kevents[i].ident) { | 215 if (event->ident == kevents[i].ident) { |
| 216 break; | 216 break; |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 std::string path_name; | 219 std::string path_name; |
| 220 if (event != events_.end()) { | 220 if (event != events_.end()) { |
| 221 EventData* event_data = EventDataForKevent(*event); | 221 EventData* event_data = EventDataForKevent(*event); |
| 222 if (event_data != NULL) { | 222 if (event_data != NULL) { |
| 223 path_name = event_data->path_.value(); | 223 path_name = event_data->path_.value(); |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 if (path_name.empty()) { | 226 if (path_name.empty()) { |
| 227 path_name = base::StringPrintf( | 227 path_name = base::StringPrintf( |
| 228 "fd %d", *reinterpret_cast<int*>(&kevents[i].ident)); | 228 "fd %d", *reinterpret_cast<int*>(&kevents[i].ident)); |
| 229 } | 229 } |
| 230 LOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name; | 230 DLOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name; |
| 231 valid = false; | 231 valid = false; |
| 232 } | 232 } |
| 233 } | 233 } |
| 234 return valid; | 234 return valid; |
| 235 } | 235 } |
| 236 | 236 |
| 237 void FilePathWatcherImpl::HandleAttributesChange( | 237 void FilePathWatcherImpl::HandleAttributesChange( |
| 238 const EventVector::iterator& event, | 238 const EventVector::iterator& event, |
| 239 bool* target_file_affected, | 239 bool* target_file_affected, |
| 240 bool* update_watches) { | 240 bool* update_watches) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 } else { | 331 } else { |
| 332 break; | 332 break; |
| 333 } | 333 } |
| 334 } | 334 } |
| 335 } | 335 } |
| 336 return true; | 336 return true; |
| 337 } | 337 } |
| 338 | 338 |
| 339 void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) { | 339 void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) { |
| 340 DCHECK(MessageLoopForIO::current()); | 340 DCHECK(MessageLoopForIO::current()); |
| 341 CHECK_EQ(fd, kqueue_); | 341 DCHECK_EQ(fd, kqueue_); |
| 342 CHECK(events_.size()); | 342 DCHECK(events_.size()); |
| 343 | 343 |
| 344 // Request the file system update notifications that have occurred and return | 344 // Request the file system update notifications that have occurred and return |
| 345 // them in |updates|. |count| will contain the number of updates that have | 345 // them in |updates|. |count| will contain the number of updates that have |
| 346 // occurred. | 346 // occurred. |
| 347 EventVector updates(events_.size()); | 347 EventVector updates(events_.size()); |
| 348 struct timespec timeout = {0, 0}; | 348 struct timespec timeout = {0, 0}; |
| 349 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), | 349 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), |
| 350 &timeout)); | 350 &timeout)); |
| 351 | 351 |
| 352 // Error values are stored within updates, so check to make sure that no | 352 // Error values are stored within updates, so check to make sure that no |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 DCHECK_EQ(kqueue_, -1); | 425 DCHECK_EQ(kqueue_, -1); |
| 426 | 426 |
| 427 delegate_ = delegate; | 427 delegate_ = delegate; |
| 428 target_ = path; | 428 target_ = path; |
| 429 | 429 |
| 430 MessageLoop::current()->AddDestructionObserver(this); | 430 MessageLoop::current()->AddDestructionObserver(this); |
| 431 io_message_loop_ = base::MessageLoopProxy::current(); | 431 io_message_loop_ = base::MessageLoopProxy::current(); |
| 432 | 432 |
| 433 kqueue_ = kqueue(); | 433 kqueue_ = kqueue(); |
| 434 if (kqueue_ == -1) { | 434 if (kqueue_ == -1) { |
| 435 PLOG(ERROR) << "kqueue"; | 435 DPLOG(ERROR) << "kqueue"; |
| 436 return false; | 436 return false; |
| 437 } | 437 } |
| 438 | 438 |
| 439 int last_entry = EventsForPath(target_, &events_); | 439 int last_entry = EventsForPath(target_, &events_); |
| 440 CHECK_NE(last_entry, 0); | 440 DCHECK_NE(last_entry, 0); |
| 441 | 441 |
| 442 EventVector responses(last_entry); | 442 EventVector responses(last_entry); |
| 443 | 443 |
| 444 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, | 444 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, |
| 445 &responses[0], last_entry, NULL)); | 445 &responses[0], last_entry, NULL)); |
| 446 if (!AreKeventValuesValid(&responses[0], count)) { | 446 if (!AreKeventValuesValid(&responses[0], count)) { |
| 447 // Calling Cancel() here to close any file descriptors that were opened. | 447 // Calling Cancel() here to close any file descriptors that were opened. |
| 448 // This would happen in the destructor anyways, but FilePathWatchers tend to | 448 // This would happen in the destructor anyways, but FilePathWatchers tend to |
| 449 // be long lived, and if an error has occurred, there is no reason to waste | 449 // be long lived, and if an error has occurred, there is no reason to waste |
| 450 // the file descriptors. | 450 // the file descriptors. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 } | 485 } |
| 486 | 486 |
| 487 } // namespace | 487 } // namespace |
| 488 | 488 |
| 489 FilePathWatcher::FilePathWatcher() { | 489 FilePathWatcher::FilePathWatcher() { |
| 490 impl_ = new FilePathWatcherImpl(); | 490 impl_ = new FilePathWatcherImpl(); |
| 491 } | 491 } |
| 492 | 492 |
| 493 } // namespace files | 493 } // namespace files |
| 494 } // namespace base | 494 } // namespace base |
| OLD | NEW |