Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ ly_out_filepath(struct ly_out *out, const char *filepath)
return out->method.fpath.filepath;
}

/* replace filepath */
/* replace filepath, the data written into the current file go there before the new file is created/opened */
f = out->method.fpath.f;
fflush(f);
out->method.fpath.f = fopen(filepath, "wb");
if (!out->method.fpath.f) {
LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", filepath, strerror(errno));
Expand Down
13 changes: 13 additions & 0 deletions tests/utests/basic/test_inout.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ test_output_filepath(void **UNUSED(state))

fclose(f1);
ly_out_free(out, NULL, 1);

/* data written before changing to the same file are not left in the file after the new data */
assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
assert_int_equal(LY_SUCCESS, ly_write(out, "previous data", 13));
assert_ptr_equal(NULL, ly_out_filepath(out, fp1));
assert_int_equal(LY_SUCCESS, ly_write(out, "new", 3));
ly_print_flush(out);
assert_non_null(f1 = fopen(fp1, "rb"));
memset(buf, 0, sizeof buf);
assert_int_equal(3, fread(buf, 1, 30, f1));
assert_string_equal("new", buf);
fclose(f1);
ly_out_free(out, NULL, 1);
}

static ssize_t
Expand Down