e4b890464a67402c3f69230fae62e6045c2ff2bf
[pintos-anon] / grading / filesys / grow-two-files.c
1 #include <random.h>
2 #include <syscall.h>
3 #include "fslib.h"
4
5 const char test_name[] = "grow-two-files";
6
7 #define FILE_SIZE 8143
8 static char buf_a[FILE_SIZE];
9 static char buf_b[FILE_SIZE];
10
11 static void
12 write_some_bytes (const char *filename, int fd, const char *buf, size_t *ofs) 
13 {
14   if (*ofs < FILE_SIZE) 
15     {
16       size_t block_size = random_ulong () % (FILE_SIZE / 8) + 1;
17       if (block_size > FILE_SIZE - *ofs)
18         block_size = FILE_SIZE - *ofs;
19   
20       if (write (fd, buf + *ofs, block_size) <= 0)
21         fail ("write %zu bytes at offset %zu in \"%s\"",
22               block_size, *ofs, filename);
23       *ofs += block_size;
24     }
25 }
26
27 void
28 test_main (void) 
29 {
30   int fd_a, fd_b;
31   size_t ofs_a, ofs_b;
32
33   random_init (0);
34   random_bytes (buf_a, sizeof buf_a);
35   random_bytes (buf_b, sizeof buf_b);
36
37   CHECK (create ("a", 0), "create \"a\"");
38   CHECK (create ("b", 0), "create \"b\"");
39
40   CHECK ((fd_a = open ("a")) > 1, "open \"a\"");
41   CHECK ((fd_b = open ("b")) > 1, "open \"b\"");
42
43   msg ("write \"a\" and \"b\" alternately");
44   while (ofs_a < FILE_SIZE || ofs_b < FILE_SIZE) 
45     {
46       write_some_bytes ("a", fd_a, buf_a, &ofs_a);
47       write_some_bytes ("b", fd_b, buf_b, &ofs_b);
48     }
49
50   msg ("close \"a\"");
51   close (fd_a);
52
53   msg ("close \"b\"");
54   close (fd_b);
55
56   check_file ("a", buf_a, FILE_SIZE);
57   check_file ("b", buf_b, FILE_SIZE);
58 }