Skip to content

Commit

Permalink
Make the extract path when using the -w option.
Browse files Browse the repository at this point in the history
If the -w option is used during extract, and the path pointed to does
not exist, it should be created. Create the missing directory or
directories if necessary, and add a test for this behavior.
  • Loading branch information
fragglet committed May 17, 2012
1 parent 8336bca commit 3013e7f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
46 changes: 26 additions & 20 deletions src/extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,26 +253,18 @@ static int check_parent_directory(char *path)
}

// Given a file header, create its parent directories as necessary.
// If include_final is zero, the final directory in the path is not
// created.

static int make_parent_directories(LHAFileHeader *header)
static int make_parent_directories(char *orig_path, int include_final)
{
int result;
char *path;
char *p;
char *path;
char saved = 0;

path = header->path;

while (path[0] == '/') {
++path;
}

if (path[0] == '\0') {
return 1;
}

result = 1;
path = strdup(path);
path = strdup(orig_path);

// Iterate through the string, finding each path separator. At
// each place, temporarily chop off the end of the path to get
Expand All @@ -291,18 +283,18 @@ static int make_parent_directories(LHAFileHeader *header)
}

// Check if this parent directory exists and create it;
// however, if this file header is for a directory,
// only create its missing parent headers - not the
// directory itself. That is to say, for an entry:
// however, don't create the final directory in the path
// if include_final is false. This is used for handling
// directories, where we should only create missing
// parent directories - not the directory itself.
// eg. for:
//
// -lhd- subdir/subdir2/
//
// Only create subdir/ - subdir/subdir2/ is created
// by the call to lha_reader_extract.

if (strcmp(header->compress_method, LHA_COMPRESS_TYPE_DIR) != 0
|| strcmp(header->path, path) != 0) {

if (include_final || strcmp(orig_path, path) != 0) {
if (!check_parent_directory(path)) {
result = 0;
break;
Expand Down Expand Up @@ -419,6 +411,7 @@ static void extract_archived_file(LHAReader *reader,
LHAOptions *options)
{
ProgressCallbackData progress;
char *path;
char *filename;
int success;
int is_dir;
Expand Down Expand Up @@ -460,7 +453,13 @@ static void extract_archived_file(LHAReader *reader,
// Create parent directories for file:

if (options->use_path && header->path != NULL) {
if (!make_parent_directories(header)) {
path = header->path;

while (path[0] == '/') {
++path;
}

if (*path != '\0' && !make_parent_directories(path, !is_dir)) {
free(filename);
return;
}
Expand Down Expand Up @@ -518,6 +517,13 @@ void extract_archive(LHAFilter *filter, LHAOptions *options)
// Change directory before extract? (-w option).

if (options->extract_path != NULL) {
if (!make_parent_directories(options->extract_path, 1)) {
fprintf(stderr,
"Failed to create extract directory: %s.\n",
options->extract_path);
exit(-1);
}

if (!lha_arch_chdir(options->extract_path)) {
fprintf(stderr, "Failed to change directory to %s.\n",
options->extract_path);
Expand Down
11 changes: 8 additions & 3 deletions test/test-extract
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,14 @@ test_w_option() {
local archive_file=$1
local filename=$2
local expected_file="$test_base/output/$archive_file-e.txt"
local extract_dir="$w_sandbox"

# Extract into a subdirectory of the 'w' sandbox that does not
# exist: the path should be created as part of the extract.

local extract_dir="$w_sandbox/dir"

if $is_cygwin; then
extract_dir=$(cygpath -w "$w_sandbox")
extract_dir=$(cygpath -w "$w_sandbox/dir")
fi

make_sandboxes
Expand All @@ -255,7 +259,8 @@ test_w_option() {
"ew=$extract_dir" $(test_arc_file "$archive_file")

x_filename=$(simplify_filename "$filename")
if [ ! -e "$w_sandbox/$x_filename" ]; then

if [ ! -e "$w_sandbox/dir/$x_filename" ]; then
fail "Failed to extract $filename from $archive_file"
fi

Expand Down

0 comments on commit 3013e7f

Please sign in to comment.