blob: e07479425b3b4c76ea9f6adf7926578cce46ee84 [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
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080030#include <boost/exception/all.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include <boost/tuple/tuple.hpp>
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080032
33#include <list>
34
35typedef std::list<FileItem> FileItems;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036typedef boost::shared_ptr<FileItem> FileItemPtr;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080037typedef 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:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 FileState(const boost::filesystem::path& path);
44 ~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 Afanasyeveda3b7a2016-12-25 11:26:40 -080050 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 Afanasyev0a30a0c2013-01-29 17:25:42 -080053
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080054 /**
55 * @brief Delete file
56 */
57 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 SetFileComplete(const std::string& filename);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080067
68 /**
69 * @brief Lookup file state using file name
70 */
71 FileItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 LookupFile(const std::string& filename);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080073
74 /**
75 * @brief Lookup file state using content hash (multiple items may be returned)
76 */
77 FileItemsPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 LookupFilesForHash(const Hash& hash);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080079
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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 LookupFilesInFolder(const boost::function<void(const FileItem&)>& visitor,
85 const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080086
87 /**
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080088 * @brief Lookup all files in the specified folder (wrapper around the overloaded version)
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080089 */
90 FileItemsPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 LookupFilesInFolder(const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080092
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080093 /**
94 * @brief Recursively lookup all files in the specified folder and call visitor(file) for each file
95 */
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -080096 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 LookupFilesInFolderRecursively(const boost::function<void(const FileItem&)>& visitor,
98 const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080099
100 /**
101 * @brief Recursively lookup all files in the specified folder (wrapper around the overloaded version)
102 */
103 FileItemsPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 LookupFilesInFolderRecursively(const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800105};
106
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800107typedef boost::shared_ptr<FileState> FileStatePtr;
108
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800109namespace Error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110struct FileState : virtual boost::exception, virtual std::exception
111{
112};
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800113}
114
115
116#endif // ACTION_LOG_H