Coverage Report

Created: 2023-06-07 06:53

/src/libvslvm/libvslvm/libvslvm_stripe.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Stripe functions
3
 *
4
 * Copyright (C) 2014-2023, 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 "libvslvm_libcerror.h"
27
#include "libvslvm_stripe.h"
28
#include "libvslvm_types.h"
29
30
/* Creates a stripe
31
 * Make sure the value stripe is referencing, is set to NULL
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libvslvm_stripe_initialize(
35
     libvslvm_stripe_t **stripe,
36
     libcerror_error_t **error )
37
186k
{
38
186k
  libvslvm_internal_stripe_t *internal_stripe = NULL;
39
186k
  static char *function                       = "libvslvm_stripe_initialize";
40
41
186k
  if( stripe == NULL )
42
0
  {
43
0
    libcerror_error_set(
44
0
     error,
45
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
46
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
47
0
     "%s: invalid stripe.",
48
0
     function );
49
50
0
    return( -1 );
51
0
  }
52
186k
  if( *stripe != NULL )
53
0
  {
54
0
    libcerror_error_set(
55
0
     error,
56
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
57
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
58
0
     "%s: invalid stripe value already set.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
186k
  internal_stripe = memory_allocate_structure(
64
186k
                     libvslvm_internal_stripe_t );
65
66
186k
  if( internal_stripe == NULL )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
71
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
72
0
     "%s: unable to create stripe.",
73
0
     function );
74
75
0
    goto on_error;
76
0
  }
77
186k
  if( memory_set(
78
186k
       internal_stripe,
79
186k
       0,
80
186k
       sizeof( libvslvm_internal_stripe_t ) ) == NULL )
81
0
  {
82
0
    libcerror_error_set(
83
0
     error,
84
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
85
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
86
0
     "%s: unable to clear stripe.",
87
0
     function );
88
89
0
    goto on_error;
90
0
  }
91
186k
  *stripe = (libvslvm_stripe_t *) internal_stripe;
92
93
186k
  return( 1 );
94
95
0
on_error:
96
0
  if( internal_stripe != NULL )
97
0
  {
98
0
    memory_free(
99
0
     internal_stripe );
100
0
  }
101
0
  return( -1 );
102
186k
}
103
104
/* Frees a stripe
105
 * Returns 1 if successful or -1 on error
106
 */
107
int libvslvm_stripe_free(
108
     libvslvm_stripe_t **stripe,
109
     libcerror_error_t **error )
110
241
{
111
241
  static char *function = "libvslvm_stripe_free";
112
113
241
  if( stripe == NULL )
114
0
  {
115
0
    libcerror_error_set(
116
0
     error,
117
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
118
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
119
0
     "%s: invalid stripe.",
120
0
     function );
121
122
0
    return( -1 );
123
0
  }
124
241
  if( *stripe != NULL )
125
241
  {
126
241
    *stripe = NULL;
127
241
  }
128
241
  return( 1 );
129
241
}
130
131
/* Frees a stripe
132
 * Returns 1 if successful or -1 on error
133
 */
134
int libvslvm_internal_stripe_free(
135
     libvslvm_internal_stripe_t **internal_stripe,
136
     libcerror_error_t **error )
137
186k
{
138
186k
  static char *function = "libvslvm_internal_stripe_free";
139
140
186k
  if( internal_stripe == NULL )
141
0
  {
142
0
    libcerror_error_set(
143
0
     error,
144
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
145
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
146
0
     "%s: invalid stripe.",
147
0
     function );
148
149
0
    return( -1 );
150
0
  }
151
186k
  if( *internal_stripe != NULL )
152
186k
  {
153
186k
    if( ( *internal_stripe )->physical_volume_name != NULL )
154
186k
    {
155
186k
      memory_free(
156
186k
       ( *internal_stripe )->physical_volume_name );
157
186k
    }
158
186k
    memory_free(
159
186k
     *internal_stripe );
160
161
186k
    *internal_stripe = NULL;
162
186k
  }
163
186k
  return( 1 );
164
186k
}
165
166
/* Retrieves the size of the ASCII formatted physical volume name
167
 * Returns 1 if successful or -1 on error
168
 */
169
int libvslvm_stripe_get_physical_volume_name_size(
170
     libvslvm_stripe_t *stripe,
171
     size_t *physical_volume_name_size,
172
     libcerror_error_t **error )
