From 871bf84d5700b87112ffa04ed9966e8223966a50 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Thu, 6 Jan 2022 14:43:58 +0100 Subject: [PATCH] unistd: add file offset checks --- src/unistd/files.c | 70 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/src/unistd/files.c b/src/unistd/files.c index d7e4aaa..4ba2ad5 100644 --- a/src/unistd/files.c +++ b/src/unistd/files.c @@ -14,6 +14,29 @@ extern char **fs_split_components(char *path, int *count); +/* Check the value that gint keeps in memory for the file offset against + BFile_GetPos() on the fx-CG 50. All of this is because the G-III has no + documented version of BFile_GetPos() so we track it ourselves. */ +#ifdef FXCG50 +static void check_offset(ft_test *t, int fd) +{ + int *data = fs_get_descriptor(fd)->data; + int fugue_fd = data[0]; + int pos = data[1]; + int bfile_pos = BFile_GetPos(fugue_fd); + + ft_assert(t, pos == bfile_pos); + if(pos != bfile_pos) { + ft_log(t, " Position tracked by gint: %d\n", pos); + ft_log(t, " True position from BFile: %d\n", bfile_pos); + } +} +#else +static void check_offset(ft_test *t, int fd) +{ +} +#endif + static void log_comps(ft_test *t, char const *path, char const *solution) { ft_log(t, "Splitting: %s\n", path); @@ -81,10 +104,13 @@ static void _ft_unistd_simple_write_switch(ft_test *t) if(fd >= 0) { DO_E(rc, write(fd, "write\n", 6), t, "%d"); ft_assert(t, rc == 6); + check_offset(t, fd); DO_E(rc, write(fd, "line #2\n", 8), t, "%d"); ft_assert(t, rc == 8); + check_offset(t, fd); DO_E(rc, write(fd, "line #3\n", 8), t, "%d"); ft_assert(t, rc == 8); + check_offset(t, fd); } DO_E(rc, close(fd), t, "%d"); @@ -112,8 +138,10 @@ static void _ft_unistd_write_odd_switch_1(ft_test *t) if(fd >= 0) { DO_E(rc, write(fd, "write odd byte count\n", 21), t, "%d"); ft_assert(t, rc == 21); + check_offset(t, fd); DO_E(rc, write(fd, "again, cancelling it\n", 21), t, "%d"); ft_assert(t, rc == 21); + check_offset(t, fd); } DO_E(rc, close(fd), t, "%d"); @@ -131,8 +159,10 @@ static void _ft_unistd_write_odd_switch_2(ft_test *t) if(fd >= 0) { DO_E(rc, write(fd, "write odd byte count\n", 21), t, "%d"); ft_assert(t, rc == 21); + check_offset(t, fd); DO_E(rc, write(fd, "then even, keeping it\n", 22), t, "%d"); ft_assert(t, rc == 22); + check_offset(t, fd); } DO_E(rc, close(fd), t, "%d"); @@ -162,6 +192,7 @@ static void _ft_unistd_simple_read_switch(ft_test *t) if(fd >= 0) { DO_E(rc, write(fd, "_ft_unistd_simple_read", 22), t, "%d"); ft_assert(t, rc == 22); + check_offset(t, fd); DO_E(rc, close(fd), t, "%d"); ft_assert(t, rc == 0); @@ -173,35 +204,51 @@ static void _ft_unistd_simple_read_switch(ft_test *t) ft_assert(t, fd >= 0); if(fd < 0) return; - int fugue_fd = (int)fs_get_descriptor(fd)->data; + // This taps into the internal structure of the fd + int fugue_fd = *(int *)fs_get_descriptor(fd)->data; ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n", lseek(fd, 0, SEEK_CUR)); + check_offset(t, fd); DO_E(rc, BFile_Size(fugue_fd), t, "%d"); char buffer[29] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; DO_E(rc, read(fd, buffer, 11), t, "%d"); ft_assert(t, rc == 11); ft_assert(t, !strcmp(buffer, "_ft_unistd_xxxxxxxxxxxxxxxxx")); + check_offset(t, fd); ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n", lseek(fd, 0, SEEK_CUR)); + check_offset(t, fd); DO_E(rc, BFile_Size(fugue_fd), t, "%d"); DO_E(rc, read(fd, buffer + 11, 11), t, "%d"); ft_assert(t, rc == 11); ft_assert(t, !strcmp(buffer, "_ft_unistd_simple_readxxxxxx")); + check_offset(t, fd); ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n", lseek(fd, 0, SEEK_CUR)); + check_offset(t, fd); DO_E(rc, BFile_Size(fugue_fd), t, "%d"); ft_log(t, "[INFO] lseek(fd, 4, SEEK_SET) = %ld\n", lseek(fd, 4, SEEK_SET)); + check_offset(t, fd); ft_log(t, "[INFO] lseek(fd, 4, SEEK_END) = %ld\n", lseek(fd, 4, SEEK_END)); + check_offset(t, fd); ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n", lseek(fd, 4, SEEK_CUR)); + check_offset(t, fd); + + DO_E(rc, lseek(fd, -4, SEEK_END), t, "%d"); + ft_assert(t, rc == 18); + check_offset(t, fd); + DO_E(rc, read(fd, buffer, 8), t, "%d"); + ft_assert(t, rc == 4); + check_offset(t, fd); DO_E(rc, close(fd), t, "%d"); ft_assert(t, rc == 0); @@ -229,6 +276,7 @@ static void _ft_unistd_seek_patterns_switch(ft_test *t) if(fd >= 0) { DO_E(rc, write(fd, "_ft_unistd_seek_patterns", 24), t, "%d"); ft_assert(t, rc == 24); + check_offset(t, fd); DO_E(rc, close(fd), t, "%d"); ft_assert(t, rc == 0); @@ -239,55 +287,70 @@ static void _ft_unistd_seek_patterns_switch(ft_test *t) ft_assert(t, fd >= 0); if(fd < 0) return; - int fugue_fd = (int)fs_get_descriptor(fd)->data; + int fugue_fd = *(int *)fs_get_descriptor(fd)->data; /* Read from the start */ char str[32] = { 0 }; DO_E(rc, read(fd, str, 8), t, "%d"); ft_assert(t, rc == 8 && !strncmp(str, "_ft_unis", 8)); + check_offset(t, fd); DO_E(rc, read(fd, str, 4), t, "%d"); ft_assert(t, rc == 4 && !strncmp(str, "td_sunis", 8)); + check_offset(t, fd); /* Rewind */ DO_E(rc, lseek(fd, 0, SEEK_SET), t, "%d"); ft_assert(t, rc == 0); + check_offset(t, fd); DO_E(rc, read(fd, str, 24), t, "%d"); ft_assert(t, rc == 24 && !strcmp(str, "_ft_unistd_seek_patterns")); + check_offset(t, fd); /* Read from the middle */ DO_E(rc, pread(fd, str, 8, (off_t)12), t, "%d"); ft_assert(t, rc == 8 && !strncmp(str, "eek_patt", 8)); + check_offset(t, fd); /* Write to the end */ DO_E(rc, lseek(fd, -8, SEEK_END), t, "%d"); ft_assert(t, rc == 16); + check_offset(t, fd); DO_E(rc, write(fd, "methods!", 8), t, "%d"); ft_assert(t, rc == 8); + check_offset(t, fd); /* Read again from the end */ DO_E(rc, pread(fd, str, 8, (off_t)16), t, "%d"); ft_assert(t, rc == 8 && !strncmp(str, "methods!", 8)); + check_offset(t, fd); /* Play around with absolute seeking */ DO_E(rc, lseek(fd, -4, SEEK_END), t, "%d"); ft_assert(t, rc == 20); + check_offset(t, fd); DO_E(rc, lseek(fd, 0, SEEK_END), t, "%d"); ft_assert(t, rc == BFile_Size(fugue_fd)); + check_offset(t, fd); DO_E(rc, lseek(fd, 8, SEEK_SET), t, "%d"); ft_assert(t, rc == 8); + check_offset(t, fd); /* Relative seeking */ DO_E(rc, lseek(fd, -6, SEEK_CUR), t, "%d"); ft_assert(t, rc == 2); + check_offset(t, fd); /* Write in the middle */ DO_E(rc, pwrite(fd, "@", 1, (off_t)3), t, "%d"); ft_assert(t, rc == 1); + check_offset(t, fd); DO_E(rc, lseek(fd, 0, SEEK_SET), t, "%d"); ft_assert(t, rc == 0); + check_offset(t, fd); memset(str, 0, sizeof str); DO_E(rc, read(fd, str, 24), t, "%d"); ft_assert(t, rc == 24 && !strcmp(str, "_ft@unistd_seek_methods!")); + check_offset(t, fd); DO_E(rc, close(fd), t, "%d"); ft_assert(t, rc == 0); @@ -311,7 +374,6 @@ static void _ft_unistd_folders_switch(ft_test *t) /* Create a directory */ DO_E(rc, mkdir("ft_test", 0755), t, "%d"); ft_assert(t, rc == 0); - ft_assert(t, rc == 0); DO_E(rc, mkdir("ft_test", 0755), t, "%d"); ft_assert(t, rc == -1 && errno == EEXIST); @@ -320,6 +382,7 @@ static void _ft_unistd_folders_switch(ft_test *t) ft_assert(t, fd >= 0); DO_E(rc, write(fd, "Some data\n", 10), t, "%d"); ft_assert(t, rc == 10); + check_offset(t, fd); DO_E(rc, close(fd), t, "%d"); ft_assert(t, rc == 0); @@ -338,6 +401,7 @@ static void _ft_unistd_folders_switch(ft_test *t) ft_assert(t, fd >= 0); DO_E(rc, write(fd, "Some data for the subfolder\n", 28), t, "%d"); ft_assert(t, rc == 28); + check_offset(t, fd); DO_E(rc, close(fd), t, "%d"); ft_assert(t, rc == 0); rc = BFile_Ext_Stat(u"\\\\fls0\\ft_test\\subfolder\\file.txt",