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
22 changes: 12 additions & 10 deletions src/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,23 +305,25 @@ ly_out_reset(struct ly_out *out)
LOGINT(NULL);
return LY_EINT;
case LY_OUT_FD:
if ((lseek(out->method.fd, 0, SEEK_SET) == -1) && (errno != ESPIPE)) {
LOGERR(NULL, LY_ESYS, "Seeking output file descriptor failed (%s).", strerror(errno));
return LY_ESYS;
}
if ((errno != ESPIPE) && (ftruncate(out->method.fd, 0) == -1)) {
if (lseek(out->method.fd, 0, SEEK_SET) == -1) {
if (errno != ESPIPE) {
LOGERR(NULL, LY_ESYS, "Seeking output file descriptor failed (%s).", strerror(errno));
return LY_ESYS;
}
} else if (ftruncate(out->method.fd, 0) == -1) {
LOGERR(NULL, LY_ESYS, "Truncating output file failed (%s).", strerror(errno));
return LY_ESYS;
}
break;
case LY_OUT_FDSTREAM:
case LY_OUT_FILE:
case LY_OUT_FILEPATH:
if ((fseek(out->method.f, 0, SEEK_SET) == -1) && (errno != ESPIPE)) {
LOGERR(NULL, LY_ESYS, "Seeking output file stream failed (%s).", strerror(errno));
return LY_ESYS;
}
if ((errno != ESPIPE) && (ftruncate(fileno(out->method.f), 0) == -1)) {
if (fseek(out->method.f, 0, SEEK_SET) == -1) {
if (errno != ESPIPE) {
LOGERR(NULL, LY_ESYS, "Seeking output file stream failed (%s).", strerror(errno));
return LY_ESYS;
}
} else if (ftruncate(fileno(out->method.f), 0) == -1) {
LOGERR(NULL, LY_ESYS, "Truncating output file failed (%s).", strerror(errno));
return LY_ESYS;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/utests/basic/test_inout.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ test_output_fd(void **UNUSED(state))
assert_int_equal(10, read(fd2, buf, 30));
assert_string_equal("test print", buf);
assert_int_equal(0, lseek(fd2, 0, SEEK_SET));
/* a value of errno left from before must not prevent truncating */
errno = ESPIPE;
assert_int_equal(LY_SUCCESS, ly_out_reset(out));

assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
Expand Down Expand Up @@ -321,13 +323,17 @@ test_output_file(void **UNUSED(state))
assert_non_null(fgets(buf, 31, f2));
assert_string_equal("test print", buf);
assert_int_equal(0, fseek(f2, 0, SEEK_SET));
/* a value of errno left from before must not prevent truncating */
errno = ESPIPE;
assert_int_equal(LY_SUCCESS, ly_out_reset(out));

assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
assert_int_equal(8, ly_out_printed(out));
ly_print_flush(out);
assert_non_null(fgets(buf, 31, f2));
assert_string_equal("rewrite", buf);
assert_int_equal(0, fseek(f2, 0, SEEK_END));
assert_int_equal(8, ftell(f2));

fclose(f2);
ly_out_free(out, NULL, 1);
Expand Down