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