liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
 
/*
 *  FILE        : lftest.c
 *  DESCRIPTION : The purpose of this test is to verify the file size limitations of a filesystem.
 *                It writes one buffer at a time and lseeks from the beginning of the file to the
 *                end of the last write position.  The intent is to test lseek64.
 *  HISTORY:
 *           06/19/01  :  Written by Jeff Martin(martinjn@us.ibm.com) to test large files on jfs.
 *           07/12/01  :  Added timing.
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
 
/* set write buffer size to whatever floats your boat.  I usually use 1M */
#define BSIZE 1048576L
char buf[BSIZE];
 
int main(int argc, char *argv[])
{
   off_t i;
   long bufnum;
   off_t fd;
   time_t time1, time2;
   int writecnt = 0, seekcnt = 0, diff;
 
   time1 = time(NULL);
 
   if (argc != 2 || atoi(argv[1]) < 1) {
       printf("usage:<# of %ld buffers to write>\n", BSIZE);
       exit(3);
   }
   bufnum = strtol(argv[1], NULL, 0);
   printf("Started building a %lu megabyte file @ %s\n", bufnum,
          asctime(localtime(&time1)));
 
   buf[0] = 'A';
   for (i = 1; i < BSIZE; i++)
       buf[i] = '0';
   buf[BSIZE - 1] = 'Z';
 
   if ((fd = creat("large_file", 0755)) == -1)
       perror("lftest: ");
 
   for (i = 0; i < bufnum; i++) {
       if (write(fd, buf, BSIZE) == -1)
           return -1;
       else {
           printf(".");
           writecnt++;
           fflush(stdout);
       }
       fsync(fd);
       if (lseek(fd, (i + 1) * BSIZE, 0) == -1)
           return -1;
       else
           seekcnt++;
   }
   close(fd);
   time2 = time(NULL);
   printf("\nFinished building a %lu megabyte file @ %s\n", bufnum,
          asctime(localtime(&time2)));
   diff = time2 - time1;
   printf("Number of Writes: %d\n"
          "Number of Seeks: %d\n"
          "Total time for test to run: %d minute(s) and %d seconds\n",
          writecnt, seekcnt, diff / 60, diff % 60);
 
   return 0;
}