-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncedFileServer.cpp
More file actions
148 lines (116 loc) · 3.86 KB
/
SyncedFileServer.cpp
File metadata and controls
148 lines (116 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by stefano on 06/08/20.
//
#include <openssl/sha.h>
#include <fstream>
#include <iomanip>
#include <utility>
#include "SyncedFileServer.h"
#include <boost/property_tree/json_parser.hpp>
#include <filesystem>
#include <iostream>
namespace pt = boost::property_tree;
static const int K_READ_BUF_SIZE{ 1024 * 16 };
SyncedFileServer::SyncedFileServer(const std::string& JSON){
// std::cout << JSON << std::endl;
std::stringstream ss(JSON);
boost::property_tree::ptree root;
boost::property_tree::read_json(ss, root);
this->hash = root.get_child("hash").data();
this->path = root.get_child("path").data();
// If no conversion could be performed, an invalid_argument exception is thrown.
this->file_size = std::stoul(root.get_child("file_size").data());
// If no conversion could be performed, an invalid_argument exception is thrown.
this->fileStatus = static_cast<FileStatus>(std::stoi(root.get_child("file_status").data()));
this->is_file = root.get_child("is_file").data()=="true";
}
void SyncedFileServer::update_file_data() {
std::string new_hash = CalcSha256(path);
if(new_hash!= this->hash){
this->is_file = std::filesystem::is_regular_file(this->path);
this->fileStatus = FileStatus::modified;
this->hash = new_hash;
this->file_size = is_file ? std::filesystem::file_size(this->path) : 0;
}
}
std::string SyncedFileServer::CalcSha256(const std::string& filename){
// Initialize openssl
SHA256_CTX context;
if(!SHA256_Init(&context))
return "";
// Read file and update calculated SHA
char buf[K_READ_BUF_SIZE];
std::ifstream file(filename, std::ifstream::binary);
while (file.good()){
file.read(buf, sizeof(buf));
if(!SHA256_Update(&context, buf, file.gcount()))
return "";
}
// Get Final SHA
unsigned char result[SHA256_DIGEST_LENGTH];
if(!SHA256_Final(result, &context))
return "";
// Transform byte-array to string
std::stringstream sha_str;
sha_str << std::hex << std::setfill('0');
for (const auto &byte: result)
sha_str << std::setw(2) << (int)byte;
return sha_str.str();
}
bool SyncedFileServer::operator==(const SyncedFileServer &rhs) const {
return path == rhs.path &&
hash == rhs.hash;
}
bool SyncedFileServer::operator!=(const SyncedFileServer &rhs) const {
return !(rhs == *this);
}
//SyncedFileServer::SyncedFileServer(SyncedFileServer const &syncedFile) {
// this->fileStatus = syncedFile.fileStatus;
// this->hash = syncedFile.hash;
// this->path = syncedFile.path;
// this->is_file = syncedFile.is_file;
//}
//SyncedFileServer::SyncedFileServer() {
// fileStatus = FileStatus::not_valid;
// is_file = false;
//}
const std::string &SyncedFileServer::getPath() const {
return path;
}
const std::string &SyncedFileServer::getHash() const {
return hash;
}
std::string SyncedFileServer::to_string() {
return this->path + " (" + this->hash + ")";
}
FileStatus SyncedFileServer::getFileStatus() const {
return fileStatus;
}
bool SyncedFileServer::isSyncing() const {
return is_syncing;
}
void SyncedFileServer::setSyncing(bool syncing) {
SyncedFileServer::is_syncing = syncing;
}
pt::ptree SyncedFileServer::getPtree(){
pt::ptree root;
root.put("path", this->path);
root.put("hash", this->hash);
root.put("file_size", this->file_size);
root.put("file_status", static_cast<int>(this->fileStatus));
root.put("is_file", this->is_file);
return root;
}
std::string SyncedFileServer::getJSON() {
pt::ptree root = this->getPtree();
std::stringstream ss;
pt::json_parser::write_json(ss, root);
// std::cout << ss.str() << std::endl;
return ss.str();
}
bool SyncedFileServer::isFile() const {
return is_file;
}
unsigned long SyncedFileServer::getFileSize() const {
return file_size;
}