Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -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 | */ |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 20 | |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 21 | #include "filesystemwatcher.h" |
| 22 | |
| 23 | FileSystemWatcher::FileSystemWatcher(QString dirPath, QObject* parent) : |
| 24 | QObject(parent), |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 25 | m_watcher(new QFileSystemWatcher()), |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 26 | m_timer(new QTimer()), |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 27 | m_dirPath(dirPath) |
| 28 | { |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 29 | // add main directory to monitor |
| 30 | m_watcher->addPath(m_dirPath); |
| 31 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 32 | // register signals (callback functions) |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 33 | connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(watcherCallbackSlot(QString))); |
| 34 | connect(m_timer, SIGNAL(timeout()), this, SLOT(timerCallbackSlot())); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 35 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 36 | // bootstrap |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 37 | QTimer::singleShot(1000, this, SLOT(bootstrap())); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 38 | |
| 39 | // start timer |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 40 | m_timer->start(300000); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 43 | FileSystemWatcher::~FileSystemWatcher() |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 44 | { |
| 45 | // clean up |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 46 | delete m_watcher; |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 47 | delete m_timer; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 50 | void FileSystemWatcher::bootstrap() |
| 51 | { |
| 52 | // bootstrap specific steps |
Jared Lindblom | d5ba95f | 2013-01-12 22:50:09 -0800 | [diff] [blame] | 53 | #if DEBUG |
| 54 | qDebug() << endl << "[BOOTSTRAP]"; |
| 55 | #endif |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 56 | timerCallbackSlot(); |
Jared Lindblom | d5ba95f | 2013-01-12 22:50:09 -0800 | [diff] [blame] | 57 | #if DEBUG |
| 58 | qDebug() << endl << "[\\BOOTSTRAP]"; |
| 59 | #endif |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 62 | void FileSystemWatcher::watcherCallbackSlot(QString dirPath) |
| 63 | { |
| 64 | // watcher specific steps |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 65 | #if DEBUG |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 66 | qDebug() << endl << "[WATCHER] Triggered Path: " << dirPath; |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 67 | #endif |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 68 | handleCallback(dirPath); |
| 69 | } |
| 70 | |
| 71 | void FileSystemWatcher::timerCallbackSlot() |
| 72 | { |
| 73 | // timer specific steps |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 74 | #if DEBUG |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 75 | qDebug() << endl << "[TIMER] Triggered Path: " << m_dirPath; |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 76 | #endif |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 77 | handleCallback(m_dirPath); |
| 78 | } |
| 79 | |
| 80 | void FileSystemWatcher::handleCallback(QString dirPath) |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 81 | { |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 82 | // scan directory and populate file list |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 83 | QHash<QString, qint64> currentState = scanDirectory(dirPath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 84 | |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 85 | // reconcile directory and report changes |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 86 | std::vector<sEventInfo> dirChanges = reconcileDirectory(currentState, dirPath); |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 87 | #if DEBUG |
| 88 | // DEBUG: Print Changes |
| 89 | printChanges(dirChanges); |
| 90 | #endif |
Jared Lindblom | d5ba95f | 2013-01-12 22:50:09 -0800 | [diff] [blame] | 91 | // emit the signal if not empty |
| 92 | if(!dirChanges.empty()) |
| 93 | emit dirEventSignal(dirChanges); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 96 | QHash<QString, qint64> FileSystemWatcher::scanDirectory(QString dirPath) |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 97 | { |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 98 | // list of files in directory |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 99 | QHash<QString, qint64> currentState; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 100 | |
| 101 | // directory iterator (recursive) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 102 | QDirIterator dirIterator(dirPath, QDirIterator::Subdirectories | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 103 | QDirIterator::FollowSymlinks); |
| 104 | |
| 105 | // iterate through directory recursively |
| 106 | while(dirIterator.hasNext()) |
| 107 | { |
| 108 | // Get Next File/Dir |
| 109 | dirIterator.next(); |
| 110 | |
| 111 | // Get FileInfo |
| 112 | QFileInfo fileInfo = dirIterator.fileInfo(); |
| 113 | |
| 114 | // if not this directory or previous directory |
| 115 | if(fileInfo.absoluteFilePath() != ".." && fileInfo.absoluteFilePath() != ".") |
| 116 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 117 | QString absFilePath = fileInfo.absoluteFilePath(); |
| 118 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 119 | // if this is a directory |
| 120 | if(fileInfo.isDir()) |
| 121 | { |
| 122 | QStringList dirList = m_watcher->directories(); |
| 123 | |
| 124 | // if the directory is not already being watched |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 125 | if (absFilePath.startsWith(m_dirPath) && !dirList.contains(absFilePath)) |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 126 | { |
| 127 | // add this directory to the watch list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 128 | m_watcher->addPath(absFilePath); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // add this file to the file list |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 134 | currentState.insert(absFilePath, fileInfo.created().toMSecsSinceEpoch()); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 139 | return currentState; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 142 | std::vector<sEventInfo> FileSystemWatcher::reconcileDirectory(QHash<QString, qint64> currentState, QString dirPath) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 143 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 144 | // list of files changed |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 145 | std::vector<sEventInfo> dirChanges; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 146 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 147 | // compare result (database/stored snapshot) to fileList (current snapshot) |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 148 | QMutableHashIterator<QString, qint64> i(m_storedState); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 149 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 150 | while(i.hasNext()) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 151 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 152 | i.next(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 153 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 154 | QString absFilePath = i.key(); |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 155 | qint64 storedCreated = i.value(); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 156 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 157 | // if this file is in a level higher than |
| 158 | // this directory, ignore |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 159 | if(!absFilePath.startsWith(dirPath)) |
| 160 | { |
| 161 | continue; |
| 162 | } |
| 163 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 164 | // check file existence |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 165 | if(currentState.contains(absFilePath)) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 166 | { |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 167 | qint64 currentCreated = currentState.value(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 168 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 169 | if(storedCreated != currentCreated) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 170 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 171 | // update stored state |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 172 | i.setValue(currentCreated); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 173 | |
| 174 | // this file has been modified |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 175 | sEventInfo eventInfo; |
| 176 | eventInfo.event = MODIFIED; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 177 | eventInfo.absFilePath = absFilePath.toStdString(); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 178 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 181 | // delete this file from fileList we have processed it |
| 182 | currentState.remove(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 186 | // delete from stored state |
| 187 | i.remove(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 188 | |
| 189 | // this file has been deleted |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 190 | sEventInfo eventInfo; |
| 191 | eventInfo.event = DELETED; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 192 | eventInfo.absFilePath = absFilePath.toStdString(); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 193 | dirChanges.push_back(eventInfo); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 194 | } |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 197 | // any files left in fileList have been added |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 198 | for(QHash<QString, qint64>::iterator i = currentState.begin(); i != currentState.end(); ++i) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 199 | { |
| 200 | QString absFilePath = i.key(); |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 201 | qint64 currentCreated = i.value(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 202 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 203 | m_storedState.insert(absFilePath, currentCreated); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 204 | |
| 205 | // this file has been added |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 206 | sEventInfo eventInfo; |
| 207 | eventInfo.event = ADDED; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 208 | eventInfo.absFilePath = absFilePath.toStdString(); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 209 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 212 | return dirChanges; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 213 | } |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 214 | |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 215 | QByteArray FileSystemWatcher::calcChecksum(QString absFilePath) |
| 216 | { |
| 217 | // initialize checksum |
| 218 | QCryptographicHash crypto(QCryptographicHash::Md5); |
| 219 | |
| 220 | // open file |
| 221 | QFile file(absFilePath); |
| 222 | file.open(QFile::ReadOnly); |
| 223 | |
| 224 | // calculate checksum |
| 225 | while(!file.atEnd()) |
| 226 | { |
| 227 | crypto.addData(file.read(8192)); |
| 228 | } |
| 229 | |
| 230 | return crypto.result(); |
| 231 | } |
| 232 | |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 233 | void FileSystemWatcher::printChanges(std::vector<sEventInfo> dirChanges) |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 234 | { |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 235 | if(!dirChanges.empty()) |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 236 | { |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 237 | for(size_t i = 0; i < dirChanges.size(); i++) |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 238 | { |
| 239 | QString tempString; |
| 240 | |
| 241 | eEvent event = dirChanges[i].event; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 242 | QString absFilePath = QString::fromStdString(dirChanges[i].absFilePath); |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 243 | |
| 244 | switch(event) |
| 245 | { |
| 246 | case ADDED: |
| 247 | tempString.append("ADDED: "); |
| 248 | break; |
| 249 | case MODIFIED: |
| 250 | tempString.append("MODIFIED: "); |
| 251 | break; |
| 252 | case DELETED: |
| 253 | tempString.append("DELETED: "); |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | tempString.append(absFilePath); |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 258 | |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame] | 259 | qDebug() << "\t" << tempString; |
| 260 | } |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 261 | } |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 262 | else |
| 263 | { |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 264 | qDebug() << "\t[EMPTY]"; |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 265 | } |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 268 | #if WAF |
| 269 | #include "filesystemwatcher.moc" |
| 270 | #include "filesystemwatcher.cpp.moc" |
| 271 | #endif |