summaryrefslogtreecommitdiff
path: root/src/share/fl.c
blob: ceb6ff316425a405890bc168b9f1ab5e5d9d8ecb (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
/* Copyright (c) 2012-2013 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <global.h>
#include <share/local.h>


share_fl_t *share_fl_create(bool isdir, const char *name, kstring_t *buf) {
	buf->l = 0;
	casestr_create(name, buf);
	share_fl_t *f = calloc(1, buf->l + (isdir ? offsetof(share_fl_t, dname) : offsetof(share_fl_t, fname)));
	if(isdir)
		f->flags = SHARE_FL_DIRFLAG;
	memcpy(share_fl_name(f), buf->s, buf->l);
	return f;
}


void share_fl_free(share_fl_t *fl) {
	size_t i;
	if(share_fl_isdir(fl)) {
		for(i=0; i<fl->sub.n; i++)
			share_fl_free(fl->sub.a[i]);
		free(fl->sub.a);
	}
	free(fl);
}


/* XXX: Unused at the moment */
share_fl_t *share_fl_lookup(share_fl_t *dir, const char *name) {
	vec_search(dir->sub, strcmp(share_fl_name(dir->sub.a[i]), name), return dir->sub.a[i]);
	return NULL;
}


share_fl_t *share_fl_insert(share_fl_t *dir, share_fl_t *item) {
	size_t r;
	vec_search_insert(dir->sub, r, strcmp(share_fl_name(dir->sub.a[i]), share_fl_name(item)));
	if(r == dir->sub.n || strcmp(share_fl_name(dir->sub.a[r]), share_fl_name(item)) != 0) {
		vec_insert_order(dir->sub, r, item);
		return NULL;
	}
	return dir->sub.a[r];
}


share_fl_t *share_fl_getdir(share_fl_t *root, char *path, char *pathf, kstring_t *buf) {
	if(*path == '/')
		path++, pathf++;
	if(!*path)
		return root;
	char *end = NULL, *endf = strchr(pathf, '/');
	if(endf) {
		end = strchr(path, '/');
		*end = *endf = 0;
	}

	size_t r;
	vec_search_insert(root->sub, r, strcmp(share_fl_name(root->sub.a[i]), pathf));
	if(r == root->sub.n || strcmp(share_fl_name(root->sub.a[r]), pathf) != 0) {
		share_fl_t *fl = share_fl_create(true, path, buf);
		vec_insert_order(root->sub, r, fl);
	}

	if(endf) {
		*end = *endf = '/';
		return share_fl_getdir(root->sub.a[r], end+1, endf+1, buf);
	} else
		return root->sub.a[r];
}


share_fl_t *share_fl_resolve(share_fl_t *root, char *path) {
	if(*path == '/')
		path++;
	if(!*path)
		return root;
	assert(share_fl_isdir(root));
	char *end = strchr(path, '/');
	if(end)
		*end = 0;

	kstring_t buf = {};
	casestr_create(path, &buf);

	size_t r = (size_t)-1;
	vec_search(root->sub, strcmp(share_fl_name(root->sub.a[i]), buf.s), r=i);

	share_fl_t *fl = r == (size_t)-1 ? NULL : root->sub.a[r];
	share_fl_t *ret = NULL;
	if(fl && casestr_cmp(share_fl_name(fl), buf.s) == 0) {
		if(!end)
			ret = fl;
		else if(share_fl_isdir(fl))
			ret = share_fl_resolve(fl, end+1);
	}
	free(buf.s);
	if(end)
		*end = '/';
	return ret;
}


void share_fl_path(share_fl_t *fl, kstring_t *dest) {
	vec_t(char *) lst = {};
	size_t i;

	for(; fl->parent; fl=fl->parent)
		vec_insert_order(lst, 0, share_fl_name(fl));

	for(i=0; i<lst.n; i++) {
		kputc('/', dest);
		casestr_orig(lst.a[i], dest);
	}

	if(!lst.n)
		kputc('/', dest);
	free(lst.a);
}

/* vim: set noet sw=4 ts=4: */