-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_op.cpp
More file actions
230 lines (189 loc) · 4.96 KB
/
Copy pathfile_op.cpp
File metadata and controls
230 lines (189 loc) · 4.96 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "file_op.h"
namespace qiniu {
namespace largefile {
FileOperation::FileOperation(const std::string& file_name, const int open_flags) :
fd_(-1), open_flags_(open_flags)
{
file_name_ = strdup(file_name.c_str()); //stdup相当于把参数中的值重新分配内存,然后返回这块内存
}
FileOperation::~FileOperation()
{
if (fd_ > 0) {
::close(fd_);
}
if (file_name_ != NULL) {
free(file_name_);
file_name_ = NULL;
}
}
int FileOperation::open_file()
{
if (fd_ > 0) { //如果fd已经打开
close(fd_);
fd_ = -1;
}
fd_ = ::open(file_name_, open_flags_, OPEN_MODE);
if (fd_ < 0) {
return -errno;
}
return fd_;
}
void FileOperation::close_file()
{
if (fd_ < 0)
return;
::close(fd_);
fd_ = -1;
}
int FileOperation::flush_file()
{
if (open_flags_ && O_SYNC)//O_SYNC以同步地方式打开数据,没必要再flush
return 0;
int fd = check_file();
if (fd < 0)
return fd;
return fsync(fd); //将缓存区(内存)的数据同步到磁盘。
//fsync的功能是确保文件fd所有已修改的内容已经正确同步到硬盘上,该调用会阻塞等待直到设备报告IO完成。
}
int FileOperation::unlink_file()
{
close_file(); //确保文件关闭
return ::unlink(file_name_); //删除文件
}
int FileOperation::pRead_file(char* buf, const int32_t nBytes, const int64_t offset)
{
int32_t left = nBytes; //剩余需要读取的Byte数
int32_t read_len = 0; //已读取的长度
int64_t read_offset = offset; //开始读取的偏移量
char* cur_buf = buf;
int i = 0; //尝试次数
while (left > 0) {
i++; //尝试次数如果超过5次,那么读取失败
if (i >= MAX_DISK_TIMES) break;
if (check_file() < 0) return -errno;
read_len = pread64(fd_, cur_buf, left, read_offset);
if (read_len < 0) { //如果读取的长度小于0,说明读取失败
read_len = -errno;
//查找读取失败的原因,如果是EINTR(被打断)和EAGAIN(资源暂时不可用)表示还可以继续尝试
if (-read_len == EINTR || -read_len == EAGAIN)
continue;
else if (-read_len == EBADF) {
fd_ = -1;
continue;
}
else
return read_len;
}
else if (read_len == 0) { //如果读到了末尾
break;
}
cur_buf += read_len;
read_offset += read_len;
left -= read_len;
}
if (left != 0) { //如果循环完成但是还有剩余,说明没有完成
return EXIT_DISK_OPER_INCOMPLETE;
}
return TFS_SUCCESS;
}
int FileOperation::pWrite_file(const char* buf, const int32_t nBytes, const int64_t offset)
{
int32_t left = nBytes;
int32_t written_len = 0;
int64_t written_offset = offset;
const char* cur_buf = buf;
int i = 0;
while (left > 0) {
i++;
if (i >= MAX_DISK_TIMES) break;
if (check_file() < 0) return -errno;
written_len = pwrite64(fd_, cur_buf, left, written_offset);
if (written_len < 0) {
written_len = -errno;
if (-written_len == EINTR || -written_len == EAGAIN)
continue;
else if (-written_len == EBADF) {
fd_ = -1;
continue;
}
else
return written_len;
}
else if (written_len == 0) { //如果读到了末尾
break;
}
cur_buf += written_len;
written_offset += written_len;
left -= written_len;
}
if (left != 0) { //如果循环完成但是还有剩余,说明没有完成
return EXIT_DISK_OPER_INCOMPLETE;
}
return TFS_SUCCESS;
}
int FileOperation::write_file(const char* buf, const int32_t nBytes)
{
int32_t left = nBytes;
int32_t written_len = 0;
const char* cur_buf = buf;
int i = 0;
while (left > 0) {
i++;
if (i >= MAX_DISK_TIMES) break;
if (check_file() < 0) return -errno;
written_len = write(fd_, cur_buf, left);
if (written_len < 0) {
written_len = -errno;
if (-written_len == EINTR || -written_len == EAGAIN)
continue;
else if (-written_len == EBADF) {
fd_ = -1;
continue;
}
else
return written_len;
}
cur_buf += written_len;
left -= written_len;
}
if (left != 0) { //如果循环完成但是还有剩余,说明没有完成
return EXIT_DISK_OPER_INCOMPLETE;
}
return TFS_SUCCESS;
}
int64_t FileOperation::get_file_size()
{
int fd = check_file();
if (fd < 0) {
return -1;
}
struct stat buf;
if (fstat(fd, &buf) != 0) {
return -1;
}
return buf.st_size;
}
int FileOperation::fTruncate_file(const int64_t length)
{
int fd = check_file();
if (fd < 0) {
return -1;
}
return ftruncate(fd, length);
}
int FileOperation::seek_file(int64_t offset)
{
int fd = check_file();
if (fd < 0)
return -1;
return lseek(fd, offset, SEEK_SET); //SEEK_SET表示offset被设置成当前位置+offset bytes
}
int FileOperation::check_file()
{
if (fd_ < 0) {
fd_ = open_file();
}
return fd_;
}
}
}