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" |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 24 | #include "db-helper.h" |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 25 | #include "logging.h" |
| 26 | |
| 27 | #include <boost/bind.hpp> |
| 28 | |
| 29 | #include <QDirIterator> |
| 30 | #include <QRegExp> |
| 31 | |
| 32 | using namespace std; |
| 33 | using namespace boost; |
| 34 | |
| 35 | INIT_LOGGER ("FsWatcher"); |
| 36 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 37 | FsWatcher::FsWatcher (QString dirPath, |
| 38 | LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete, |
| 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 | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 46 | { |
| 47 | _LOG_DEBUG ("Monitor dir: " << m_dirPath.toStdString ()); |
| 48 | // add main directory to monitor |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 49 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 50 | initFileStateDb(); |
| 51 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 52 | m_watcher->addPath (m_dirPath); |
| 53 | |
| 54 | // register signals (callback functions) |
| 55 | connect (m_watcher, SIGNAL (directoryChanged (QString)), this, SLOT (DidDirectoryChanged (QString))); |
| 56 | connect (m_watcher, SIGNAL (fileChanged (QString)), this, SLOT (DidFileChanged (QString))); |
| 57 | |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 58 | m_scheduler->start (); |
| 59 | |
Alexander Afanasyev | 7943c6b | 2013-01-29 18:43:24 -0800 | [diff] [blame] | 60 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 61 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, m_dirPath), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 62 | "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] | 63 | |
Alexander Afanasyev | 7943c6b | 2013-01-29 18:43:24 -0800 | [diff] [blame] | 64 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 65 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, m_dirPath), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 66 | "rescan-" +m_dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | FsWatcher::~FsWatcher() |
| 70 | { |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 71 | m_scheduler->shutdown (); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 72 | sqlite3_close(m_db); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | FsWatcher::DidDirectoryChanged (QString dirPath) |
| 77 | { |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 78 | _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 80 | filesystem::path absPathTriggeredDir (dirPath.toStdString ()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 81 | if (!filesystem::exists (filesystem::path (absPathTriggeredDir))) |
| 82 | { |
| 83 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 84 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, dirPath), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 85 | "r-" + dirPath.toStdString ()); // only one task will be scheduled per directory |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | // m_executor.execute (bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath)); |
| 90 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 91 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 92 | dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 93 | |
| 94 | // m_executor.execute (bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath)); |
| 95 | Scheduler::scheduleOneTimeTask (m_scheduler, 300, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 96 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath), |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 97 | "rescan-"+dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 98 | |
| 99 | Scheduler::scheduleOneTimeTask (m_scheduler, 300, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 100 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, m_dirPath), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 101 | "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] | 102 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | FsWatcher::DidFileChanged (QString filePath) |
| 107 | { |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 108 | if (!filePath.startsWith (m_dirPath)) |
| 109 | { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 110 | _LOG_ERROR ("Got notification about a file not from the monitored directory: " << filePath.toStdString()); |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 111 | return; |
| 112 | } |
Alexander Afanasyev | e70a2d8 | 2013-02-19 22:49:10 -0800 | [diff] [blame^] | 113 | QString absFilePath = filePath; |
| 114 | |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 115 | filesystem::path absPathTriggeredFile (filePath.toStdString ()); |
| 116 | filePath.remove (0, m_dirPath.size ()); |
| 117 | |
| 118 | filesystem::path triggeredFile (filePath.toStdString ()); |
| 119 | if (filesystem::exists (filesystem::path (absPathTriggeredFile))) |
| 120 | { |
| 121 | _LOG_DEBUG ("Triggered UPDATE of file: " << triggeredFile.relative_path ().generic_string ()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 122 | // m_onChange (triggeredFile.relative_path ()); |
| 123 | |
Alexander Afanasyev | e70a2d8 | 2013-02-19 22:49:10 -0800 | [diff] [blame^] | 124 | m_watcher->removePath (absFilePath); |
| 125 | m_watcher->addPath (absFilePath); |
| 126 | |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 127 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
| 128 | bind (m_onChange, triggeredFile.relative_path ()), |
| 129 | triggeredFile.relative_path ().string()); |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
| 133 | _LOG_DEBUG ("Triggered DELETE of file: " << triggeredFile.relative_path ().generic_string ()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 134 | // m_onDelete (triggeredFile.relative_path ()); |
| 135 | |
Alexander Afanasyev | e70a2d8 | 2013-02-19 22:49:10 -0800 | [diff] [blame^] | 136 | m_watcher->removePath (absFilePath); |
| 137 | |
Zhenkai Zhu | 50746f6 | 2013-02-19 21:20:25 -0800 | [diff] [blame] | 138 | deleteFile(triggeredFile.relative_path()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 139 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
| 140 | bind (m_onDelete, triggeredFile.relative_path ()), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 141 | "r-" + triggeredFile.relative_path ().string()); |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 142 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 145 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 146 | FsWatcher::ScanDirectory_NotifyUpdates_Execute (QString dirPath) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 147 | { |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 148 | _LOG_TRACE (" >> ScanDirectory_NotifyUpdates_Execute"); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 150 | // exclude working only on last component, not the full path; iterating through all directories, even excluded from monitoring |
| 151 | QRegExp exclude ("^(\\.|\\.\\.|\\.chronoshare|.*~|.*\\.swp)$"); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 152 | |
| 153 | QDirIterator dirIterator (dirPath, |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 154 | QDir::Dirs | QDir::Files | /*QDir::Hidden |*/ QDir::NoSymLinks | QDir::NoDotAndDotDot, |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 155 | QDirIterator::Subdirectories); // directory iterator (recursive) |
| 156 | |
| 157 | // iterate through directory recursively |
| 158 | while (dirIterator.hasNext ()) |
| 159 | { |
| 160 | dirIterator.next (); |
| 161 | |
| 162 | // Get FileInfo |
| 163 | QFileInfo fileInfo = dirIterator.fileInfo (); |
| 164 | |
| 165 | QString name = fileInfo.fileName (); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 166 | _LOG_DEBUG ("+++ Scanning: " << name.toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 167 | |
| 168 | if (!exclude.exactMatch (name)) |
| 169 | { |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 170 | _LOG_DEBUG ("Not excluded file/dir: " << fileInfo.absoluteFilePath ().toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 171 | QString absFilePath = fileInfo.absoluteFilePath (); |
| 172 | |
| 173 | // _LOG_DEBUG ("Attempt to add path to watcher: " << absFilePath.toStdString ()); |
Alexander Afanasyev | c494204 | 2013-02-19 13:54:25 -0800 | [diff] [blame] | 174 | m_watcher->removePath (absFilePath); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 175 | m_watcher->addPath (absFilePath); |
| 176 | |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 177 | if (fileInfo.isFile ()) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 178 | { |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 179 | QString relFile = absFilePath; |
| 180 | relFile.remove (0, m_dirPath.size ()); |
| 181 | filesystem::path aFile (relFile.toStdString ()); |
| 182 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 183 | if ( |
| 184 | //!m_fileState->LookupFile (aFile.relative_path ().generic_string ()) /* file does not exist there, but exists locally: added */) |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 185 | !fileExists(aFile.relative_path()) /*file does not exist in db, but exists in fs: add */) |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 186 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 187 | addFile(aFile.relative_path()); |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 188 | DidFileChanged (absFilePath); |
| 189 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 190 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 191 | } |
| 192 | else |
| 193 | { |
| 194 | // _LOG_DEBUG ("Excluded file/dir: " << fileInfo.filePath ().toStdString ()); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 199 | |
| 200 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 201 | FsWatcher::ScanDirectory_NotifyRemovals_Execute (QString dirPath) |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 202 | { |
| 203 | _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ()); |
| 204 | |
| 205 | filesystem::path absPathTriggeredDir (dirPath.toStdString ()); |
| 206 | dirPath.remove (0, m_dirPath.size ()); |
| 207 | |
| 208 | filesystem::path triggeredDir (dirPath.toStdString ()); |
| 209 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 210 | /* |
Alexander Afanasyev | 506ff22 | 2013-01-29 18:12:55 -0800 | [diff] [blame] | 211 | FileItemsPtr files = m_fileState->LookupFilesInFolderRecursively (triggeredDir.relative_path ().generic_string ()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 212 | for (std::list<FileItem>::iterator file = files->begin (); file != files->end (); file ++) |
| 213 | { |
| 214 | filesystem::path testFile = filesystem::path (m_dirPath.toStdString ()) / file->filename (); |
| 215 | _LOG_DEBUG ("Check file for deletion [" << testFile.generic_string () << "]"); |
| 216 | |
| 217 | if (!filesystem::exists (testFile)) |
| 218 | { |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 219 | if (removeIncomplete || file->is_complete ()) |
| 220 | { |
| 221 | _LOG_DEBUG ("Notifying about removed file [" << file->filename () << "]"); |
| 222 | m_onDelete (file->filename ()); |
| 223 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 226 | */ |
| 227 | |
| 228 | vector<string> files; |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 229 | getFilesInDir(triggeredDir.relative_path(), files); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 230 | for (vector<string>::iterator file = files.begin(); file != files.end(); file++) |
| 231 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 232 | filesystem::path targetFile = filesystem::path (m_dirPath.toStdString()) / *file; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 233 | if (!filesystem::exists (targetFile)) |
| 234 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 235 | deleteFile(*file); |
| 236 | m_onDelete(*file); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 241 | const string INIT_DATABASE = "\ |
| 242 | CREATE TABLE IF NOT EXISTS \n\ |
| 243 | Files( \n\ |
| 244 | filename TEXT NOT NULL, \n\ |
| 245 | PRIMARY KEY (filename) \n\ |
| 246 | ); \n\ |
| 247 | CREATE INDEX filename_index ON Files (filename); \n\ |
| 248 | "; |
| 249 | |
| 250 | void |
| 251 | FsWatcher::initFileStateDb() |
| 252 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 253 | filesystem::path dbFolder = filesystem::path (m_dirPath.toStdString()) / ".chronoshare" / "fs_watcher"; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 254 | filesystem::create_directories(dbFolder); |
| 255 | |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 256 | int res = sqlite3_open((dbFolder / "filestate.db").string ().c_str (), &m_db); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 257 | if (res != SQLITE_OK) |
| 258 | { |
| 259 | BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot open database: " + (dbFolder / "filestate.db").string())); |
| 260 | } |
| 261 | |
| 262 | char *errmsg = 0; |
| 263 | res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg); |
| 264 | if (res != SQLITE_OK && errmsg != 0) |
| 265 | { |
| 266 | // _LOG_TRACE ("Init \"error\": " << errmsg); |
| 267 | cout << "FS-Watcher DB error: " << errmsg << endl; |
| 268 | sqlite3_free (errmsg); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | bool |
| 273 | FsWatcher::fileExists(const filesystem::path &filename) |
| 274 | { |
| 275 | sqlite3_stmt *stmt; |
| 276 | sqlite3_prepare_v2(m_db, "SELECT * FROM Files WHERE filename = ?;", -1, &stmt, 0); |
| 277 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 278 | bool retval = false; |
| 279 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 280 | { |
| 281 | retval = true; |
| 282 | } |
| 283 | sqlite3_finalize(stmt); |
| 284 | |
| 285 | return retval; |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | FsWatcher::addFile(const filesystem::path &filename) |
| 290 | { |
| 291 | sqlite3_stmt *stmt; |
| 292 | sqlite3_prepare_v2(m_db, "INSERT OR IGNORE INTO Files (filename) VALUES (?);", -1, &stmt, 0); |
| 293 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 294 | sqlite3_step(stmt); |
| 295 | sqlite3_finalize(stmt); |
| 296 | } |
| 297 | |
| 298 | void |
| 299 | FsWatcher::deleteFile(const filesystem::path &filename) |
| 300 | { |
| 301 | sqlite3_stmt *stmt; |
| 302 | sqlite3_prepare_v2(m_db, "DELETE FROM Files WHERE filename = ?;", -1, &stmt, 0); |
| 303 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 304 | sqlite3_step(stmt); |
| 305 | sqlite3_finalize(stmt); |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | FsWatcher::getFilesInDir(const filesystem::path &dir, vector<string> &files) |
| 310 | { |
| 311 | sqlite3_stmt *stmt; |
| 312 | sqlite3_prepare_v2(m_db, "SELECT * FROM Files WHERE filename LIKE ?;", -1, &stmt, 0); |
| 313 | |
| 314 | string dirStr = dir.string(); |
| 315 | ostringstream escapedDir; |
| 316 | for (string::const_iterator ch = dirStr.begin (); ch != dirStr.end (); ch ++) |
| 317 | { |
| 318 | if (*ch == '%') |
| 319 | escapedDir << "\\%"; |
| 320 | else |
| 321 | escapedDir << *ch; |
| 322 | } |
| 323 | escapedDir << "/" << "%"; |
| 324 | string escapedDirStr = escapedDir.str (); |
| 325 | sqlite3_bind_text (stmt, 1, escapedDirStr.c_str (), escapedDirStr.size (), SQLITE_STATIC); |
| 326 | |
| 327 | while(sqlite3_step(stmt) == SQLITE_ROW) |
| 328 | { |
| 329 | string filename(reinterpret_cast<const char *>(sqlite3_column_text (stmt, 0)), sqlite3_column_bytes(stmt, 0)); |
| 330 | files.push_back(filename); |
| 331 | } |
| 332 | |
| 333 | sqlite3_finalize (stmt); |
| 334 | |
| 335 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 336 | |
| 337 | #if WAF |
| 338 | #include "fs-watcher.moc" |
| 339 | #include "fs-watcher.cc.moc" |
| 340 | #endif |