Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Jared Lindblom <lindblom@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "fs-watcher.h" |
| 24 | #include "logging.h" |
| 25 | |
| 26 | #include <boost/bind.hpp> |
| 27 | |
| 28 | #include <QDirIterator> |
| 29 | #include <QRegExp> |
| 30 | |
| 31 | using namespace std; |
| 32 | using namespace boost; |
| 33 | |
| 34 | INIT_LOGGER ("FsWatcher"); |
| 35 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 36 | FsWatcher::FsWatcher (QString dirPath, |
| 37 | LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete, |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 38 | FileState *fileState, |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 39 | QObject* parent) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 40 | : QObject(parent) |
| 41 | , m_watcher (new QFileSystemWatcher()) |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 42 | , m_scheduler (new Scheduler ()) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 43 | , m_dirPath (dirPath) |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 44 | , m_onChange (onChange) |
| 45 | , m_onDelete (onDelete) |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 46 | , m_fileState (fileState) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 47 | { |
| 48 | _LOG_DEBUG ("Monitor dir: " << m_dirPath.toStdString ()); |
| 49 | // add main directory to monitor |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 51 | m_watcher->addPath (m_dirPath); |
| 52 | |
| 53 | // register signals (callback functions) |
| 54 | connect (m_watcher, SIGNAL (directoryChanged (QString)), this, SLOT (DidDirectoryChanged (QString))); |
| 55 | connect (m_watcher, SIGNAL (fileChanged (QString)), this, SLOT (DidFileChanged (QString))); |
| 56 | |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 57 | m_scheduler->start (); |
| 58 | |
Alexander Afanasyev | 7943c6b | 2013-01-29 18:43:24 -0800 | [diff] [blame] | 59 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 60 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, m_dirPath, false/* don't remove incomplete files*/), |
| 61 | "rescan-r-" + m_dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 7943c6b | 2013-01-29 18:43:24 -0800 | [diff] [blame] | 63 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 64 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, m_dirPath, true), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 65 | "rescan-" +m_dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | FsWatcher::~FsWatcher() |
| 69 | { |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 70 | m_scheduler->shutdown (); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | FsWatcher::DidDirectoryChanged (QString dirPath) |
| 75 | { |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 76 | _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 78 | filesystem::path absPathTriggeredDir (dirPath.toStdString ()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 79 | if (!filesystem::exists (filesystem::path (absPathTriggeredDir))) |
| 80 | { |
| 81 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 82 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, dirPath, true/* ignore incomplete file flag. the whole directory got removed*/), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 83 | "r-" + dirPath.toStdString ()); // only one task will be scheduled per directory |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | // m_executor.execute (bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath)); |
| 88 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 89 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath, false), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 90 | dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 91 | |
| 92 | // m_executor.execute (bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath)); |
| 93 | Scheduler::scheduleOneTimeTask (m_scheduler, 300, |
| 94 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath, true), |
| 95 | "rescan-"+dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 96 | |
| 97 | Scheduler::scheduleOneTimeTask (m_scheduler, 300, |
| 98 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, m_dirPath, false/* don't remove incomplete files*/), |
| 99 | "rescan-r-" + m_dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 100 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
| 104 | FsWatcher::DidFileChanged (QString filePath) |
| 105 | { |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 106 | if (!filePath.startsWith (m_dirPath)) |
| 107 | { |
| 108 | _LOG_ERROR ("Got notification about a file not from the monitored directory"); |
| 109 | return; |
| 110 | } |
| 111 | filesystem::path absPathTriggeredFile (filePath.toStdString ()); |
| 112 | filePath.remove (0, m_dirPath.size ()); |
| 113 | |
| 114 | filesystem::path triggeredFile (filePath.toStdString ()); |
| 115 | if (filesystem::exists (filesystem::path (absPathTriggeredFile))) |
| 116 | { |
| 117 | _LOG_DEBUG ("Triggered UPDATE of file: " << triggeredFile.relative_path ().generic_string ()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 118 | // m_onChange (triggeredFile.relative_path ()); |
| 119 | |
| 120 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
| 121 | bind (m_onChange, triggeredFile.relative_path ()), |
| 122 | triggeredFile.relative_path ().string()); |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 123 | } |
| 124 | else |
| 125 | { |
| 126 | _LOG_DEBUG ("Triggered DELETE of file: " << triggeredFile.relative_path ().generic_string ()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 127 | // m_onDelete (triggeredFile.relative_path ()); |
| 128 | |
| 129 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
| 130 | bind (m_onDelete, triggeredFile.relative_path ()), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 131 | "r-" + triggeredFile.relative_path ().string()); |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 132 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 135 | void |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 136 | FsWatcher::ScanDirectory_NotifyUpdates_Execute (QString dirPath, bool notifyCallbacks) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 137 | { |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 138 | _LOG_TRACE (" >> ScanDirectory_NotifyUpdates_Execute"); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 140 | // exclude working only on last component, not the full path; iterating through all directories, even excluded from monitoring |
| 141 | QRegExp exclude ("^(\\.|\\.\\.|\\.chronoshare|.*~|.*\\.swp)$"); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 142 | |
| 143 | QDirIterator dirIterator (dirPath, |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 144 | QDir::Dirs | QDir::Files | /*QDir::Hidden |*/ QDir::NoSymLinks | QDir::NoDotAndDotDot, |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 145 | QDirIterator::Subdirectories); // directory iterator (recursive) |
| 146 | |
| 147 | // iterate through directory recursively |
| 148 | while (dirIterator.hasNext ()) |
| 149 | { |
| 150 | dirIterator.next (); |
| 151 | |
| 152 | // Get FileInfo |
| 153 | QFileInfo fileInfo = dirIterator.fileInfo (); |
| 154 | |
| 155 | QString name = fileInfo.fileName (); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 156 | _LOG_DEBUG ("+++ Scanning: " << name.toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 157 | |
| 158 | if (!exclude.exactMatch (name)) |
| 159 | { |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 160 | _LOG_DEBUG ("Not excluded file/dir: " << fileInfo.absoluteFilePath ().toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 161 | QString absFilePath = fileInfo.absoluteFilePath (); |
| 162 | |
| 163 | // _LOG_DEBUG ("Attempt to add path to watcher: " << absFilePath.toStdString ()); |
| 164 | m_watcher->addPath (absFilePath); |
| 165 | |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 166 | if (notifyCallbacks && fileInfo.isFile ()) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 167 | { |
| 168 | DidFileChanged (absFilePath); |
| 169 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
| 173 | // _LOG_DEBUG ("Excluded file/dir: " << fileInfo.filePath ().toStdString ()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 178 | |
| 179 | void |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 180 | FsWatcher::ScanDirectory_NotifyRemovals_Execute (QString dirPath, bool removeIncomplete) |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 181 | { |
| 182 | _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ()); |
| 183 | |
| 184 | filesystem::path absPathTriggeredDir (dirPath.toStdString ()); |
| 185 | dirPath.remove (0, m_dirPath.size ()); |
| 186 | |
| 187 | filesystem::path triggeredDir (dirPath.toStdString ()); |
| 188 | |
Alexander Afanasyev | 506ff22 | 2013-01-29 18:12:55 -0800 | [diff] [blame] | 189 | FileItemsPtr files = m_fileState->LookupFilesInFolderRecursively (triggeredDir.relative_path ().generic_string ()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 190 | for (std::list<FileItem>::iterator file = files->begin (); file != files->end (); file ++) |
| 191 | { |
| 192 | filesystem::path testFile = filesystem::path (m_dirPath.toStdString ()) / file->filename (); |
| 193 | _LOG_DEBUG ("Check file for deletion [" << testFile.generic_string () << "]"); |
| 194 | |
| 195 | if (!filesystem::exists (testFile)) |
| 196 | { |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 197 | if (removeIncomplete || file->is_complete ()) |
| 198 | { |
| 199 | _LOG_DEBUG ("Notifying about removed file [" << file->filename () << "]"); |
| 200 | m_onDelete (file->filename ()); |
| 201 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 206 | |
| 207 | #if WAF |
| 208 | #include "fs-watcher.moc" |
| 209 | #include "fs-watcher.cc.moc" |
| 210 | #endif |