Created
October 3, 2022 04:57
-
-
Save ddk50/a5bde333d907458f883d1d8dfe0296cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <dirent.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <sys/stat.h> | |
int exist_in_dir(const char *path, const char *findname); | |
int get_file_stat(const char *path); | |
int get_file_stat_by_fd(int fildes); | |
int write_and_read(int file, const char *buf); | |
int exist_in_dir(const char *path, const char *findname) | |
{ | |
DIR *d; | |
struct dirent *dir; | |
d = opendir(path); | |
if (d) { | |
while ((dir = readdir(d)) != NULL) { | |
if (strcmp(dir->d_name, findname) == 0) { | |
return 1; | |
} | |
} | |
closedir(d); | |
} | |
return -1; | |
} | |
int get_file_stat(const char *path) | |
{ | |
struct stat stat_buf; | |
if (stat(path, &stat_buf) == 0) { | |
printf("get_file_stat: %lld\n", stat_buf.st_size); | |
} | |
return 0; | |
} | |
int get_file_stat_by_fd(int fildes) | |
{ | |
struct stat stat_buf; | |
if (fstat(fildes, &stat_buf) == 0) { | |
printf("st_size: %lld\n" | |
"st_nlink: %d\n", | |
stat_buf.st_size, | |
stat_buf.st_nlink); | |
} | |
return 0; | |
} | |
int write_and_read(int file, const char *buf) | |
{ | |
char *read_buf; | |
int ret; | |
ret = ftruncate(file, 0); | |
if (ret == -1) { | |
goto ret_error; | |
} | |
ret = lseek(file, 0, SEEK_SET); | |
if (ret == -1) { | |
goto free_and_ret_error; | |
} | |
ret = write(file, buf, strlen(buf) + 1); | |
if (ret < 0) { | |
goto ret_error; | |
} | |
read_buf = malloc(strlen(buf) + 1); | |
if (read_buf == NULL) { | |
goto ret_error; | |
} | |
memset(read_buf, '\0', strlen(buf) + 1); | |
ret = lseek(file, 0, SEEK_SET); | |
if (ret == -1) { | |
goto free_and_ret_error; | |
} | |
ret = read(file, read_buf, strlen(buf)); | |
if (ret < 0) { | |
goto free_and_ret_error; | |
} | |
if (strcmp(read_buf, buf) == 0) { | |
free(read_buf); | |
return 0; | |
} | |
free_and_ret_error: | |
free(read_buf); | |
ret_error: | |
return -1; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
static const char buf[] = "fenauig78raiufhiuea"; | |
static char *read_buf; | |
char *target_path = argv[1]; | |
int file; | |
int ret; | |
printf("delete benchmark\n"); | |
if (argc < 2) { | |
printf("lack of target path\n"); | |
exit(EXIT_FAILURE); | |
} | |
printf("target %s\n", target_path); | |
if ((file = open(target_path, O_RDWR)) < 0) { | |
perror("open\n"); | |
exit(EXIT_FAILURE); | |
} | |
ftruncate(file, 0); | |
ret = lseek(file, 0, SEEK_SET); | |
printf("seek: %d\n", ret); | |
ret = write(file, buf, sizeof(buf) - 1); | |
if (ret < 0) { | |
perror("write\n"); | |
exit(EXIT_FAILURE); | |
} | |
printf("wrote: %d\n", ret); | |
ret = lseek(file, 0, SEEK_SET); | |
printf("seek: %d\n", ret); | |
read_buf = malloc(sizeof(buf) + 1); | |
if (read_buf == NULL) { | |
perror("readbuf malloc\n"); | |
exit(EXIT_FAILURE); | |
} | |
memset(read_buf, '\0', sizeof(buf) + 1); | |
if (exist_in_dir(".", target_path) == 1) { | |
printf("EXIST! (%s)\n", target_path); | |
} else { | |
printf("NO EXIST! (%s)\n", target_path); | |
exit(EXIT_FAILURE); | |
} | |
get_file_stat_by_fd(file); | |
/* 削除したあとどうなるのか? */ | |
/* unlink */ | |
ret = unlink(target_path); | |
if (ret < 0) { | |
perror("unlink"); | |
exit(EXIT_FAILURE); | |
} | |
printf("unlink: %s\n", target_path); | |
get_file_stat_by_fd(file); | |
if (exist_in_dir(".", target_path) == 1) { | |
printf("EXIST! (%s)\n", target_path); | |
} else { | |
printf("NO EXIST! (%s)\n", target_path); | |
} | |
ret = read(file, read_buf, sizeof(buf) - 1); | |
if (ret < 0) { | |
perror("read\n"); | |
exit(EXIT_FAILURE); | |
} | |
if (memcmp(read_buf, buf, sizeof(buf) - 1) == 0) { | |
printf("COMP OK\n"); | |
} else { | |
printf("COMP ERROR\n"); | |
exit(EXIT_FAILURE); | |
} | |
if (write_and_read(file, "THISISIT!!") == 0) { | |
printf("COMP OK\n"); | |
} else { | |
perror("1. write_and_read\n"); | |
printf("COMP ERROR\n"); | |
} | |
if (write_and_read(file, "HOOOOOBAR") == 0) { | |
printf("COMP OK\n"); | |
} else { | |
perror("2. write_and_read\n"); | |
printf("COMP ERROR\n"); | |
} | |
get_file_stat(target_path); | |
for (;;) { | |
sleep(1); | |
} | |
close(file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
二重OPENしたりしてその時nlinkの値がどうなってるのかのテスト