aboutsummaryrefslogtreecommitdiff
path: root/user/usr/bin/tests/eatinodes.c
blob: 7e6f8a1cf012decbffc6b680d45a2d8352523a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
}