blob: 1eff885a9b274a790fcd62891b45c3c50ec4c43e [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyev0a30a0c2013-01-29 17:25:42 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyev0a30a0c2013-01-29 17:25:42 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyev0a30a0c2013-01-29 17:25:42 -080019 */
20
21#ifndef FILE_STATE_H
22#define FILE_STATE_H
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "db-helper.hpp"
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080025
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "ccnx-name.hpp"
27#include "file-item.pb.hpp"
28#include "hash-helper.hpp"
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080029
30#include <boost/tuple/tuple.hpp>
31#include <boost/exception/all.hpp>
32
33#include <list>
34
35typedef std::list<FileItem> FileItems;
36typedef boost::shared_ptr<FileItem> FileItemPtr;
37typedef boost::shared_ptr<FileItems> FileItemsPtr;
38
39
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080040class FileState : public DbHelper
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080041{
42public:
Yingdi Yuadb54eb2013-08-15 10:28:28 -070043 FileState (const boost::filesystem::path &path, bool cow = false);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080044 ~FileState ();
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080045
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080046 /**
47 * @brief Update or add a file
48 */
49 void
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -080050 UpdateFile (const std::string &filename, sqlite3_int64 version,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070051 const Hash &hash, const Ndnx::NdnxCharbuf &device_name, sqlite3_int64 seqno,
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080052 time_t atime, time_t mtime, time_t ctime, int mode, int seg_num);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080053
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080054 /**
55 * @brief Delete file
56 */
57 void
58 DeleteFile (const std::string &filename);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080059
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080060 /**
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 Afanasyev95f9f552013-02-26 23:05:20 -080081 * @brief Lookup all files in the specified folder and call visitor(file) for each file
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080082 */
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080083 void
84 LookupFilesInFolder (const boost::function<void (const FileItem&)> &visitor, const std::string &folder, int offset=0, int limit=-1);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080085
86 /**
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080087 * @brief Lookup all files in the specified folder (wrapper around the overloaded version)
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080088 */
89 FileItemsPtr
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080090 LookupFilesInFolder (const std::string &folder, int offset=0, int limit=-1);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080091
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080092 /**
93 * @brief Recursively lookup all files in the specified folder and call visitor(file) for each file
94 */
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -080095 bool
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080096 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 Afanasyev0a30a0c2013-01-29 17:25:42 -0800103};
104
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800105typedef boost::shared_ptr<FileState> FileStatePtr;
106
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800107namespace Error {
108struct FileState : virtual boost::exception, virtual std::exception { };
109}
110
111
112#endif // ACTION_LOG_H