Skip to content

Commit aa4e6b7

Browse files
committed
posix: fs: implement fdatasync()
`fdatasync()` is basically equivalent to `fsync()` according to the standards. Added test for it. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 97dadce commit aa4e6b7

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

doc/services/portability/posix/option_groups/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ _POSIX_SYNCHRONIZED_IO
803803
:header: API, Supported
804804
:widths: 50,10
805805

806-
fdatasync(),
806+
fdatasync(),yes
807807
fsync(),yes
808808
msync(),yes
809809

include/zephyr/posix/unistd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ off_t lseek(int file, off_t offset, int whence);
3535
int fsync(int fd);
3636
int ftruncate(int fd, off_t length);
3737

38+
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
39+
int fdatasync(int fd);
40+
#endif /* CONFIG_POSIX_SYNCHRONIZED_IO */
41+
3842
/* File System related operations */
3943
int rename(const char *old, const char *newp);
4044
int unlink(const char *path);

lib/posix/options/fsync.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ int fsync(int fd)
1616
#ifdef CONFIG_POSIX_FILE_SYSTEM_ALIAS_FSYNC
1717
FUNC_ALIAS(fsync, _fsync, int);
1818
#endif
19+
20+
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
21+
int fdatasync(int fd)
22+
{
23+
return fsync(fd);
24+
}
25+
#endif /* CONFIG_POSIX_SYNCHRONIZED_IO */

tests/posix/fs/src/test_fs_file.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ static int test_file_fsync(void)
159159
return res;
160160
}
161161

162+
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
163+
static int test_file_fdatasync(void)
164+
{
165+
int res = 0;
166+
167+
if (file < 0)
168+
return res;
169+
170+
res = fdatasync(file);
171+
if (res < 0) {
172+
TC_ERROR("Failed to sync file: %d, errno = %d\n", res, errno);
173+
res = TC_FAIL;
174+
}
175+
176+
close(file);
177+
file = -1;
178+
return res;
179+
}
180+
#endif /* CONFIG_POSIX_SYNCHRONIZED_IO */
181+
162182
static int test_file_truncate(void)
163183
{
164184
int res = 0;
@@ -251,6 +271,23 @@ ZTEST(posix_fs_file_test, test_fs_sync)
251271
zassert_true(test_file_fsync() == TC_PASS);
252272
}
253273

274+
/**
275+
* @brief Test for POSIX fdatasync API
276+
*
277+
* @details Test sync the file through POSIX fdatasync API.
278+
*/
279+
ZTEST(posix_fs_file_test, test_fs_datasync)
280+
{
281+
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
282+
/* FIXME: restructure tests as per #46897 */
283+
zassert_true(test_file_open() == TC_PASS);
284+
zassert_true(test_file_write() == TC_PASS);
285+
zassert_true(test_file_fdatasync() == TC_PASS);
286+
#else
287+
ztest_test_skip();
288+
#endif
289+
}
290+
254291
/**
255292
* @brief Test for POSIX ftruncate API
256293
*

tests/posix/headers/src/unistd_h.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ ZTEST(posix_headers, test_unistd_h)
230230
/* zassert_not_null(fchdir); */ /* not implemented */
231231
/* zassert_not_null(fchown); */ /* not implemented */
232232
/* zassert_not_null(fchownat); */ /* not implemented */
233-
/* zassert_not_null(fdatasync); */ /* not implemented */
233+
zassert_not_null(fdatasync);
234234
/* zassert_not_null(fexecve); */ /* not implemented */
235235
/* zassert_not_null(fork); */ /* not implemented */
236236
/* zassert_not_null(fpathconf); */ /* not implemented */

0 commit comments

Comments
 (0)