Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -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 | |
| 21 | #ifndef FILE_STATE_H |
| 22 | #define FILE_STATE_H |
| 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "db-helper.hpp" |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 25 | #include "core/chronoshare-common.hpp" |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 26 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 27 | #include "file-item.pb.h" |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 28 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 29 | #include <ndn-cxx/util/digest.hpp> |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 30 | |
| 31 | #include <list> |
| 32 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace chronoshare { |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 35 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 36 | typedef std::list<FileItem> FileItems; |
| 37 | typedef shared_ptr<FileItem> FileItemPtr; |
| 38 | typedef shared_ptr<FileItems> FileItemsPtr; |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 40 | class FileState : public DbHelper |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 41 | { |
| 42 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 43 | FileState(const boost::filesystem::path& path); |
| 44 | ~FileState(); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 46 | /** |
| 47 | * @brief Update or add a file |
| 48 | */ |
| 49 | void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 50 | UpdateFile(const std::string& filename, sqlite3_int64 version, const Buffer& hash, |
| 51 | const Buffer& device_name, sqlite3_int64 seqno, time_t atime, time_t mtime, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 52 | time_t ctime, int mode, int seg_num); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 54 | /** |
| 55 | * @brief Delete file |
| 56 | */ |
| 57 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 58 | DeleteFile(const std::string& filename); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 60 | /** |
| 61 | * @brief Set "complete" flag |
| 62 | * |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 63 | * The call will do nothing if FileState does not have a record for the file(e.g., file got |
| 64 | * subsequently deleted) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 65 | */ |
| 66 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 67 | SetFileComplete(const std::string& filename); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * @brief Lookup file state using file name |
| 71 | */ |
| 72 | FileItemPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | LookupFile(const std::string& filename); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 76 | * @brief Lookup file state using content hash(multiple items may be returned) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 77 | */ |
| 78 | FileItemsPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 79 | LookupFilesForHash(const Buffer& hash); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 82 | * @brief Lookup all files in the specified folder and call visitor(file) for each file |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 83 | */ |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 84 | void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 85 | LookupFilesInFolder(const function<void(const FileItem&)>& visitor, const std::string& folder, |
| 86 | int offset = 0, int limit = -1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 89 | * @brief Lookup all files in the specified folder(wrapper around the overloaded version) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 90 | */ |
| 91 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 92 | LookupFilesInFolder(const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 94 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 95 | * @brief Recursively lookup all files in the specified folder and call visitor(file) for each |
| 96 | * file |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 97 | */ |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 98 | bool |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 99 | LookupFilesInFolderRecursively(const function<void(const FileItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 100 | const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 103 | * @brief Recursively lookup all files in the specified folder(wrapper around the overloaded |
| 104 | * version) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 105 | */ |
| 106 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 107 | LookupFilesInFolderRecursively(const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 110 | typedef shared_ptr<FileState> FileStatePtr; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 111 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 112 | namespace error { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 113 | struct FileState : virtual boost::exception, virtual std::exception |
| 114 | { |
| 115 | }; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 116 | } // namespace error |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 117 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 118 | } // namespace chronoshare |
| 119 | } // namespace ndn |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 120 | |
| 121 | #endif // ACTION_LOG_H |