Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "fs-watcher.hpp" |
| 22 | #include "db-helper.hpp" |
| 23 | #include "logging.hpp" |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 24 | |
| 25 | #include <boost/bind.hpp> |
| 26 | |
| 27 | #include <QDirIterator> |
| 28 | #include <QRegExp> |
| 29 | |
| 30 | using namespace std; |
| 31 | using namespace boost; |
| 32 | |
| 33 | INIT_LOGGER ("FsWatcher"); |
| 34 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 35 | FsWatcher::FsWatcher (QString dirPath, |
| 36 | LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete, |
| 37 | QObject* parent) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 38 | : QObject(parent) |
| 39 | , m_watcher (new QFileSystemWatcher()) |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 40 | , m_scheduler (new Scheduler ()) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 41 | , m_dirPath (dirPath) |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 42 | , m_onChange (onChange) |
| 43 | , m_onDelete (onDelete) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 44 | { |
| 45 | _LOG_DEBUG ("Monitor dir: " << m_dirPath.toStdString ()); |
| 46 | // add main directory to monitor |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 47 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 48 | initFileStateDb(); |
| 49 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 50 | m_watcher->addPath (m_dirPath); |
| 51 | |
| 52 | // register signals (callback functions) |
| 53 | connect (m_watcher, SIGNAL (directoryChanged (QString)), this, SLOT (DidDirectoryChanged (QString))); |
| 54 | connect (m_watcher, SIGNAL (fileChanged (QString)), this, SLOT (DidFileChanged (QString))); |
| 55 | |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 56 | m_scheduler->start (); |
| 57 | |
Alexander Afanasyev | 7943c6b | 2013-01-29 18:43:24 -0800 | [diff] [blame] | 58 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 59 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, m_dirPath), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 60 | "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] | 61 | |
Alexander Afanasyev | 7943c6b | 2013-01-29 18:43:24 -0800 | [diff] [blame] | 62 | Scheduler::scheduleOneTimeTask (m_scheduler, 0, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 63 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, m_dirPath), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 64 | "rescan-" +m_dirPath.toStdString ()); // only one task will be scheduled per directory |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | FsWatcher::~FsWatcher() |
| 68 | { |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 69 | m_scheduler->shutdown (); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 70 | sqlite3_close(m_db); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | FsWatcher::DidDirectoryChanged (QString dirPath) |
| 75 | { |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 76 | _LOG_DEBUG ("Triggered DirPath(DidDirectoryChanged): " << 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, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 82 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, dirPath), |
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, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 89 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath), |
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, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 94 | bind (&FsWatcher::ScanDirectory_NotifyUpdates_Execute, this, dirPath), |
Alexander Afanasyev | c80207d | 2013-01-29 20:07:39 -0800 | [diff] [blame] | 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, |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 98 | bind (&FsWatcher::ScanDirectory_NotifyRemovals_Execute, this, m_dirPath), |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 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 | { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 108 | _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] | 109 | return; |
| 110 | } |
Alexander Afanasyev | e70a2d8 | 2013-02-19 22:49:10 -0800 | [diff] [blame] | 111 | QString absFilePath = filePath; |
| 112 | |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 113 | filesystem::path absPathTriggeredFile (filePath.toStdString ()); |
| 114 | filePath.remove (0, m_dirPath.size ()); |
| 115 | |
| 116 | filesystem::path triggeredFile (filePath.toStdString ()); |
| 117 | if (filesystem::exists (filesystem::path (absPathTriggeredFile))) |
| 118 | { |
| 119 | _LOG_DEBUG ("Triggered UPDATE of file: " << triggeredFile.relative_path ().generic_string ()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 120 | // m_onChange (triggeredFile.relative_path ()); |
| 121 | |
Alexander Afanasyev | e70a2d8 | 2013-02-19 22:49:10 -0800 | [diff] [blame] | 122 | m_watcher->removePath (absFilePath); |
| 123 | m_watcher->addPath (absFilePath); |
| 124 | |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 125 | Scheduler::scheduleDelayOneTimeTask (m_scheduler, 0.5, |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 126 | bind (m_onChange, triggeredFile.relative_path ()), |
| 127 | triggeredFile.relative_path ().string()); |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
| 132 | _LOG_DEBUG ("Triggered DELETE of file: " << triggeredFile.relative_path ().generic_string ()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 133 | // m_onDelete (triggeredFile.relative_path ()); |
| 134 | |
Alexander Afanasyev | e70a2d8 | 2013-02-19 22:49:10 -0800 | [diff] [blame] | 135 | m_watcher->removePath (absFilePath); |
| 136 | |
Zhenkai Zhu | 50746f6 | 2013-02-19 21:20:25 -0800 | [diff] [blame] | 137 | deleteFile(triggeredFile.relative_path()); |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 138 | Scheduler::scheduleOneTimeTask (m_scheduler, 0.5, |
| 139 | bind (m_onDelete, triggeredFile.relative_path ()), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 140 | "r-" + triggeredFile.relative_path ().string()); |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 141 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 144 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 145 | FsWatcher::ScanDirectory_NotifyUpdates_Execute (QString dirPath) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 146 | { |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 147 | _LOG_TRACE (" >> ScanDirectory_NotifyUpdates_Execute " << dirPath.toStdString()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 149 | // exclude working only on last component, not the full path; iterating through all directories, even excluded from monitoring |
| 150 | QRegExp exclude ("^(\\.|\\.\\.|\\.chronoshare|.*~|.*\\.swp)$"); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 151 | |
| 152 | QDirIterator dirIterator (dirPath, |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 153 | QDir::Dirs | QDir::Files | /*QDir::Hidden |*/ QDir::NoSymLinks | QDir::NoDotAndDotDot, |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 154 | QDirIterator::Subdirectories); // directory iterator (recursive) |
| 155 | |
| 156 | // iterate through directory recursively |
| 157 | while (dirIterator.hasNext ()) |
| 158 | { |
| 159 | dirIterator.next (); |
| 160 | |
| 161 | // Get FileInfo |
| 162 | QFileInfo fileInfo = dirIterator.fileInfo (); |
| 163 | |
| 164 | QString name = fileInfo.fileName (); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 165 | _LOG_DEBUG ("+++ Scanning: " << name.toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 166 | |
| 167 | if (!exclude.exactMatch (name)) |
| 168 | { |
Alexander Afanasyev | 69e1317 | 2013-01-25 17:16:27 -0800 | [diff] [blame] | 169 | _LOG_DEBUG ("Not excluded file/dir: " << fileInfo.absoluteFilePath ().toStdString ()); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 170 | QString absFilePath = fileInfo.absoluteFilePath (); |
| 171 | |
| 172 | // _LOG_DEBUG ("Attempt to add path to watcher: " << absFilePath.toStdString ()); |
Alexander Afanasyev | c494204 | 2013-02-19 13:54:25 -0800 | [diff] [blame] | 173 | m_watcher->removePath (absFilePath); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 174 | m_watcher->addPath (absFilePath); |
| 175 | |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 176 | if (fileInfo.isFile ()) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 177 | { |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 178 | QString relFile = absFilePath; |
| 179 | relFile.remove (0, m_dirPath.size ()); |
| 180 | filesystem::path aFile (relFile.toStdString ()); |
| 181 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 182 | if ( |
| 183 | //!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] | 184 | !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] | 185 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 186 | addFile(aFile.relative_path()); |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 187 | DidFileChanged (absFilePath); |
| 188 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 189 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
| 193 | // _LOG_DEBUG ("Excluded file/dir: " << fileInfo.filePath ().toStdString ()); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 198 | |
| 199 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 200 | FsWatcher::ScanDirectory_NotifyRemovals_Execute (QString dirPath) |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 201 | { |
| 202 | _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ()); |
| 203 | |
| 204 | filesystem::path absPathTriggeredDir (dirPath.toStdString ()); |
| 205 | dirPath.remove (0, m_dirPath.size ()); |
| 206 | |
| 207 | filesystem::path triggeredDir (dirPath.toStdString ()); |
| 208 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 209 | /* |
Alexander Afanasyev | 506ff22 | 2013-01-29 18:12:55 -0800 | [diff] [blame] | 210 | FileItemsPtr files = m_fileState->LookupFilesInFolderRecursively (triggeredDir.relative_path ().generic_string ()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 211 | for (std::list<FileItem>::iterator file = files->begin (); file != files->end (); file ++) |
| 212 | { |
| 213 | filesystem::path testFile = filesystem::path (m_dirPath.toStdString ()) / file->filename (); |
| 214 | _LOG_DEBUG ("Check file for deletion [" << testFile.generic_string () << "]"); |
| 215 | |
| 216 | if (!filesystem::exists (testFile)) |
| 217 | { |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 218 | if (removeIncomplete || file->is_complete ()) |
| 219 | { |
| 220 | _LOG_DEBUG ("Notifying about removed file [" << file->filename () << "]"); |
| 221 | m_onDelete (file->filename ()); |
| 222 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 225 | */ |
| 226 | |
| 227 | vector<string> files; |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 228 | getFilesInDir(triggeredDir.relative_path(), files); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 229 | for (vector<string>::iterator file = files.begin(); file != files.end(); file++) |
| 230 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 231 | filesystem::path targetFile = filesystem::path (m_dirPath.toStdString()) / *file; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 232 | if (!filesystem::exists (targetFile)) |
| 233 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 234 | deleteFile(*file); |
| 235 | m_onDelete(*file); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 240 | const string INIT_DATABASE = "\ |
| 241 | CREATE TABLE IF NOT EXISTS \n\ |
| 242 | Files( \n\ |
| 243 | filename TEXT NOT NULL, \n\ |
| 244 | PRIMARY KEY (filename) \n\ |
| 245 | ); \n\ |
| 246 | CREATE INDEX filename_index ON Files (filename); \n\ |
| 247 | "; |
| 248 | |
| 249 | void |
| 250 | FsWatcher::initFileStateDb() |
| 251 | { |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 252 | filesystem::path dbFolder = filesystem::path (m_dirPath.toStdString()) / ".chronoshare" / "fs_watcher"; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 253 | filesystem::create_directories(dbFolder); |
| 254 | |
Alexander Afanasyev | 89024ac | 2013-02-14 09:38:10 -0800 | [diff] [blame] | 255 | int res = sqlite3_open((dbFolder / "filestate.db").string ().c_str (), &m_db); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 256 | if (res != SQLITE_OK) |
| 257 | { |
| 258 | BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot open database: " + (dbFolder / "filestate.db").string())); |
| 259 | } |
| 260 | |
| 261 | char *errmsg = 0; |
| 262 | res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg); |
| 263 | if (res != SQLITE_OK && errmsg != 0) |
| 264 | { |
| 265 | // _LOG_TRACE ("Init \"error\": " << errmsg); |
| 266 | cout << "FS-Watcher DB error: " << errmsg << endl; |
| 267 | sqlite3_free (errmsg); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | bool |
| 272 | FsWatcher::fileExists(const filesystem::path &filename) |
| 273 | { |
| 274 | sqlite3_stmt *stmt; |
| 275 | sqlite3_prepare_v2(m_db, "SELECT * FROM Files WHERE filename = ?;", -1, &stmt, 0); |
| 276 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 277 | bool retval = false; |
| 278 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 279 | { |
| 280 | retval = true; |
| 281 | } |
| 282 | sqlite3_finalize(stmt); |
| 283 | |
| 284 | return retval; |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | FsWatcher::addFile(const filesystem::path &filename) |
| 289 | { |
| 290 | sqlite3_stmt *stmt; |
| 291 | sqlite3_prepare_v2(m_db, "INSERT OR IGNORE INTO Files (filename) VALUES (?);", -1, &stmt, 0); |
| 292 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 293 | sqlite3_step(stmt); |
| 294 | sqlite3_finalize(stmt); |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | FsWatcher::deleteFile(const filesystem::path &filename) |
| 299 | { |
| 300 | sqlite3_stmt *stmt; |
| 301 | sqlite3_prepare_v2(m_db, "DELETE FROM Files WHERE filename = ?;", -1, &stmt, 0); |
| 302 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 303 | sqlite3_step(stmt); |
| 304 | sqlite3_finalize(stmt); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | FsWatcher::getFilesInDir(const filesystem::path &dir, vector<string> &files) |
| 309 | { |
| 310 | sqlite3_stmt *stmt; |
| 311 | sqlite3_prepare_v2(m_db, "SELECT * FROM Files WHERE filename LIKE ?;", -1, &stmt, 0); |
| 312 | |
| 313 | string dirStr = dir.string(); |
| 314 | ostringstream escapedDir; |
| 315 | for (string::const_iterator ch = dirStr.begin (); ch != dirStr.end (); ch ++) |
| 316 | { |
| 317 | if (*ch == '%') |
| 318 | escapedDir << "\\%"; |
| 319 | else |
| 320 | escapedDir << *ch; |
| 321 | } |
| 322 | escapedDir << "/" << "%"; |
| 323 | string escapedDirStr = escapedDir.str (); |
| 324 | sqlite3_bind_text (stmt, 1, escapedDirStr.c_str (), escapedDirStr.size (), SQLITE_STATIC); |
| 325 | |
| 326 | while(sqlite3_step(stmt) == SQLITE_ROW) |
| 327 | { |
| 328 | string filename(reinterpret_cast<const char *>(sqlite3_column_text (stmt, 0)), sqlite3_column_bytes(stmt, 0)); |
| 329 | files.push_back(filename); |
| 330 | } |
| 331 | |
| 332 | sqlite3_finalize (stmt); |
| 333 | |
| 334 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 335 | |
| 336 | #if WAF |
| 337 | #include "fs-watcher.moc" |
| 338 | #include "fs-watcher.cc.moc" |
| 339 | #endif |