173
0
{
174
0
  libvslvm_internal_stripe_t *internal_stripe = NULL;
175
0
  static char *function                       = "libvslvm_stripe_get_physical_volume_name_size";
176
177
0
  if( stripe == NULL )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
182
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
183
0
     "%s: invalid stripe.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
0
  internal_stripe = (libvslvm_internal_stripe_t *) stripe;
189
190
0
  if( physical_volume_name_size == NULL )
191
0
  {
192
0
    libcerror_error_set(
193
0
     error,
194
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
195
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
196
0
     "%s: invalid physical volume name size.",
197
0
     function );
198
199
0
    return( -1 );
200
0
  }
201
0
  *physical_volume_name_size = internal_stripe->physical_volume_name_size;
202
203
0
  return( 1 );
204
0
}
205
206
/* Retrieves the ASCII formatted physical volume name
207
 * Returns 1 if successful or -1 on error
208
 */
209
int libvslvm_stripe_get_physical_volume_name(
210
     libvslvm_stripe_t *stripe,
211
     char *physical_volume_name,
212
     size_t physical_volume_name_size,
213
     libcerror_error_t **error )
214
241
{
215
241
  libvslvm_internal_stripe_t *internal_stripe = NULL;
216
241
  static char *function                       = "libvslvm_stripe_get_physical_volume_name";
217
218
241
  if( stripe == NULL )
219
0
  {
220
0
    libcerror_error_set(
221
0
     error,
222
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
223
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
224
0
     "%s: invalid stripe.",
225
0
     function );
226
227
0
    return( -1 );
228
0
  }
229
241
  internal_stripe = (libvslvm_internal_stripe_t *) stripe;
230
231
241
  if( physical_volume_name == NULL )
232
0
  {
233
0
    libcerror_error_set(
234
0
     error,
235
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
236
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
237
0
     "%s: invalid physical volume name.",
238
0
     function );
239
240
0
    return( -1 );
241
0
  }
242
241
  if( physical_volume_name_size > (size_t) SSIZE_MAX )
243
0
  {
244
0
    libcerror_error_set(
245
0
     error,
246
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
247
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
248
0
     "%s: invalid physical volume name size value exceeds maximum.",
249
0
     function );
250
251
0
    return( -1 );
252
0
  }
253
241
  if( physical_volume_name_size < internal_stripe->physical_volume_name_size )
254
26
  {
255
26
    libcerror_error_set(
256
26
     error,
257
26
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
258
26
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
259
26
     "%s: invalid physical volume name size value too small.",
260
26
     function );
261
262
26
    return( -1 );
263
26
  }
264
215
  if( memory_copy(
265
215
       physical_volume_name,
266
215
       internal_stripe->physical_volume_name,
267
215
       internal_stripe->physical_volume_name_size ) == NULL )
268
0
  {
269
0
    libcerror_error_set(
270
0
     error,
271
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
272
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
273
0
     "%s: unable to copy physical volume name.",
274
0
     function );
275
276
0
    return( -1 );
277
0
  }
278
215
  physical_volume_name[ internal_stripe->physical_volume_name_size - 1 ] = 0;
279
280
215
  return( 1 );
281
215
}
282
283
/* Sets the physical volume name
284
 * Returns 1 if successful or -1 on error
285
 */
286
int libvslvm_internal_stripe_set_physical_volume_name(
287
     libvslvm_internal_stripe_t *internal_stripe,
288
     const char *physical_volume_name,
289
     size_t physical_volume_name_size,
290
     libcerror_error_t **error )
