/* Determines if a file exists using get file attibutes * This function uses the WINAPI functions for Windows XP or later * Returns 1 if the file exists, 0 if not or -1 on error */ int libcfile_file_exists( const char *filename, libcerror_error_t **error ) { static char *function = "libcfile_file_exists"; int result = 1; DWORD error_code = 0; DWORD file_attributes = 0; if( filename == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid filename.", function ); return( -1 ); } file_attributes = GetFileAttributesA( (LPCSTR) filename ); if( file_attributes == INVALID_FILE_ATTRIBUTES ) { error_code = GetLastError(); switch( error_code ) { case ERROR_ACCESS_DENIED: result = 1; break; case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: result = 0; break; default: libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_GENERIC, error_code, "%s: unable to determine attributes of file: %s.", function, filename ); return( -1 ); } } return( result ); }
/* Releases a read/write lock for writing * Returns 1 if successful or -1 on error */ int libcthreads_read_write_lock_release_for_write( libcthreads_read_write_lock_t *read_write_lock, libcerror_error_t **error ) { libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL; static char *function = "libcthreads_read_write_lock_release_for_write"; #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( read_write_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid read/write lock.", function ); return( -1 ); } internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) ReleaseSRWLockExclusive( &( internal_read_write_lock->slim_read_write_lock ) ); #elif defined( WINAPI ) && ( WINVER > 0x0500 ) LeaveCriticalSection( &( internal_read_write_lock->write_critical_section ) ); #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_rwlock_unlock( &( internal_read_write_lock->read_write_lock ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to unlock read/write lock.", function ); return( -1 ); } #endif return( 1 ); }
/* Releases a lock * Returns 1 if successful or -1 on error */ int libcthreads_lock_release( const libcthreads_lock_t *lock, libcerror_error_t **error ) { libcthreads_internal_lock_t *internal_lock = NULL; static char *function = "libcthreads_lock_release"; #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid lock.", function ); return( -1 ); } internal_lock = (libcthreads_internal_lock_t *) lock; #if defined( WINAPI ) LeaveCriticalSection( &( internal_lock->critical_section ) ); #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_unlock( &( internal_lock->mutex ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to unlock mutex.", function ); return( -1 ); } #endif return( 1 ); }
/* Closes the file stream * This function uses the POSIX fclose function or equivalent * Returns 0 if successful or -1 on error */ int libcfile_stream_close( libcfile_stream_t *stream, libcerror_error_t **error ) { libcfile_internal_stream_t *internal_stream = NULL; static char *function = "libcfile_stream_close"; if( stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid stream.", function ); return( -1 ); } internal_stream = (libcfile_internal_stream_t *) stream; if( internal_stream->stream != NULL ) { if( fclose( internal_stream->stream ) != 0 ) { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_CLOSE_FAILED, errno, "%s: unable to close stream.", function ); return( -1 ); } internal_stream->stream = NULL; } return( 0 ); }
/* Broadcasts a condition * The of this function must be locked by the same mutex as used to wait * This is necessary for the WINAPI pre Vista (0x0600) implementation * Returns 1 if successful or -1 on error */ int libcthreads_condition_broadcast( libcthreads_condition_t *condition, libcerror_error_t **error ) { libcthreads_internal_condition_t *internal_condition = NULL; static char *function = "libcthreads_condition_broadcast"; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; DWORD wait_status = 0; BOOL result = 1; int number_of_waiting_threads = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( condition == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid condition.", function ); return( -1 ); } internal_condition = (libcthreads_internal_condition_t *) condition; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) WakeAllConditionVariable( &( internal_condition->condition_variable ) ); #elif defined( WINAPI ) EnterCriticalSection( &( internal_condition->wait_critical_section ) ); number_of_waiting_threads = internal_condition->number_of_waiting_threads; if( number_of_waiting_threads > 0 ) { internal_condition->signal_is_broadcast = 1; result = ReleaseSemaphore( internal_condition->signal_semaphore_handle, number_of_waiting_threads, 0 ); if( result == 0 ) { error_code = GetLastError(); internal_condition->signal_is_broadcast = 0; } } LeaveCriticalSection( &( internal_condition->wait_critical_section ) ); if( result == 0 ) { libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to release signal semaphore handle.", function ); return( -1 ); } if( number_of_waiting_threads > 0 ) { wait_status = WaitForSingleObject( internal_condition->signal_event_handle, INFINITE ); if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); internal_condition->signal_is_broadcast = 0; libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: wait for no read event handle failed.", function ); return( -1 ); } internal_condition->signal_is_broadcast = 0; } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_cond_broadcast( &( internal_condition->condition ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to broadcast condition.", function ); return( -1 ); } #endif return( 1 ); }
/* Frees a mutex * Returns 1 if successful or -1 on error */ int libcthreads_mutex_free( libcthreads_mutex_t **mutex, libcerror_error_t **error ) { libcthreads_internal_mutex_t *internal_mutex = NULL; static char *function = "libcthreads_mutex_free"; int result = 1; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( mutex == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mutex.", function ); return( -1 ); } if( *mutex != NULL ) { internal_mutex = (libcthreads_internal_mutex_t *) *mutex; *mutex = NULL; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) DeleteCriticalSection( &( internal_mutex->critical_section ) ); #elif defined( WINAPI ) if( CloseHandle( internal_mutex->mutex_handle ) == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free mutex handle.", function ); result = -1; } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_destroy( &( internal_mutex->mutex ) ); if( pthread_result != 0 ) { switch( pthread_result ) { case EBUSY: libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy mutex with error: Resource busy.", function ); break; default: libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy mutex.", function ); break; } result = -1; } #endif memory_free( internal_mutex ); } return( result ); }
/* Creates a read/write lock * Make sure the value read_write_lock is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int libcthreads_read_write_lock_initialize( libcthreads_read_write_lock_t **read_write_lock, libcerror_error_t **error ) { libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL; static char *function = "libcthreads_read_write_lock_initialize"; #if defined( WINAPI ) && ( WINVER > 0x0500 ) && ( WINVER < 0x0600 ) DWORD error_code = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( read_write_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid read/write lock.", function ); return( -1 ); } if( *read_write_lock != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid read/write lock value already set.", function ); return( -1 ); } internal_read_write_lock = memory_allocate_structure( libcthreads_internal_read_write_lock_t ); if( internal_read_write_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create read/write lock.", function ); goto on_error; } if( memory_set( internal_read_write_lock, 0, sizeof( libcthreads_internal_read_write_lock_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear read/write lock.", function ); memory_free( internal_read_write_lock ); return( -1 ); } #if defined( WINAPI ) && ( WINVER >= 0x0600 ) InitializeSRWLock( &( internal_read_write_lock->slim_read_write_lock ) ); #elif defined( WINAPI ) && ( WINVER > 0x0500 ) InitializeCriticalSection( &( internal_read_write_lock->write_critical_section ) ); InitializeCriticalSection( &( internal_read_write_lock->read_critical_section ) ); internal_read_write_lock->no_read_event_handle = CreateEvent( NULL, TRUE, TRUE, NULL ); if( internal_read_write_lock->no_read_event_handle == NULL ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize no read event handle.", function ); goto on_error; } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_rwlock_init( &( internal_read_write_lock->read_write_lock ), NULL ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize read/write lock.", function ); goto on_error; } #endif *read_write_lock = (libcthreads_read_write_lock_t *) internal_read_write_lock; return( 1 ); on_error: if( internal_read_write_lock != NULL ) { #if defined( WINAPI ) && ( WINVER > 0x0500 ) && ( WINVER < 0x0600 ) DeleteCriticalSection( &( internal_read_write_lock->read_critical_section ) ); DeleteCriticalSection( &( internal_read_write_lock->write_critical_section ) ); #endif memory_free( internal_read_write_lock ); } return( -1 ); }
/* Frees a read/write lock * Returns 1 if successful or -1 on error */ int libcthreads_read_write_lock_free( libcthreads_read_write_lock_t **read_write_lock, libcerror_error_t **error ) { libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL; static char *function = "libcthreads_read_write_lock_free"; int result = 1; #if defined( WINAPI ) && ( WINVER > 0x0500 ) && ( WINVER < 0x0600 ) DWORD error_code = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( read_write_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid read/write lock.", function ); return( -1 ); } if( *read_write_lock != NULL ) { internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) *read_write_lock; *read_write_lock = NULL; #if defined( WINAPI ) && ( WINVER > 0x0500 ) && ( WINVER < 0x0600 ) if( CloseHandle( internal_read_write_lock->no_read_event_handle ) == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free no read event handle.", function ); result = -1; } DeleteCriticalSection( &( internal_read_write_lock->read_critical_section ) ); DeleteCriticalSection( &( internal_read_write_lock->write_critical_section ) ); #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_rwlock_destroy( &( internal_read_write_lock->read_write_lock ) ); if( pthread_result != 0 ) { switch( pthread_result ) { case EBUSY: libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy read/write lock with error: Resource busy.", function ); break; default: libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy read/write lock.", function ); break; } result = -1; } #endif memory_free( internal_read_write_lock ); } return( result ); }
/* Waits for a condition * Returns 1 if successful or -1 on error */ int libcthreads_condition_wait( libcthreads_condition_t *condition, libcthreads_mutex_t *mutex, libcerror_error_t **error ) { libcthreads_internal_condition_t *internal_condition = NULL; libcthreads_internal_mutex_t *internal_mutex = NULL; static char *function = "libcthreads_condition_wait"; #if defined( WINAPI ) DWORD error_code = 0; DWORD wait_status = 0; #if ( WINVER >= 0x0600 ) BOOL result = 0; #else int is_last_waiting_thread = 0; #endif #elif defined( HAVE_PTHREAD_H ) int pthread_result = 0; #endif if( condition == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid condition.", function ); return( -1 ); } internal_condition = (libcthreads_internal_condition_t *) condition; if( mutex == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mutex.", function ); return( -1 ); } internal_mutex = (libcthreads_internal_mutex_t *) mutex; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) result = SleepConditionVariableCS( &( internal_condition->condition_variable ), &( internal_mutex->critical_section ), INFINITE ); if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to sleep on condition variable.", function ); return( -1 ); } #elif defined( WINAPI ) && ( WINVER >= 0x0400 ) EnterCriticalSection( &( internal_condition->wait_critical_section ) ); internal_condition->number_of_waiting_threads++; LeaveCriticalSection( &( internal_condition->wait_critical_section ) ); wait_status = SignalObjectAndWait( internal_mutex->mutex_handle, internal_condition->signal_semaphore_handle, INFINITE, FALSE ); if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable signal mutex handle and wait for signal semaphore handle.", function ); return( -1 ); } EnterCriticalSection( &( internal_condition->wait_critical_section ) ); internal_condition->number_of_waiting_threads--; if( ( internal_condition->number_of_waiting_threads == 0 ) && ( internal_condition->signal_is_broadcast != 0 ) ) { is_last_waiting_thread = 1; } LeaveCriticalSection( &( internal_condition->wait_critical_section ) ); if( is_last_waiting_thread != 0 ) { wait_status = SignalObjectAndWait( internal_condition->signal_event_handle, internal_mutex->mutex_handle, INFINITE, FALSE ); if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to signal signal event handle and wait for mutex handle.", function ); return( -1 ); } } else { wait_status = WaitForSingleObject( internal_mutex->mutex_handle, INFINITE ); if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: wait for mutex handle failed.", function ); return( -1 ); } } #elif defined( WINAPI ) #error libcthreads_condition_wait for Windows earlier than NT4 not implemented #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_cond_wait( &( internal_condition->condition ), &( internal_mutex->mutex ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to wait for condition.", function ); return( -1 ); } #endif return( 1 ); }
/* Writes a buffer to the file stream * This function uses the POSIX fwrite function or equivalent * Returns the number of bytes written if successful, or -1 on error */ ssize_t libcfile_stream_write_buffer( libcfile_stream_t *stream, const uint8_t *buffer, size_t size, libcerror_error_t **error ) { libcfile_internal_stream_t *internal_stream = NULL; static char *function = "libcfile_stream_write_buffer"; ssize_t write_count = 0; if( stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid stream.", function ); return( -1 ); } internal_stream = (libcfile_internal_stream_t *) stream; if( internal_stream->stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid stream - missing stream.", function ); return( -1 ); } if( buffer == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid buffer.", function ); return( -1 ); } if( size > (size_t) SSIZE_MAX ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, "%s: invalid size value exceeds maximum.", function ); return( -1 ); } write_count = fwrite( (void *) buffer, 1, size, internal_stream->stream ); if( write_count == 0 ) { if( feof( internal_stream->stream ) == 0 ) { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_WRITE_FAILED, errno, "%s: unable to write to stream.", function ); clearerr( internal_stream->stream ); return( -1 ); } clearerr( internal_stream->stream ); } return( write_count ); }
/* Opens a file stream * This function uses the POSIX fopen function or equivalent * Returns 1 if successful or -1 on error */ int libcfile_stream_open( libcfile_stream_t *stream, const char *filename, int access_flags, libcerror_error_t **error ) { libcfile_internal_stream_t *internal_stream = NULL; const char *stream_io_mode = NULL; static char *function = "libcfile_stream_open"; if( stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid stream.", function ); return( -1 ); } internal_stream = (libcfile_internal_stream_t *) stream; if( ( ( access_flags & LIBCFILE_ACCESS_FLAG_READ ) != 0 ) && ( ( access_flags & LIBCFILE_ACCESS_FLAG_WRITE ) != 0 ) ) { if( ( access_flags & LIBCFILE_ACCESS_FLAG_TRUNCATE ) != 0 ) { stream_io_mode = "wb+"; } else { stream_io_mode = "ab+"; } } else if( ( access_flags & LIBCFILE_ACCESS_FLAG_READ ) != 0 ) { stream_io_mode = "rb"; } else if( ( access_flags & LIBCFILE_ACCESS_FLAG_WRITE ) != 0 ) { if( ( access_flags & LIBCFILE_ACCESS_FLAG_TRUNCATE ) != 0 ) { stream_io_mode = "wb"; } else { stream_io_mode = "ab"; } } else { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, "%s: unsupported access flags: 0x%02x.", function, access_flags ); return( -1 ); } #if defined( HAVE_GLIB_H ) internal_stream->stream = g_fopen( filename, stream_io_mode ); #else internal_stream->stream = fopen( filename, stream_io_mode ); #endif if( internal_stream->stream == NULL ) { switch( errno ) { case EACCES: libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_ACCESS_DENIED, "%s: access denied to file: %" PRIs_LIBCSTRING_SYSTEM ".", function, filename ); break; case ENOENT: libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_INVALID_RESOURCE, "%s: no such file: %" PRIs_LIBCSTRING_SYSTEM ".", function, filename ); break; default: libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, errno, "%s: unable to open file: %" PRIs_LIBCSTRING_SYSTEM ".", function, filename ); break; } return( -1 ); } return( 1 ); }
/* Reads a chunk of data from the file descriptor into the buffer * Returns the number of bytes read, 0 if at end of input or -1 on error */ ssize_t ewfacquirestream_read_chunk( libewf_handle_t *handle, int input_file_descriptor, uint8_t *buffer, size_t buffer_size, size32_t chunk_size, ssize64_t total_read_count, uint8_t read_error_retries, libcerror_error_t **error ) { static char *function = "ewfacquirestream_read_chunk"; ssize_t read_count = 0; ssize_t buffer_offset = 0; size_t read_size = 0; size_t bytes_to_read = 0; int32_t read_number_of_errors = 0; uint32_t read_error_offset = 0; if( handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid handle.", function ); return( -1 ); } if( input_file_descriptor == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid input file descriptor.", function ); return( -1 ); } if( buffer == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid buffer.", function ); return( -1 ); } if( buffer_size > (size_t) SSIZE_MAX ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, "%s: invalid buffer size value exceeds maximum.", function ); return( -1 ); } if( chunk_size == 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, "%s: invalid chunk size value zero or less.", function ); return( -1 ); } if( total_read_count <= -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO, "%s: invalid total read count value less than zero.", function ); return( -1 ); } while( buffer_size > 0 ) { /* Determine the number of bytes to read from the input * Read as much as possible in chunk sizes */ if( buffer_size < (size_t) chunk_size ) { read_size = buffer_size; } else { read_size = chunk_size; } bytes_to_read = read_size; while( read_number_of_errors <= read_error_retries ) { read_count = libcsystem_file_io_read( input_file_descriptor, &( buffer[ buffer_offset + read_error_offset ] ), bytes_to_read ); #if defined( HAVE_VERBOSE_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "%s: read buffer at: %" PRIu64 " of size: %" PRIzd ".\n", function, total_read_count, read_count ); } #endif if( read_count <= -1 ) { if( ( errno == ESPIPE ) || ( errno == EPERM ) || ( errno == ENXIO ) || ( errno == ENODEV ) ) { if( errno == ESPIPE ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: error reading data: invalid seek.", function ); } else if( errno == EPERM ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: error reading data: operation not permitted.", function ); } else if( errno == ENXIO ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: error reading data: no such device or address.", function ); } else if( errno == ENODEV ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: error reading data: no such device.", function ); } else { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, errno, "%s: error reading data.", function ); } return( -1 ); } } else { /* The last read is OK, correct read_count */ if( read_count == (ssize_t) bytes_to_read ) { read_count = read_error_offset + bytes_to_read; } /* The entire read is OK */ if( read_count == (ssize_t) read_size ) { break; } /* If no end of input can be determined */ /* If some bytes were read it is possible that the end of the input reached */ if( read_count > 0 ) { return( (ssize32_t) ( buffer_offset + read_count ) ); } /* No bytes were read */ if( read_count == 0 ) { return( 0 ); } #if defined( HAVE_VERBOSE_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "%s: read error at offset %" PRIjd " after reading %" PRIzd " bytes.\n", function, total_read_count, read_count ); } #endif /* There was a read error at a certain offset */ read_error_offset += (uint32_t) read_count; bytes_to_read -= read_count; } read_number_of_errors++; if( read_number_of_errors > read_error_retries ) { return( 0 ); } } buffer_size -= read_count; buffer_offset += read_count; /* At the end of the input */ if( ewfacquirestream_abort != 0 ) { break; } } return( buffer_offset ); }
/* Creates a mutex * Make sure the value mutex is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int libcthreads_mutex_initialize( libcthreads_mutex_t **mutex, libcerror_error_t **error ) { libcthreads_internal_mutex_t *internal_mutex = NULL; static char *function = "libcthreads_mutex_initialize"; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( mutex == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mutex.", function ); return( -1 ); } if( *mutex != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid mutex value already set.", function ); return( -1 ); } internal_mutex = memory_allocate_structure( libcthreads_internal_mutex_t ); if( internal_mutex == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create mutex.", function ); goto on_error; } if( memory_set( internal_mutex, 0, sizeof( libcthreads_internal_mutex_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear mutex.", function ); goto on_error; } #if defined( WINAPI ) && ( WINVER >= 0x0600 ) InitializeCriticalSection( &( internal_mutex->critical_section ) ); #elif defined( WINAPI ) internal_mutex->mutex_handle = CreateMutex( NULL, FALSE, NULL ); if( internal_mutex->mutex_handle == NULL ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize mutex handle.", function ); goto on_error; } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_init( &( internal_mutex->mutex ), NULL ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize mutex.", function ); goto on_error; } #endif *mutex = (libcthreads_mutex_t *) internal_mutex; return( 1 ); on_error: if( internal_mutex != NULL ) { memory_free( internal_mutex ); } return( -1 ); }
/* Releases a mutex * Returns 1 if successful or -1 on error */ int libcthreads_mutex_release( libcthreads_mutex_t *mutex, libcerror_error_t **error ) { libcthreads_internal_mutex_t *internal_mutex = NULL; static char *function = "libcthreads_mutex_release"; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; BOOL result = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( mutex == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mutex.", function ); return( -1 ); } internal_mutex = (libcthreads_internal_mutex_t *) mutex; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) LeaveCriticalSection( &( internal_mutex->critical_section ) ); #elif defined( WINAPI ) result = ReleaseMutex( internal_mutex->mutex_handle ); if( result == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to relates mutex handle.", function ); return( -1 ); } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_unlock( &( internal_mutex->mutex ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to unlock mutex.", function ); return( -1 ); } #endif return( 1 ); }
/* Tries to grabs a mutex * Returns 1 if successful, 0 if not or -1 on error */ int libcthreads_mutex_try_grab( libcthreads_mutex_t *mutex, libcerror_error_t **error ) { libcthreads_internal_mutex_t *internal_mutex = NULL; static char *function = "libcthreads_mutex_try_grab"; int result = 1; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; DWORD wait_status = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( mutex == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mutex.", function ); return( -1 ); } internal_mutex = (libcthreads_internal_mutex_t *) mutex; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) if( TryEnterCriticalSection( &( internal_mutex->critical_section ) ) != 0 ) { result = 1; } else { result = 0; } #elif defined( WINAPI ) wait_status = WaitForSingleObject( internal_mutex->mutex_handle, 0 ); if( wait_status == WAIT_TIMEOUT ) { result = 0; } else if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: wait for mutex handle failed.", function ); return( -1 ); } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_trylock( &( internal_mutex->mutex ) ); if( pthread_result == EBUSY ) { result = 0; } else if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to lock mutex.", function ); return( -1 ); } #endif return( result ); }
/* Signals a condition * The of this function must be locked by the same mutex as used to wait * This is necessary for the WINAPI pre Vista (0x0600) implementation * Returns 1 if successful or -1 on error */ int libcthreads_condition_signal( libcthreads_condition_t *condition, libcerror_error_t **error ) { libcthreads_internal_condition_t *internal_condition = NULL; static char *function = "libcthreads_condition_signal"; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; BOOL result = 1; int number_of_waiting_threads = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( condition == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid condition.", function ); return( -1 ); } internal_condition = (libcthreads_internal_condition_t *) condition; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) WakeConditionVariable( &( internal_condition->condition_variable ) ); #elif defined( WINAPI ) EnterCriticalSection( &( internal_condition->wait_critical_section ) ); number_of_waiting_threads = internal_condition->number_of_waiting_threads; LeaveCriticalSection( &( internal_condition->wait_critical_section ) ); if( number_of_waiting_threads > 0 ) { result = ReleaseSemaphore( internal_condition->signal_semaphore_handle, 1, 0 ); if( result == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to release signal semaphore handle.", function ); return( -1 ); } } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_cond_signal( &( internal_condition->condition ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to signal condition.", function ); return( -1 ); } #endif return( 1 ); }
/* Creates a condition * Make sure the value condition is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int libcthreads_condition_initialize( libcthreads_condition_t **condition, libcerror_error_t **error ) { libcthreads_internal_condition_t *internal_condition = NULL; static char *function = "libcthreads_condition_initialize"; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( condition == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid condition.", function ); return( -1 ); } if( *condition != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid condition value already set.", function ); return( -1 ); } internal_condition = memory_allocate_structure( libcthreads_internal_condition_t ); if( internal_condition == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create condition.", function ); goto on_error; } if( memory_set( internal_condition, 0, sizeof( libcthreads_internal_condition_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear condition.", function ); memory_free( internal_condition ); return( -1 ); } #if defined( WINAPI ) && ( WINVER >= 0x0600 ) InitializeConditionVariable( &( internal_condition->condition_variable ) ); #elif defined( WINAPI ) InitializeCriticalSection( &( internal_condition->wait_critical_section ) ); internal_condition->signal_semaphore_handle = CreateSemaphore ( NULL, 0, INT_MAX, NULL ); if( internal_condition->signal_semaphore_handle == NULL ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize signal semaphore handle.", function ); goto on_error; } internal_condition->signal_event_handle = CreateEvent ( NULL, FALSE, FALSE, NULL ); if( internal_condition->signal_event_handle == NULL ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize signal event handle.", function ); goto on_error; } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_cond_init( &( internal_condition->condition ), NULL ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize condition.", function ); goto on_error; } #endif *condition = (libcthreads_condition_t *) internal_condition; return( 1 ); on_error: if( internal_condition != NULL ) { #if defined( WINAPI ) && ( WINVER < 0x0600 ) if( internal_condition->signal_semaphore_handle != NULL ) { CloseHandle( internal_condition->signal_semaphore_handle ); } DeleteCriticalSection( &( internal_condition->wait_critical_section ) ); #endif memory_free( internal_condition ); } return( -1 ); }
/* Seeks a certain offset within the file stream * This function uses the POSIX fseeko and ftello functions or equivalent * Returns the offset if the seek is successful or -1 on error */ off64_t libcfile_stream_seek_offset( libcfile_stream_t *stream, off64_t offset, int whence, libcerror_error_t **error ) { libcfile_internal_stream_t *internal_stream = NULL; static char *function = "libcfile_stream_seek_offset"; int result = 0; if( stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid stream.", function ); return( -1 ); } internal_stream = (libcfile_internal_stream_t *) stream; if( internal_stream->stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid stream - missing stream.", function ); return( -1 ); } #if defined( WINAPI ) && ( LONG_MAX < INT64_MAX ) if( offset > (off64_t) LONG_MAX ) #else if( offset > (off64_t) INT64_MAX ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, "%s: invalid offset value exceeds maximum.", function ); return( -1 ); } if( ( whence != SEEK_CUR ) && ( whence != SEEK_END ) && ( whence != SEEK_SET ) ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, "%s: unsupported whence.", function ); return( -1 ); } #if defined( WINAPI ) result = fseek( internal_stream->stream, (long) offset, whence ); #elif defined( HAVE_FSEEKO64 ) && !defined( HAVE_FSEEKO ) result = fseeko64( internal_stream->stream, offset, whence ); #else result = fseeko( internal_stream->stream, (off_t) offset, whence ); #endif if( result != 0 ) { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_SEEK_FAILED, errno, "%s: unable to seek offset in stream.", function ); return( -1 ); } #if defined( WINAPI ) offset = (off64_t) ftell( internal_stream->stream ); #elif defined( HAVE_FTELLO64 ) && !defined( HAVE_FTELLO ) offset = ftello64( internal_stream->stream ); #else offset = (off64_t) ftello( internal_stream->stream ); #endif if( offset < 0 ) { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, errno, "%s: unable to get offset from stream.", function ); return( -1 ); } return( offset ); }
/* Creates a file system * Make sure the value file_system is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int mount_file_system_initialize( mount_file_system_t **file_system, libcerror_error_t **error ) { #if defined( WINAPI ) FILETIME filetime; SYSTEMTIME systemtime; #elif defined( HAVE_CLOCK_GETTIME ) struct timespec time_structure; #endif static char *function = "mount_file_system_initialize"; #if defined( WINAPI ) DWORD error_code = 0; #else int64_t timestamp = 0; #endif if( file_system == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid file system.", function ); return( -1 ); } if( *file_system != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid file system value already set.", function ); return( -1 ); } *file_system = memory_allocate_structure( mount_file_system_t ); if( *file_system == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create file system.", function ); goto on_error; } if( memory_set( *file_system, 0, sizeof( mount_file_system_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear file system.", function ); memory_free( *file_system ); *file_system = NULL; return( -1 ); } if( libcdata_array_initialize( &( ( *file_system )->handles_array ), 0, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize handles array.", function ); goto on_error; } #if defined( WINAPI ) if( memory_set( &systemtime, 0, sizeof( SYSTEMTIME ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear systemtime.", function ); goto on_error; } GetSystemTime( &systemtime ); if( SystemTimeToFileTime( &systemtime, &filetime ) == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, error_code, "%s: unable to retrieve FILETIME of current time.", function ); goto on_error; } ( *file_system )->mounted_timestamp = ( (uint64_t) filetime.dwHighDateTime << 32 ) | filetime.dwLowDateTime; #elif defined( HAVE_CLOCK_GETTIME ) if( clock_gettime( CLOCK_REALTIME, &time_structure ) != 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve current time structure.", function ); goto on_error; } timestamp = ( (int64_t) time_structure.tv_sec * 1000000000 ) + time_structure.tv_nsec; ( *file_system )->mounted_timestamp = (uint64_t) timestamp; #else timestamp = (int64_t) time( NULL ); if( timestamp == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve current time.", function ); goto on_error; } timestamp *= 1000000000; ( *file_system )->mounted_timestamp = (uint64_t) timestamp; #endif /* defined( HAVE_CLOCK_GETTIME ) */ return( 1 ); on_error: if( *file_system != NULL ) { memory_free( *file_system ); *file_system = NULL; } return( -1 ); }
/* Retrieves the current offset in the file stream * This function uses the POSIX ftello function or equivalent * Returns 1 if successful or -1 on error */ int libcfile_stream_get_offset( libcfile_stream_t *stream, off64_t *offset, libcerror_error_t **error ) { libcfile_internal_stream_t *internal_stream = NULL; static char *function = "libcfile_stream_get_offset"; if( stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid stream.", function ); return( -1 ); } internal_stream = (libcfile_internal_stream_t *) stream; if( internal_stream->stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid stream - missing stream.", function ); return( -1 ); } if( offset == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid offset.", function ); return( -1 ); } #if defined( WINAPI ) *offset = (off64_t) ftell( internal_stream->stream ); #elif defined( HAVE_FTELLO64 ) && !defined( HAVE_FTELLO ) *offset = ftello64( internal_stream->stream ); #else *offset = (off64_t) ftello( internal_stream->stream ); #endif if( *offset < 0 ) { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, errno, "%s: unable to get offset from stream.", function ); return( -1 ); } return( 1 ); }
/* Grabs a read/write lock for writing * Returns 1 if successful or -1 on error */ int libcthreads_read_write_lock_grab_for_write( libcthreads_read_write_lock_t *read_write_lock, libcerror_error_t **error ) { libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL; static char *function = "libcthreads_read_write_lock_grab_for_write"; #if defined( WINAPI ) && ( WINVER > 0x0500 ) && ( WINVER < 0x0600 ) DWORD error_code = 0; DWORD wait_status = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( read_write_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid read/write lock.", function ); return( -1 ); } internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) AcquireSRWLockExclusive( &( internal_read_write_lock->slim_read_write_lock ) ); #elif defined( WINAPI ) && ( WINVER > 0x0500 ) EnterCriticalSection( &( internal_read_write_lock->write_critical_section ) ); wait_status = WaitForSingleObject( internal_read_write_lock->no_read_event_handle, INFINITE ); if( wait_status == WAIT_FAILED ) { error_code = GetLastError(); LeaveCriticalSection( &( internal_read_write_lock->write_critical_section ) ); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: wait for no read event handle failed.", function ); return( -1 ); } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_rwlock_wrlock( &( internal_read_write_lock->read_write_lock ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to lock read/write lock for write.", function ); return( -1 ); } #endif return( 1 ); }
/* Determines if a file exists * This function uses the POSIX stat function or equivalent * Returns 1 if the file exists, 0 if not or -1 on error */ int libcfile_file_exists( const char *filename, libcerror_error_t **error ) { struct stat file_statistics; static char *function = "libcfile_file_exists"; int result = 0; if( filename == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid filename.", function ); return( -1 ); } if( memory_set( &file_statistics, 0, sizeof( struct stat ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear file statistics.", function ); return( -1 ); } result = stat( filename, &file_statistics ); if( result != 0 ) { switch( errno ) { case EACCES: result = 1; break; case ENOENT: result = 0; break; default: libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_GENERIC, errno, "%s: unable to stat file: %" PRIs_LIBCSTRING_SYSTEM ".", function, filename ); return( -1 ); } } else { result = 1; } return( result ); }
/* Releases a read/write lock for reading * Returns 1 if successful or -1 on error */ int libcthreads_read_write_lock_release_for_read( libcthreads_read_write_lock_t *read_write_lock, libcerror_error_t **error ) { libcthreads_internal_read_write_lock_t *internal_read_write_lock = NULL; static char *function = "libcthreads_read_write_lock_release_for_read"; #if defined( WINAPI ) && ( WINVER > 0x0500 ) && ( WINVER < 0x0600 ) DWORD error_code = 0; BOOL result = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( read_write_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid read/write lock.", function ); return( -1 ); } internal_read_write_lock = (libcthreads_internal_read_write_lock_t *) read_write_lock; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) ReleaseSRWLockShared( &( internal_read_write_lock->slim_read_write_lock ) ); #elif defined( WINAPI ) && ( WINVER > 0x0500 ) EnterCriticalSection( &( internal_read_write_lock->read_critical_section ) ); internal_read_write_lock->number_of_readers -= 1; if( internal_read_write_lock->number_of_readers == 0 ) { result = SetEvent( internal_read_write_lock->no_read_event_handle ); if( result == 0 ) { error_code = GetLastError(); internal_read_write_lock->number_of_readers += 1; } } else { result = 1; } LeaveCriticalSection( &( internal_read_write_lock->read_critical_section ) ); if( result == 0 ) { libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: set of no read event failed.", function ); return( -1 ); } #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_rwlock_unlock( &( internal_read_write_lock->read_write_lock ) ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to unlock read/write lock.", function ); return( -1 ); } #endif return( 1 ); }
/* Determines if a file exists * This function uses the POSIX stat function or equivalent * Returns 1 if the file exists, 0 if not or -1 on error */ int libcfile_file_exists_wide( const wchar_t *filename, libcerror_error_t **error ) { struct stat file_statistics; char *narrow_filename = NULL; static char *function = "libcfile_file_exists_wide"; size_t narrow_filename_size = 0; size_t filename_size = 0; int result = 0; if( filename == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid filename.", function ); return( -1 ); } filename_size = 1 + libcstring_wide_string_length( filename ); /* Convert the filename to a narrow string * if the platform has no wide character open function */ if( libclocale_codepage == 0 ) { #if SIZEOF_WCHAR_T == 4 result = libuna_utf8_string_size_from_utf32( (libuna_utf32_character_t *) filename, filename_size, &narrow_filename_size, error ); #elif SIZEOF_WCHAR_T == 2 result = libuna_utf8_string_size_from_utf16( (libuna_utf16_character_t *) filename, filename_size, &narrow_filename_size, error ); #else #error Unsupported size of wchar_t #endif /* SIZEOF_WCHAR_T */ } else { #if SIZEOF_WCHAR_T == 4 result = libuna_byte_stream_size_from_utf32( (libuna_utf32_character_t *) filename, filename_size, libclocale_codepage, &narrow_filename_size, error ); #elif SIZEOF_WCHAR_T == 2 result = libuna_byte_stream_size_from_utf16( (libuna_utf16_character_t *) filename, filename_size, libclocale_codepage, &narrow_filename_size, error ); #else #error Unsupported size of wchar_t #endif /* SIZEOF_WCHAR_T */ } if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_CONVERSION, LIBCERROR_CONVERSION_ERROR_GENERIC, "%s: unable to determine narrow character filename size.", function ); return( -1 ); } narrow_filename = libcstring_narrow_string_allocate( narrow_filename_size ); if( narrow_filename == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create narrow character filename.", function ); return( -1 ); } if( libclocale_codepage == 0 ) { #if SIZEOF_WCHAR_T == 4 result = libuna_utf8_string_copy_from_utf32( (libuna_utf8_character_t *) narrow_filename, narrow_filename_size, (libuna_utf32_character_t *) filename, filename_size, error ); #elif SIZEOF_WCHAR_T == 2 result = libuna_utf8_string_copy_from_utf16( (libuna_utf8_character_t *) narrow_filename, narrow_filename_size, (libuna_utf16_character_t *) filename, filename_size, error ); #else #error Unsupported size of wchar_t #endif /* SIZEOF_WCHAR_T */ } else { #if SIZEOF_WCHAR_T == 4 result = libuna_byte_stream_copy_from_utf32( (uint8_t *) narrow_filename, narrow_filename_size, libclocale_codepage, (libuna_utf32_character_t *) filename, filename_size, error ); #elif SIZEOF_WCHAR_T == 2 result = libuna_byte_stream_copy_from_utf16( (uint8_t *) narrow_filename, narrow_filename_size, libclocale_codepage, (libuna_utf16_character_t *) filename, filename_size, error ); #else #error Unsupported size of wchar_t #endif /* SIZEOF_WCHAR_T */ } if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_CONVERSION, LIBCERROR_CONVERSION_ERROR_GENERIC, "%s: unable to set narrow character filename.", function ); memory_free( narrow_filename ); return( -1 ); } result = stat( narrow_filename, &file_statistics ); memory_free( narrow_filename ); if( result != 0 ) { switch( errno ) { case EACCES: result = 1; break; case ENOENT: result = 0; break; default: libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_GENERIC, errno, "%s: unable to stat file: %" PRIs_LIBCSTRING_SYSTEM ".", function, filename ); return( -1 ); } } else { result = 1; } return( result ); }
/* Frees a lock * Returns 1 if successful or -1 on error */ int libcthreads_lock_free( libcthreads_lock_t **lock, libcerror_error_t **error ) { libcthreads_internal_lock_t *internal_lock = NULL; static char *function = "libcthreads_lock_free"; int result = 1; #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid lock.", function ); return( -1 ); } if( *lock != NULL ) { internal_lock = (libcthreads_internal_lock_t *) *lock; *lock = NULL; #if defined( WINAPI ) DeleteCriticalSection( &( internal_lock->critical_section ) ); #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_destroy( &( internal_lock->mutex ) ); if( pthread_result != 0 ) { switch( pthread_result ) { case EBUSY: libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy mutex with error: Resource busy.", function ); break; default: libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy mutex.", function ); break; } result = -1; } #endif memory_free( internal_lock ); } return( result ); }
/* Frees a condition * Returns 1 if successful or -1 on error */ int libcthreads_condition_free( libcthreads_condition_t **condition, libcerror_error_t **error ) { libcthreads_internal_condition_t *internal_condition = NULL; static char *function = "libcthreads_condition_free"; int result = 1; #if defined( WINAPI ) && ( WINVER < 0x0600 ) DWORD error_code = 0; #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( condition == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid condition.", function ); return( -1 ); } if( *condition != NULL ) { internal_condition = (libcthreads_internal_condition_t *) *condition; *condition = NULL; #if defined( WINAPI ) && ( WINVER >= 0x0600 ) #elif defined( WINAPI ) if( CloseHandle( internal_condition->signal_event_handle ) == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free signal event handle.", function ); result = -1; } if( CloseHandle( internal_condition->signal_semaphore_handle ) == 0 ) { error_code = GetLastError(); libcerror_system_set_error( error, error_code, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free signal semaphore handle.", function ); result = -1; } DeleteCriticalSection( &( internal_condition->wait_critical_section ) ); #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_cond_destroy( &( internal_condition->condition ) ); if( pthread_result != 0 ) { switch( pthread_result ) { case EBUSY: libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy condition with error: Resource busy.", function ); break; default: libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to destroy condition.", function ); break; } result = -1; } #endif memory_free( internal_condition ); } return( result ); }
/* Creates a lock * Make sure the value lock is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int libcthreads_lock_initialize( libcthreads_lock_t **lock, libcerror_error_t **error ) { libcthreads_internal_lock_t *internal_lock = NULL; static char *function = "libcthreads_lock_initialize"; #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI ) int pthread_result = 0; #endif if( lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid lock.", function ); return( -1 ); } if( *lock != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid lock value already set.", function ); return( -1 ); } internal_lock = memory_allocate_structure( libcthreads_internal_lock_t ); if( internal_lock == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create lock.", function ); goto on_error; } if( memory_set( internal_lock, 0, sizeof( libcthreads_internal_lock_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear lock.", function ); goto on_error; } #if defined( WINAPI ) InitializeCriticalSection( &( internal_lock->critical_section ) ); #elif defined( HAVE_PTHREAD_H ) pthread_result = pthread_mutex_init( &( internal_lock->mutex ), NULL ); if( pthread_result != 0 ) { libcerror_system_set_error( error, pthread_result, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize mutex.", function ); goto on_error; } #endif *lock = (libcthreads_lock_t *) internal_lock; return( 1 ); on_error: if( internal_lock != NULL ) { memory_free( internal_lock ); } return( -1 ); }
/* Sends a ATA DEVICE CONFIGURATION IDENTIFY to the file descriptor * Returns 1 if successful or -1 on error */ int libsmdev_ata_get_device_configuration( int file_descriptor, struct hd_driveid *device_configuration, libcerror_error_t **error ) { static char *function = "libsmdev_ata_get_device_configuration"; if( file_descriptor == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid file descriptor.", function ); return( -1 ); } if( device_configuration == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid device configuration.", function ); return( -1 ); } #if defined( HDIO_GET_IDENTITY ) if( ioctl( file_descriptor, HDIO_GET_IDENTITY, device_configuration ) == -1 ) { libcerror_system_set_error( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_IOCTL_FAILED, errno, "%s: unable to query device for: HDIO_GET_IDENTITY.", function ); return( -1 ); } #if defined( HAVE_DEBUG_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "%s: HDIO_GET_IDENTITY:\n", function ); libcnotify_print_data( (uint8_t *) device_configuration, sizeof( struct hd_driveid ), 0 ); libcnotify_printf( "Feature sets:\n" ); libcnotify_printf( "SMART:\t\t\t%d\n", ( device_configuration->command_set_1 & 0x0001 ) ); libcnotify_printf( "Security Mode:\t\t%d (%d)\n", ( device_configuration->command_set_1 & 0x0002 ) >> 1, ( device_configuration->dlf & 0x0001 ) ); libcnotify_printf( "Security Mode enabled:\t%d\n", ( device_configuration->dlf & 0x0002 ) >> 1 ); libcnotify_printf( "Removable Media:\t%d\n", ( device_configuration->command_set_1 & 0x0004 ) >> 2 ); libcnotify_printf( "HPA:\t\t\t%d\n", ( device_configuration->command_set_1 & 0x0400 ) >> 10 ); libcnotify_printf( "DCO:\t\t\t%d\n", ( device_configuration->command_set_2 & 0x0800 ) >> 11 ); libcnotify_printf( "Media serial:\t\t%d\n", ( device_configuration->cfsse & 0x0004 ) >> 2 ); libcnotify_printf( "\n" ); }