Skip to content

Commit 9194f47

Browse files
authored
Merge pull request #258 from yuchen0cc/main
fix bug in retry open zfile
2 parents 0b3d978 + ad2a737 commit 9194f47

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/image_file.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "switch_file.h"
3535
#include "overlaybd/gzip/gz.h"
3636
#include "overlaybd/gzindex/gzfile.h"
37+
#include "overlaybd/tar/tar_file.h"
3738

3839
#define PARALLEL_LOAD_INDEX 32
3940
using namespace photon::fs;
@@ -71,10 +72,18 @@ IFile *ImageFile::__open_ro_file(const std::string &path) {
7172
}
7273
file = aligned_file;
7374
}
75+
76+
auto tar_file = new_tar_file_adaptor(file);
77+
if (!tar_file) {
78+
set_failed("failed to open file as tar file " + path);
79+
delete file;
80+
LOG_ERROR_RETURN(0, nullptr, "new_tar_file_adaptor(`) failed", path);
81+
}
82+
file = tar_file;
7483
// set to local, no need to switch, for zfile and audit
7584
ISwitchFile *switch_file = new_switch_file(file, true, path.c_str());
7685
if (!switch_file) {
77-
set_failed("failed to open switch file `" + path);
86+
set_failed("failed to open switch file " + path);
7887
delete file;
7988
LOG_ERRNO_RETURN(0, nullptr, "new_switch_file(`) failed", path);
8089
}
@@ -158,10 +167,17 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
158167
remote_file->ioctl(SET_SIZE, size);
159168
remote_file->ioctl(SET_LOCAL_DIR, dir);
160169

161-
ISwitchFile *switch_file = new_switch_file(remote_file);
162-
if (!switch_file) {
163-
set_failed("failed to open switch file `" + url);
170+
IFile *tar_file = new_tar_file_adaptor(remote_file);
171+
if (!tar_file) {
172+
set_failed("failed to open remote file as tar file " + url);
164173
delete remote_file;
174+
LOG_ERROR_RETURN(0, nullptr, "failed to open remote file as tar file `", url);
175+
}
176+
177+
ISwitchFile *switch_file = new_switch_file(tar_file, false, url.c_str());
178+
if (!switch_file) {
179+
set_failed("failed to open switch file " + url);
180+
delete tar_file;
165181
LOG_ERROR_RETURN(0, nullptr, "failed to open switch file `", url);
166182
}
167183

src/overlaybd/zfile/zfile.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,18 @@ class CompressionFile : public VirtualReadOnlyFile {
338338

339339
int get_current_block() {
340340
m_reader->m_buf_offset = m_reader->get_buf_offset(m_reader->m_idx);
341-
if ((size_t)(m_reader->m_buf_offset) > sizeof(m_buf)) {
341+
if ((size_t)(m_reader->m_buf_offset) >= sizeof(m_buf)) {
342342
m_reader->m_eno = ERANGE;
343343
LOG_ERRNO_RETURN(0, -1, "get inner buffer offset failed.");
344344
}
345345

346346
auto blk_idx = m_reader->m_idx;
347347
compressed_size = m_reader->compressed_size();
348+
if ((size_t)(m_reader->m_buf_offset) + compressed_size > sizeof(m_buf)) {
349+
m_reader->m_eno = ERANGE;
350+
LOG_ERRNO_RETURN(0, -1, "inner buffer offset (`) + compressed size (`) overflow.",
351+
m_reader->m_buf_offset, compressed_size);
352+
}
348353

349354
if (blk_idx == m_reader->m_begin_idx) {
350355
cp_begin = m_reader->get_inblock_offset(m_reader->m_offset);

src/switch_file.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ using namespace photon::fs;
3939
static IFile *try_open_zfile(IFile *file, bool verify, const char *file_path) {
4040
auto is_zfile = ZFile::is_zfile(file);
4141
if (is_zfile == -1) {
42-
delete file;
4342
LOG_ERRNO_RETURN(0, nullptr, "check file type failed.");
4443
}
4544
// open zfile
@@ -82,9 +81,16 @@ class SwitchFile : public ISwitchFile {
8281
LOG_ERROR("failed to open commit file, path: `", m_filepath);
8382
return;
8483
}
85-
86-
file = try_open_zfile(new_tar_file_adaptor(file), false, m_filepath.c_str());
87-
if (file == nullptr) {
84+
auto tarfile = new_tar_file_adaptor(file);
85+
if (tarfile == nullptr) {
86+
delete file;
87+
LOG_ERROR("failed to open commit file as tar file, path: `", m_filepath);
88+
return;
89+
}
90+
file = tarfile;
91+
auto zfile = try_open_zfile(file, false, m_filepath.c_str());
92+
if (zfile == nullptr) {
93+
delete file;
8894
LOG_ERROR("failed to open commit file as zfile, path: `", m_filepath);
8995
return;
9096
}
@@ -157,11 +163,11 @@ class SwitchFile : public ISwitchFile {
157163
};
158164

159165
ISwitchFile *new_switch_file(IFile *source, bool local, const char *file_path) {
160-
// if tar file, open tar file
161166
int retry = 1;
162167
again:
163-
auto file = try_open_zfile(new_tar_file_adaptor(source), !local, file_path);
168+
auto file = try_open_zfile(source, !local, file_path);
164169
if (file == nullptr) {
170+
LOG_ERROR("failed to open source file as zfile, path: `, retry: `", file_path, retry);
165171
if (retry--) // may retry after cache evict
166172
goto again;
167173
return nullptr;

0 commit comments

Comments
 (0)