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 | |
| 30 | #include <boost/tuple/tuple.hpp> |
| 31 | #include <boost/exception/all.hpp> |
| 32 | |
| 33 | #include <list> |
| 34 | |
| 35 | typedef std::list<FileItem> FileItems; |
| 36 | typedef boost::shared_ptr<FileItem> FileItemPtr; |
| 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: |
Yingdi Yu | adb54eb | 2013-08-15 10:28:28 -0700 | [diff] [blame] | 43 | FileState (const boost::filesystem::path &path, bool cow = false); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 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 | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 50 | UpdateFile (const std::string &filename, sqlite3_int64 version, |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 51 | const Hash &hash, const Ndnx::NdnxCharbuf &device_name, sqlite3_int64 seqno, |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 52 | time_t atime, time_t mtime, 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 |
| 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 |
| 66 | SetFileComplete (const std::string &filename); |
| 67 | |
| 68 | /** |
| 69 | * @brief Lookup file state using file name |
| 70 | */ |
| 71 | FileItemPtr |
| 72 | LookupFile (const std::string &filename) ; |
| 73 | |
| 74 | /** |
| 75 | * @brief Lookup file state using content hash (multiple items may be returned) |
| 76 | */ |
| 77 | FileItemsPtr |
| 78 | LookupFilesForHash (const Hash &hash); |
| 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 |
| 84 | 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] | 85 | |
| 86 | /** |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 87 | * @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] | 88 | */ |
| 89 | FileItemsPtr |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 90 | LookupFilesInFolder (const std::string &folder, int offset=0, int limit=-1); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 92 | /** |
| 93 | * @brief Recursively lookup all files in the specified folder and call visitor(file) for each file |
| 94 | */ |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 95 | bool |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 96 | LookupFilesInFolderRecursively (const boost::function<void (const FileItem&)> &visitor, const std::string &folder, int offset=0, int limit=-1); |
| 97 | |
| 98 | /** |
| 99 | * @brief Recursively lookup all files in the specified folder (wrapper around the overloaded version) |
| 100 | */ |
| 101 | FileItemsPtr |
| 102 | LookupFilesInFolderRecursively (const std::string &folder, int offset=0, int limit=-1); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 105 | typedef boost::shared_ptr<FileState> FileStatePtr; |
| 106 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 107 | namespace Error { |
| 108 | struct FileState : virtual boost::exception, virtual std::exception { }; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | #endif // ACTION_LOG_H |