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