OpenrelikWorkerCommon.FileUtils (openrelik_worker_common v0.1.0)

Copy Markdown View Source

File helper functions for OpenRelik workers (Elixir port of openrelik_worker_common.file_utils).

Summary

Functions

Creates the original file tree structure from a list of %OutputFile{} structs.

Counts the number of lines in a file using wc -l.

Creates and returns an %OutputFile{} struct.

Deletes a temporary file tree folder structure created by build_file_tree/2.

Converts a full path to a relative path without the root anchor.

Checks if inputfile is a disk image based on its "display_name" extension.

Generates a 32-character lowercase hexadecimal UUIDv4 string (uuid4().hex).

Functions

build_file_tree(output_path, files)

@spec build_file_tree(Path.t(), list()) ::
  OpenrelikWorkerCommon.FileUtils.TemporaryDirectory.t() | nil

Creates the original file tree structure from a list of %OutputFile{} structs.

Returns a %TemporaryDirectory{} representing the root path of the file tree, or nil if files is empty or contains non-%OutputFile{} elements.

count_file_lines(file_path)

@spec count_file_lines(Path.t()) :: non_neg_integer()

Counts the number of lines in a file using wc -l.

create_output_file(output_base_path, opts \\ [])

@spec create_output_file(String.t(), keyword() | map()) ::
  OpenrelikWorkerCommon.FileUtils.OutputFile.t()

Creates and returns an %OutputFile{} struct.

Parameters

  • output_base_path - The path to the output directory.
  • opts - Keyword list or map of optional parameters:
    • :display_name - The name of the output file (optional, defaults to generated UUID).
    • :extension - File extension (optional).
    • :data_type - The data type of the output file (optional).
    • :original_path - The original path of the file (optional).
    • :source_file_id - The OutputFile or ID this file belongs to (optional).
    • :register_in_db - Whether the mediator should create a DB row for this file (default true).
    • :uuid - Optional override for the generated UUIDv4 hex string.
    • :uuid_generator - Optional 0-arity function returning a UUID hex string (for testing).

delete_file_tree(root_path)

@spec delete_file_tree(OpenrelikWorkerCommon.FileUtils.TemporaryDirectory.t()) :: :ok

Deletes a temporary file tree folder structure created by build_file_tree/2.

Raises ArgumentError if root_path is not a %TemporaryDirectory{} struct.

get_relative_path(path)

@spec get_relative_path(Path.t()) :: String.t()

Converts a full path to a relative path without the root anchor.

is_disk_image(inputfile)

@spec is_disk_image(map()) :: boolean()

Checks if inputfile is a disk image based on its "display_name" extension.

Raises RuntimeError if "display_name" is not present in inputfile.

uuid4_hex()

@spec uuid4_hex() :: String.t()

Generates a 32-character lowercase hexadecimal UUIDv4 string (uuid4().hex).