add test_file_write_hole.c

pull/37/head
YuQing 2020-04-24 22:15:41 +08:00
parent 688fcf4b74
commit 4011fcb39e
4 changed files with 88 additions and 2 deletions

View File

@ -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

View File

@ -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:

View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/time.h>
#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 <filename>\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;
}

View File

@ -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))