aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/file.c
blob: 6bb2683df811106812aac0dcc5e8afdd617ddee3 (plain) (blame)
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
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "file.h"
#include "compiler.h"
#include "log.h"

#include <fcntl.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

static int unlink_cb(const char *fpath, UNUSED const struct stat *sb, UNUSED int typeflag,
                     UNUSED struct FTW *ftwbuf)
{
	int ret = 0;

	ret = remove(fpath);
	if (ret < 0) {
		log_errno("remove");
		return ret;
	}

	return ret;
}

int rm_rf(const char *dir)
{
	log("Recursively removing directory: %s\n", dir);
	return nftw(dir, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
}

int my_chdir(const char *dir, char **old)
{
	int ret = 0;

	if (old) {
		*old = get_current_dir_name();
		if (!*old) {
			log_errno("get_current_dir_name");
			return -1;
		}
	}

	ret = chdir(dir);
	if (ret < 0) {
		log_errno("chdir");
		goto free_old;
	}

	return ret;

free_old:
	if (old)
		free(*old);

	return ret;
}

char *my_readlink(const char *path)
{
	size_t current_size = 256;
	char *buf = NULL;

	while (1) {
		char *tmp_buf = realloc(buf, current_size);
		if (!tmp_buf) {
			log_errno("realloc");
			goto free;
		}
		buf = tmp_buf;

		ssize_t res = readlink(path, buf, current_size);
		if (res < 0) {
			log_errno("readlink");
			goto free;
		}

		if ((size_t)res == current_size) {
			current_size *= 2;
			continue;
		}

		buf[res] = '\0';
		break;
	}

	return buf;

free:
	free(buf);

	return NULL;
}

int file_dup(int fd)
{
	int ret = 0;

	ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);
	if (ret < 0) {
		log_errno("fcntl");
		return ret;
	}

	return ret;
}

void file_close(int fd)
{
	log_errno_if(close(fd), "close");
}

int file_exists(const char *path)
{
	struct stat stat;
	int ret = lstat(path, &stat);
	return !ret && S_ISREG(stat.st_mode);
}

int file_read(int fd, unsigned char **_contents, size_t *_size)
{
	size_t alloc_size = 256;
	unsigned char *contents = NULL;
	size_t size = 0;

	while (1) {
		unsigned char *tmp_contents = realloc(contents, alloc_size);
		if (!tmp_contents) {
			log_errno("realloc");
			free(contents);
			return -1;
		}
		contents = tmp_contents;

		ssize_t read_size = read(fd, contents + size, alloc_size - size);

		if (read_size < 0) {
			log_errno("read");
			free(contents);
			return read_size;
		}

		if (!read_size) {
			*_contents = contents;
			*_size = size;
			return 0;
		}

		size += read_size;

		if (size == alloc_size) {
			alloc_size *= 2;
		}
	}
}