chkdwfile.c

来自「这是一个针对大数据的反复多线程读写文件系统的测试代码。」· C语言 代码 · 共 70 行

C
70
字号
/* Companion to some test programs... Verify that a file contains a certain   progression: the file it expected to contain a sequence of 8byte values,   each 8byte having the current file offset encoded in its 48LSbs. */#define _GNU_SOURCE#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <unistd.h>int main(int argc, char *argv[]){  int fd, i, r, err;  unsigned long long buf[2048];  unsigned long long pos;  if(argc != 2) {    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);    exit(1);  }  if((fd = open(argv[1], O_RDONLY)) <0) {    perror("open"); exit(1);  }  err = 0;  pos = 0LL;  while(1) {    int rem;    if((r = read(fd, buf, sizeof(buf))) <0) {      perror("read"); exit(1);    }else if(r==0)      break;    rem = (r % sizeof(unsigned long long));    r /= sizeof(unsigned long long);    for(i=0; i<r; i++) {      if((buf[i] & 0x0000FFFFFFFFFFFF) != pos) {	printf("Error: offset %12llu, "		"data 0x%.16llx expected 0x%.16llx\n",		pos, buf[i], pos);	err = 1;	goto out;      }		      pos += sizeof(unsigned long long);    }    if(rem) {      fprintf(stderr,"Extra %d martian bytes\n", rem);      err = 1;      pos += rem;      if((r = read(fd, buf, sizeof(buf))) <0) {	perror("read"); exit(1);      }else if(r!=0)	fprintf(stderr,"And file wasn't finished???\n");      break;    }  }  printf("File size %llu\n", pos); out:  close(fd);  if(err) {    fprintf(stderr, "Error!!\n");    return 99;  }else fprintf(stderr, "OK\n");  return 0;}

⌨️ 快捷键说明

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