/src/libphdi/libphdi/libphdi_snapshot.c
Line | Count | Source |
1 | | /* |
2 | | * Snapshot functions |
3 | | * |
4 | | * Copyright (C) 2015-2026, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libphdi_libcerror.h" |
27 | | #include "libphdi_libcthreads.h" |
28 | | #include "libphdi_snapshot.h" |
29 | | #include "libphdi_snapshot_values.h" |
30 | | #include "libphdi_types.h" |
31 | | |
32 | | /* Creates a snapshot |
33 | | * Make sure the value snapshot is referencing, is set to NULL |
34 | | * Returns 1 if successful or -1 on error |
35 | | */ |
36 | | int libphdi_snapshot_initialize( |
37 | | libphdi_snapshot_t **snapshot, |
38 | | libphdi_snapshot_values_t *snapshot_values, |
39 | | libcerror_error_t **error ) |
40 | 0 | { |
41 | 0 | libphdi_internal_snapshot_t *internal_snapshot = NULL; |
42 | 0 | static char *function = "libphdi_snapshot_initialize"; |
43 | |
|
44 | 0 | if( snapshot == NULL ) |
45 | 0 | { |
46 | 0 | libcerror_error_set( |
47 | 0 | error, |
48 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
49 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
50 | 0 | "%s: invalid snapshot.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 0 | if( *snapshot != NULL ) |
56 | 0 | { |
57 | 0 | libcerror_error_set( |
58 | 0 | error, |
59 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
60 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
61 | 0 | "%s: invalid snapshot value already set.", |
62 | 0 | function ); |
63 | |
|
64 | 0 | return( -1 ); |
65 | 0 | } |
66 | 0 | if( snapshot_values == NULL ) |
67 | 0 | { |
68 | 0 | libcerror_error_set( |
69 | 0 | error, |
70 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
71 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
72 | 0 | "%s: invalid snapshot values.", |
73 | 0 | function ); |
74 | |
|
75 | 0 | return( -1 ); |
76 | 0 | } |
77 | 0 | internal_snapshot = memory_allocate_structure( |
78 | 0 | libphdi_internal_snapshot_t ); |
79 | |
|
80 | 0 | if( internal_snapshot == NULL ) |
81 | 0 | { |
82 | 0 | libcerror_error_set( |
83 | 0 | error, |
84 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
85 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
86 | 0 | "%s: unable to create snapshot.", |
87 | 0 | function ); |
88 | |
|
89 | 0 | goto on_error; |
90 | 0 | } |
91 | 0 | if( memory_set( |
92 | 0 | internal_snapshot, |
93 | 0 | 0, |
94 | 0 | sizeof( libphdi_internal_snapshot_t ) ) == NULL ) |
95 | 0 | { |
96 | 0 | libcerror_error_set( |
97 | 0 | error, |
98 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
99 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
100 | 0 | "%s: unable to clear snapshot.", |
101 | 0 | function ); |
102 | |
|
103 | 0 | memory_free( |
104 | 0 | internal_snapshot ); |
105 | |
|
106 | 0 | return( -1 ); |
107 | 0 | } |
108 | 0 | #if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT ) |
109 | 0 | if( libcthreads_read_write_lock_initialize( |
110 | 0 | &( internal_snapshot->read_write_lock ), |
111 | 0 | error ) != 1 ) |
112 | 0 | { |
113 | 0 | libcerror_error_set( |
114 | 0 | error, |
115 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
116 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
117 | 0 | "%s: unable to initialize read/write lock.", |
118 | 0 | function ); |
119 | |
|
120 | 0 | goto on_error; |
121 | 0 | } |
122 | 0 | #endif |
123 | 0 | internal_snapshot->snapshot_values = snapshot_values; |
124 | |
|
125 | 0 | *snapshot = (libphdi_snapshot_t *) internal_snapshot; |
126 | |
|
127 | 0 | return( 1 ); |
128 | | |
129 | 0 | on_error: |
130 | 0 | if( internal_snapshot != NULL ) |
131 | 0 | { |
132 | 0 | memory_free( |
133 | 0 | internal_snapshot ); |
134 | 0 | } |
135 | 0 | return( -1 ); |
136 | 0 | } |
137 | | |
138 | | /* Frees a snapshot |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libphdi_snapshot_free( |
142 | | libphdi_snapshot_t **snapshot, |
143 | | libcerror_error_t **error ) |
144 | 0 | { |
145 | 0 | libphdi_internal_snapshot_t *internal_snapshot = NULL; |
146 | 0 | static char *function = "libphdi_snapshot_free"; |
147 | 0 | int result = 1; |
148 | |
|
149 | 0 | if( snapshot == NULL ) |
150 | 0 | { |
151 | 0 | libcerror_error_set( |
152 | 0 | error, |
153 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
154 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
155 | 0 | "%s: invalid snapshot.", |
156 | 0 | function ); |
157 | |
|
158 | 0 | return( -1 ); |
159 | 0 | } |
160 | 0 | if( *snapshot != NULL ) |
161 | 0 | { |
162 | 0 | internal_snapshot = (libphdi_internal_snapshot_t *) *snapshot; |
163 | 0 | *snapshot = NULL; |
164 | |
|
165 | 0 | #if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT ) |
166 | 0 | if( libcthreads_read_write_lock_free( |
167 | 0 | &( internal_snapshot->read_write_lock ), |
168 | 0 | error ) != 1 ) |
169 | 0 | { |
170 | 0 | libcerror_error_set( |
171 | 0 | error, |
172 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
173 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
174 | 0 | "%s: unable to free read/write lock.", |
175 | 0 | function ); |
176 | |
|
177 | 0 | result = -1; |
178 | 0 | } |
179 | 0 | #endif |
180 | | /* The snapshot values reference is freed elsewhere |
181 | | */ |
182 | 0 | memory_free( |
183 | 0 | internal_snapshot ); |
184 | 0 | } |
185 | 0 | return( result ); |
186 | 0 | } |
187 | | |
188 | | /* Retrieves the identifier |
189 | | * The identifier is a big-endian GUID and is 16 bytes of size |
190 | | * Returns 1 if successful or -1 on error |
191 | | */ |
192 | | int libphdi_snapshot_get_identifier( |
193 | | libphdi_snapshot_t *snapshot, |
194 | | uint8_t *guid_data, |
195 | | size_t guid_data_size, |
196 | | libcerror_error_t **error ) |
197 | 0 | { |
198 | 0 | libphdi_internal_snapshot_t *internal_snapshot = NULL; |
199 | 0 | static char *function = "libphdi_snapshot_get_identifier"; |
200 | 0 | int result = 1; |
201 | |
|
202 | 0 | if( snapshot == NULL ) |
203 | 0 | { |
204 | 0 | libcerror_error_set( |
205 | 0 | error, |
206 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
207 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
208 | 0 | "%s: invalid snapshot.", |
209 | 0 | function ); |
210 | |
|
211 | 0 | return( -1 ); |
212 | 0 | } |
213 | 0 | internal_snapshot = (libphdi_internal_snapshot_t *) snapshot; |
214 | |
|
215 | 0 | #if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT ) |
216 | 0 | if( libcthreads_read_write_lock_grab_for_read( |
217 | 0 | internal_snapshot->read_write_lock, |
218 | 0 | error ) != 1 ) |
219 | 0 | { |
220 | 0 | libcerror_error_set( |
221 | 0 | error, |
222 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
223 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
224 | 0 | "%s: unable to grab read/write lock for reading.", |
225 | 0 | function ); |
226 | |
|
227 | 0 | return( -1 ); |
228 | 0 | } |
229 | 0 | #endif |
230 | 0 | if( libphdi_snapshot_values_get_identifier( |
231 | 0 | internal_snapshot->snapshot_values, |
232 | 0 | guid_data, |
233 | 0 | guid_data_size, |
234 | 0 | error ) != 1 ) |
235 | 0 | { |
236 | 0 | libcerror_error_set( |
237 | 0 | error, |
238 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
239 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
240 | 0 | "%s: unable to retrieve identifier.", |
241 | 0 | function ); |
242 | |
|
243 | 0 | result = -1; |
244 | 0 | } |
245 | 0 | #if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT ) |
246 | 0 | if( libcthreads_read_write_lock_release_for_read( |
247 | 0 | internal_snapshot->read_write_lock, |
248 | 0 | error ) != 1 ) |
249 | 0 | { |
250 | 0 | libcerror_error_set( |
251 | 0 | error, |
252 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
253 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
254 | 0 | "%s: unable to release read/write lock for reading.", |
255 | 0 | function ); |
256 | |
|
257 | 0 | return( -1 ); |
258 | 0 | } |
259 | 0 | #endif |
260 | 0 | return( result ); |
261 | 0 | } |
262 | | |
263 | | /* Retrieves the parent identifier |
264 | | * The identifier is a big-endian GUID and is 16 bytes of size |
265 | | * Returns 1 if successful, 0 if not available or -1 on error |
266 | | */ |
267 | | int libphdi_snapshot_get_parent_identifier( |
268 | | libphdi_snapshot_t *snapshot, |
269 | | uint8_t *guid_data, |
270 | | size_t guid_data_size, |
271 | | libcerror_error_t **error ) |
272 | 0 | { |
273 | 0 | libphdi_internal_snapshot_t *internal_snapshot = NULL; |
274 | 0 | static char *function = "libphdi_snapshot_get_parent_identifier"; |
275 | 0 | int result = 0; |
276 | |
|
277 | 0 | if( snapshot == NULL ) |
278 | 0 | { |
279 | 0 | libcerror_error_set( |
280 | 0 | error, |
281 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
282 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
283 | 0 | "%s: invalid snapshot.", |
284 | 0 | function ); |
285 | |
|
286 | 0 | return( -1 ); |
287 | 0 | } |
288 | 0 | internal_snapshot = (libphdi_internal_snapshot_t *) snapshot; |
289 | |
|
290 | 0 | #if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT ) |
291 | 0 | if( libcthreads_read_write_lock_grab_for_read( |
292 | 0 | internal_snapshot->read_write_lock, |
293 | 0 | error ) != 1 ) |
294 | 0 | { |
295 | 0 | libcerror_error_set( |
296 | 0 | error, |
297 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
298 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
299 | 0 | "%s: unable to grab read/write lock for reading.", |
300 | 0 | function ); |
301 | |
|
302 | 0 | return( -1 ); |
303 | 0 | } |
304 | 0 | #endif |
305 | 0 | result = libphdi_snapshot_values_get_parent_identifier( |
306 | 0 | internal_snapshot->snapshot_values, |
307 | 0 | guid_data, |
308 | 0 | guid_data_size, |
309 | 0 | error ); |
310 | |
|
311 | 0 | if( result == -1 ) |
312 | 0 | { |
313 | 0 | libcerror_error_set( |
314 | 0 | error, |
315 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
316 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
317 | 0 | "%s: unable to retrieve parent identifier.", |
318 | 0 | function ); |
319 | |
|
320 | 0 | result = -1; |
321 | 0 | } |
322 | 0 | #if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT ) |
323 | 0 | if( libcthreads_read_write_lock_release_for_read( |
324 | 0 | internal_snapshot->read_write_lock, |
325 | 0 | error ) != 1 ) |
326 | 0 | { |
327 | 0 | libcerror_error_set( |
328 | 0 | error, |
329 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
330 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
331 | 0 | "%s: unable to release read/write lock for reading.", |
332 | 0 | function ); |
333 | |
|
334 | 0 | return( -1 ); |
335 | 0 | } |
336 | 0 | #endif |
337 | 0 | return( result ); |
338 | 0 | } |
339 | | |