summaryrefslogtreecommitdiffstats
path: root/backend/fileaccess.c
blob: e3e5240af194a6d4aa7d5d1519f82457530c7af7 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "fileaccess.h"
#include "log.h"
#include "util.h"

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>


#define PROCFS_BASE	"/proc"
#define SYSFS_BASE	"/sys"


static void file_rewind(struct fileaccess *fa)
{
	rewind(fa->stream);
	lseek(fa->fd, 0, SEEK_SET);
}

void file_close(struct fileaccess *fa)
{
	if (fa) {
		fclose(fa->stream);
		close(fa->fd);
		free(fa);
	}
}

struct fileaccess * file_open(int flags, const char *path_fmt, ...)
{
	char path[PATH_MAX + 1];
	va_list ap;
	int fd;
	FILE *stream;
	struct fileaccess *fa;
	const char *opentype;

	va_start(ap, path_fmt);
	vsnprintf(path, sizeof(path), path_fmt, ap);
	va_end(ap);

	fd = open(path, flags);
	if (fd < 0)
		goto error;
	if (flags == O_RDONLY)
		opentype = "r";
	else if (flags == O_WRONLY)
		opentype = "w";
	else if (flags == O_RDWR)
		opentype = "w+";
	else
		goto err_close;
	stream = fdopen(fd, opentype);
	if (!stream)
		goto err_close;

	fa = zalloc(sizeof(*fa));
	if (!fa)
		goto err_fclose;

	fa->fd = fd;
	fa->stream = stream;

	return fa;

err_fclose:
	fclose(stream);
err_close:
	close(fd);
error:
	return NULL;
}

struct fileaccess * sysfs_file_open(int flags, const char *path_fmt, ...)
{
	char path[PATH_MAX + 1];
	va_list ap;

	va_start(ap, path_fmt);
	vsnprintf(path, sizeof(path), path_fmt, ap);
	va_end(ap);

	return file_open(flags, "%s/%s", SYSFS_BASE, path);
}

struct fileaccess * procfs_file_open(int flags, const char *path_fmt, ...)
{
	char path[PATH_MAX + 1];
	va_list ap;

	va_start(ap, path_fmt);
	vsnprintf(path, sizeof(path), path_fmt, ap);
	va_end(ap);

	return file_open(flags, "%s/%s", PROCFS_BASE, path);
}

int file_read_buf(struct fileaccess *fa, char *buf, size_t size)
{
	ssize_t count;
	size_t pos = 0;

	if (!size)
		return 0;

	file_rewind(fa);
	while (size) {
		count = read(fa->fd, buf + pos, size);
		if (count < 0)
			return -errno;
		if (!count)
			break;
		size -= count;
		pos += count;
	}

	return pos;
}

int file_read_string(struct fileaccess *fa, char *buf, size_t size)
{
	int count;

	if (!size)
		return 0;
	count = file_read_buf(fa, buf, size - 1);
	if (count < 0)
		return count;
	buf[count] = '\0';

	return count;
}

int file_read_int(struct fileaccess *fa, int *value, int base)
{
	char buf[64];
	int count;
	long val;
	char *tail;

	count = file_read_buf(fa, buf, sizeof(buf) - 1);
	if (count < 0)
		return count;
	buf[count] = '\0';

	errno = 0;
	val = strtol(buf, &tail, base);
	if (errno || (*tail != '\0' && *tail != '\n'))
		return -ETXTBSY;
	*value = val;

	return 0;
}

int file_write_int(struct fileaccess *fa, int value, int base)
{
	char buf[64];
	const char *buf_ptr;
	const char *fmt;
	size_t count;
	ssize_t res;

	if (base == 0 || base == 10)
		fmt = "%d";
	else if (base == 16)
		fmt = "0x%X";
	else
		return -EINVAL;

	snprintf(buf, sizeof(buf), fmt, value);

	file_rewind(fa);
	count = strlen(buf);
	buf_ptr = buf;
	while (count) {
		res = write(fa->fd, buf_ptr, count);
		if (res < 0)
			return -ETXTBSY;
		count -= (size_t)res;
		buf_ptr += (size_t)res;
	}

	return 0;
}

int file_read_bool(struct fileaccess *fa, int *value)
{
	int val, err;

	err = file_read_int(fa, &val, 0);
	if (err)
		return err;
	if (val < 0)
		return -ETXTBSY;
	*value = !!val;

	return 0;
}

