Skip to content

Commit eea67c6

Browse files
author
Gleb Zhizhchenko
committed
fix: fix Read method
feat: add Close method to ReadFile class
1 parent 4fa7dc5 commit eea67c6

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

example/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ int main() {
5252
return 1;
5353
}
5454

55-
fs_db_rf_destroy(&rf);
55+
err = fs_db_rf_close(&rf);
56+
if (err) {
57+
printf("%s\n", fs_db_err_msg(err));
58+
return 1;
59+
}
5660
printf("\n");
5761

5862
err = fs_db_delete((fs_db_store*)conn, KEY);

inc/read_file.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ namespace fs_db {
2626

2727
public:
2828
ReadFile(ReadFile &&other) noexcept;
29+
~ReadFile();
2930

3031
size_t Read(char data[], size_t size);
32+
void Close();
3133

3234
ReadFile &operator=(ReadFile &&other) noexcept;
3335
};
@@ -45,7 +47,7 @@ typedef struct fs_db_rf fs_db_rf;
4547
extern "C" {
4648
#endif
4749
fs_db_err fs_db_rf_read(fs_db_rf *rf, char data[], size_t size, size_t *read);
48-
void fs_db_rf_destroy(fs_db_rf **rf);
50+
fs_db_err fs_db_rf_close(fs_db_rf **rf);
4951
#ifdef __cplusplus
5052
}
5153
#endif

src/read_file.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ fs_db::ReadFile::ReadFile(ReadFile &&other) noexcept: buf(std::move(other.buf)),
66

77
fs_db::ReadFile::ReadFile(std::unique_ptr<grpc::ClientContext> &&ctx, std::unique_ptr<grpc::ClientReader<store::GetFileResponse>> &&r): ctx(std::move(ctx)), r(std::move(r)) {}
88

9+
fs_db::ReadFile::~ReadFile() {
10+
Close();
11+
}
12+
913
size_t fs_db::ReadFile::Read(char data[], size_t size) {
1014
if (r == nullptr) {
1115
return 0;
1216
}
1317

1418
store::GetFileResponse msg;
1519
while (buf.size() < size && r->Read(&msg)) {
16-
handleError(r->Finish());
1720
buf += msg.chunk();
1821
}
1922

@@ -24,6 +27,18 @@ size_t fs_db::ReadFile::Read(char data[], size_t size) {
2427
return size;
2528
}
2629

30+
void fs_db::ReadFile::Close() {
31+
if (r == nullptr) {
32+
return;
33+
}
34+
35+
handleError(r->Finish());
36+
buf.clear();
37+
ctx.reset();
38+
r.reset();
39+
}
40+
41+
2742
fs_db::ReadFile &fs_db::ReadFile::operator=(ReadFile &&other) noexcept {
2843
buf = std::move(other.buf);
2944
ctx = std::move(other.ctx);
@@ -38,7 +53,13 @@ fs_db_err fs_db_rf_read(fs_db_rf *rf, char data[], const size_t size, size_t *re
3853
});
3954
}
4055

41-
void fs_db_rf_destroy(fs_db_rf **rf) {
56+
fs_db_err fs_db_rf_close(fs_db_rf **rf) {
57+
const fs_db_err err = fs_db::Exception::try_catch([&] {
58+
(*rf)->Close();
59+
});
60+
4261
delete *rf;
4362
*rf = nullptr;
63+
64+
return err;
4465
}

src/write_file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void fs_db::WriteFile::Close() {
4747

4848
w->WritesDone();
4949
handleError(w->Finish());
50+
ctx.reset();
51+
resp.reset();
5052
w.reset();
5153
}
5254

0 commit comments

Comments
 (0)