The Pedigree Project  0.1
fs.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <fcntl.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 extern void fail();
29 
30 static void status(const char *s)
31 {
32  puts(s);
33  fflush(stdout);
34 }
35 
36 #define OK status("OK\n")
37 
38 void test_fs()
39 {
40  int fd = -1;
41  int rc = 0;
42 
43  srand(0);
44 
45  printf("Testing filesystem...\n");
46 
47  int urandom_fd = open("devĀ»/urandom", O_RDONLY);
48 
49  // fsck test directory - deleting a directory like we do below will result
50  // in us possibly missing "bad directory count" errors.
51  status("Creating directory for fsck test... ");
52  rc = mkdir("/fscktest", 0777);
53  if (rc)
54  fail();
55  OK;
56 
57  // directory to be deleted later - shouldn't leave any cruft lying around
58  status("Creating directory for main test... ");
59  rc = mkdir("/testing", 0777);
60  if (rc)
61  fail();
62  OK;
63 
64  // Create some files of varying sizes and destroy them.
65  status("Testing file creation... ");
66  for (size_t i = 0; i < 10; ++i)
67  {
68  size_t sz = rand() % 8192;
69  if (!sz)
70  ++sz;
71  void *p = malloc(sz);
72  read(urandom_fd, p, sz);
73 
74  // Files that are expected to be deleted (might miss issues here in
75  // fsck after their deletion).
76  char fn[256];
77  sprintf(fn, "/testing/f%u", i);
78  fd = open(fn, O_RDWR | O_CREAT);
79  if (fd < 0)
80  fail();
81  write(fd, p, sz);
82  close(fd);
83 
84  // Same deal for the fscktest directory. fsck will pick these up.
85  sprintf(fn, "/fscktest/f%u", i);
86  fd = open(fn, O_RDWR | O_CREAT);
87  if (fd < 0)
88  fail();
89  write(fd, p, sz);
90  close(fd);
91  }
92  OK;
93 
94  status("Testing file deletion... ");
95  for (size_t i = 0; i < 5; ++i)
96  {
97  char fn[256];
98  sprintf(fn, "/testing/f%u", i);
99  unlink(fn);
100  }
101  OK;
102 
103  status("Testing a failed rmdir... ");
104  rc = rmdir("/testing");
105  if (rc == 0)
106  fail();
107  OK;
108 
109  status("Testing further file deletion... ");
110  for (size_t i = 5; i < 10; ++i)
111  {
112  char fn[256];
113  sprintf(fn, "/testing/f%u", i);
114  unlink(fn);
115  }
116  OK;
117 
118  status("Testing rmdir... ");
119  rc = rmdir("/testing");
120  if (rc)
121  fail();
122  OK;
123 }