完善http文件获取相关代码

This commit is contained in:
ziyue 2022-02-16 16:58:13 +08:00
parent 81199fabd4
commit 304c898a3a

View File

@ -140,6 +140,7 @@ HttpFileBody::HttpFileBody(const string &file_path, bool use_mmap) {
} }
#endif #endif
if (!_map_addr && _read_to != -1) { if (!_map_addr && _read_to != -1) {
//mmap失败(且不是由于文件不存在导致的)或未执行mmap时才进入fread逻辑分支
_fp.reset(fopen(file_path.data(), "rb"), [](FILE *fp) { _fp.reset(fopen(file_path.data(), "rb"), [](FILE *fp) {
if (fp) { if (fp) {
fclose(fp); fclose(fp);
@ -150,9 +151,13 @@ HttpFileBody::HttpFileBody(const string &file_path, bool use_mmap) {
_read_to = -1; _read_to = -1;
return; return;
} }
if (!_read_to) {
//_read_to等于0时说明还未尝试获取文件大小
//加上该判断逻辑在mmap失败时可以省去一次该操作
_read_to = File::fileSize(_fp.get()); _read_to = File::fileSize(_fp.get());
} }
} }
}
void HttpFileBody::setRange(uint64_t offset, uint64_t max_size) { void HttpFileBody::setRange(uint64_t offset, uint64_t max_size) {
CHECK((int64_t)offset <= _read_to && (int64_t)(max_size + offset) <= _read_to); CHECK((int64_t)offset <= _read_to && (int64_t)(max_size + offset) <= _read_to);