Coverage Report

Created: 2025-07-04 07:01

/src/libfsapfs/libfsapfs/libfsapfs_io_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Input/Output (IO) handle functions
3
 *
4
 * Copyright (C) 2018-2024, 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 "libfsapfs_io_handle.h"
27
#include "libfsapfs_libcerror.h"
28
#include "libfsapfs_profiler.h"
29
30
const char fsapfs_container_signature[ 4 ] = { 'N', 'X', 'S', 'B' };
31
const char fsapfs_volume_signature[ 4 ]    = { 'A', 'P', 'S', 'B' };
32
33
/* Creates an IO handle
34
 * Make sure the value io_handle is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfsapfs_io_handle_initialize(
38
     libfsapfs_io_handle_t **io_handle,
39
     libcerror_error_t **error )
40
9.31k
{
41
9.31k
  static char *function = "libfsapfs_io_handle_initialize";
42
43
9.31k
  if( io_handle == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid IO handle.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
9.31k
  if( *io_handle != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid IO handle value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
9.31k
  *io_handle = memory_allocate_structure(
66
9.31k
                libfsapfs_io_handle_t );
67
68
9.31k
  if( *io_handle == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
73
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
74
0
     "%s: unable to create IO handle.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
9.31k
  if( memory_set(
80
9.31k
       *io_handle,
81
9.31k
       0,
82
9.31k
       sizeof( libfsapfs_io_handle_t ) ) == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
88
0
     "%s: unable to clear IO handle.",
89
0
     function );
90
91
0
    memory_free(
92
0
     *io_handle );
93
94
0
    *io_handle = NULL;
95
96
0
    return( -1 );
97
0
  }
98
#if defined( HAVE_PROFILER )
99
  if( libfsapfs_profiler_initialize(
100
       &( ( *io_handle )->profiler ),
101
       error ) != 1 )
102
  {
103
    libcerror_error_set(
104
     error,
105
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
106
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
107
     "%s: unable to initialize profiler.",
108
     function );
109
110
    goto on_error;
111
  }
112
  if( libfsapfs_profiler_open(
113
       ( *io_handle )->profiler,
114
       "profiler.csv",
115
       error ) != 1 )
116
  {
117
    libcerror_error_set(
118
     error,
119
     LIBCERROR_ERROR_DOMAIN_IO,
120
     LIBCERROR_IO_ERROR_OPEN_FAILED,
121
     "%s: unable to open profiler.",
122
     function );
123
124
    goto on_error;
125
  }
126
#endif /* defined( HAVE_PROFILER ) */
127
128
9.31k
  ( *io_handle )->bytes_per_sector = 512;
129
9.31k
  ( *io_handle )->block_size       = 4096;
130
131
9.31k
  return( 1 );
132
133
0
on_error:
134
0
  if( *io_handle != NULL )
135
0
  {
136
#if defined( HAVE_PROFILER )
137
    if( ( *io_handle )->profiler != NULL )
138
    {
139
      libfsapfs_profiler_free(
140
       &( ( *io_handle )->profiler ),
141
       NULL );
142
    }
143
#endif
144
0
    memory_free(
145
0
     *io_handle );
146
147
0
    *io_handle = NULL;
148
0
  }
149
0
  return( -1 );
150
9.31k
}
151
152
/* Frees an IO handle
153
 * Returns 1 if successful or -1 on error
154
 */
155
int libfsapfs_io_handle_free(
156
     libfsapfs_io_handle_t **io_handle,
157
     libcerror_error_t **error )
158
9.31k
{
159
9.31k
  static char *function = "libfsapfs_io_handle_free";
160
9.31k
  int result            = 1;
161
162
9.31k
  if( io_handle == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
168
0
     "%s: invalid IO handle.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
9.31k
  if( *io_handle != NULL )
174
9.31k
  {
175
#if defined( HAVE_PROFILER )
176
    if( libfsapfs_profiler_close(
177
         ( *io_handle )->profiler,
178
         error ) != 0 )
179
    {
180
      libcerror_error_set(
181
       error,
182
       LIBCERROR_ERROR_DOMAIN_IO,
183
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
184
       "%s: unable to close profiler.",
185
       function );
186
187
      result = -1;
188
    }
189
    if( libfsapfs_profiler_free(
190
         &( ( *io_handle )->profiler ),
191
         error ) != 1 )
192
    {
193
      libcerror_error_set(
194
       error,
195
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
196
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
197
       "%s: unable to free profiler.",
198
       function );
199
200
      result = -1;
201
    }
202
#endif /* defined( HAVE_PROFILER ) */
203
204
9.31k
    memory_free(
205
9.31k
     *io_handle );
206
207
9.31k
    *io_handle = NULL;
208
9.31k
  }
209
9.31k
  return( result );
210
9.31k
}
211
212
/* Clears the IO handle
213
 * Returns 1 if successful or -1 on error
214
 */
215
int libfsapfs_io_handle_clear(
216
     libfsapfs_io_handle_t *io_handle,
217
     libcerror_error_t **error )
218
5.26k
{
219
5.26k
  static char *function          = "libfsapfs_io_handle_clear";
220
221
#if defined( HAVE_PROFILER )
222
  libfsapfs_profiler_t *profiler = NULL;
223
#endif
224
225
5.26k
  if( io_handle == NULL )
226
0
  {
227
0
    libcerror_error_set(
228
0
     error,
229
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
230
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
231
0
     "%s: invalid IO handle.",
232
0
     function );
233
234
0
    return( -1 );
235
0
  }
236
#if defined( HAVE_PROFILER )
237
  profiler = io_handle->profiler;
238
#endif
239
5.26k
  if( memory_set(
240
5.26k
       io_handle,
241
5.26k
       0,
242
5.26k
       sizeof( libfsapfs_io_handle_t ) ) == NULL )
243
0
  {
244
0
    libcerror_error_set(
245
0
     error,
246
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
247
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
248
0
     "%s: unable to clear IO handle.",
249
0
     function );
250
251
0
    return( -1 );
252
0
  }
253
5.26k
  io_handle->bytes_per_sector = 512;
254
5.26k
  io_handle->block_size       = 4096;
255
256
#if defined( HAVE_PROFILER )
257
  io_handle->profiler = profiler;
258
#endif
259
5.26k
  return( 1 );
260
5.26k
}
261