PKUOS - Pintos
Pintos source browser for PKU Operating System course
dir-rm-cwd.c
Go to the documentation of this file.
1/** Tries to remove the current directory, which may succeed or
2 fail. The requirements in each case are different; refer to
3 the assignment for details. */
4
5#include <syscall.h>
6#include "tests/lib.h"
7#include "tests/main.h"
8
9static int
10wrap_open (const char *name)
11{
12 static int fds[8], fd_cnt;
13 int fd, i;
14
15 CHECK ((fd = open (name)) > 1, "open \"%s\"", name);
16 for (i = 0; i < fd_cnt; i++)
17 if (fds[i] == fd)
18 fail ("fd returned is not unique");
19 fds[fd_cnt++] = fd;
20 return fd;
21}
22
23void
24test_main (void)
25{
26 int root_fd, a_fd0;
27 char name[READDIR_MAX_LEN + 1];
28
29 root_fd = wrap_open ("/");
30 CHECK (mkdir ("a"), "mkdir \"a\"");
31
32 a_fd0 = wrap_open ("/a");
33 CHECK (!readdir (a_fd0, name), "verify \"/a\" is empty");
34 CHECK (inumber (root_fd) != inumber (a_fd0),
35 "\"/\" and \"/a\" must have different inumbers");
36
37 CHECK (chdir ("a"), "chdir \"a\"");
38
39 msg ("try to remove \"/a\"");
40 if (remove ("/a"))
41 {
42 msg ("remove successful");
43
44 CHECK (open ("/a") == -1, "open \"/a\" (must fail)");
45 CHECK (open (".") == -1, "open \".\" (must fail)");
46 CHECK (open ("..") == -1, "open \"..\" (must fail)");
47 CHECK (!create ("x", 512), "create \"x\" (must fail)");
48 }
49 else
50 {
51 int a_fd1, a_fd2, a_fd3;
52
53 msg ("remove failed");
54
55 CHECK (!remove ("../a"), "try to remove \"../a\" (must fail)");
56 CHECK (!remove (".././a"), "try to remove \".././a\" (must fail)");
57 CHECK (!remove ("/./a"), "try to remove \"/./a\" (must fail)");
58
59 a_fd1 = wrap_open ("/a");
60 a_fd2 = wrap_open (".");
61 CHECK (inumber (a_fd1) == inumber (a_fd2),
62 "\"/a\" and \".\" must have same inumber");
63 CHECK (inumber (root_fd) != inumber (a_fd1),
64 "\"/\" and \"/a\" must have different inumbers");
65
66 CHECK (chdir ("/a"), "chdir \"/a\"");
67 a_fd3 = wrap_open (".");
68 CHECK (inumber (a_fd3) == inumber (a_fd1),
69 "\".\" must have same inumber as before");
70
71 CHECK (chdir ("/"), "chdir \"/\"");
72 CHECK (!remove ("a"), "try to remove \"a\" (must fail: still open)");
73 }
74 CHECK (!readdir (a_fd0, name), "verify \"/a\" is empty");
75}
void test_main(void)
tests/main.h
Definition: dir-rm-cwd.c:24
static int wrap_open(const char *name)
Tries to remove the current directory, which may succeed or fail.
Definition: dir-rm-cwd.c:10
char * name[]
Definition: insult.c:47
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
int open(const char *file)
Definition: syscall.c:103
bool remove(const char *file)
Definition: syscall.c:97
int inumber(int fd)
lib/user/syscall.h
Definition: syscall.c:181
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 fail(const char *format,...)
Definition: lib.c:40
void msg(const char *format,...)
Definition: lib.c:28
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29