Skip to content

Commit 4a40def

Browse files
edsipercosmo0920
authored andcommitted
fstore: safe checks for active chunks on deletion
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent eb94a06 commit 4a40def

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/flb_fstore.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,33 @@ struct flb_fstore_file *flb_fstore_file_get(struct flb_fstore *fs,
215215
* Set a file to inactive mode. Inactive means just to remove the reference
216216
* from the list.
217217
*/
218+
static int chunk_is_linked_to_stream(struct flb_fstore_file *fsf)
219+
{
220+
struct mk_list *head;
221+
struct cio_chunk *chunk;
222+
223+
if (fsf == NULL || fsf->chunk == NULL || fsf->stream == NULL) {
224+
return FLB_FALSE;
225+
}
226+
227+
mk_list_foreach(head, &fsf->stream->chunks) {
228+
chunk = mk_list_entry(head, struct cio_chunk, _head);
229+
230+
if (chunk == fsf->chunk) {
231+
return FLB_TRUE;
232+
}
233+
}
234+
235+
return FLB_FALSE;
236+
}
237+
218238
int flb_fstore_file_inactive(struct flb_fstore *fs,
219239
struct flb_fstore_file *fsf)
220240
{
221241
/* close the Chunk I/O reference, but don't delete the real file */
222-
if (fsf->chunk) {
242+
if (chunk_is_linked_to_stream(fsf) == FLB_TRUE) {
223243
cio_chunk_close(fsf->chunk, CIO_FALSE);
244+
fsf->chunk = NULL;
224245
}
225246

226247
/* release */
@@ -239,7 +260,10 @@ int flb_fstore_file_delete(struct flb_fstore *fs,
239260
struct flb_fstore_file *fsf)
240261
{
241262
/* close the Chunk I/O reference, but don't delete it the real file */
242-
cio_chunk_close(fsf->chunk, CIO_TRUE);
263+
if (chunk_is_linked_to_stream(fsf) == FLB_TRUE) {
264+
cio_chunk_close(fsf->chunk, CIO_TRUE);
265+
fsf->chunk = NULL;
266+
}
243267

244268
/* release */
245269
mk_list_del(&fsf->_head);
@@ -415,6 +439,7 @@ static int map_chunks(struct flb_fstore *ctx, struct flb_fstore_stream *fs_strea
415439
return -1;
416440
}
417441

442+
fsf->stream = stream;
418443
fsf->chunk = chunk;
419444

420445
/* load metadata */

0 commit comments

Comments
 (0)