| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 34 |
|
| 35 |
FILE *libcnotify_stream = NULL; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
int libcnotify_stream_opened_in_library = 0; |
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 156 |
|
| 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 |
|