random: Fix behavior of kernel option "-rs".
[pintos-anon] / src / tests / filesys / extended / dir-vine.c
1 /* Create a very deep "vine" of directories: /dir0/dir1/dir2/...
2    and an ordinary file in each of them, until we fill up the
3    disk.
4    
5    Then delete most of them, for two reasons.  First, "tar"
6    limits file names to 100 characters (which could be extended
7    to 256 without much trouble).  Second, a full disk has no room
8    for the tar archive. */
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <syscall.h>
13 #include "tests/lib.h"
14 #include "tests/main.h"
15
16 void
17 test_main (void) 
18 {
19   int i;
20
21   msg ("creating many levels of files and directories...");
22   quiet = true;
23   CHECK (mkdir ("start"), "mkdir \"start\"");
24   CHECK (chdir ("start"), "chdir \"start\"");
25   for (i = 0; ; i++) 
26     {
27       char name[3][READDIR_MAX_LEN + 1];
28       char file_name[16], dir_name[16];
29       char contents[128];
30       int fd;
31
32       /* Create file. */
33       snprintf (file_name, sizeof file_name, "file%d", i);
34       if (!create (file_name, 0))
35         break;
36       CHECK ((fd = open (file_name)) > 1, "open \"%s\"", file_name);
37       snprintf (contents, sizeof contents, "contents %d\n", i);
38       if (write (fd, contents, strlen (contents)) != (int) strlen (contents)) 
39         {
40           CHECK (remove (file_name), "remove \"%s\"", file_name);
41           close (fd);
42           break;
43         }
44       close (fd);
45       
46       /* Create directory. */
47       snprintf (dir_name, sizeof dir_name, "dir%d", i);
48       if (!mkdir (dir_name)) 
49         {
50           CHECK (remove (file_name), "remove \"%s\"", file_name);
51           break; 
52         }
53
54       /* Check for file and directory. */
55       CHECK ((fd = open (".")) > 1, "open \".\"");
56       CHECK (readdir (fd, name[0]), "readdir \".\"");
57       CHECK (readdir (fd, name[1]), "readdir \".\"");
58       CHECK (!readdir (fd, name[2]), "readdir \".\" (should fail)");
59       CHECK ((!strcmp (name[0], dir_name) && !strcmp (name[1], file_name))
60              || (!strcmp (name[1], dir_name) && !strcmp (name[0], file_name)),
61              "names should be \"%s\" and \"%s\", "
62              "actually \"%s\" and \"%s\"",
63              file_name, dir_name, name[0], name[1]);
64       close (fd);
65
66       /* Descend into directory. */
67       CHECK (chdir (dir_name), "chdir \"%s\"", dir_name);
68     }
69   CHECK (i > 200, "created files and directories only to level %d", i);
70   quiet = false;
71
72   msg ("removing all but top 10 levels of files and directories...");
73   quiet = true;
74   while (i-- > 10) 
75     {
76       char file_name[16], dir_name[16];
77
78       snprintf (file_name, sizeof file_name, "file%d", i);
79       snprintf (dir_name, sizeof dir_name, "dir%d", i);
80       CHECK (chdir (".."), "chdir \"..\"");
81       CHECK (remove (dir_name), "remove \"%s\"", dir_name);
82       CHECK (remove (file_name), "remove \"%s\"", file_name);
83     }
84   quiet = false;
85 }