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 | 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" |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "ccnx-name.hpp" |
| 27 | #include "file-item.pb.hpp" |
| 28 | #include "hash-helper.hpp" |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 30 | #include <boost/exception/all.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 31 | #include <boost/tuple/tuple.hpp> |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 32 | |
| 33 | #include <list> |
| 34 | |
| 35 | typedef std::list<FileItem> FileItems; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 36 | typedef boost::shared_ptr<FileItem> FileItemPtr; |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 37 | typedef boost::shared_ptr<FileItems> FileItemsPtr; |
| 38 | |
| 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 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 50 | UpdateFile(const std::string& filename, sqlite3_int64 version, const Hash& hash, |
| 51 | const Ccnx::CcnxCharbuf& device_name, sqlite3_int64 seqno, time_t atime, time_t mtime, |
| 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 | * |
| 63 | * The call will do nothing if FileState does not have a record for the file (e.g., file got subsequently deleted) |
| 64 | */ |
| 65 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 66 | SetFileComplete(const std::string& filename); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * @brief Lookup file state using file name |
| 70 | */ |
| 71 | FileItemPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 72 | LookupFile(const std::string& filename); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * @brief Lookup file state using content hash (multiple items may be returned) |
| 76 | */ |
| 77 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 78 | LookupFilesForHash(const Hash& hash); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 81 | * @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] | 82 | */ |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 83 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 84 | LookupFilesInFolder(const boost::function<void(const FileItem&)>& visitor, |
| 85 | const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 88 | * @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] | 89 | */ |
| 90 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 91 | LookupFilesInFolder(const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 93 | /** |
| 94 | * @brief Recursively lookup all files in the specified folder and call visitor(file) for each file |
| 95 | */ |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 96 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 97 | LookupFilesInFolderRecursively(const boost::function<void(const FileItem&)>& visitor, |
| 98 | const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * @brief Recursively lookup all files in the specified folder (wrapper around the overloaded version) |
| 102 | */ |
| 103 | FileItemsPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 104 | LookupFilesInFolderRecursively(const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 107 | typedef boost::shared_ptr<FileState> FileStatePtr; |
| 108 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 109 | namespace Error { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 110 | struct FileState : virtual boost::exception, virtual std::exception |
| 111 | { |
| 112 | }; |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | #endif // ACTION_LOG_H |