File: makesparse.c

package info (click to toggle)
afio 2.5-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 616 kB
  • ctags: 475
  • sloc: ansic: 4,678; sh: 535; makefile: 127; awk: 19
file content (56 lines) | stat: -rw-r--r-- 1,110 bytes parent folder | download | duplicates (3)
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

/* test large file compile environment and make a large sparse file
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>


void fatal(const char *s)
{
  printf("makesparse: %s\n",s);
  exit(1);
}


int main(int argc, char ** argv)
{
  struct stat s;
  int f;
  unsigned long long pos;
  char *bla="bla bla bla\n";

  printf("sizeof st_size = %d\n",sizeof(s.st_size));

  if(sizeof(s.st_size)<8) fatal("Large file compile environment not present!");

  if(argc!=2) fatal("Wrong number of args");

  f=open(argv[1],O_RDWR);

  if(!f) fatal("Open failed");

  pos=(unsigned long long)(4LL*1024LL*1024LL*1024LL);

  if(lseek(f,(off_t)pos,SEEK_SET)==((off_t)-1)) 
    { perror("lseek"); fatal("lseek failed"); }

  if(!write(f,bla,strlen(bla))) { perror("write"); fatal("write failed"); }

  close(f);

  if(lstat(argv[1],&s)) { perror("stat"); fatal("stat failed"); }

  if(s.st_size!=pos+strlen(bla)) 
    fatal("created file does not have the expected length");

  return 0;
}