⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sanity.c

📁 非常经典的一个分布式系统
💻 C
📖 第 1 页 / 共 4 页
字号:
        char buf[1024];        const int nfiles = 256;        char *prefix = "test14_filename_long_prefix_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA___";        struct dirent64 *ent;        int fd, i, rc, pos, index;        loff_t base = 0;        ENTRY(">1 block(4k) directory readdir");        snprintf(dir, MAX_PATH_LENGTH, "%s/test_t14_dir/", lustre_path);        rc = mkdir(dir, 0755);        if (rc < 0 && errno != EEXIST) {                printf("mkdir(%s) error: %s\n", dir, strerror(errno));                exit(1);        }        printf("Creating %d files...\n", nfiles);        for (i = 0; i < nfiles; i++) {                sprintf(path, "%s%s%05d", dir, prefix, i);                t_touch(path);        }        fd = t_opendir(dir);        printf("Listing...\n");        index = 0;        while ((rc = getdirentries64(fd, buf, 1024, &base)) > 0) {                pos = 0;                while (pos < rc) {                        char *item;                        ent = (struct dirent64 *) ((char*) buf + pos);                        item = (char *) ent->d_name;                        if (!strcmp(item, ".") || !strcmp(item, ".."))                                goto iter;                        if (strstr(item, prefix) != item) {                                printf("found bad name %s\n", item);                                return(-1);                        }                        printf("[%03d]: %s\n",                                index++, item + strlen(prefix));iter:                        pos += ent->d_reclen;                }        }        if (rc < 0) {                printf("getdents error %d\n", rc);                return(-1);        }        if (index != nfiles) {                printf("get %d files != %d\n", index, nfiles);                return(-1);        }        t_close(fd);        printf("Cleanup...\n");        for (i = 0; i < nfiles; i++) {                sprintf(path, "%s%s%05d", dir, prefix, i);                t_unlink(path);        }        t_rmdir(dir);        LEAVE();}int t15(char *name){        char file[MAX_PATH_LENGTH] = "";        int fd;        ENTRY("open-stat-close");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t15_file", lustre_path);        t_touch(file);        fd = t_open(file);        t_check_stat(file, NULL);        t_close(fd);        t_unlink(file);        LEAVE();}int t16(char *name){        char file[MAX_PATH_LENGTH] = "";        ENTRY("small-write-read");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t16_file", lustre_path);        t_echo_create(file, "aaaaaaaaaaaaaaaaaaaaaa");        t_grep(file, "aaaaaaaaaaaaaaaaaaaaaa");        t_unlink(file);        LEAVE();}int t17(char *name){        char file[MAX_PATH_LENGTH] = "";        int fd;        ENTRY("open-unlink without close");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t17_file", lustre_path);        fd = open(file, O_WRONLY | O_CREAT, 0666);        if (fd < 0) {                printf("failed to create file: %s\n", strerror(errno));                return(-1);        }        t_unlink(file);        LEAVE();}int t18(char *name){        char file[MAX_PATH_LENGTH] = "";        char buf[128];        int fd, i;        struct stat statbuf[3];        ENTRY("write should change mtime/ctime");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t18_file", lustre_path);        for (i = 0; i < 3; i++) {                fd = open(file, O_RDWR|O_CREAT|O_APPEND, (mode_t)0666);                if (fd < 0) {                        printf("error open file: %s\n", strerror(errno));                        return(-1);                }                if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {                        printf("error write file\n");                        return(-1);                }                close(fd);                if(stat(file, &statbuf[i]) != 0) {                        printf("Error stat\n");                        return(1);                }                printf("ctime %lu, mtime %lu\n",                        statbuf[i].st_ctime, statbuf[i].st_mtime);                sleep(2);        }        for (i = 1; i < 3; i++) {                if ((statbuf[i].st_ctime <= statbuf[i-1].st_ctime) ||                    (statbuf[i].st_mtime <= statbuf[i-1].st_mtime)) {                        printf("time error\n");                        return(-1);                }        }        t_unlink(file);        LEAVE();}int t18b(char *name){        char file[MAX_PATH_LENGTH] = "";        int i;        struct stat statbuf[3];        ENTRY("utime should change mtime/atime/ctime");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t18b_file", lustre_path);        t_touch(file);        for (i = 0; i < 3; i++) {                t_utime(file, NULL);                if(stat(file, &statbuf[i]) != 0) {                        printf("Error stat\n");                        return(1);                }                printf("atime %lu, mtime %lu, ctime %lu\n",                       statbuf[i].st_atime, statbuf[i].st_mtime,                       statbuf[i].st_ctime);                sleep(2);        }        for (i = 1; i < 3; i++) {                if ((statbuf[i].st_atime <= statbuf[i-1].st_atime) ||                    (statbuf[i].st_mtime <= statbuf[i-1].st_mtime) ||                    (statbuf[i].st_ctime <= statbuf[i-1].st_ctime)) {                        printf("time error\n");                        return(-1);                }        }        t_unlink(file);        LEAVE();}static int check_file_size(char *file, off_t size){        struct stat statbuf;        if (stat(file, &statbuf) != 0) {                printf("Error stat(%s)\n", file);                return(1);        }        if (statbuf.st_size != size) {                printf("size of %s: %ld != %lld\n", file, statbuf.st_size, (unsigned long long )size);                return(-1);        }        return 0;}int t19(char *name){        char file[MAX_PATH_LENGTH] = "";        int fd;        int result;        ENTRY("open(O_TRUNC) should truncate file to 0-length");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t19_file", lustre_path);        t_echo_create(file, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");        fd = open(file, O_RDWR|O_CREAT|O_TRUNC, (mode_t)0666);        if (fd < 0) {                printf("error open file: %s\n", strerror(errno));                return(-1);        }        close(fd);        result = check_file_size(file, 0);        if (result != 0)                return result;        t_unlink(file);        LEAVE();}int t20(char *name){        char file[MAX_PATH_LENGTH] = "";        int fd;        struct iovec iov[2];        char buf[100];        ssize_t ret;        ENTRY("trap app's general bad pointer for file i/o");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t20_file", lustre_path);        fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);        if (fd < 0) {                printf("error open file: %s\n", strerror(errno));                return(-1);        }        ret = write(fd, NULL, 20);        if (ret != -1 || errno != EFAULT) {                printf("write 1: ret %ld, errno %d\n", ret, errno);                return(1);        }        ret = write(fd, (void *)-1, 20);        if (ret != -1 || errno != EFAULT) {                printf("write 2: ret %ld, errno %d\n", ret, errno);                return(1);        }        iov[0].iov_base = NULL;        iov[0].iov_len = 10;        iov[1].iov_base = (void *)-1;        iov[1].iov_len = 10;        ret = writev(fd, iov, 2);        if (ret != -1 || errno != EFAULT) {                printf("writev 1: ret %ld, errno %d\n", ret, errno);                return(1);        }        iov[0].iov_base = NULL;        iov[0].iov_len = 0;        iov[1].iov_base = buf;        iov[1].iov_len = sizeof(buf);        ret = writev(fd, iov, 2);        if (ret != sizeof(buf)) {                printf("write 3 ret %ld, error %d\n", ret, errno);                return(1);        }        lseek(fd, 0, SEEK_SET);        ret = read(fd, NULL, 20);        if (ret != -1 || errno != EFAULT) {                printf("read 1: ret %ld, errno %d\n", ret, errno);                return(1);        }        ret = read(fd, (void *)-1, 20);        if (ret != -1 || errno != EFAULT) {                printf("read 2: ret %ld, errno %d\n", ret, errno);                return(1);        }        iov[0].iov_base = NULL;        iov[0].iov_len = 10;        iov[1].iov_base = (void *)-1;        iov[1].iov_len = 10;        ret = readv(fd, iov, 2);        if (ret != -1 || errno != EFAULT) {                printf("readv 1: ret %ld, errno %d\n", ret, errno);                return(1);        }        iov[0].iov_base = NULL;        iov[0].iov_len = 0;        iov[1].iov_base = buf;        iov[1].iov_len = sizeof(buf);        ret = readv(fd, iov, 2);        if (ret != sizeof(buf)) {                printf("read 3 ret %ld, error %d\n", ret, errno);                return(1);        }        close(fd);        t_unlink(file);        LEAVE();}int t21(char *name){        char file[MAX_PATH_LENGTH] = "";        int fd, ret;        struct flock lock = {                .l_type = F_RDLCK,                .l_whence = SEEK_SET,        };        ENTRY("basic fcntl support");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t21_file", lustre_path);        fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);        if (fd < 0) {                printf("error open file: %s\n", file);                return(-1);        }        t_fcntl(fd, F_SETFL, O_APPEND);        if (!(ret = t_fcntl(fd, F_GETFL)) & O_APPEND) {                printf("error get flag: ret %x\n", ret);                return(-1);        }        t_fcntl(fd, F_SETLK, &lock);        t_fcntl(fd, F_GETLK, &lock);        lock.l_type = F_WRLCK;        t_fcntl(fd, F_SETLKW, &lock);        t_fcntl(fd, F_GETLK, &lock);        lock.l_type = F_UNLCK;        t_fcntl(fd, F_SETLK, &lock);        close(fd);        t_unlink(file);        LEAVE();}int t22(char *name){        char file[MAX_PATH_LENGTH] = "";        int fd;        char *str = "1234567890";        char buf[100];        ssize_t ret;        ENTRY("make sure O_APPEND take effect");        snprintf(file, MAX_PATH_LENGTH, "%s/test_t22_file", lustre_path);        fd = open(file, O_RDWR|O_CREAT|O_APPEND, (mode_t)0666);        if (fd < 0) {                printf("error open file: %s\n", strerror(errno));                return(-1);        }        lseek(fd, 100, SEEK_SET);        ret = write(fd, str, strlen(str));        if (ret != strlen(str)) {                printf("write 1: ret %ld, errno %d\n", ret, errno);                return(1);        }        lseek(fd, 0, SEEK_SET);        ret = read(fd, buf, sizeof(buf));        if (ret != strlen(str)) {                printf("read 1 got %ld\n", ret);                return(1);        }        if (memcmp(buf, str, strlen(str))) {                printf("read 1 data err\n");                return(1);        }        if (fcntl(fd, F_SETFL, 0)) {                printf("fcntl err: %s\n", strerror(errno));                return(1);        }        lseek(fd, 100, SEEK_SET);        ret = write(fd, str, strlen(str));        if (ret != strlen(str)) {                printf("write 2: ret %ld, errno %d\n", ret, errno);                return(1);        }        lseek(fd, 100, SEEK_SET);        ret = read(fd, buf, sizeof(buf));        if (ret != strlen(str)) {                printf("read 2 got %ld\n", ret);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -