PKUOS - Pintos
Pintos source browser for PKU Operating System course
dir-vine.c
Go to the documentation of this file.
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
16void
17test_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}
void test_main(void)
Create a very deep "vine" of directories: /dir0/dir1/dir2/... and an ordinary file in each of them,...
Definition: dir-vine.c:17
char * name[]
Definition: insult.c:47
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
bool readdir(int fd, char name[READDIR_MAX_LEN+1])
Definition: syscall.c:169
bool chdir(const char *dir)
Project 4 only.
Definition: syscall.c:157
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
bool remove(const char *file)
Definition: syscall.c:97
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
bool mkdir(const char *dir)
Definition: syscall.c:163
#define READDIR_MAX_LEN
Maximum characters in a filename written by readdir().
Definition: syscall.h:16
void msg(const char *format,...)
Definition: lib.c:28
bool quiet
Definition: lib.c:9
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
size_t strlen(const char *string)
Returns the length of STRING.
Definition: string.c:293
int strcmp(const char *a_, const char *b_)
Finds the first differing characters in strings A and B.
Definition: string.c:73
static const char file_name[]
tests/filesys/base/syn-read.h
Definition: syn-read.h:5