summaryrefslogtreecommitdiffstats
path: root/tests/basic/quota.c
blob: 50f56d6d7181f9533bf728ff64bea58e972f4f27 (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
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

ssize_t
nwrite (int fd, const void *buf, size_t count)
{
        ssize_t  ret        = 0;
        ssize_t  written    = 0;

        for (written = 0; written != count; written += ret) {
                ret = write (fd, buf + written, count - written);
                if (ret < 0) {
                        if (errno == EINTR)
                                ret = 0;
                        else
                                goto out;
                }
        }

        ret = written;
out:
        return ret;
}

int
file_write (char *filename, int bs, int count)
{
        int  fd              = 0;
        int  ret             = -1;
        int  i               = 0;
        char *buf            = NULL;

        bs = bs * 1024;

        buf = (char *) malloc (bs);
        if (buf == NULL)
                goto out;

        memset (buf, 0, bs);

        fd = open (filename, O_RDWR|O_CREAT|O_SYNC, 0600);
        while (i < count) {
                ret = nwrite(fd, buf, bs);
                if (ret == -1) {
                        close (fd);
                        goto out;
                }
                i++;
        }

        ret = fdatasync(fd);
        if (ret) {
                close (fd);
                goto out;
        }

        ret = close(fd);
        if (ret)
                goto out;

        ret = 0;

out:
        if (buf)
                free (buf);
        return ret;
}

int
main (int argc, char **argv)
{
        if (argc != 4) {
                printf("Usage: %s <filename> <block size in k> <count>\n",
                        argv[0]);
                return EXIT_FAILURE;
        }

        if (file_write (argv[1], atoi(argv[2]), atoi(argv[3])) < 0) {
                perror ("write failed");
                return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
}