summaryrefslogtreecommitdiffstats
path: root/src/dirent.c
blob: 778312038bdc38b953656f0dbccef314c47b5f68 (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
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opk.
 *
 * opkg-opk 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 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opk 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "dirent.h"

static int
_opkg_opk_dirent_name_prefix(struct opkg_opk_dirent *dirent, char **name_buf,
		size_t *name_buf_len, char **pref_buf, size_t *pref_buf_len,
		size_t *name_len, size_t *pref_len)
{
	size_t   len;
	char   **buf;

	len = strlen(dirent->name) + 1;
	if (*name_len + len <= *name_buf_len) {
		*name_buf_len -= len;
		buf = name_buf;
	} else if (*pref_len + len <= *pref_buf_len) {
		*pref_buf_len -= len;
		buf = pref_buf;
	} else {
		return OPKG_OPK_ERROR;
	}

	if (dirent->parent != NULL &&
			_opkg_opk_dirent_name_prefix(dirent->parent,
				name_buf, name_buf_len, pref_buf, pref_buf_len,
				name_len, pref_len) == OPKG_OPK_ERROR) {
		return OPKG_OPK_ERROR;
	}

	**buf = '/';
	*buf += 1;
	memcpy(*buf, dirent->name, len - 1);
	*buf += len - 1;

	return OPKG_OPK_OK;
}

int
opkg_opk_dirent_name_prefix(struct opkg_opk_dirent *dirent, char *name_buf,
		size_t name_buf_len, char *pref_buf, size_t pref_buf_len)
{
	size_t name_len = 0;
	size_t pref_len = 0;

	if (_opkg_opk_dirent_name_prefix(dirent, &name_buf, &name_buf_len,
				&pref_buf, &pref_buf_len, &name_len, &pref_len)
			== OPKG_OPK_ERROR) {
		return OPKG_OPK_ERROR;
	}
	name_buf[name_len] = '\0';
	pref_buf[pref_len] = '\0';
	return OPKG_OPK_OK;
}

static char *
_opkg_opk_dirent_full_name(struct opkg_opk_dirent *dirent, char **cur,
		size_t *buf_len)
{
	size_t  len;
	char   *buf;

	len = strlen(dirent->name) + 1;
	*buf_len += len;

	if (dirent->parent == NULL) {
		buf = malloc(*buf_len);
		if (buf == NULL) {
			return NULL;
		}
		*cur = buf;
	} else if ((buf = _opkg_opk_dirent_full_name(dirent->parent, cur,
					buf_len)) == NULL) {
		return NULL;
	}

	**cur = '/';
	*cur += 1;
	memcpy(*cur, dirent->name, len - 1);
	*cur += len - 1;

	return buf;
}

char *
opkg_opk_dirent_full_name(struct opkg_opk_dirent *dirent)
{
	char   *name;
	char   *cur     = NULL;
	size_t  buf_len = 1;     /* 1 for NUL */

	if ((name = _opkg_opk_dirent_full_name(dirent, &cur, &buf_len)) == NULL)
	{
		return NULL;
	}
	*cur = '\0';
	return name;
}