Skip to content

pwrite-with-append.c: ensure that O_APPEND is actually working #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
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
29 changes: 17 additions & 12 deletions tests/c/testsuite/pwrite-with-append.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
#include <assert.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
char buf[4];
int fd;
FILE *file;
size_t size;

file = fopen("fs-tests.dir/pwrite.cleanup", "a+");
assert(file != NULL);
fd = open("fs-tests.dir/pwrite.cleanup",
O_CREAT | O_TRUNC | O_WRONLY | O_APPEND);
assert(fd != -1);

fd = fileno(file);
size = write(fd, buf, 2);
assert(size == 2);

size = fwrite(buf, 1, 4, file);
assert(size == sizeof(buf));
fflush(file);
// test if O_APPEND is working
assert(lseek(fd, 0, SEEK_SET) == 0);
size = write(fd, buf, 2);
assert(size == 2);
assert(lseek(fd, 0, SEEK_CUR) == 4);

size = pwrite(fd, buf, 4, 0);
assert(size == sizeof(buf));
fflush(file);
size = pwrite(fd, buf, 3, 0);
assert(size == 3);

// fd_pwrite should write from offset 0 regardless of append.
// (thus shouln't extend the file.)
// it shouldn't move the file offset either.
assert(lseek(fd, 0, SEEK_CUR) == 4);
assert(lseek(fd, 0, SEEK_END) == 4);

fclose(file);
close(fd);

return EXIT_SUCCESS;
}