This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.cpp
186 lines (153 loc) · 4.82 KB
/
file.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Created by TYTY on 2019-04-21 021.
//
#include "file.h"
using namespace file;
using std::string;
file_reader::file_reader(string _file_name, const int &buff_size)
: buff_s(buff_size) {
// using std::filesystem library to check file availability
try {
#ifdef WIN32
std::replace(_file_name.begin(), _file_name.end(), '/', '\\');
#else
std::replace(_file_name.begin(), _file_name.end(), '\\', '/');
#endif
file_size = fs::file_size(_file_name);
}
catch (fs::filesystem_error &e) {
LOG(ERROR) << "Failed in reading file: " << e.what();
// since some error occured during reading the file, we simply exit
exit(1);
}
if (file_size == -1) {
LOG(ERROR) << "Failed in reading file: File not exist.";
// since the file not exist, we simply exit
exit(1);
}
// open the file in binary read mode
_file.open(_file_name, std::ios::in | std::ios::binary);
// put the pointer to the begin of the file
_file.seekg(0);
// file opened successfully
ok = true;
LOG(INFO) << "File opened for reading: " << _file_name;
}
file_reader::~file_reader() {
// close the file when file_reader object is recycled
// - but only if it is open now
if (_file.is_open()) {
_file.close();
}
LOG(INFO) << "File reading closed.";
}
int file_reader::read(char *buffer, std::uintmax_t &offset_) {
// confirm that there's only one thread doing file reading operation
std::lock_guard<std::mutex> lock(_lock);
// check file status before reading it
if (!(ok && _file.is_open())) {
return 0;
} else if (_file.eof()) {
// transfer thread should send a packet inform file transfer finished
// after got 0 read byte return
LOG(DEBUG) << "File reached end before reading.";
ok = false;
return 0;
} else if (file_size < buff_s) {
// there we add an offset data member to store piece count
// see below for reason
offset_ = offset * buff_s;
++offset;
_file.read(buffer, buff_s);
// LOG(DEBUG) << "File read: offset " << offset_ << " block_size "
// << file_size;
// below decrease the file_size in each opetation so now it is
// the size of the last piece
return file_size;
} else {
_file.read(buffer, buff_s);
// during debug the offset increcement has some strange disorder
// seems compiler will do out of order execution here
// which can mess up the reading offset config
// offset_ = _file.tellg()
offset_ = offset * buff_s;
++offset;
// decrease the file_size to calculate size of the last piece
file_size -= buff_s;
// LOG(TRACE) << "File read: offset " << offset_ << " block_size " << buff_s;
return buff_s;
}
}
int file_reader::read_all(char *buffer) {
_file.read(buffer, file_size);
return 0;
}
file_writer::file_writer(string _file_name, const std::uintmax_t &file_size) {
// get remaining space of current directory
fs::space_info space = fs::space(fs::current_path());
// throw exception when no enough space.
if (space.available < file_size) {
LOG(ERROR) << "Failed in writing file: No enough space.";
throw NoEnoughSpace();
}
#ifdef WIN32
std::replace(_file_name.begin(), _file_name.end(), '/', '\\');
#else
std::replace(_file_name.begin(), _file_name.end(), '\\', '/');
#endif
// open file in binary write mode
_file.open(_file_name, std::ios::out | std::ios::binary);
// put the pointer to the begin of the file
_file.seekp(0);
// check if file is open
if (_file.is_open()) {
ok = true;
}
LOG(INFO) << "File opened for writing: " << _file_name;
}
file_writer::~file_writer() {
if (_file.is_open()) {
// flush to make everything write into disk
_file.flush();
_file.close();
LOG(INFO) << "File writing closed.";
}
}
void file_writer::close() {
if (_file.is_open()) {
_file.flush();
_file.close();
LOG(INFO) << "File writing closed.";
}
}
int file_writer::write(const char *buffer,
const int &len,
const std::uintmax_t &offset) {
// only one thread can do write operation
std::lock_guard<std::mutex> lock(_lock);
if (!(_file.is_open())) {
LOG(DEBUG) << "File not open";
return 0;
} else {
// move pointer to piece offset
_file.seekp(offset);
_file.write(buffer, len);
// LOG(TRACE) << "File write: offset " << offset << " block_size " << len;
return len;
}
}
// same function with different input type
int file_writer::write(const string &buffer,
const int &len,
const std::uintmax_t &offset) {
std::lock_guard<std::mutex> lock(_lock);
if (!(_file.is_open())) {
LOG(DEBUG) << "File not open";
return 0;
} else {
_file.seekp(offset);
_file.write(buffer.c_str(), len);
// LOG(TRACE) << "File write: offset " << offset << " block_size " << len;
return len;
}
}