Debian Codegraph @

libcnotify/libcnotify_stream.c

raw symbols SCIP
1 /*
2 * Notification stream functions
3 *
4 * Copyright (C) 2008-2024, Joachim Metz <[email protected]>
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 <file_stream.h>
24 #include <types.h>
25
26 #if defined( HAVE_ERRNO_H ) || defined( WINAPI )
27 #include <errno.h>
28 #endif
29
30 #include "libcnotify_libcerror.h"
31 #include "libcnotify_stream.h"
32
33 /* The notification stream
34 */
35 FILE *libcnotify_stream = NULL;
36
37 /* Value to indicate if the notification stream
38 * was opened by the library
39 */
40 int libcnotify_stream_opened_in_library = 0;
41
42 /* Set the stream
43 * Returns 1 if successful or -1 on error
44 */
45 int libcnotify_stream_set(
46 FILE *stream,
47 libcerror_error_t **error )
48 {
49 static char *function = "libcnotify_stream_set";
50
51 if( libcnotify_stream_opened_in_library != 0 )
52 {
53 if( libcnotify_stream_close(
54 error ) != 0 )
55 {
56 libcerror_error_set(
57 error,
58 LIBCERROR_ERROR_DOMAIN_IO,
59 LIBCERROR_IO_ERROR_OPEN_FAILED,
60 "%s: unable to close notify stream.",
61 function );
62
63 return( -1 );
64 }
65 }
66 libcnotify_stream = stream;
67
68 return( 1 );
69 }
70
71 /* Opens the notification stream using a filename
72 * The stream is opened in append mode
73 * Returns 1 if successful or -1 on error
74 */
75 int libcnotify_stream_open(
76 const char *filename,
77 libcerror_error_t **error )
78 {
79 static char *function = "libcnotify_stream_open";
80
81 if( filename == NULL )
82 {
83 libcerror_error_set(
84 error,
85 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
86 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
87 "%s: invalid filename.",
88 function );
89
90 return( -1 );
91 }
92 if( libcnotify_stream_opened_in_library != 0 )
93 {
94 if( libcnotify_stream_close(
95 error ) != 0 )
96 {
97 libcerror_error_set(
98 error,
99 LIBCERROR_ERROR_DOMAIN_IO,
100 LIBCERROR_IO_ERROR_OPEN_FAILED,
101 "%s: unable to close notify stream.",
102 function );
103
104 return( -1 );
105 }
106 }
107 libcnotify_stream = file_stream_open(
108 filename,
109 FILE_STREAM_OPEN_APPEND );
110
111 if( libcnotify_stream == NULL )
112 {
113 switch( errno )
114 {
115 case EACCES:
116 libcerror_error_set(
117 error,
118 LIBCERROR_ERROR_DOMAIN_IO,
119 LIBCERROR_IO_ERROR_ACCESS_DENIED,
120 "%s: access denied to file: %s.",
121 function,
122 filename );
123
124 break;
125
126 case ENOENT:
127 libcerror_error_set(
128 error,
129 LIBCERROR_ERROR_DOMAIN_IO,
130 LIBCERROR_IO_ERROR_INVALID_RESOURCE,
131 "%s: no such file: %s.",
132 function,
133 filename );
134
135 break;
136
137 default:
138 libcerror_error_set(
139 error,
140 LIBCERROR_ERROR_DOMAIN_IO,
141 LIBCERROR_IO_ERROR_OPEN_FAILED,
142 "%s: unable to open file: %s.",
143 function,
144 filename );
145
146 break;
147 }
148 return( -1 );
149 }
150 libcnotify_stream_opened_in_library = 1;
151
152 return( 1 );
153 }
154
155 /* Closes the notification stream if opened using a filename
156 * Returns 0 if successful or -1 on error
157 */
158 int libcnotify_stream_close(
159 libcerror_error_t **error )
160 {
161 static char *function = "libcnotify_stream_close";
162
163 if( libcnotify_stream_opened_in_library != 0 )
164 {
165 if( file_stream_close(
166 libcnotify_stream ) != 0 )
167 {
168 libcerror_error_set(
169 error,
170 LIBCERROR_ERROR_DOMAIN_IO,
171 LIBCERROR_IO_ERROR_OPEN_FAILED,
172 "%s: unable to close stream.",
173 function );
174
175 return( -1 );
176 }
177 libcnotify_stream = NULL;
178 libcnotify_stream_opened_in_library = 0;
179 }
180 return( 0 );
181 }
182