291
186k
{
292
186k
  static char *function = "libvslvm_internal_stripe_set_physical_volume_name";
293
294
186k
  if( internal_stripe == NULL )
295
0
  {
296
0
    libcerror_error_set(
297
0
     error,
298
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
299
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
300
0
     "%s: invalid stripe.",
301
0
     function );
302
303
0
    return( -1 );
304
0
  }
305
186k
  if( internal_stripe->physical_volume_name != NULL )
306
0
  {
307
0
    libcerror_error_set(
308
0
     error,
309
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
310
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
311
0
     "%s: invalid stripe - physical volume name value already set.",
312
0
     function );
313
314
0
    return( -1 );
315
0
  }
316
186k
  if( physical_volume_name == NULL )
317
0
  {
318
0
    libcerror_error_set(
319
0
     error,
320
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
321
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
322
0
     "%s: invalid physical volume name.",
323
0
     function );
324
325
0
    return( -1 );
326
0
  }
327
186k
  if( ( physical_volume_name_size == 0 )
328
186k
   || ( physical_volume_name_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
329
3
  {
330
3
    libcerror_error_set(
331
3
     error,
332
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
333
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
334
3
     "%s: invalid physical volume name size value out of bounds.",
335
3
     function );
336
337
3
    return( -1 );
338
3
  }
339
186k
  internal_stripe->physical_volume_name = (char *) memory_allocate(
340
186k
                                                    sizeof( char ) * physical_volume_name_size );
341
342
186k
  if( internal_stripe->physical_volume_name == NULL )
343
0
  {
344
0
    libcerror_error_set(
345
0
     error,
346
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
347
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
348
0
     "%s: unable to create physical volume name.",
349
0
     function );
350
351
0
    goto on_error;
352
0
  }
353
186k
  if( memory_copy(
354
186k
       internal_stripe->physical_volume_name,
355
186k
       physical_volume_name,
356
186k
       physical_volume_name_size ) == NULL )
357
0
  {
358
0
    libcerror_error_set(
359
0
     error,
360
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
361
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
362
0
     "%s: unable to copy physical volume name.",
363
0
     function );
364
365
0
    goto on_error;
366
0
  }
367
186k
  internal_stripe->physical_volume_name[ physical_volume_name_size - 1 ] = 0;
368
369
186k
  internal_stripe->physical_volume_name_size = physical_volume_name_size;
370
371
186k
  return( 1 );
372
373
0
on_error:
374
0
  if( internal_stripe->physical_volume_name != NULL )
375
0
  {
376
0
    memory_free(
377
0
     internal_stripe->physical_volume_name );
378
379
0
    internal_stripe->physical_volume_name = NULL;
380
0
  }
381
0
  internal_stripe->physical_volume_name_size = 0;
382
383
0
  return( -1 );
384
186k
}
385
386
/* Retrieves the data area offset
387
 * Returns 1 if successful or -1 on error
388
 */
389
int libvslvm_stripe_get_data_area_offset(
390
     libvslvm_stripe_t *stripe,
391
     off64_t *data_area_offset,
392
     libcerror_error_t **error )
393
215
{
394
215
  libvslvm_internal_stripe_t *internal_stripe = NULL;
395
215
  static char *function                       = "libvslvm_stripe_get_data_area_offset";
396
397
215
  if( stripe == NULL )
398
0
  {
399
0
    libcerror_error_set(
400
0
     error,
401
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
402
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
403
0
     "%s: invalid stripe.",
404
0
     function );
405
406
0
    return( -1 );
407
0
  }
408
215
  internal_stripe = (libvslvm_internal_stripe_t *) stripe;
409
410
215
  if( data_area_offset == NULL )
411
0
  {
412
0
    libcerror_error_set(
413
0
     error,
414
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
415
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
416
0
     "%s: invalid data area offset.",
417
0
     function );
418
419
0
    return( -1 );
420
0
  }
421
215
  *data_area_offset = internal_stripe->data_area_offset;
422
423
215
  return( 1 );
424
215
}
425
426
/* Sets the data area offset
427
 * Returns 1 if successful or -1 on error
428
 */
429
int libvslvm_stripe_set_data_area_offset(
430
     libvslvm_stripe_t *stripe,
431
     off64_t data_area_offset,
432
     libcerror_error_t **error )
433
186k
{
434
186k
  libvslvm_internal_stripe_t *internal_stripe = NULL;
435
186k
  static char *function                       = "libvslvm_stripe_set_data_area_offset";
436
437
186k
  if( stripe == NULL )
438
0
  {
439
0
    libcerror_error_set(
440
0
     error,
441
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
442
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
443
0
     "%s: invalid stripe.",
444
0
     function );
445
446
0
    return( -1 );
447
0
  }
448
186k
  internal_stripe = (libvslvm_internal_stripe_t *) stripe;
449
450
186k
  internal_stripe->data_area_offset = data_area_offset;
451
452
186k
  return( 1 );
453
186k
}
454