aboutsummaryrefslogtreecommitdiff
path: root/user/usr/bin/tests/eatinodes.c
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /user/usr/bin/tests/eatinodes.c
Created student weenix repository
Diffstat (limited to 'user/usr/bin/tests/eatinodes.c')
-rw-r--r--user/usr/bin/tests/eatinodes.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/user/usr/bin/tests/eatinodes.c b/user/usr/bin/tests/eatinodes.c
new file mode 100644
index 0000000..7e6f8a1
--- /dev/null
+++ b/user/usr/bin/tests/eatinodes.c
@@ -0,0 +1,90 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static char root_dir[64];
+
+static void eatinodes_start(void)
+{
+ int err;
+
+ root_dir[0] = '\0';
+ do
+ {
+ snprintf(root_dir, sizeof(root_dir), "eatinodes-%d", rand());
+ err = mkdir(root_dir, 0777);
+ if (err && errno != EEXIST)
+ {
+ printf("Failed to make test root directory: %s\n", strerror(errno));
+ exit(errno);
+ }
+ } while (err != 0);
+ printf("Created test root directory: ./%s\n", root_dir);
+
+ err = chdir(root_dir);
+ if (err < 0)
+ {
+ printf("Could not cd into test directory\n");
+ exit(1);
+ }
+}
+
+static void eatinodes(void)
+{
+ int i;
+ int fd;
+ int err = 0;
+ char fname[32];
+
+ for (i = 0; !err; ++i)
+ {
+ snprintf(fname, sizeof(fname), "test-%d", i);
+ fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0666);
+ if (fd < 0)
+ {
+ printf("Could not open file %d: %s\n", i, strerror(errno));
+ break;
+ }
+ err = close(fd);
+ if (err < 0)
+ {
+ printf("Could not close fd %d: %s\n", fd, strerror(errno));
+ break;
+ }
+ printf("Created %d files\n", i);
+ }
+ int j;
+ printf("Cleaning up...\n");
+ for (j = 0; j < i; ++j)
+ {
+ snprintf(fname, sizeof(fname), "test-%d", j);
+ err = unlink(fname);
+ if (err < 0)
+ {
+ printf("Could not remove file %d: %s\n", j, strerror(errno));
+ }
+ }
+}
+
+static void eatinodes_end(void)
+{
+ chdir("..");
+ int err = rmdir(root_dir);
+ if (err < 0)
+ {
+ printf("Could not remove test directory: %s\n", strerror(errno));
+ }
+}
+
+int main(int argc, char **argv)
+{
+ eatinodes_start();
+ eatinodes();
+ eatinodes_end();
+
+ return 0;
+}