summaryrefslogtreecommitdiffstats
path: root/c_pgms/truncate_write.c
blob: d6dcce90800f019c866835bf4abb0a7fbd3a2741 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define UNIT_KB 1024ULL
#define UNIT_MB UNIT_KB*1024ULL
#define UNIT_GB UNIT_MB*1024ULL
#define UNIT_TB UNIT_GB*1024ULL
#define UNIT_PB UNIT_TB*1024ULL

#define UNIT_KB_STRING    "KB"
#define UNIT_MB_STRING    "MB"
#define UNIT_GB_STRING    "GB"
#define UNIT_TB_STRING    "TB"
#define UNIT_PB_STRING    "PB"

int
string2bytesize (const char *str, unsigned long long *n)
{
        unsigned long long value = 0ULL;
        char *tail = NULL;
        int old_errno = 0;
        const char *s = NULL;

        if (str == NULL || n == NULL)
        {
                errno = EINVAL;
                return -1;
        }

        for (s = str; *s != '\0'; s++)
        {
                if (isspace (*s))
                {
                        continue;
                }
                if (*s == '-')
                {
                        return -1;
                }
                break;
        }

        old_errno = errno;
        errno = 0;
        value = strtoull (str, &tail, 10);

        if (errno == ERANGE || errno == EINVAL)
        {
                return -1;
        }

        if (errno == 0)
        {
                errno = old_errno;
        }

        if (tail[0] != '\0')
        {
                if (strcasecmp (tail, UNIT_KB_STRING) == 0)
                {
                        value *= UNIT_KB;
                }
                else if (strcasecmp (tail, UNIT_MB_STRING) == 0)
                {
                        value *= UNIT_MB;
                }
                else if (strcasecmp (tail, UNIT_GB_STRING) == 0)
                {
                        value *= UNIT_GB;
                }
                else if (strcasecmp (tail, UNIT_TB_STRING) == 0)
                {
                        value *= UNIT_TB;
                }
                else if (strcasecmp (tail, UNIT_PB_STRING) == 0)
                {
                        value *= UNIT_PB;
                }

                else
                {
                        return -1;
                }
        }

        *n = value;

        return 0;
}

int main (int argc, char *argv[])
{
        int ret = -1;
        int fd  = -1;
        char  *buf = "This is a string";
        char  *filename = NULL;
        unsigned long long size = 0;

        if (argc < 2) {
                fprintf (stderr, "Usage:./a.out <filename>\n");
                return 2;
        }

        filename = argv[1];

        fd = open (filename, O_CREAT|O_RDWR, 0644);
        if (fd == -1) {
                fprintf (stderr, "OPEN: cannot open the file fff. (%s)",
                         strerror (errno));
                return 2;
        }

	if (argc < 3)
	        size = 5 * UNIT_GB;
	else
	        string2bytesize (argv[2], &size);

        ret = ftruncate (fd, size);
        if (ret == -1) {
                fprintf (stderr, "TRUNCATE: cannot truncate the file %s. (%s)",
                         filename, strerror (errno));
                goto out;
        }

        printf ("File got truncated tp %llu bytes. Sleeping for 22 seconds. "
                "Kill the brick.....\n", size);

        sleep (22);

        ret = lseek (fd, 1048576, SEEK_SET);
        if (ret == -1) {
                fprintf (stderr, "LSEEK: cannot seek to the offset file fff. "
                         "(%s)", strerror (errno));
                goto out;
        }

        ret = write (fd, buf, strlen (buf));
        if (ret == -1) {
                fprintf (stderr, "WRITE: cannot write to the  file fff. (%s)",
                         strerror (errno));
                goto out;
        }

        ret = 0;
out:
        if (fd)
                close (fd);

        return ret;
}