Skip to content

Commit

Permalink
add tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed Jul 19, 2024
1 parent e0f72a4 commit 2051488
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/wasix/src/syscalls/wasi/fd_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ pub(crate) fn fd_write_internal<M: MemorySize>(
// max of the two is the correct new size.
stat.st_size = stat.st_size.max(curr_offset);
} else {
// pwrite does not update the cursor of the file so to calculate the final
// size of the file we compute where the cursor would have been if it was updated,
// and get the max value between it and the current size.
stat.st_size = stat.st_size.max(offset + bytes_written as u64);
}
} else {
Expand Down
86 changes: 86 additions & 0 deletions tests/wasix/pwrite-and-size/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

int main()
{
int fd;
char *data;
struct stat statbuf;

fd = open("/data/my_file.txt", O_CREAT | O_WRONLY, 0644);
if (fd == -1)
{
goto fail;
}

data = "ABC";

if (pwrite(fd, data, 3, 0) == -1)
{
goto fail;
}
if (fstat(fd, &statbuf) == -1)
{
goto fail;
}

if (statbuf.st_size != 3)
{
goto fail;
}

data = "D";
if (pwrite(fd, data, 1, 1) == -1)
{
goto fail;
}

if (fstat(fd, &statbuf) == -1)
{
goto fail;
}
if (statbuf.st_size != 3)
{
goto fail;
}

data = "XYZ";
if (pwrite(fd, data, 3, 3) == -1)
{
goto fail;
}
if (fstat(fd, &statbuf) == -1)
{
goto fail;
}
if (statbuf.st_size != 6)
{
goto fail;
}

data = "GHIJKLMJ";
if (pwrite(fd, data, 10, 1) == -1)
{
goto fail;
}
if (fstat(fd, &statbuf) == -1)
{
goto fail;
}

if (statbuf.st_size != 11)
{
goto fail;
}

printf("0");

return 0;

fail:
printf("1");

return 1;
}
7 changes: 7 additions & 0 deletions tests/wasix/pwrite-and-size/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

$WASMER -q run main.wasm --mapdir=/data:. > output

printf "0" | diff -u output - 1>/dev/null

rm my_file.txt

0 comments on commit 2051488

Please sign in to comment.