/src/libfwevt/libcnotify/libcnotify_stream.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Notification stream functions |
3 | | * |
4 | | * Copyright (C) 2008-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 <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 | 0 | { |
49 | 0 | static char *function = "libcnotify_stream_set"; |
50 | |
|
51 | 0 | if( libcnotify_stream_opened_in_library != 0 ) |
52 | 0 | { |
53 | 0 | if( libcnotify_stream_close( |
54 | 0 | error ) != 0 ) |
55 | 0 | { |
56 | 0 | libcerror_error_set( |
57 | 0 | error, |
58 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
59 | 0 | LIBCERROR_IO_ERROR_OPEN_FAILED, |
60 | 0 | "%s: unable to close notify stream.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 0 | } |
66 | 0 | libcnotify_stream = stream; |
67 | |
|
68 | 0 | return( 1 ); |
69 | 0 | } |
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 | 0 | { |
79 | 0 | static char *function = "libcnotify_stream_open"; |
80 | |
|
81 | 0 | if( filename == NULL ) |
82 | 0 | { |
83 | 0 | libcerror_error_set( |
84 | 0 | error, |
85 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
86 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
87 | 0 | "%s: invalid filename.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | return( -1 ); |
91 | 0 | } |
92 | 0 | if( libcnotify_stream_opened_in_library != 0 ) |
93 | 0 | { |
94 | 0 | if( libcnotify_stream_close( |
95 | 0 | error ) != 0 ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
100 | 0 | LIBCERROR_IO_ERROR_OPEN_FAILED, |
101 | 0 | "%s: unable to close notify stream.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | return( -1 ); |
105 | 0 | } |
106 | 0 | } |
107 | 0 | libcnotify_stream = file_stream_open( |
108 | 0 | filename, |
109 | 0 | FILE_STREAM_OPEN_APPEND ); |
110 | | |
111 | 0 | if( libcnotify_stream == NULL ) |
112 | 0 | { |
113 | 0 | switch( errno ) |
114 | 0 | { |
115 | 0 | case EACCES: |
116 | 0 | libcerror_error_set( |
117 | 0 | error, |
118 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
119 | 0 | LIBCERROR_IO_ERROR_ACCESS_DENIED, |
120 | 0 | "%s: access denied to file: %s.", |
121 | 0 | function, |
122 | 0 | filename ); |
123 | |
|
124 | 0 | break; |
125 | | |
126 | 0 | case ENOENT: |
127 | 0 | libcerror_error_set( |
128 | 0 | error, |
129 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
130 | 0 | LIBCERROR_IO_ERROR_INVALID_RESOURCE, |
131 | 0 | "%s: no such file: %s.", |
132 | 0 | function, |
133 | 0 | filename ); |
134 | |
|
135 | 0 | break; |
136 | | |
137 | 0 | default: |
138 | 0 | libcerror_error_set( |
139 | 0 | error, |
140 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
141 | 0 | LIBCERROR_IO_ERROR_OPEN_FAILED, |
142 | 0 | "%s: unable to open file: %s.", |
143 | 0 | function, |
144 | 0 | filename ); |
145 | |
|
146 | 0 | break; |
147 | 0 | } |
148 | 0 | return( -1 ); |
149 | 0 | } |
150 | 0 | libcnotify_stream_opened_in_library = 1; |
151 | |
|
152 | 0 | return( 1 ); |
153 | 0 | } |
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 | 0 | { |
161 | 0 | static char *function = "libcnotify_stream_close"; |
162 | |
|
163 | 0 | if( libcnotify_stream_opened_in_library != 0 ) |
164 | 0 | { |
165 | 0 | if( file_stream_close( |
166 | 0 | libcnotify_stream ) != 0 ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
171 | 0 | LIBCERROR_IO_ERROR_OPEN_FAILED, |
172 | 0 | "%s: unable to close stream.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | return( -1 ); |
176 | 0 | } |
177 | 0 | libcnotify_stream = NULL; |
178 | 0 | libcnotify_stream_opened_in_library = 0; |
179 | 0 | } |
180 | 0 | return( 0 ); |
181 | 0 | } |
182 | | |