Ensure to advising the remaining of the file when it's closed
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-win (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run

This commit is contained in:
Andrea Mazzoleni 2026-05-21 00:39:03 +02:00
parent 88a6dd1e81
commit 65a20f48c6
5 changed files with 67 additions and 1 deletions

View file

@ -196,10 +196,12 @@ int handle_close(struct snapraid_handle* handle)
/* close if open */
if (handle->f != -1) {
advise_close(&handle->advise, handle->f);
ret = close(handle->f);
if (ret != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error closing file '%s'. %s.\n", handle->file->sub, strerror(errno));
log_fatal(errno, "Error closing file '%s'. %s.\n", handle->path, strerror(errno));
/* invalidate for error */
handle->file = 0;

View file

@ -111,6 +111,8 @@ static void import_file(struct snapraid_state* state, const char* path, uint64_t
size -= read_size;
}
advise_close(&advise, f);
ret = close(f);
if (ret != 0) {
/* LCOV_EXCL_START */

View file

@ -763,6 +763,8 @@ int parity_close(struct snapraid_parity_handle* handle)
struct snapraid_split_handle* split = &handle->split_map[s];
int ret;
advise_close(&split->advise, split->f);
ret = close(split->f);
if (ret != 0) {
/* LCOV_EXCL_START */

View file

@ -1636,6 +1636,65 @@ int advise_read(struct advise_struct* advise, int f, data_off_t offset, data_off
return 0;
}
void advise_close(struct advise_struct* advise, int f)
{
data_off_t flush_offset;
data_off_t flush_size;
data_off_t discard_offset;
data_off_t discard_size;
(void)advise;
(void)f;
(void)flush_offset;
(void)flush_size;
(void)discard_offset;
(void)discard_size;
flush_offset = 0;
flush_size = 0;
discard_offset = 0;
discard_size = 0;
/* assume it's a write if advise->dirty_begin != advise->dirty_end */
switch (advise->mode) {
case ADVISE_FLUSH_WINDOW :
flush_offset = advise->dirty_begin;
flush_size = advise->dirty_end - advise->dirty_begin;
break;
case ADVISE_DISCARD_WINDOW :
discard_offset = advise->dirty_begin;
discard_size = advise->dirty_end - advise->dirty_begin;
break;
}
#if HAVE_SYNC_FILE_RANGE
if (flush_size != 0) {
/* start writing immediately */
sync_file_range(f, flush_offset, flush_size, SYNC_FILE_RANGE_WRITE);
}
#endif
#if HAVE_SYNC_FILE_RANGE && HAVE_POSIX_FADVISE
if (discard_size != 0) {
int ret;
/* send the data to the disk and wait until it's written */
ret = sync_file_range(f, discard_offset, discard_size, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER);
if (ret != 0) {
/* LCOV_EXCL_START */
return;
/* LCOV_EXCL_STOP */
}
/* flush the data from the cache */
posix_fadvise_wrapper(f, discard_offset, discard_size, POSIX_FADV_DONTNEED);
}
#endif
advise->dirty_begin = 0;
advise->dirty_end = 0;
}
/****************************************************************************/
/* memory */

View file

@ -419,6 +419,7 @@ int advise_flags(struct advise_struct* advise);
int advise_open(struct advise_struct* advise, int f);
int advise_write(struct advise_struct* advise, int f, data_off_t offset, data_off_t size);
int advise_read(struct advise_struct* advise, int f, data_off_t offset, data_off_t size);
void advise_close(struct advise_struct* advise, int f);
/****************************************************************************/
/* memory */