]> git.proxmox.com Git - mirror_lxcfs.git/blob - tests/test-read.c
Merge pull request #473 from brauner/2021-08-31.fixes
[mirror_lxcfs.git] / tests / test-read.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE
5 #endif
6
7 #ifndef FUSE_USE_VERSION
8 #define FUSE_USE_VERSION 26
9 #endif
10
11 #define _FILE_OFFSET_BITS 64
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #define BUFSIZE 1025
23 char buf[BUFSIZE];
24
25 int read_count = 2;
26
27 int main(int argc, char *argv[]){
28 if(argc < 3){
29 fprintf(stderr, "usage: %s <file> <count> [buffer|direct]\n", argv[0]);
30 exit(1);
31 }
32 char *file = argv[1];
33 read_count = atoi(argv[2]);
34 int ret = 0,sum = 0, i = 0, fd = -1;
35 if(argc == 4 && strncmp(argv[3], "direct",6) == 0)
36 fd = open(file, O_RDONLY|O_DIRECT);
37 else
38 fd = open(file, O_RDONLY);
39
40 while(i++ < read_count){
41 memset(buf, 0, BUFSIZE);
42 ret = read(fd, buf, BUFSIZE-1);
43 if(ret > 0){
44 write(STDOUT_FILENO, buf, ret);
45 sum += ret;
46 }else if(ret == 0){
47 printf("======read end======\n");
48 break;
49 }else{
50 printf("error:%d\n", errno);
51 break;
52 }
53 sleep(1);
54 }
55 printf("======read sum: %d======\n", sum);
56 close(fd);
57 return 0;
58 }