os_calls: Add g_file_is_open()

This commit is contained in:
matt335672 2023-04-03 13:39:00 +01:00
parent 563cfaf009
commit ff24984cf3
3 changed files with 31 additions and 0 deletions

View File

@ -2160,6 +2160,13 @@ g_file_close(int fd)
return 0;
}
/*****************************************************************************/
int
g_file_is_open(int fd)
{
return (fcntl(fd, F_GETFD) >= 0);
}
/*****************************************************************************/
/* read from file, returns the number of bytes read or -1 on error */
int

View File

@ -210,6 +210,12 @@ int g_file_open(const char *file_name);
int g_file_open_ex(const char *file_name, int aread, int awrite,
int acreate, int atrunc);
int g_file_close(int fd);
/**
* Returns 1 if a file is open (i.e. the file descriptor is valid)
* @param fd File descriptor
* @return 1 for file open, 0 for not open
*/
int g_file_is_open(int fd);
int g_file_read(int fd, char *ptr, int len);
int g_file_write(int fd, const char *ptr, int len);
int g_file_seek(int fd, int offset);

View File

@ -262,6 +262,23 @@ START_TEST(test_g_file_get_open_fds)
}
END_TEST
/******************************************************************************/
START_TEST(test_g_file_is_open)
{
int devzerofd = g_file_open("/dev/zero");
ck_assert(devzerofd >= 0);
// Check open file comes up as open
ck_assert_int_ne(g_file_is_open(devzerofd), 0);
g_file_close(devzerofd);
// Check the now-closed file no longer registers as open
ck_assert_int_eq(g_file_is_open(devzerofd), 0);
}
END_TEST
/******************************************************************************/
START_TEST(test_g_sck_fd_passing)
{
@ -448,6 +465,7 @@ make_suite_test_os_calls(void)
#endif
tcase_add_test(tc_os_calls, test_g_file_cloexec);
tcase_add_test(tc_os_calls, test_g_file_get_open_fds);
tcase_add_test(tc_os_calls, test_g_file_is_open);
tcase_add_test(tc_os_calls, test_g_sck_fd_passing);
tcase_add_test(tc_os_calls, test_g_sck_fd_overflow);
return s;