/src/strongswan/src/libimcv/pts/pts_symlinks.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2020 Andreas Steffen |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU General Public License as published by the |
8 | | * Free Software Foundation; either version 2 of the License, or (at your |
9 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | * for more details. |
15 | | */ |
16 | | |
17 | | #include "pts_symlinks.h" |
18 | | |
19 | | #include <collections/linked_list.h> |
20 | | #include <utils/debug.h> |
21 | | |
22 | | typedef struct private_pts_symlinks_t private_pts_symlinks_t; |
23 | | typedef struct entry_t entry_t; |
24 | | |
25 | | /** |
26 | | * Private data of a pts_symlinks_t object. |
27 | | * |
28 | | */ |
29 | | struct private_pts_symlinks_t { |
30 | | |
31 | | /** |
32 | | * Public pts_symlinks_t interface. |
33 | | */ |
34 | | pts_symlinks_t public; |
35 | | |
36 | | /** |
37 | | * List of symbolic links pointing to directories |
38 | | */ |
39 | | linked_list_t *list; |
40 | | |
41 | | /** |
42 | | * Reference count |
43 | | */ |
44 | | refcount_t ref; |
45 | | |
46 | | }; |
47 | | |
48 | | /** |
49 | | * Symlink entry |
50 | | */ |
51 | | struct entry_t { |
52 | | chunk_t symlink; |
53 | | chunk_t dir; |
54 | | }; |
55 | | |
56 | | /** |
57 | | * Free an entry_t object |
58 | | */ |
59 | | static void free_entry(entry_t *entry) |
60 | 29.4k | { |
61 | 29.4k | if (entry) |
62 | 29.4k | { |
63 | 29.4k | free(entry->symlink.ptr); |
64 | 29.4k | free(entry->dir.ptr); |
65 | 29.4k | free(entry); |
66 | 29.4k | } |
67 | 29.4k | } |
68 | | |
69 | | METHOD(pts_symlinks_t, get_count, int, |
70 | | private_pts_symlinks_t *this) |
71 | 0 | { |
72 | 0 | return this->list->get_count(this->list); |
73 | 0 | } |
74 | | |
75 | | METHOD(pts_symlinks_t, add, void, |
76 | | private_pts_symlinks_t *this, chunk_t symlink, chunk_t dir) |
77 | 29.4k | { |
78 | 29.4k | entry_t *entry; |
79 | | |
80 | 29.4k | entry = malloc_thing(entry_t); |
81 | 29.4k | entry->symlink = chunk_clone(symlink); |
82 | 29.4k | entry->dir = chunk_clone(dir); |
83 | | |
84 | 29.4k | this->list->insert_last(this->list, entry); |
85 | 29.4k | } |
86 | | |
87 | | CALLBACK(symlink_filter, bool, |
88 | | void *null, enumerator_t *orig, va_list args) |
89 | 0 | { |
90 | 0 | entry_t *entry; |
91 | 0 | chunk_t *symlink; |
92 | 0 | chunk_t *dir; |
93 | |
|
94 | 0 | VA_ARGS_VGET(args, symlink, dir); |
95 | |
|
96 | 0 | if (orig->enumerate(orig, &entry)) |
97 | 0 | { |
98 | 0 | *symlink = entry->symlink; |
99 | 0 | *dir = entry->dir; |
100 | 0 | return TRUE; |
101 | 0 | } |
102 | 0 | return FALSE; |
103 | 0 | } |
104 | | |
105 | | METHOD(pts_symlinks_t, create_enumerator, enumerator_t*, |
106 | | private_pts_symlinks_t *this) |
107 | 0 | { |
108 | 0 | return enumerator_create_filter(this->list->create_enumerator(this->list), |
109 | 0 | symlink_filter, NULL, NULL); |
110 | 0 | } |
111 | | |
112 | | METHOD(pts_symlinks_t, get_ref, pts_symlinks_t*, |
113 | | private_pts_symlinks_t *this) |
114 | 0 | { |
115 | 0 | ref_get(&this->ref); |
116 | 0 | return &this->public; |
117 | 0 | } |
118 | | |
119 | | METHOD(pts_symlinks_t, destroy, void, |
120 | | private_pts_symlinks_t *this) |
121 | 628 | { |
122 | 628 | if (ref_put(&this->ref)) |
123 | 628 | { |
124 | 628 | this->list->destroy_function(this->list, (void *)free_entry); |
125 | 628 | free(this); |
126 | 628 | } |
127 | 628 | } |
128 | | |
129 | | /** |
130 | | * See header |
131 | | */ |
132 | | pts_symlinks_t *pts_symlinks_create() |
133 | 628 | { |
134 | 628 | private_pts_symlinks_t *this; |
135 | | |
136 | 628 | INIT(this, |
137 | 628 | .public = { |
138 | 628 | .get_count = _get_count, |
139 | 628 | .add = _add, |
140 | 628 | .create_enumerator = _create_enumerator, |
141 | 628 | .get_ref = _get_ref, |
142 | 628 | .destroy = _destroy, |
143 | 628 | }, |
144 | 628 | .list = linked_list_create(), |
145 | 628 | .ref = 1, |
146 | 628 | ); |
147 | | |
148 | 628 | return &this->public; |
149 | 628 | } |
150 | | |