Implement read command in tool.

This commit is contained in:
2022-10-12 13:09:41 +01:00
parent a373388c3a
commit a09a9821f8
4 changed files with 122 additions and 2 deletions

View File

@@ -4,5 +4,5 @@ find_package(ICU COMPONENTS uc REQUIRED)
include_directories(${ICU_INCLUDE_DIRS}) include_directories(${ICU_INCLUDE_DIRS})
add_executable(aaruformattool main.c main.h aaruformattool.h identify.c info.c helpers.c printhex.c) add_executable(aaruformattool main.c main.h aaruformattool.h identify.c info.c helpers.c read.c printhex.c)
target_link_libraries(aaruformattool "aaruformat" ICU::uc) target_link_libraries(aaruformattool "aaruformat" ICU::uc)

View File

@@ -24,6 +24,7 @@
int identify(char* path); int identify(char* path);
int info(char* path); int info(char* path);
char* byte_array_to_hex_string(const unsigned char* array, int array_size); char* byte_array_to_hex_string(const unsigned char* array, int array_size);
int read(unsigned long long sector_no, char* path);
int printhex(unsigned char* array, unsigned int length, int width, bool color); int printhex(unsigned char* array, unsigned int length, int width, bool color);
#endif // LIBAARUFORMAT_TOOL_AARUFORMATTOOL_H_ #endif // LIBAARUFORMAT_TOOL_AARUFORMATTOOL_H_

View File

@@ -17,7 +17,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <aaruformat.h> #include <aaruformat.h>
@@ -33,6 +35,7 @@ void usage()
printf("Available verbs:\n"); printf("Available verbs:\n");
printf("\tidentify\tIdentifies if the indicated file is a supported AaruFormat image.\n"); printf("\tidentify\tIdentifies if the indicated file is a supported AaruFormat image.\n");
printf("\tinfo\tPrints information about a given AaruFormat image.\n"); printf("\tinfo\tPrints information about a given AaruFormat image.\n");
printf("\tread\tReads a sector and prints it out on screen.\n");
printf("\n"); printf("\n");
printf("For help on the verb invoke the tool with the verb and no arguments.\n"); printf("For help on the verb invoke the tool with the verb and no arguments.\n");
} }
@@ -59,8 +62,22 @@ void usage_info()
printf("\t<filename>\tPath to AaruFormat image to print information from.\n"); printf("\t<filename>\tPath to AaruFormat image to print information from.\n");
} }
void usage_read()
{
printf("\n");
printf("Usage:\n");
printf("aaruformattool read <sector_number> <filename>\n");
printf("Reads a sector and prints it out on screen.\n");
printf("\n");
printf("Arguments:\n");
printf("\t<sector_number>\tSector number to read and print out.\n");
printf("\t<filename>\tPath to AaruFormat image to print information from.\n");
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
uint64_t sector_no = 0;
printf("AaruFormat Tool version %d.%d\n", AARUFORMAT_TOOL_MAJOR_VERSION, AARUFORMAT_TOOL_MINOR_VERSION); printf("AaruFormat Tool version %d.%d\n", AARUFORMAT_TOOL_MAJOR_VERSION, AARUFORMAT_TOOL_MINOR_VERSION);
printf("Copyright (C) 2019-2022 Natalia Portillo\n"); printf("Copyright (C) 2019-2022 Natalia Portillo\n");
printf("libaaruformat version %d.%d\n", LIBAARUFORMAT_MAJOR_VERSION, LIBAARUFORMAT_MINOR_VERSION); printf("libaaruformat version %d.%d\n", LIBAARUFORMAT_MAJOR_VERSION, LIBAARUFORMAT_MINOR_VERSION);
@@ -90,7 +107,6 @@ int main(int argc, char* argv[])
return identify(argv[2]); return identify(argv[2]);
} }
if(strncmp(argv[1], "info", strlen("info")) == 0) if(strncmp(argv[1], "info", strlen("info")) == 0)
{ {
if(argc == 2) if(argc == 2)
@@ -109,6 +125,34 @@ int main(int argc, char* argv[])
return info(argv[2]); return info(argv[2]);
} }
if(strncmp(argv[1], "read", strlen("read")) == 0)
{
if(argc < 4)
{
usage_read();
return -1;
}
if(argc > 5)
{
fprintf(stderr, "Invalid number of arguments\n");
usage_read();
return -1;
}
errno = 0;
sector_no = strtoll(argv[2], NULL, 10);
if(errno != 0)
{
fprintf(stderr, "Invalid sector number\n");
usage_read();
return -1;
}
return read(sector_no, argv[3]);
}
return 0; return 0;
} }

75
tool/read.c Normal file
View File

@@ -0,0 +1,75 @@
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2022 Natalia Portillo.
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.
*/
#include <errno.h>
#include <stdint.h>
#include <aaruformat.h>
#include "aaruformattool.h"
int read(unsigned long long sector_no, char* path)
{
aaruformatContext* ctx;
int32_t res;
uint32_t length;
uint8_t* data;
ctx = aaruf_open(path);
if(ctx == NULL)
{
printf("Error %d when opening AaruFormat image.\n", errno);
return errno;
}
res = aaruf_read_sector(ctx, sector_no, NULL, &length);
if(res != AARUF_STATUS_OK && res != AARUF_ERROR_BUFFER_TOO_SMALL)
{
printf("Error %d when guessing sector size.\n", res);
aaruf_close(ctx);
return res;
}
printf("Sector is %d bytes.\n", length);
data = malloc(length);
if(data == NULL)
{
printf("Error allocating memory for sector.\n");
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
}
res = aaruf_read_sector(ctx, sector_no, data, &length);
if(res != AARUF_STATUS_OK)
{
printf("Error %d when reading sector.\n", res);
aaruf_close(ctx);
return res;
}
printhex(data, length, 16, true);
free(data);
aaruf_close(ctx);
}