[Bug 270632] [ext2fs] files <4096 bytes are corrupted on ext4 filesystems

From: <bugzilla-noreply_at_freebsd.org>
Date: Sun, 09 Apr 2023 07:23:54 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=270632

--- Comment #10 from Rajeev Pillai <rajeev_v_pillai@yahoo.com> ---
(In reply to Rajeev Pillai from comment #9)

> If you add `O_DIRECT' to the second open(), [...]

As below:

```
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>

static bool prompt(char* msg) {
        char line[64] = "";

        printf("%s", msg);
        fflush(stdout);
        fgets(line, sizeof line, stdin);
        return (line[0] == 'Y' || line[0] == 'y');
}

int main(int argc, char* argv[]) {
        const char* const S = "hello world\n";
        char* fn;
        size_t n;
        int fd, flag;

        if (argc != 2)
                errx(1, "filename required");

        fn = argv[1];
        if ((fd = open(fn, O_WRONLY|O_CREAT, 0666)) == -1)
                err(1, "%s: open failed", fn);
        n = strlen(S);
        if (write(fd, S, n) != n)
                err(1, "%s: write failed", fn); 
        if (close(fd) == -1)
                err(1, "%s: close failed", fn);
        prompt("Created file. Enter to continue: ");

        flag = O_WRONLY | O_TRUNC;
        if (prompt("Re-creating file. Add O_DIRECT (y/n)? "))
                flag |= O_DIRECT;
        if ((fd = open(fn, flag, 0666)) == -1)
                err(1, "%s: open failed", fn);
        if (prompt("Re-created file. Do lseek to beginning (y/n)? "))
                if (lseek(fd, 0L, SEEK_SET) == -1)
                        err(1, "%s: lseek failed", fn);
        prompt("Truncated file. Enter to continue: ");

        if (write(fd, S, n) != n)                                               
                err(1, "%s: write failed", fn);                                 
        if (prompt("Re-wrote file. fsync (y/n)? "))
                if (fsync(fd))
                        err(1, "fsync failed");
        if (close(fd) == -1)                                                    
                err(1, "%s: close failed", fn);    
        prompt("Done. Enter to exit: ");

        return 0;
}
```

-- 
You are receiving this mail because:
You are the assignee for the bug.