Coverage Report

Created: 2025-06-13 07:22

/src/libfshfs/libfshfs/libfshfs_io_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Input/Output (IO) handle functions
3
 *
4
 * Copyright (C) 2009-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfshfs_io_handle.h"
28
#include "libfshfs_libcerror.h"
29
#include "libfshfs_profiler.h"
30
31
/* Creates an IO handle
32
 * Make sure the value io_handle is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libfshfs_io_handle_initialize(
36
     libfshfs_io_handle_t **io_handle,
37
     libcerror_error_t **error )
38
5.02k
{
39
5.02k
  static char *function = "libfshfs_io_handle_initialize";
40
41
5.02k
  if( io_handle == 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 IO handle.",
48
0
     function );
49
50
0
    return( -1 );
51
0
  }
52
5.02k
  if( *io_handle != 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 IO handle value already set.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
5.02k
  *io_handle = memory_allocate_structure(
64
5.02k
                libfshfs_io_handle_t );
65
66
5.02k
  if( *io_handle == 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 IO handle.",
73
0
     function );
74
75
0
    goto on_error;
76
0
  }
77
5.02k
  if( memory_set(
78
5.02k
       *io_handle,
79
5.02k
       0,
80
5.02k
       sizeof( libfshfs_io_handle_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 IO handle.",
87
0
     function );
88
89
0
    memory_free(
90
0
     *io_handle );
91
92
0
    *io_handle = NULL;
93
94
0
    return( -1 );
95
0
  }
96
#if defined( HAVE_PROFILER )
97
  if( libfshfs_profiler_initialize(
98
       &( ( *io_handle )->profiler ),
99
       error ) != 1 )
100
  {
101
    libcerror_error_set(
102
     error,
103
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
104
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
105
     "%s: unable to initialize profiler.",
106
     function );
107
108
    goto on_error;
109
  }
110
  if( libfshfs_profiler_open(
111
       ( *io_handle )->profiler,
112
       "profiler.csv",
113
       error ) != 1 )
114
  {
115
    libcerror_error_set(
116
     error,
117
     LIBCERROR_ERROR_DOMAIN_IO,
118
     LIBCERROR_IO_ERROR_OPEN_FAILED,
119
     "%s: unable to open profiler.",
120
     function );
121
122
    goto on_error;
123
  }
124
#endif /* defined( HAVE_PROFILER ) */
125
126
5.02k
  return( 1 );
127
128
0
on_error:
129
0
  if( *io_handle != NULL )
130
0
  {
131
#if defined( HAVE_PROFILER )
132
    if( ( *io_handle )->profiler != NULL )
133
    {
134
      libfshfs_profiler_free(
135
       &( ( *io_handle )->profiler ),
136
       NULL );
137
    }
138
#endif
139
0
    memory_free(
140
0
     *io_handle );
141
142
0
    *io_handle = NULL;
143
0
  }
144
0
  return( -1 );
145
5.02k
}
146
147
/* Frees an IO handle
148
 * Returns 1 if successful or -1 on error
149
 */
150
int libfshfs_io_handle_free(
151
     libfshfs_io_handle_t **io_handle,
152
     libcerror_error_t **error )
153
5.02k
{
154
5.02k
  static char *function = "libfshfs_io_handle_free";
155
5.02k
  int result            = 1;
156
157
5.02k
  if( io_handle == NULL )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
162
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
163
0
     "%s: invalid IO handle.",
164
0
     function );
165
166
0
    return( -1 );
167
0
  }
168
5.02k
  if( *io_handle != NULL )
169
5.02k
  {
170
#if defined( HAVE_PROFILER )
171
    if( libfshfs_profiler_close(
172
         ( *io_handle )->profiler,
173
         error ) != 0 )
174
    {
175
      libcerror_error_set(
176
       error,
177
       LIBCERROR_ERROR_DOMAIN_IO,
178
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
179
       "%s: unable to close profiler.",
180
       function );
181
182
      result = -1;
183
    }
184
    if( libfshfs_profiler_free(
185
         &( ( *io_handle )->profiler ),
186
         error ) != 1 )
187
    {
188
      libcerror_error_set(
189
       error,
190
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
191
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
192
       "%s: unable to free profiler.",
193
       function );
194
195
      result = -1;
196
    }
197
#endif /* defined( HAVE_PROFILER ) */
198
5.02k
    memory_free(
199
5.02k
     *io_handle );
200
201
5.02k
    *io_handle = NULL;
202
5.02k
  }
203
5.02k
  return( result );
204
5.02k
}
205
206
/* Clears the IO handle
207
 * Returns 1 if successful or -1 on error
208
 */
209
int libfshfs_io_handle_clear(
210
     libfshfs_io_handle_t *io_handle,
211
     libcerror_error_t **error )
212
2.65k
{
213
2.65k
  static char *function         = "libfshfs_io_handle_clear";
214
215
#if defined( HAVE_PROFILER )
216
  libfshfs_profiler_t *profiler = NULL;
217
#endif
218
219
2.65k
  if( io_handle == NULL )
220
0
  {
221
0
    libcerror_error_set(
222
0
     error,
223
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
224
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
225
0
     "%s: invalid IO handle.",
226
0
     function );
227
228
0
    return( -1 );
229
0
  }
230
#if defined( HAVE_PROFILER )
231
  profiler = io_handle->profiler;
232
#endif
233
2.65k
  if( memory_set(
234
2.65k
       io_handle,
235
2.65k
       0,
236
2.65k
       sizeof( libfshfs_io_handle_t ) ) == NULL )
237
0
  {
238
0
    libcerror_error_set(
239
0
     error,
240
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
241
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
242
0
     "%s: unable to clear IO handle.",
243
0
     function );
244
245
0
    return( -1 );
246
0
  }
247
248
#if defined( HAVE_PROFILER )
249
  io_handle->profiler = profiler;
250
#endif
251
2.65k
  return( 1 );
252
2.65k
}
253