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 | #ifndef FILE_STATE_H |
| 23 | #define FILE_STATE_H |
| 24 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 25 | #include "db-helper.h" |
| 26 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 27 | #include "ndnx-name.h" |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 28 | #include "file-item.pb.h" |
| 29 | #include "hash-helper.h" |
| 30 | |
| 31 | #include <boost/tuple/tuple.hpp> |
| 32 | #include <boost/exception/all.hpp> |
| 33 | |
| 34 | #include <list> |
| 35 | |
| 36 | typedef std::list<FileItem> FileItems; |
| 37 | typedef boost::shared_ptr<FileItem> FileItemPtr; |
| 38 | typedef boost::shared_ptr<FileItems> FileItemsPtr; |
| 39 | |
| 40 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 41 | class FileState : public DbHelper |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
Yingdi Yu | adb54eb | 2013-08-15 10:28:28 -0700 | [diff] [blame] | 44 | FileState (const boost::filesystem::path &path, bool cow = false); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 45 | ~FileState (); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 47 | /** |
| 48 | * @brief Update or add a file |
| 49 | */ |
| 50 | void |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 51 | UpdateFile (const std::string &filename, sqlite3_int64 version, |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame^] | 52 | const Hash &hash, const Ndnx::NdnxCharbuf &device_name, sqlite3_int64 seqno, |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 53 | 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] | 54 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 55 | /** |
| 56 | * @brief Delete file |
| 57 | */ |
| 58 | void |
| 59 | DeleteFile (const std::string &filename); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 61 | /** |
| 62 | * @brief Set "complete" flag |
| 63 | * |
| 64 | * The call will do nothing if FileState does not have a record for the file (e.g., file got subsequently deleted) |
| 65 | */ |
| 66 | void |
| 67 | SetFileComplete (const std::string &filename); |
| 68 | |
| 69 | /** |
| 70 | * @brief Lookup file state using file name |
| 71 | */ |
| 72 | FileItemPtr |
| 73 | LookupFile (const std::string &filename) ; |
| 74 | |
| 75 | /** |
| 76 | * @brief Lookup file state using content hash (multiple items may be returned) |
| 77 | */ |
| 78 | FileItemsPtr |
| 79 | LookupFilesForHash (const Hash &hash); |
| 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 |
| 85 | 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] | 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 | 95f9f55 | 2013-02-26 23:05:20 -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 | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 97 | LookupFilesInFolderRecursively (const boost::function<void (const FileItem&)> &visitor, const std::string &folder, int offset=0, int limit=-1); |
| 98 | |
| 99 | /** |
| 100 | * @brief Recursively lookup all files in the specified folder (wrapper around the overloaded version) |
| 101 | */ |
| 102 | FileItemsPtr |
| 103 | LookupFilesInFolderRecursively (const std::string &folder, int offset=0, int limit=-1); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 106 | typedef boost::shared_ptr<FileState> FileStatePtr; |
| 107 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 108 | namespace Error { |
| 109 | struct FileState : virtual boost::exception, virtual std::exception { }; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | #endif // ACTION_LOG_H |