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 | a9369b4 | 2017-01-11 11:58:00 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -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 | 0a30a0c | 2013-01-29 17:25:42 -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 | 0a30a0c | 2013-01-29 17:25:42 -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 | 0a30a0c | 2013-01-29 17:25:42 -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 | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 21 | #include "file-state.hpp" |
| 22 | #include "core/logging.hpp" |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 24 | _LOG_INIT(FileState); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 25 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 26 | namespace ndn { |
| 27 | namespace chronoshare { |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 28 | |
| 29 | const std::string INIT_DATABASE = "\ |
| 30 | \n\ |
| 31 | CREATE TABLE FileState ( \n\ |
| 32 | type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\ |
| 33 | filename TEXT NOT NULL, \n\ |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 34 | version INTEGER, \n\ |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 35 | directory TEXT, \n\ |
| 36 | device_name BLOB NOT NULL, \n\ |
| 37 | seq_no INTEGER NOT NULL, \n\ |
| 38 | file_hash BLOB NOT NULL, \n\ |
| 39 | file_atime TIMESTAMP, \n\ |
| 40 | file_mtime TIMESTAMP, \n\ |
| 41 | file_ctime TIMESTAMP, \n\ |
| 42 | file_chmod INTEGER, \n\ |
| 43 | file_seg_num INTEGER, \n\ |
| 44 | is_complete INTEGER, \n\ |
| 45 | \n\ |
| 46 | PRIMARY KEY (type, filename) \n\ |
| 47 | ); \n\ |
| 48 | \n\ |
| 49 | CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\ |
| 50 | CREATE INDEX FileState_type_file_hash ON FileState (type, file_hash); \n\ |
| 51 | "; |
| 52 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 53 | FileState::FileState(const boost::filesystem::path& path) |
| 54 | : DbHelper(path / ".chronoshare", "file-state.db") |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 55 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, NULL); |
| 57 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 60 | FileState::~FileState() |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 61 | { |
| 62 | } |
| 63 | |
| 64 | void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 65 | FileState::UpdateFile(const std::string& filename, sqlite3_int64 version, const Buffer& hash, |
| 66 | const Buffer& device_name, sqlite3_int64 seq_no, time_t atime, time_t mtime, |
| 67 | time_t ctime, int mode, int seg_num) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 68 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | sqlite3_stmt* stmt; |
| 70 | sqlite3_prepare_v2(m_db, "UPDATE FileState " |
| 71 | "SET " |
| 72 | "device_name=?, seq_no=?, " |
| 73 | "version=?," |
| 74 | "file_hash=?," |
| 75 | "file_atime=datetime(?, 'unixepoch')," |
| 76 | "file_mtime=datetime(?, 'unixepoch')," |
| 77 | "file_ctime=datetime(?, 'unixepoch')," |
| 78 | "file_chmod=?, " |
| 79 | "file_seg_num=? " |
| 80 | "WHERE type=0 AND filename=?", |
| 81 | -1, &stmt, 0); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 82 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 83 | sqlite3_bind_blob(stmt, 1, device_name.buf(), device_name.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 84 | sqlite3_bind_int64(stmt, 2, seq_no); |
| 85 | sqlite3_bind_int64(stmt, 3, version); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 86 | sqlite3_bind_blob(stmt, 4, hash.buf(), hash.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | sqlite3_bind_int64(stmt, 5, atime); |
| 88 | sqlite3_bind_int64(stmt, 6, mtime); |
| 89 | sqlite3_bind_int64(stmt, 7, ctime); |
| 90 | sqlite3_bind_int(stmt, 8, mode); |
| 91 | sqlite3_bind_int(stmt, 9, seg_num); |
| 92 | sqlite3_bind_text(stmt, 10, filename.c_str(), -1, SQLITE_STATIC); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 94 | sqlite3_step(stmt); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 96 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_ROW && sqlite3_errcode(m_db) != SQLITE_DONE, |
| 97 | sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 99 | sqlite3_finalize(stmt); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | int affected_rows = sqlite3_changes(m_db); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 102 | if (affected_rows == 0) // file didn't exist |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | { |
| 104 | sqlite3_stmt* stmt; |
| 105 | sqlite3_prepare_v2(m_db, |
| 106 | "INSERT INTO FileState " |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 107 | "(type,filename,version,device_name,seq_no,file_hash," |
| 108 | "file_atime,file_mtime,file_ctime,file_chmod,file_seg_num) " |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 109 | "VALUES (0, ?, ?, ?, ?, ?, " |
| 110 | "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?)", |
| 111 | -1, &stmt, 0); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 113 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 115 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 116 | sqlite3_bind_int64(stmt, 2, version); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 117 | sqlite3_bind_blob(stmt, 3, device_name.buf(), device_name.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | sqlite3_bind_int64(stmt, 4, seq_no); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 119 | sqlite3_bind_blob(stmt, 5, hash.buf(), hash.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | sqlite3_bind_int64(stmt, 6, atime); |
| 121 | sqlite3_bind_int64(stmt, 7, mtime); |
| 122 | sqlite3_bind_int64(stmt, 8, ctime); |
| 123 | sqlite3_bind_int(stmt, 9, mode); |
| 124 | sqlite3_bind_int(stmt, 10, seg_num); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 126 | sqlite3_step(stmt); |
| 127 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
| 128 | sqlite3_finalize(stmt); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 130 | sqlite3_prepare_v2(m_db, |
| 131 | "UPDATE FileState SET directory=directory_name(filename) WHERE filename=?", |
| 132 | -1, &stmt, 0); |
| 133 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 135 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 136 | sqlite3_step(stmt); |
| 137 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
| 138 | sqlite3_finalize(stmt); |
| 139 | } |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 142 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 143 | FileState::DeleteFile(const std::string& filename) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 144 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 145 | sqlite3_stmt* stmt; |
| 146 | sqlite3_prepare_v2(m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 147 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 148 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 149 | _LOG_DEBUG("Delete " << filename); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 151 | sqlite3_step(stmt); |
| 152 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
| 153 | sqlite3_finalize(stmt); |
| 154 | } |
| 155 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 156 | void |
| 157 | FileState::SetFileComplete(const std::string& filename) |
| 158 | { |
| 159 | sqlite3_stmt* stmt; |
| 160 | sqlite3_prepare_v2(m_db, "UPDATE FileState SET is_complete=1 WHERE type = 0 AND filename = ?", -1, |
| 161 | &stmt, 0); |
| 162 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
| 163 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 164 | |
| 165 | sqlite3_step(stmt); |
| 166 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
| 167 | |
| 168 | sqlite3_finalize(stmt); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 171 | /** |
| 172 | * @todo Implement checking modification time and permissions |
| 173 | */ |
| 174 | FileItemPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 175 | FileState::LookupFile(const std::string& filename) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 177 | sqlite3_stmt* stmt; |
| 178 | sqlite3_prepare_v2(m_db, |
| 179 | "SELECT filename,version,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete " |
| 180 | " FROM FileState " |
| 181 | " WHERE type = 0 AND filename = ?", |
| 182 | -1, &stmt, 0); |
| 183 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 184 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 185 | |
| 186 | FileItemPtr retval; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 187 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 188 | retval = make_shared<FileItem>(); |
| 189 | retval->set_filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 190 | sqlite3_column_bytes(stmt, 0)); |
| 191 | retval->set_version(sqlite3_column_int64(stmt, 1)); |
| 192 | retval->set_device_name(sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2)); |
| 193 | retval->set_seq_no(sqlite3_column_int64(stmt, 3)); |
| 194 | retval->set_file_hash(sqlite3_column_blob(stmt, 4), sqlite3_column_bytes(stmt, 4)); |
| 195 | retval->set_mtime(sqlite3_column_int(stmt, 5)); |
| 196 | retval->set_mode(sqlite3_column_int(stmt, 6)); |
| 197 | retval->set_seg_num(sqlite3_column_int64(stmt, 7)); |
| 198 | retval->set_is_complete(sqlite3_column_int(stmt, 8)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 199 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 200 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
| 201 | sqlite3_finalize(stmt); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 202 | |
| 203 | return retval; |
| 204 | } |
| 205 | |
| 206 | FileItemsPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 207 | FileState::LookupFilesForHash(const Buffer& hash) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 208 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 209 | sqlite3_stmt* stmt; |
| 210 | sqlite3_prepare_v2(m_db, |
| 211 | "SELECT filename,version,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete " |
| 212 | " FROM FileState " |
| 213 | " WHERE type = 0 AND file_hash = ?", |
| 214 | -1, &stmt, 0); |
| 215 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 216 | sqlite3_bind_blob(stmt, 1, hash.buf(), hash.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 217 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 218 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 219 | FileItemsPtr retval = make_shared<FileItems>(); |
| 220 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 221 | FileItem file; |
| 222 | file.set_filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 223 | sqlite3_column_bytes(stmt, 0)); |
| 224 | file.set_version(sqlite3_column_int64(stmt, 1)); |
| 225 | file.set_device_name(sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2)); |
| 226 | file.set_seq_no(sqlite3_column_int64(stmt, 3)); |
| 227 | file.set_file_hash(sqlite3_column_blob(stmt, 4), sqlite3_column_bytes(stmt, 4)); |
| 228 | file.set_mtime(sqlite3_column_int(stmt, 5)); |
| 229 | file.set_mode(sqlite3_column_int(stmt, 6)); |
| 230 | file.set_seg_num(sqlite3_column_int64(stmt, 7)); |
| 231 | file.set_is_complete(sqlite3_column_int(stmt, 8)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 232 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 233 | retval->push_back(file); |
| 234 | } |
| 235 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 236 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 237 | sqlite3_finalize(stmt); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 238 | |
| 239 | return retval; |
| 240 | } |
| 241 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 242 | void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 243 | FileState::LookupFilesInFolder(const function<void(const FileItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 244 | const std::string& folder, int offset /*=0*/, int limit /*=-1*/) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 245 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 246 | sqlite3_stmt* stmt; |
| 247 | sqlite3_prepare_v2(m_db, |
| 248 | "SELECT filename,version,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete " |
| 249 | " FROM FileState " |
| 250 | " WHERE type = 0 AND directory = ?" |
| 251 | " LIMIT ? OFFSET ?", |
| 252 | -1, &stmt, 0); |
| 253 | if (folder.size() == 0) |
| 254 | sqlite3_bind_null(stmt, 1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 255 | else |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 256 | sqlite3_bind_text(stmt, 1, folder.c_str(), folder.size(), SQLITE_STATIC); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 257 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 258 | sqlite3_bind_int(stmt, 2, limit); |
| 259 | sqlite3_bind_int(stmt, 3, offset); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 260 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 261 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 262 | FileItem file; |
| 263 | file.set_filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 264 | sqlite3_column_bytes(stmt, 0)); |
| 265 | file.set_version(sqlite3_column_int64(stmt, 1)); |
| 266 | file.set_device_name(sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2)); |
| 267 | file.set_seq_no(sqlite3_column_int64(stmt, 3)); |
| 268 | file.set_file_hash(sqlite3_column_blob(stmt, 4), sqlite3_column_bytes(stmt, 4)); |
| 269 | file.set_mtime(sqlite3_column_int(stmt, 5)); |
| 270 | file.set_mode(sqlite3_column_int(stmt, 6)); |
| 271 | file.set_seg_num(sqlite3_column_int64(stmt, 7)); |
| 272 | file.set_is_complete(sqlite3_column_int(stmt, 8)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 273 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 274 | visitor(file); |
| 275 | } |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 276 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 277 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 278 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 279 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 283 | FileState::LookupFilesInFolder(const std::string& folder, int offset /*=0*/, int limit /*=-1*/) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 284 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 285 | FileItemsPtr retval = make_shared<FileItems>(); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 286 | LookupFilesInFolder(bind(static_cast<void (FileItems::*)(const FileItem&)>(&FileItems::push_back), |
| 287 | retval.get(), _1), |
| 288 | folder, offset, limit); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 289 | |
| 290 | return retval; |
| 291 | } |
| 292 | |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 293 | bool |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 294 | FileState::LookupFilesInFolderRecursively(const function<void(const FileItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 295 | const std::string& folder, int offset /*=0*/, |
| 296 | int limit /*=-1*/) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 297 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 298 | _LOG_DEBUG("LookupFilesInFolderRecursively: [" << folder << "]"); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 299 | |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 300 | if (limit >= 0) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 301 | limit++; |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 302 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 303 | sqlite3_stmt* stmt; |
| 304 | if (folder != "") { |
| 305 | /// @todo Do something to improve efficiency of this query. Right now it is basically scanning the whole database |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 306 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 307 | sqlite3_prepare_v2(m_db, |
| 308 | "SELECT filename,version,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete " |
| 309 | " FROM FileState " |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 310 | " WHERE type = 0 AND is_dir_prefix(?, directory)=1 " |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 311 | " ORDER BY filename " |
| 312 | " LIMIT ? OFFSET ?", |
| 313 | -1, &stmt, |
| 314 | 0); // there is a small ambiguity with is_prefix matching, but should be ok for now |
| 315 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 316 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 317 | sqlite3_bind_text(stmt, 1, folder.c_str(), folder.size(), SQLITE_STATIC); |
| 318 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 319 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 320 | sqlite3_bind_int(stmt, 2, limit); |
| 321 | sqlite3_bind_int(stmt, 3, offset); |
| 322 | } |
| 323 | else { |
| 324 | sqlite3_prepare_v2(m_db, |
| 325 | "SELECT filename,version,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete " |
| 326 | " FROM FileState " |
| 327 | " WHERE type = 0" |
| 328 | " ORDER BY filename " |
| 329 | " LIMIT ? OFFSET ?", |
| 330 | -1, &stmt, 0); |
| 331 | sqlite3_bind_int(stmt, 1, limit); |
| 332 | sqlite3_bind_int(stmt, 2, offset); |
| 333 | } |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 334 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 335 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 336 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 337 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 338 | if (limit == 1) |
| 339 | break; |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 340 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 341 | FileItem file; |
| 342 | file.set_filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 343 | sqlite3_column_bytes(stmt, 0)); |
| 344 | file.set_version(sqlite3_column_int64(stmt, 1)); |
| 345 | file.set_device_name(sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2)); |
| 346 | file.set_seq_no(sqlite3_column_int64(stmt, 3)); |
| 347 | file.set_file_hash(sqlite3_column_blob(stmt, 4), sqlite3_column_bytes(stmt, 4)); |
| 348 | file.set_mtime(sqlite3_column_int(stmt, 5)); |
| 349 | file.set_mode(sqlite3_column_int(stmt, 6)); |
| 350 | file.set_seg_num(sqlite3_column_int64(stmt, 7)); |
| 351 | file.set_is_complete(sqlite3_column_int(stmt, 8)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 352 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 353 | visitor(file); |
| 354 | limit--; |
| 355 | } |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 356 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 357 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 358 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 359 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 360 | |
| 361 | return (limit == 1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 364 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 365 | FileState::LookupFilesInFolderRecursively(const std::string& folder, int offset /*=0*/, |
| 366 | int limit /*=-1*/) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 367 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 368 | FileItemsPtr retval = make_shared<FileItems>(); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 369 | LookupFilesInFolder(bind(static_cast<void (FileItems::*)(const FileItem&)>(&FileItems::push_back), |
| 370 | retval.get(), _1), |
| 371 | folder, offset, limit); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 372 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 373 | return retval; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 374 | } |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 375 | |
| 376 | } // namespace chronoshare |
| 377 | } // namespace ndn |