int file_write_bool(struct fileaccess *fa, int value)
{
	return file_write_int(fa, !!value, 0);
}

int file_read_text_lines(struct fileaccess *fa, struct list_head *lines_list,
			 int strip_whitespace)
{
	char *lineptr = NULL, *str;
	size_t size = 0;
	ssize_t count;
	struct text_line *tl;
	int err;

	INIT_LIST_HEAD(lines_list);
	file_rewind(fa);
	while (1) {
		count = getline(&lineptr, &size, fa->stream);
		if (count <= 0)
			break;
		while (count > 0 &&
			(lineptr[count - 1] == '\r' ||
				lineptr[count - 1] == '\n')) {
			lineptr[count - 1] = '\0';
			count--;
		}

		tl = malloc(sizeof(*tl));
		if (!tl) {
			err = -ENOMEM;
			goto error_unwind;
		}
		if (strip_whitespace)
			str = string_strip(lineptr);
		else
			str = lineptr;
		tl->text = strdup(str);
		if (!tl->text) {
			err = -ENOMEM;
			free(tl);
			goto error_unwind;
		}
		list_add_tail(&tl->list, lines_list);
	}
	free(lineptr);

	return 0;

error_unwind:
	text_lines_free(lines_list);
	free(lineptr);

	return err;
}

void text_line_free(struct text_line *tl)
{
	if (tl) {
		list_del(&tl->list);
		free(tl->text);
		free(tl);
	}
}

void text_lines_free(struct list_head *lines_list)
{
	struct text_line *tl, *tl_tmp;

	if (lines_list) {
		list_for_each_entry_safe(tl, tl_tmp, lines_list, list)
			text_line_free(tl);
		INIT_LIST_HEAD(lines_list);
	}
}

int list_directory(struct list_head *dir_entries, const char *path_fmt, ...)
{
	char path[PATH_MAX + 1];
	va_list ap;
	DIR *dir;
	struct dirent *dirent;
	int err;
	struct dir_entry *de;
	int count = 0;

	va_start(ap, path_fmt);
	vsnprintf(path, sizeof(path), path_fmt, ap);
	va_end(ap);

	dir = opendir(path);
	if (!dir)
		return -errno;
	INIT_LIST_HEAD(dir_entries);
	while (1) {
		errno = 0;
		dirent = readdir(dir);
		if (!dirent) {
			err = errno;
			if (err)
				goto error_unwind;
			break;
		}

		if (strcmp(dirent->d_name, ".") == 0 ||
		    strcmp(dirent->d_name, "..") == 0)
			continue;

		de = malloc(sizeof(*de));
		if (!de) {
			err = -ENOMEM;
			goto error_unwind;
		}
		de->name = strdup(dirent->d_name);
		if (!de->name) {
			err = -ENOMEM;
			free(de);
			goto error_unwind;
		}
		de->type = dirent->d_type;
		list_add_tail(&de->list, dir_entries);
		count++;
	}
	closedir(dir);

	return count;

error_unwind:
	dir_entries_free(dir_entries);
	closedir(dir);

	return err;
}

int list_sysfs_directory(struct list_head *dir_entries, const char *path_fmt, ...)
{
	char path[PATH_MAX + 1];
	va_list ap;

	va_start(ap, path_fmt);
	vsnprintf(path, sizeof(path), path_fmt, ap);
	va_end(ap);

	return list_directory(dir_entries, "%s/%s", SYSFS_BASE, path);
}

int list_procfs_directory(struct list_head *dir_entries, const char *path_fmt, ...)
{
	char path[PATH_MAX + 1];
	va_list ap;

	va_start(ap, path_fmt);
	vsnprintf(path, sizeof(path), path_fmt, ap);
	va_end(ap);

	return list_directory(dir_entries, "%s/%s", PROCFS_BASE, path);
}

void dir_entry_free(struct dir_entry *dir_entry)
{
	if (dir_entry) {
		list_del(&dir_entry->list);
		free(dir_entry->name);
		free(dir_entry);
	}
}

void dir_entries_free(struct list_head *dir_entries)
{
	struct dir_entry *d, *d_tmp;

	if (dir_entries) {
		list_for_each_entry_safe(d, d_tmp, dir_entries, list)
			dir_entry_free(d);
		INIT_LIST_HEAD(dir_entries);
	}
}
bues.ch cgit interface