-
Notifications
You must be signed in to change notification settings - Fork 4
/
ghost_file.cc
67 lines (52 loc) · 1.58 KB
/
ghost_file.cc
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
/*
Ghost File System, or simply GhostFS
Copyright (C) 2016 Raphael S. Carvalho
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include "ghost_file.h"
#include "utils.h"
ghost_file::ghost_file(const char *data)
: _data(data)
, _length(strlen(data)) {}
ghost_file::ghost_file()
: _data(nullptr)
, _length(0) {}
const char *ghost_file::data() const {
return _data;
}
bool ghost_file::is_static() const {
return _data != nullptr;
}
size_t ghost_file::length() const {
return _length;
}
void ghost_file::update_length(size_t new_length, size_t block_size) {
_length = new_length;
log("File length: %ld\n", new_length);
auto blocks = new_length / block_size + 1;
_file_blocks.resize(blocks);
}
void ghost_file::add_attribute(const char *attribute, const char *value) {
_attributes.emplace(std::string(attribute), std::string(value));
}
void ghost_file::remove_attribute(const char *attribute) {
_attributes.erase(std::string(attribute));
}
bool ghost_file::attribute_exists(const char *attribute) const {
auto it = _attributes.find(std::string(attribute));
return (it == _attributes.end()) ? false : true;
}
const std::unordered_map<std::string, std::string> &ghost_file::attributes() const {
return _attributes;
}
std::vector<block_info> &ghost_file::get_file_blocks() {
return _file_blocks;
}
const char *ghost_file::get_url() const {
auto it = _attributes.find(std::string("url"));
if (it == _attributes.end()) {
return nullptr;
}
return it->second.data();
}