diff --git a/HISTORY b/HISTORY index d9f91bd..f8fa1e1 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-04-22 +Version 1.44 2020-04-24 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex diff --git a/src/tests/Makefile b/src/tests/Makefile index 6e6adc3..5603b50 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -8,7 +8,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo test_id_generator test_ini_parser test_char_convert test_char_convert_loader \ test_logger test_skiplist_set test_crc32 test_thourands_seperator test_sched_thread \ test_json_parser test_pthread_lock test_uniq_skiplist test_split_string \ - test_server_id_func test_pipe test_atomic + test_server_id_func test_pipe test_atomic test_file_write_hole all: $(ALL_PRGS) .c: diff --git a/src/tests/test_file_write_hole.c b/src/tests/test_file_write_hole.c new file mode 100644 index 0000000..0c508a3 --- /dev/null +++ b/src/tests/test_file_write_hole.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "fastcommon/logger.h" +#include "fastcommon/shared_func.h" + +int main(int argc, char *argv[]) +{ +#define SEEK_POS (2 * 1024) + char *filename; + int fd; + int result; + int n; + char buf[1024]; + + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + log_init(); + filename = argv[1]; + if (access(filename, F_OK) == 0) { + logError("file: "__FILE__", line: %d, " + "file %s already exists", __LINE__, filename); + return EEXIST; + } + + fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644); + if (fd < 0) { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " \ + "open file %s fail, " \ + "errno: %d, error info: %s", \ + __LINE__, filename, \ + result, STRERROR(result)); + return result; + } + + if (lseek(fd, SEEK_POS, SEEK_SET) < 0) { + logError("file: "__FILE__", line: %d, " \ + "lseek file %s fail, " \ + "errno: %d, error info: %s", __LINE__, \ + filename, errno, STRERROR(errno)); + return errno != 0 ? errno : EIO; + } + + memset(buf, '\n', sizeof(buf)); + if ((n=write(fd, buf, sizeof(buf))) <= 0) { + logError("file: "__FILE__", line: %d, " \ + "write to file %s fail, " \ + "errno: %d, error info: %s", __LINE__, \ + filename, errno, STRERROR(errno)); + return errno != 0 ? errno : EIO; + } + printf("write bytes: %d\n", n); + + if (lseek(fd, 0, SEEK_SET) < 0) { + logError("file: "__FILE__", line: %d, " \ + "lseek file %s fail, " \ + "errno: %d, error info: %s", __LINE__, \ + filename, errno, STRERROR(errno)); + return errno != 0 ? errno : EIO; + } + + if ((n=read(fd, buf, sizeof(buf))) <= 0) { + logError("file: "__FILE__", line: %d, " \ + "read from file %s fail, " \ + "errno: %d, error info: %s", __LINE__, \ + filename, errno, STRERROR(errno)); + return errno != 0 ? errno : EIO; + } + + printf("read bytes: %d, 0 => 0x%02x, %d => 0x%02x\n", + n, (unsigned char)buf[0], n - 1, (unsigned char)buf[n - 1]); + return 0; +} diff --git a/src/uniq_skiplist.h b/src/uniq_skiplist.h index 83550ad..35980d9 100644 --- a/src/uniq_skiplist.h +++ b/src/uniq_skiplist.h @@ -157,6 +157,9 @@ static inline bool uniq_skiplist_empty(UniqSkiplist *sl) #define LEVEL0_DOUBLE_CHAIN_PREV_LINK(node) node->links[node->level_index + 1] #define LEVEL0_DOUBLE_CHAIN_TAIL(sl) LEVEL0_DOUBLE_CHAIN_PREV_LINK(sl->top) +#define UNIQ_SKIPLIST_LEVEL0_TAIL_NODE(sl) ((UniqSkiplistNode *) \ + LEVEL0_DOUBLE_CHAIN_TAIL(sl)) + #define UNIQ_SKIPLIST_LEVEL0_PREV_NODE(node) ((UniqSkiplistNode *) \ LEVEL0_DOUBLE_CHAIN_PREV_LINK(node))