Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 int FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) { | 198 int FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) { |
| 199 return HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY)); | 199 return HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY)); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void FilePathWatcherImpl::CloseFileDescriptor(int *fd) { | 202 void FilePathWatcherImpl::CloseFileDescriptor(int *fd) { |
| 203 if (*fd == -1) { | 203 if (*fd == -1) { |
| 204 return; | 204 return; |
| 205 } | 205 } |
| 206 | 206 |
| 207 if (HANDLE_EINTR(close(*fd)) != 0) { | 207 if (HANDLE_EINTR(close(*fd)) != 0) { |
| 208 PLOG(ERROR) << "close"; | 208 DPLOG(ERROR) << "close"; |
|
Mattias Nissler (ping if slow)
2012/06/04 07:40:50
Why are you muting all the error logging? Many of
Joao da Silva
2012/06/04 08:06:44
From http://codereview.chromium.org/8341026/, whic
| |
| 209 } | 209 } |
| 210 *fd = -1; | 210 *fd = -1; |
| 211 } | 211 } |
| 212 | 212 |
| 213 bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents, | 213 bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents, |
| 214 int count) { | 214 int count) { |
| 215 if (count < 0) { | 215 if (count < 0) { |
| 216 PLOG(ERROR) << "kevent"; | 216 DPLOG(ERROR) << "kevent"; |
| 217 return false; | 217 return false; |
| 218 } | 218 } |
| 219 bool valid = true; | 219 bool valid = true; |
| 220 for (int i = 0; i < count; ++i) { | 220 for (int i = 0; i < count; ++i) { |
| 221 if (kevents[i].flags & EV_ERROR && kevents[i].data) { | 221 if (kevents[i].flags & EV_ERROR && kevents[i].data) { |
| 222 // Find the kevent in |events_| that matches the kevent with the error. | 222 // Find the kevent in |events_| that matches the kevent with the error. |
| 223 EventVector::iterator event = events_.begin(); | 223 EventVector::iterator event = events_.begin(); |
| 224 for (; event != events_.end(); ++event) { | 224 for (; event != events_.end(); ++event) { |
| 225 if (event->ident == kevents[i].ident) { | 225 if (event->ident == kevents[i].ident) { |
| 226 break; | 226 break; |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 std::string path_name; | 229 std::string path_name; |
| 230 if (event != events_.end()) { | 230 if (event != events_.end()) { |
| 231 EventData* event_data = EventDataForKevent(*event); | 231 EventData* event_data = EventDataForKevent(*event); |
| 232 if (event_data != NULL) { | 232 if (event_data != NULL) { |
| 233 path_name = event_data->path_.value(); | 233 path_name = event_data->path_.value(); |
| 234 } | 234 } |
| 235 } | 235 } |
| 236 if (path_name.empty()) { | 236 if (path_name.empty()) { |
| 237 path_name = base::StringPrintf( | 237 path_name = base::StringPrintf( |
| 238 "fd %d", *reinterpret_cast<int*>(&kevents[i].ident)); | 238 "fd %d", *reinterpret_cast<int*>(&kevents[i].ident)); |
| 239 } | 239 } |
| 240 LOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name; | 240 DLOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name; |
| 241 valid = false; | 241 valid = false; |
| 242 } | 242 } |
| 243 } | 243 } |
| 244 return valid; | 244 return valid; |
| 245 } | 245 } |
| 246 | 246 |
| 247 void FilePathWatcherImpl::HandleAttributesChange( | 247 void FilePathWatcherImpl::HandleAttributesChange( |
| 248 const EventVector::iterator& event, | 248 const EventVector::iterator& event, |
| 249 bool* target_file_affected, | 249 bool* target_file_affected, |
| 250 bool* update_watches) { | 250 bool* update_watches) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 } else { | 341 } else { |
| 342 break; | 342 break; |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 } | 345 } |
| 346 return true; | 346 return true; |
| 347 } | 347 } |
| 348 | 348 |
| 349 void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) { | 349 void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) { |
| 350 DCHECK(MessageLoopForIO::current()); | 350 DCHECK(MessageLoopForIO::current()); |
| 351 CHECK_EQ(fd, kqueue_); | 351 DCHECK_EQ(fd, kqueue_); |
| 352 CHECK(events_.size()); | 352 DCHECK(events_.size()); |
| 353 | 353 |
| 354 // Request the file system update notifications that have occurred and return | 354 // Request the file system update notifications that have occurred and return |
| 355 // them in |updates|. |count| will contain the number of updates that have | 355 // them in |updates|. |count| will contain the number of updates that have |
| 356 // occurred. | 356 // occurred. |
| 357 EventVector updates(events_.size()); | 357 EventVector updates(events_.size()); |
| 358 struct timespec timeout = {0, 0}; | 358 struct timespec timeout = {0, 0}; |
| 359 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), | 359 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), |
| 360 &timeout)); | 360 &timeout)); |
| 361 | 361 |
| 362 // Error values are stored within updates, so check to make sure that no | 362 // 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... | |
| 435 DCHECK_EQ(kqueue_, -1); | 435 DCHECK_EQ(kqueue_, -1); |
| 436 | 436 |
| 437 delegate_ = delegate; | 437 delegate_ = delegate; |
| 438 target_ = path; | 438 target_ = path; |
| 439 | 439 |
| 440 MessageLoop::current()->AddDestructionObserver(this); | 440 MessageLoop::current()->AddDestructionObserver(this); |
| 441 io_message_loop_ = base::MessageLoopProxy::current(); | 441 io_message_loop_ = base::MessageLoopProxy::current(); |
| 442 | 442 |
| 443 kqueue_ = kqueue(); | 443 kqueue_ = kqueue(); |
| 444 if (kqueue_ == -1) { | 444 if (kqueue_ == -1) { |
| 445 PLOG(ERROR) << "kqueue"; | 445 DPLOG(ERROR) << "kqueue"; |
| 446 return false; | 446 return false; |
| 447 } | 447 } |
| 448 | 448 |
| 449 int last_entry = EventsForPath(target_, &events_); | 449 int last_entry = EventsForPath(target_, &events_); |
| 450 CHECK_NE(last_entry, 0); | 450 DCHECK_NE(last_entry, 0); |
| 451 | 451 |
| 452 EventVector responses(last_entry); | 452 EventVector responses(last_entry); |
| 453 | 453 |
| 454 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, | 454 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, |
| 455 &responses[0], last_entry, NULL)); | 455 &responses[0], last_entry, NULL)); |
| 456 if (!AreKeventValuesValid(&responses[0], count)) { | 456 if (!AreKeventValuesValid(&responses[0], count)) { |
| 457 // Calling Cancel() here to close any file descriptors that were opened. | 457 // Calling Cancel() here to close any file descriptors that were opened. |
| 458 // This would happen in the destructor anyways, but FilePathWatchers tend to | 458 // This would happen in the destructor anyways, but FilePathWatchers tend to |
| 459 // be long lived, and if an error has occurred, there is no reason to waste | 459 // be long lived, and if an error has occurred, there is no reason to waste |
| 460 // the file descriptors. | 460 // the file descriptors. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 } | 495 } |
| 496 | 496 |
| 497 } // namespace | 497 } // namespace |
| 498 | 498 |
| 499 FilePathWatcher::FilePathWatcher() { | 499 FilePathWatcher::FilePathWatcher() { |
| 500 impl_ = new FilePathWatcherImpl(); | 500 impl_ = new FilePathWatcherImpl(); |
| 501 } | 501 } |
| 502 | 502 |
| 503 } // namespace files | 503 } // namespace files |
| 504 } // namespace base | 504 } // namespace base |
| OLD | NEW |