mirror of
https://github.com/martijnvanbrummelen/nwipe.git
synced 2026-02-19 21:22:12 +00:00
Formatting device module.
This commit is contained in:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -23,4 +23,4 @@ jobs:
|
||||
- name: verifying code style
|
||||
# TODO use check-format when all the code has been formatted.
|
||||
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c && git diff-index --quiet HEAD
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c && git diff-index --quiet HEAD
|
||||
|
||||
2
.github/workflows/ci_ubuntu-16.04.yml
vendored
2
.github/workflows/ci_ubuntu-16.04.yml
vendored
@@ -23,4 +23,4 @@ jobs:
|
||||
- name: verifying code style
|
||||
# TODO use check-format when all the code has been formatted.
|
||||
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c && git diff-index --quiet HEAD
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c && git diff-index --quiet HEAD
|
||||
|
||||
316
src/device.c
316
src/device.c
@@ -2,9 +2,9 @@
|
||||
* device.c: Device routines for nwipe.
|
||||
*
|
||||
* Copyright Darik Horn <dajhorn-dban@vanadac.com>.
|
||||
*
|
||||
*
|
||||
* Modifications to original dwipe Copyright Andy Beverley <andy@andybev.com>
|
||||
*
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, version 2.
|
||||
@@ -16,7 +16,7 @@
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include "options.h"
|
||||
#include "logging.h"
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/hdreg.h> //Drive specific defs
|
||||
#include <linux/hdreg.h> // Drive specific defs
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -39,183 +39,201 @@
|
||||
#include <parted/debug.h>
|
||||
|
||||
int check_device( nwipe_context_t*** c, PedDevice* dev, int dcount );
|
||||
char *trim(char *str);
|
||||
char* trim( char* str );
|
||||
|
||||
int nwipe_device_scan( nwipe_context_t*** c )
|
||||
{
|
||||
/**
|
||||
* Scans the filesystem for storage device names.
|
||||
*
|
||||
* @parameter device_names A reference to a null array pointer.
|
||||
* @modifies device_names Populates device_names with an array of nwipe_contect_t
|
||||
* @returns The number of strings in the device_names array.
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* Scans the filesystem for storage device names.
|
||||
*
|
||||
* @parameter device_names A reference to a null array pointer.
|
||||
* @modifies device_names Populates device_names with an array of nwipe_contect_t
|
||||
* @returns The number of strings in the device_names array.
|
||||
*
|
||||
*/
|
||||
|
||||
PedDevice* dev = NULL;
|
||||
ped_device_probe_all();
|
||||
|
||||
int dcount = 0;
|
||||
PedDevice* dev = NULL;
|
||||
ped_device_probe_all();
|
||||
|
||||
while ((dev = ped_device_get_next (dev)))
|
||||
{
|
||||
if (check_device(c, dev, dcount))
|
||||
dcount++;
|
||||
}
|
||||
int dcount = 0;
|
||||
|
||||
/* Return the number of devices that were found. */
|
||||
return dcount;
|
||||
while( ( dev = ped_device_get_next( dev ) ) )
|
||||
{
|
||||
if( check_device( c, dev, dcount ) )
|
||||
dcount++;
|
||||
}
|
||||
|
||||
/* Return the number of devices that were found. */
|
||||
return dcount;
|
||||
|
||||
} /* nwipe_device_scan */
|
||||
|
||||
int nwipe_device_get( nwipe_context_t*** c, char **devnamelist, int ndevnames )
|
||||
int nwipe_device_get( nwipe_context_t*** c, char** devnamelist, int ndevnames )
|
||||
{
|
||||
/**
|
||||
* Gets information about devices
|
||||
*
|
||||
* @parameter device_names A reference to a null array pointer.
|
||||
* @parameter devnamelist An array of string pointers to the device names
|
||||
* @parameter ndevnames Number of elements in devnamelist
|
||||
* @modifies device_names Populates device_names with an array of nwipe_contect_t
|
||||
* @returns The number of strings in the device_names array.
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* Gets information about devices
|
||||
*
|
||||
* @parameter device_names A reference to a null array pointer.
|
||||
* @parameter devnamelist An array of string pointers to the device names
|
||||
* @parameter ndevnames Number of elements in devnamelist
|
||||
* @modifies device_names Populates device_names with an array of nwipe_contect_t
|
||||
* @returns The number of strings in the device_names array.
|
||||
*
|
||||
*/
|
||||
|
||||
PedDevice* dev = NULL;
|
||||
|
||||
int i;
|
||||
int dcount = 0;
|
||||
PedDevice* dev = NULL;
|
||||
|
||||
for(i = 0; i < ndevnames; i++) {
|
||||
int i;
|
||||
int dcount = 0;
|
||||
|
||||
dev = ped_device_get(devnamelist[i]);
|
||||
if (!dev)
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_WARNING, "Device %s not found", devnamelist[i] );
|
||||
continue;
|
||||
}
|
||||
for( i = 0; i < ndevnames; i++ )
|
||||
{
|
||||
|
||||
if (check_device(c, dev, dcount))
|
||||
dcount++;
|
||||
}
|
||||
dev = ped_device_get( devnamelist[i] );
|
||||
if( !dev )
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_WARNING, "Device %s not found", devnamelist[i] );
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Return the number of devices that were found. */
|
||||
return dcount;
|
||||
if( check_device( c, dev, dcount ) )
|
||||
dcount++;
|
||||
}
|
||||
|
||||
/* Return the number of devices that were found. */
|
||||
return dcount;
|
||||
|
||||
} /* nwipe_device_get */
|
||||
|
||||
int check_device( nwipe_context_t*** c, PedDevice* dev, int dcount )
|
||||
{
|
||||
/* Populate this struct, then assign it to overall array of structs. */
|
||||
nwipe_context_t* next_device;
|
||||
int fd;
|
||||
int idx;
|
||||
/* Populate this struct, then assign it to overall array of structs. */
|
||||
nwipe_context_t* next_device;
|
||||
int fd;
|
||||
int idx;
|
||||
|
||||
/* Check whether this drive is on the excluded drive list ? */
|
||||
idx=0;
|
||||
while ( idx < 10 )
|
||||
{
|
||||
if ( !strcmp( dev->path, nwipe_options.exclude[idx++] ))
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_NOTICE, "Device %s excluded as per command line option -e", dev->path );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/* Check whether this drive is on the excluded drive list ? */
|
||||
idx = 0;
|
||||
while( idx < 10 )
|
||||
{
|
||||
if( !strcmp( dev->path, nwipe_options.exclude[idx++] ) )
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_NOTICE, "Device %s excluded as per command line option -e", dev->path );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Try opening the device to see if it's valid. Close on completion. */
|
||||
if (!ped_device_open(dev))
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_FATAL, "Unable to open device" );
|
||||
return 0;
|
||||
}
|
||||
ped_device_close(dev);
|
||||
|
||||
/* New device, reallocate memory for additional struct pointer */
|
||||
*c = realloc (*c, (dcount+1) * sizeof(nwipe_context_t *));
|
||||
|
||||
next_device = malloc (sizeof(nwipe_context_t));
|
||||
/* Try opening the device to see if it's valid. Close on completion. */
|
||||
if( !ped_device_open( dev ) )
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_FATAL, "Unable to open device" );
|
||||
return 0;
|
||||
}
|
||||
ped_device_close( dev );
|
||||
|
||||
/* Check the allocation. */
|
||||
if( ! next_device )
|
||||
{
|
||||
nwipe_perror( errno, __FUNCTION__, "malloc" );
|
||||
nwipe_log( NWIPE_LOG_FATAL, "Unable to create the array of enumeration contexts." );
|
||||
return 0;
|
||||
}
|
||||
/* New device, reallocate memory for additional struct pointer */
|
||||
*c = realloc( *c, ( dcount + 1 ) * sizeof( nwipe_context_t* ) );
|
||||
|
||||
/* Zero the allocation. */
|
||||
memset( next_device , 0, sizeof( nwipe_context_t ) );
|
||||
next_device = malloc( sizeof( nwipe_context_t ) );
|
||||
|
||||
/* Get device information */
|
||||
next_device->label = dev->model;
|
||||
next_device->device_name = dev->path;
|
||||
next_device->device_size = dev->length * dev->sector_size;
|
||||
next_device->device_size_text = ped_unit_format_byte(dev, dev->length * dev->sector_size);
|
||||
next_device->result = -2;
|
||||
/* Attempt to get serial number of device. */
|
||||
if ( (fd = open ( next_device->device_name = dev->path, O_RDONLY)) == ERR )
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_WARNING, "Unable to open device %s to obtain serial number", next_device->device_name );
|
||||
}
|
||||
/* We don't check the ioctl return status because there are plenty of situations where a serial number may not be
|
||||
* returned by ioctl such as USB drives, logical volumes, encryted volumes, so the log file would have multiple
|
||||
* benign ioctl errors reported which isn't necessarily a problem.
|
||||
*/
|
||||
ioctl(fd, HDIO_GET_IDENTITY, &next_device->identity);
|
||||
close( fd );
|
||||
/* Check the allocation. */
|
||||
if( !next_device )
|
||||
{
|
||||
nwipe_perror( errno, __FUNCTION__, "malloc" );
|
||||
nwipe_log( NWIPE_LOG_FATAL, "Unable to create the array of enumeration contexts." );
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (idx=0; idx<20; idx++) next_device->serial_no[idx]=next_device->identity.serial_no[idx];
|
||||
|
||||
next_device->serial_no[20]=0; /* terminate the string */
|
||||
trim ( (char*) next_device->serial_no ); /* Remove leading/training whitespace from serial number and left justify */
|
||||
/* Zero the allocation. */
|
||||
memset( next_device, 0, sizeof( nwipe_context_t ) );
|
||||
|
||||
nwipe_log( NWIPE_LOG_INFO,"Found drive model=\"%s\", device path=\"%s\", size=\"%s\", serial number=\"%s\"", next_device->label, next_device->device_name, next_device->device_size_text, next_device->serial_no);
|
||||
/* Get device information */
|
||||
next_device->label = dev->model;
|
||||
next_device->device_name = dev->path;
|
||||
next_device->device_size = dev->length * dev->sector_size;
|
||||
next_device->device_size_text = ped_unit_format_byte( dev, dev->length * dev->sector_size );
|
||||
next_device->result = -2;
|
||||
|
||||
(*c)[dcount] = next_device;
|
||||
return 1;
|
||||
/* Attempt to get serial number of device. */
|
||||
if( ( fd = open( next_device->device_name = dev->path, O_RDONLY ) ) == ERR )
|
||||
{
|
||||
nwipe_log( NWIPE_LOG_WARNING, "Unable to open device %s to obtain serial number", next_device->device_name );
|
||||
}
|
||||
|
||||
/*
|
||||
* We don't check the ioctl return status because there are plenty of situations where a serial number may not be
|
||||
* returned by ioctl such as USB drives, logical volumes, encryted volumes, so the log file would have multiple
|
||||
* benign ioctl errors reported which isn't necessarily a problem.
|
||||
*/
|
||||
ioctl( fd, HDIO_GET_IDENTITY, &next_device->identity );
|
||||
close( fd );
|
||||
|
||||
for( idx = 0; idx < 20; idx++ )
|
||||
next_device->serial_no[idx] = next_device->identity.serial_no[idx];
|
||||
|
||||
next_device->serial_no[20] = 0; /* terminate the string */
|
||||
trim( (char*) next_device->serial_no ); /* Remove leading/training whitespace from serial number and left justify */
|
||||
|
||||
nwipe_log( NWIPE_LOG_INFO,
|
||||
"Found drive model=\"%s\", device path=\"%s\", size=\"%s\", serial number=\"%s\"",
|
||||
next_device->label,
|
||||
next_device->device_name,
|
||||
next_device->device_size_text,
|
||||
next_device->serial_no );
|
||||
|
||||
( *c )[dcount] = next_device;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Remove leading/training whitespace from a string and left justify result */
|
||||
char *trim(char *str)
|
||||
char* trim( char* str )
|
||||
{
|
||||
size_t len = 0;
|
||||
char *frontp = str;
|
||||
char *endp = NULL;
|
||||
size_t len = 0;
|
||||
char* frontp = str;
|
||||
char* endp = NULL;
|
||||
|
||||
if( str == NULL )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if( str[0] == '\0' )
|
||||
{
|
||||
return str;
|
||||
}
|
||||
len = strlen(str);
|
||||
endp = str + len;
|
||||
if( str == NULL )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if( str[0] == '\0' )
|
||||
{
|
||||
return str;
|
||||
}
|
||||
len = strlen( str );
|
||||
endp = str + len;
|
||||
|
||||
/* Move the front and back pointers to address the first non-whitespace
|
||||
* characters from each end.
|
||||
*/
|
||||
while( isspace((unsigned char) *frontp) ) { ++frontp; }
|
||||
if( endp != frontp )
|
||||
{
|
||||
while( isspace((unsigned char) *(--endp)) && endp != frontp ) {}
|
||||
}
|
||||
if( str + len - 1 != endp )
|
||||
*(endp + 1) = '\0';
|
||||
else if( frontp != str && endp == frontp )
|
||||
*str = '\0';
|
||||
/* Shift the string so that it starts at str so that if it's dynamically
|
||||
* allocated, we can still free it on the returned pointer. Note the reuse
|
||||
* of endp to mean the front of the string buffer now.
|
||||
*/
|
||||
endp = str;
|
||||
if( frontp != str )
|
||||
{
|
||||
while( *frontp ) { *endp++ = *frontp++; }
|
||||
*endp = '\0';
|
||||
}
|
||||
return str;
|
||||
/*
|
||||
* Move the front and back pointers to address the first non-whitespace
|
||||
* characters from each end.
|
||||
*/
|
||||
while( isspace( (unsigned char) *frontp ) )
|
||||
{
|
||||
++frontp;
|
||||
}
|
||||
if( endp != frontp )
|
||||
{
|
||||
while( isspace( (unsigned char) *( --endp ) ) && endp != frontp )
|
||||
{
|
||||
}
|
||||
}
|
||||
if( str + len - 1 != endp )
|
||||
*( endp + 1 ) = '\0';
|
||||
else if( frontp != str && endp == frontp )
|
||||
*str = '\0';
|
||||
/*
|
||||
* Shift the string so that it starts at str so that if it's dynamically
|
||||
* allocated, we can still free it on the returned pointer. Note the reuse
|
||||
* of endp to mean the front of the string buffer now.
|
||||
*/
|
||||
endp = str;
|
||||
if( frontp != str )
|
||||
{
|
||||
while( *frontp )
|
||||
{
|
||||
*endp++ = *frontp++;
|
||||
}
|
||||
*endp = '\0';
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/* eof */
|
||||
|
||||
13
src/device.h
13
src/device.h
@@ -2,7 +2,7 @@
|
||||
* device.h: Device routines for nwipe.
|
||||
*
|
||||
* Copyright Darik Horn <dajhorn-dban@vanadac.com>.
|
||||
*
|
||||
*
|
||||
* Modifications to original dwipe Copyright Andy Beverley <andy@andybev.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
@@ -16,18 +16,15 @@
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef DEVICE_H_
|
||||
#define DEVICE_H_
|
||||
|
||||
void nwipe_device_identify( nwipe_context_t* c ); /* Get hardware information about the device. */
|
||||
int nwipe_device_scan( nwipe_context_t*** c ); /* Find devices that we can wipe. */
|
||||
int nwipe_device_get( nwipe_context_t*** c, char **devnamelist, int ndevnames ); /* Get info about devices to wipe */
|
||||
void nwipe_device_identify( nwipe_context_t* c ); // Get hardware information about the device.
|
||||
int nwipe_device_scan( nwipe_context_t*** c ); // Find devices that we can wipe.
|
||||
int nwipe_device_get( nwipe_context_t*** c, char** devnamelist, int ndevnames ); // Get info about devices to wipe.
|
||||
|
||||
#endif /* DEVICE_H_ */
|
||||
|
||||
/* eof */
|
||||
|
||||
Reference in New Issue
Block a user