Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernization refactorization #123

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aff4/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ libaff4_include_HEADERS = \
rdf.h\
aff4_errors.h \
aff4_init.h \
aff4_registry.h \
aff4_utils.h \
data_store.h \
libaff4.h \
Expand All @@ -61,7 +60,8 @@ libaff4_include_HEADERS = \
aff4_symstream.h \
libaff4-c.h \
volume_group.h \
threadpool.h
threadpool.h \
attributes.h

libaff4_la_SOURCES = \
aff4_image.cc \
Expand Down
10 changes: 5 additions & 5 deletions aff4/aff4_directory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ AFF4Status AFF4Directory::NewAFF4Directory(
RETURN_IF_ERROR(MkDir(resolver, root_path.c_str()));
}

resolver->Set(new_obj->urn, AFF4_TYPE, new URN(AFF4_DIRECTORY_TYPE),
resolver->Set(new_obj->urn, AFF4_TYPE, URN(AFF4_DIRECTORY_TYPE),
/* replace= */ false);

resolver->Set(new_obj->urn, AFF4_STORED, new URN(URN::NewURNFromFilename(root_path, true)));
resolver->Set(new_obj->urn, AFF4_STORED, URN::NewURNFromFilename(root_path, true));

result = std::move(new_obj);

Expand All @@ -95,9 +95,9 @@ AFF4Status AFF4Directory::CreateMemberStream(
std::string filename = member_name_for_urn(child, urn, false);

// We are allowed to create any files inside the directory volume.
resolver->Set(child, AFF4_TYPE, new URN(AFF4_FILE_TYPE));
resolver->Set(child, AFF4_STORED, new URN(urn));
resolver->Set(child, AFF4_DIRECTORY_CHILD_FILENAME, new XSDString(filename));
resolver->Set(child, AFF4_TYPE, URN(AFF4_FILE_TYPE));
resolver->Set(child, AFF4_STORED, URN(urn));
resolver->Set(child, AFF4_DIRECTORY_CHILD_FILENAME, XSDString(filename));

// Store the member inside our storage location.
std::string full_path = root_path + PATH_SEP_STR + filename;
Expand Down
25 changes: 12 additions & 13 deletions aff4/aff4_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,11 @@ AFF4Status AFF4Image::NewAFF4Image(
new_obj->urn = image_urn;
new_obj->current_volume = volume;

resolver->Set(image_urn, AFF4_TYPE, new URN(AFF4_IMAGESTREAM_TYPE),
resolver->Set(image_urn, AFF4_TYPE, URN(AFF4_IMAGESTREAM_TYPE),
/* replace = */ false);
resolver->Set(image_urn, AFF4_STORED, new URN(volume->urn));
resolver->Set(image_urn, AFF4_STORED, volume->urn);
if(!resolver->HasURNWithAttribute(image_urn, AFF4_STREAM_SIZE)) {
resolver->Set(image_urn, AFF4_STREAM_SIZE, new XSDInteger((uint64_t)0));
resolver->Set(image_urn, AFF4_STREAM_SIZE, XSDInteger(0ULL));
}

result = std::move(new_obj);
Expand Down Expand Up @@ -541,7 +541,7 @@ AFF4Status AFF4Image::FlushBevy() {
RETURN_IF_ERROR(bevy_member->WriteStream(&bevy_stream, &empty_progress));

// Done with this bevy - make a new writer.
bevy_writer.reset(new _BevyWriter(
bevy_writer.reset(new _BevyWriter( // "new" needed here due to custom _BevyWriter deleter
resolver, compression, chunk_size,
chunks_per_segment));
bevy_number++;
Expand All @@ -556,8 +556,8 @@ AFF4Status AFF4Image::Write(const char* data, size_t length) {

// Prepare a bevy writer to collect the first bevy.
if (bevy_writer == nullptr) {
bevy_writer.reset(new _BevyWriter(resolver, compression, chunk_size,
chunks_per_segment));
bevy_writer.reset(new _BevyWriter(resolver, // "new" needed here due to custom _BevyWriter deleter
compression, chunk_size, chunks_per_segment));
}

// This object is now dirty.
Expand Down Expand Up @@ -882,18 +882,17 @@ AFF4Status AFF4Image::ReadBuffer(char* data, size_t* length) {
}

AFF4Status AFF4Image::_write_metadata() {
resolver->Set(urn, AFF4_TYPE, new URN(AFF4_IMAGESTREAM_TYPE),
resolver->Set(urn, AFF4_TYPE, URN(AFF4_IMAGESTREAM_TYPE),
/* replace = */ false);

resolver->Set(urn, AFF4_STREAM_CHUNK_SIZE, new XSDInteger(chunk_size));
resolver->Set(urn, AFF4_STREAM_CHUNK_SIZE, XSDInteger(chunk_size));

resolver->Set(urn, AFF4_STREAM_CHUNKS_PER_SEGMENT,
new XSDInteger(chunks_per_segment));
XSDInteger(chunks_per_segment));

resolver->Set(urn, AFF4_STREAM_SIZE, new XSDInteger(size));
resolver->Set(urn, AFF4_STREAM_SIZE, XSDInteger(size));

resolver->Set(urn, AFF4_IMAGE_COMPRESSION, new URN(
CompressionMethodToURN(compression)));
resolver->Set(urn, AFF4_IMAGE_COMPRESSION, CompressionMethodToURN(compression));

return STATUS_OK;
}
Expand Down Expand Up @@ -925,7 +924,7 @@ AFF4Status AFF4Image::Flush() {

std::string AFF4Image::_FixupBevyData(std::string* data){
const uint32_t index_size = data->length() / sizeof(uint32_t);
std::unique_ptr<BevyIndex[]> bevy_index_array(new BevyIndex[index_size]);
auto bevy_index_array = absl::make_unique<BevyIndex[]>(index_size);
uint32_t* bevy_index_data = reinterpret_cast<uint32_t*>(&(*data)[0]);

uint64_t cOffset = 0;
Expand Down
24 changes: 9 additions & 15 deletions aff4/aff4_imager_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ AFF4Status BasicImager::ParseArgs() {
if (Get("threads")->isSet()) {
int threads = GetArg<TCLAP::ValueArg<int>>("threads")->getValue();
resolver.logger->info("Will use {} threads.", threads);
resolver.pool.reset(new ThreadPool(threads));
resolver.pool = absl::make_unique<ThreadPool>(threads);
}

// Check for incompatible commands.
Expand Down Expand Up @@ -257,9 +257,7 @@ AFF4Status BasicImager::handle_aff4_volumes() {
}

AFF4Status BasicImager::handle_list() {
URN image_type(AFF4_IMAGE_TYPE);
for (const auto& subject: resolver.Query(
URN(AFF4_TYPE), &image_type)) {
for (const URN& subject: resolver.Query(AFF4_TYPE, URN(AFF4_IMAGE_TYPE))) {
std::cout << subject.SerializeToString() << "\n";
}

Expand Down Expand Up @@ -327,8 +325,7 @@ AFF4Status BasicImager::process_input() {
image_urn.Set(volume->urn.Append(input_stream->urn.Path()));

// Store the original filename.
resolver.Set(image_urn, AFF4_STREAM_ORIGINAL_FILENAME,
new XSDString(input));
resolver.Set(image_urn, AFF4_STREAM_ORIGINAL_FILENAME, XSDString(input));
}

// For very small streams, it is more efficient to just store them without
Expand All @@ -353,7 +350,7 @@ AFF4Status BasicImager::process_input() {

// Make this stream as an Image (Should we have
// another type for a LogicalImage?
resolver.Set(segment->urn, AFF4_TYPE, new URN(AFF4_IMAGE_TYPE),
resolver.Set(segment->urn, AFF4_TYPE, URN(AFF4_IMAGE_TYPE),
/* replace = */ false);

// We need to explicitly check the abort status here.
Expand All @@ -379,7 +376,7 @@ AFF4Status BasicImager::process_input() {

// Make this stream as an Image (Should we have
// another type for a LogicalImage?
resolver.Set(image_urn, AFF4_TYPE, new URN(AFF4_IMAGE_TYPE),
resolver.Set(image_urn, AFF4_TYPE, URN(AFF4_IMAGE_TYPE),
/* replace = */ false);
}
}
Expand Down Expand Up @@ -414,11 +411,8 @@ AFF4Status BasicImager::handle_export() {
}
} else {
// These are all acceptable stream types.
for (const URN image_type : std::vector<URN>{
URN(AFF4_IMAGE_TYPE),
URN(AFF4_MAP_TYPE)
}) {
for (const URN& image: resolver.Query(AFF4_TYPE, &image_type)) {
for (const URN &image_type : std::vector<URN>{ URN(AFF4_IMAGE_TYPE), URN(AFF4_MAP_TYPE)}) {
for (const URN& image: resolver.Query(AFF4_TYPE, image_type)) {
if (aff4::fnmatch(
export_pattern.c_str(),
image.SerializeToString().c_str()) == 0) {
Expand Down Expand Up @@ -561,10 +555,10 @@ AFF4Status BasicImager::GetOutputVolumeURN(URN* volume_urn) {
resolver.logger->warn("Output file {} will be truncated.",
output_volume_backing_urn);
resolver.Set(output_volume_backing_urn,
AFF4_STREAM_WRITE_MODE, new XSDString("truncate"));
AFF4_STREAM_WRITE_MODE, XSDString("truncate"));
} else {
resolver.Set(output_volume_backing_urn,
AFF4_STREAM_WRITE_MODE, new XSDString("append"));
AFF4_STREAM_WRITE_MODE, XSDString("append"));
}

// The output is a directory volume.
Expand Down
64 changes: 33 additions & 31 deletions aff4/aff4_imager_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ class BasicImager {
// We use a list here to preserve insertion order.
std::list<std::unique_ptr<TCLAP::Arg>> args;

void AddArg(TCLAP::Arg* arg) {
args.push_back(std::unique_ptr<TCLAP::Arg>(arg));
template <typename T, typename ...Args>
auto AddArg(Args && ...arg)
-> absl::enable_if_t<std::is_base_of<TCLAP::Arg, T>::value, void> {
args.emplace_back(absl::make_unique<T>(std::forward<Args>(arg)...));
}

TCLAP::Arg* Get(std::string name) {
Expand Down Expand Up @@ -174,81 +176,81 @@ class BasicImager {
virtual AFF4Status Initialize();

virtual AFF4Status RegisterArgs() {
AddArg(new TCLAP::SwitchArg("V", "view", "View AFF4 metadata", false));
AddArg(new TCLAP::SwitchArg("l", "list", "List all image streams in the volume.", false));
AddArg(new TCLAP::MultiSwitchArg(
"d", "debug", "Display debugging logging (repeat for more info)",
false));
AddArg<TCLAP::SwitchArg>("V", "view", "View AFF4 metadata", false);

AddArg(new TCLAP::SwitchArg(
"v", "verbose", "Display more verbose information", false));
AddArg<TCLAP::SwitchArg>("l", "list", "List all image streams in the volume.", false);

AddArg(new TCLAP::SwitchArg(
AddArg<TCLAP::MultiSwitchArg>("d", "debug",
"Display debugging logging (repeat for more info)", false);

AddArg<TCLAP::SwitchArg>("v", "verbose", "Display more verbose information", false);

AddArg<TCLAP::SwitchArg>(
"t", "truncate", "Truncate the output file. Normally volumes and "
"images are appended to existing files, but this flag forces the "
"output file to be truncated first.",
false));
false);

AddArg(new TCLAP::MultiArgToNextFlag(
AddArg<TCLAP::MultiArgToNextFlag>(
"i", "input", "File to image. If specified we copy these file to the "
"output volume located at --output. If there is no AFF4 volume on "
"--output yet, we create a new volume on it. A filename of @ means "
"to read filenames from stdin (In this case no glob expansion will "
"occur). \n"
"This can be specified multiple times with shell expansion. e.g.:\n"
"-i /bin/*",
false, "/path/to/file/or/device"));
false, "/path/to/file/or/device");

AddArg(new TCLAP::SwitchArg(
AddArg<TCLAP::SwitchArg>(
"", "relative", "If specified all files will be written relative to "
"the current directory. By default we write all files' absolute paths",
false));
false);

AddArg(new TCLAP::ValueArg<std::string>(
AddArg<TCLAP::ValueArg<std::string>>(
"e", "export", "Name of the streams to export. If specified we try "
"to open this stream and write it to the export directory. Note that "
"you will also need to specify an AFF4 volume path to load so we know "
"where to find the stream. Specifying a relative URN "
"implies a stream residing in a loaded volume. E.g.\n"

" -e /dev/sda -D /tmp/ my_volume.aff4",
false, "", "string"));
false, "", "string");

AddArg(new TCLAP::ValueArg<std::string>(
AddArg<TCLAP::ValueArg<std::string>>(
"", "logfile", "Specify a file to store log messages to",
false, "", "string"));
false, "", "string");

AddArg(new TCLAP::ValueArg<std::string>(
AddArg<TCLAP::ValueArg<std::string>>(
"D", "export_dir", "Directory to export to (Defaults to current "
"directory)",
false, ".", "path to directory"));
false, ".", "path to directory");

AddArg(new TCLAP::ValueArg<std::string>(
AddArg<TCLAP::ValueArg<std::string>>(
"o", "output", "Output file to write to. If the file does not "
"exist we create it. NOTE: If output is a directory we write an "
"AFF4 Directory volume when imaging. If the output is '-' we write "
"to stdout.", false, "",
"/path/to/file"));
"/path/to/file");

AddArg(new TCLAP::SizeArg(
AddArg<TCLAP::SizeArg>(
"s", "split", "Split output volumes at this size.", false, 0,
"Size (E.g. 100Mb)"));
"Size (E.g. 100Mb)");

AddArg(new TCLAP::ValueArg<std::string>(
AddArg<TCLAP::ValueArg<std::string>>(
"c", "compression", "Type of compression to use (default deflate).",
false, "", "deflate, zlib, snappy, lz4, none"));
false, "", "deflate, zlib, snappy, lz4, none");

AddArg(new TCLAP::ValueArg<int>(
AddArg<TCLAP::ValueArg<int>>(
"", "threads", "Total number of threads to use.",
false, 2, "(default 2)"));
false, 2, "(default 2)");

AddArg(new TCLAP::UnlabeledMultiArg<std::string>(
AddArg<TCLAP::UnlabeledMultiArg<std::string>>(
"aff4_volumes",
"These AFF4 Volumes will be loaded and their metadata will "
"be parsed before the program runs.\n"
"Note that this is necessary before you can extract streams with the "
"--export flag.",
false, "/path/to/aff4/volume"));
false, "/path/to/aff4/volume");

return STATUS_OK;
}
Expand Down
4 changes: 3 additions & 1 deletion aff4/aff4_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ specific language governing permissions and limitations under the License.
#include "aff4/aff4_utils.h"
#include "aff4/rdf.h"

#include <absl/memory/memory.h>

// A constant for various buffers used by the AFF4 library.
#define AFF4_BUFF_SIZE (32 * 1024)

Expand Down Expand Up @@ -200,7 +202,7 @@ class StringIO: public AFF4Stream {

// Convenience constructors.
static std::unique_ptr<StringIO> NewStringIO() {
return std::unique_ptr<StringIO>(new StringIO());
return absl::make_unique<StringIO>();
}

std::string Read(size_t length) override;
Expand Down
9 changes: 5 additions & 4 deletions aff4/aff4_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ specific language governing permissions and limitations under the License.
#include "aff4/volume_group.h"

#include <memory>
#include <absl/memory/memory.h>

#define BUFF_SIZE 1024*1024

Expand Down Expand Up @@ -64,9 +65,9 @@ AFF4Status AFF4Map::NewAFF4Map(
new_object->last_target = data_stream;
new_object->targets.push_back(data_stream);

resolver->Set(object_urn, AFF4_TYPE, new URN(AFF4_MAP_TYPE),
resolver->Set(object_urn, AFF4_TYPE, URN(AFF4_MAP_TYPE),
/* replace= */ false);
resolver->Set(object_urn, AFF4_STORED, new URN(volume->urn));
resolver->Set(object_urn, AFF4_STORED, URN(volume->urn));
new_object->current_volume = volume;

result = std::move(new_object);
Expand Down Expand Up @@ -115,7 +116,7 @@ AFF4Status AFF4Map::OpenAFF4Map(
// Ensure the Range type hasn't added any extra data members
static_assert(sizeof(BinaryRange) == sizeof(Range),
"Range has been extended and must be converted here");
auto buffer = std::unique_ptr<Range[]>{new Range[n]};
auto buffer = absl::make_unique<Range[]>(n);

map_stream->ReadIntoBuffer(buffer.get(), n * sizeof(BinaryRange));

Expand Down Expand Up @@ -514,7 +515,7 @@ AFF4Status AFF4Map::Flush() {
}

// Add the stream size property to the map
resolver->Set(urn, AFF4_STREAM_SIZE, new XSDInteger(size));
resolver->Set(urn, AFF4_STREAM_SIZE, XSDInteger(size));
}

return AFF4Stream::Flush();
Expand Down
Loading