summaryrefslogtreecommitdiff
path: root/ioreplay/src/utils/futils.c
blob: 5b3561810fd303bf782e41522f23fcf96fecbe35 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2018 Mimecast Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "futils.h"

#include <libgen.h>
#include <pwd.h>
#include <unistd.h>
#include <limits.h>

#include "../macros.h"

void append_random_to_file(char *path, unsigned long bytes)
{
    char *buf = NULL;
    int max_chunk = 50000000; // 50 mebibyetes
    FILE *fp = Fopen(path, "a");

    for (;;) {
        if (bytes > max_chunk) {
            if (!buf)
                buf = Calloc(max_chunk+1, char);

            Fill_with_stuff(buf, max_chunk);
            buf[max_chunk] = '\0';
            fprintf(fp, "%s", buf);
            bytes -= max_chunk;

            // Print out a dot every time we wrote 'much' data to a file
            Out(".");

        } else {
            if (!buf)
                buf = Calloc(bytes+1, char);

            Fill_with_stuff(buf, bytes);
            buf[bytes] = '\0';
            fprintf(fp, "%s", buf);

            break;
        }
    }

    if (buf)
        free(buf);
    fclose(fp);
}

long ensure_dir_exists(const char *path)
{
    long num_dirs_created = 0;
    int ret = mkdir_p(path, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH,
                      &num_dirs_created);
    if (ret != 0) {
        Errno("Could not create dir '%s'", path);
    }

    return num_dirs_created;
}

void ensure_parent_dir_exists(const char *path)
{
    char *clone = Clone(path);
    char *parent = dirname(clone);

    int ret = mkdir_p(parent, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH, NULL);
    if (ret != 0) {
        Errno("Could not create dir %s", parent);
    }

    free(clone);
}

void ensure_dir_empty(const char *path)
{
    DIR *dh = opendir(path);

    if (!dh) {
        Errno("Unable to empty %s", path);
    }

    struct dirent *de;

    while ((de = readdir(dh))) {
        if (0 == strcmp(de->d_name, ".") ||
            0 == strcmp(de->d_name, ".."))
            continue;

        char *absolute;
        asprintf(&absolute, "%s/%s", path, de->d_name);

        if (is_dir(absolute))
            ensure_dir_empty(absolute);

        if (remove(absolute) == -1)
            // Don't throw an error if there is no such file or directory
            if (errno != 2) {
                Errno("Unable to remove %s", absolute);
            }

        free(absolute);
    }

    closedir(dh);
}

int ensure_file_exists(char *path, long *num_dirs_created)
{
    if (is_reg(path))
        return SUCCESS;

    char *dirname = dirname_r(Clone(path));
    *num_dirs_created += ensure_dir_exists(dirname);
    free(dirname);

    FILE *fp = fopen(path, "a");
    if (fp) {
        // We only need some data, less than 1 block in size, this is answer:
        fprintf(fp, "42");
        fclose(fp);
        return SUCCESS;
    }

    return ERROR;
}

char* dirname_r(char *path)
{
    int len = strlen(path);
    int has = 0;
    int i = len-1;

    if (strcmp(path, "..") == 0) {
        return path;
    }

    if (path[i] == '/') {
        // Root directory
        if (len == 1)
            return path;

        // Remove all trailing /
        for (; i >= 0; --i) {
            if (path[i] == '/') {
                path[i] = '\0';
                has = 1;
            } else {
                break;
            }
        }
    }

    // Find next /
    for (; i >= 0; --i) {
        if (path[i] == '/') {
            path[i] = '\0';
            has = 1;
            break;
        }
    }

    // If no /
    if (has == 0) {
        path[0] = '.';
        path[1] = '\0';
    }

    return path;
}

bool is_dir(const char *path)
{
    struct stat path_stat;
    if (stat(path, &path_stat) == 0 && S_ISDIR(path_stat.st_mode))
        return true;
    return false;
}

bool is_reg(const char *path)
{
    struct stat path_stat;
    if (stat(path, &path_stat) == 0 && S_ISREG(path_stat.st_mode))
        return true;
    return false;
}

bool exists(const char *path)
{
    struct stat path_stat;
    if (stat(path, &path_stat) == 0)
        return true;
    return false;
}

int mkdir_p(const char *path, mode_t mode, long *num_dirs_created)
{
    int res = 0;

    if (is_dir(path))
        return 0;

    if (is_reg(path))
        unlink(path);

    char *top = dirname_r(Clone(path));
    if (0 != mkdir_p(top, mode, num_dirs_created))
        goto cleanup;

    if ((mkdir(path, mode) == -1) && (errno != EEXIST))
        res = -1;

    if (res != -1)
        *num_dirs_created = *num_dirs_created+1;

cleanup:
    free(top);

    return res;
}

void cache_file(const char *file)
{
    Out("Caching file %s... it can take a while", file);
    FILE *fd = Fopen(file, "r");
    char *line = NULL;
    size_t len = 0, read = 0;

    while ((read = getline(&line, &len, fd)) != -1);
    fclose(fd);
}

void drop_caches(void)
{
    Out("Dropping all Linux caches...");

    if (getuid() != 0) {
        Out("\n");
        Error("I need to be root to do this, aborting!");
    }

    // echo 3 > /proc/sys/vm/drop_caches
    char *drop_caches = "/proc/sys/vm/drop_caches";
    FILE *fd = Fopen(drop_caches, "w");
    fprintf(fd, "3");
    fclose(fd);

    Put("done");
}

void chown_path(const char *user, const char *path)
{
    struct passwd *pwd = getpwnam(user);
    if (!pwd) {
        Errno("Unable to retrieve information about system user %s!", user);
    }

    if (chown(path, pwd->pw_uid, -1) == -1) {
        Errno("Could not change ownership of '%s' to '%s'!", path, user);
    }
}

char *absolute_path(const char *path)
{
    if (path[0] == '/')
        return Clone(path);

    char cwd[MAX_LINE_LEN];
    getcwd(cwd, sizeof(char)*MAX_LINE_LEN);

    if (!getcwd(cwd, sizeof(cwd))) {
        Errno("Could not get current working directory");
    }

    char *absolute = NULL;
    if (-1 == asprintf(&absolute, "%s/%s", cwd, path)) {
        Error("Could not get absolute path of '%s'", path);
    }

    return absolute;
}