blob: d9ea339ab3e4f0cb8a7a041d4374567550d6c3cf [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wang51837ad2016-12-25 14:43:53 -08003 * Copyright (c) 2013-2017, 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"
Lijing Wang51837ad2016-12-25 14:43:53 -080025#include "core/chronoshare-common.hpp"
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080026
Lijing Wang51837ad2016-12-25 14:43:53 -080027#include "file-item.pb.h"
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080028
Lijing Wang51837ad2016-12-25 14:43:53 -080029#include <ndn-cxx/util/digest.hpp>
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080030
31#include <list>
32
Lijing Wang51837ad2016-12-25 14:43:53 -080033namespace ndn {
34namespace chronoshare {
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080035
Lijing Wang51837ad2016-12-25 14:43:53 -080036typedef std::list<FileItem> FileItems;
37typedef shared_ptr<FileItem> FileItemPtr;
38typedef shared_ptr<FileItems> FileItemsPtr;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080039
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
Lijing Wang51837ad2016-12-25 14:43:53 -080050 UpdateFile(const std::string& filename, sqlite3_int64 version, const Buffer& hash,
51 const Buffer& device_name, sqlite3_int64 seqno, time_t atime, time_t mtime,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 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 *
Lijing Wang51837ad2016-12-25 14:43:53 -080063 * The call will do nothing if FileState does not have a record for the file(e.g., file got
64 * subsequently deleted)
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080065 */
66 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 SetFileComplete(const std::string& filename);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080068
69 /**
70 * @brief Lookup file state using file name
71 */
72 FileItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 LookupFile(const std::string& filename);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080074
75 /**
Lijing Wang51837ad2016-12-25 14:43:53 -080076 * @brief Lookup file state using content hash(multiple items may be returned)
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080077 */
78 FileItemsPtr
Lijing Wang51837ad2016-12-25 14:43:53 -080079 LookupFilesForHash(const Buffer& hash);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080080
81 /**
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080082 * @brief Lookup all files in the specified folder and call visitor(file) for each file
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080083 */
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080084 void
Lijing Wang51837ad2016-12-25 14:43:53 -080085 LookupFilesInFolder(const function<void(const FileItem&)>& visitor, const std::string& folder,
86 int offset = 0, int limit = -1);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080087
88 /**
Lijing Wang51837ad2016-12-25 14:43:53 -080089 * @brief Lookup all files in the specified folder(wrapper around the overloaded version)
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080090 */
91 FileItemsPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 LookupFilesInFolder(const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080093
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080094 /**
Lijing Wang51837ad2016-12-25 14:43:53 -080095 * @brief Recursively lookup all files in the specified folder and call visitor(file) for each
96 * file
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080097 */
Alexander Afanasyev6fb5dc62013-02-27 11:34:52 -080098 bool
Lijing Wang51837ad2016-12-25 14:43:53 -080099 LookupFilesInFolderRecursively(const function<void(const FileItem&)>& visitor,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800101
102 /**
Lijing Wang51837ad2016-12-25 14:43:53 -0800103 * @brief Recursively lookup all files in the specified folder(wrapper around the overloaded
104 * version)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800105 */
106 FileItemsPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 LookupFilesInFolderRecursively(const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800108};
109
Lijing Wang51837ad2016-12-25 14:43:53 -0800110typedef shared_ptr<FileState> FileStatePtr;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800111
Lijing Wang51837ad2016-12-25 14:43:53 -0800112namespace error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800113struct FileState : virtual boost::exception, virtual std::exception
114{
115};
Lijing Wang51837ad2016-12-25 14:43:53 -0800116} // namespace error
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800117
Lijing Wang51837ad2016-12-25 14:43:53 -0800118} // namespace chronoshare
119} // namespace ndn
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800120
121#endif // ACTION_LOG_H