Add sys_fdatasync and sync_data

This commit is contained in:
Fabing Li
2024-06-27 10:46:58 +08:00
committed by Tate, Hongliang Tian
parent 5edc110f9d
commit 212dd562a0
18 changed files with 178 additions and 81 deletions

View File

@ -17,6 +17,7 @@ TEST_APPS := \
epoll \
eventfd2 \
execve \
fdatasync \
file_io \
fork \
fork_c \

View File

@ -0,0 +1,5 @@
# SPDX-License-Identifier: MPL-2.0
include ../test_common.mk
EXTRA_C_FLAGS := -static

View File

@ -0,0 +1,49 @@
// SPDX-License-Identifier: MPL-2.0
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
void test_fdatasync_on_fs(const char *directory)
{
char filepath[256];
snprintf(filepath, sizeof(filepath), "%s/test_fdatasync.txt",
directory);
int fd =
open(filepath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char *data = "Hello, fdatasync test!\n";
if (write(fd, data, strlen(data)) != strlen(data)) {
perror("Error writing data");
close(fd);
exit(EXIT_FAILURE);
}
if (fdatasync(fd) == -1) {
perror("Error syncing data");
close(fd);
exit(EXIT_FAILURE);
}
printf("Data written and synced on %s\n", directory);
close(fd);
}
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: %s <directory>\n", argv[0]);
return EXIT_FAILURE;
}
test_fdatasync_on_fs(argv[1]);
return EXIT_SUCCESS;
}

View File

@ -1,44 +0,0 @@
#!/bin/sh
# SPDX-License-Identifier: MPL-2.0
set -e
set -x
check_file_size() {
local file_name="$1"
local expected_size="$2"
if [ ! -f "$file_name" ]; then
echo "Error: File does not exist."
return 1
fi
actual_size=$(du -b "$file_name" | cut -f1)
if [ "$actual_size" -eq "$expected_size" ]; then
return 0
else
echo "Error: File size is incorrect: expected ${expected_size}, but got ${actual_size}."
return 1
fi
}
EXT2_DIR=/ext2
cd ${EXT2_DIR}
echo "Start ext2 fs test......"
# Test case for the big file feature
for i in $(seq 1 10); do
truncate -s 500M test_file.txt
check_file_size test_file.txt $((500 * 1024 * 1024))
truncate -s 2K test_file.txt
check_file_size test_file.txt $((2 * 1024))
done
# Clean up
rm -f test_file.txt
sync
echo "All ext2 fs test passed."

62
regression/apps/scripts/fs.sh Executable file
View File

@ -0,0 +1,62 @@
#!/bin/sh
# SPDX-License-Identifier: MPL-2.0
set -e
set -x
check_file_size() {
local file_name="$1"
local expected_size="$2"
if [ ! -f "$file_name" ]; then
echo "Error: File does not exist."
return 1
fi
actual_size=$(du -b "$file_name" | cut -f1)
if [ "$actual_size" -eq "$expected_size" ]; then
return 0
else
echo "Error: File size is incorrect: expected ${expected_size}, but got ${actual_size}."
return 1
fi
}
test_ext2() {
local ext2_dir="$1"
local test_file="$2"
cd ${ext2_dir}
# Test case for the big file feature
for i in $(seq 1 10); do
truncate -s 500M ${test_file}
check_file_size ${test_file} $((500 * 1024 * 1024))
truncate -s 2K ${test_file}
check_file_size ${test_file} $((2 * 1024))
done
# Clean up
rm -f ${test_file}
sync
cd -
}
test_fdatasync() {
fdatasync/fdatasync /
rm -f /test_fdatasync.txt
fdatasync/fdatasync /ext2
rm -f /ext2/test_fdatasync.txt
fdatasync/fdatasync /exfat
rm -f /exfat/test_fdatasync.txt
}
echo "Start ext2 fs test......"
test_ext2 "/ext2" "test_file.txt"
echo "All ext2 fs test passed."
echo "Start fdatasync test......"
test_fdatasync
echo "All fdatasync test passed."

View File

@ -8,7 +8,7 @@ SCRIPT_DIR=/regression
cd ${SCRIPT_DIR}
./shell_cmd.sh
./ext2.sh
./fs.sh
./process.sh
./network.sh
./test_epoll_pwait.sh