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