Skip to content

Commit

Permalink
fix #9: -o option appears to early with device relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Nov 12, 2016
1 parent 5992291 commit 4f62424
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
12 changes: 8 additions & 4 deletions recoverjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,19 @@ file_name(const char *dir_format, const char *file_format, unsigned int index)
exit(1);
}
strncat(dir_buffer, "/", sizeof dir_buffer - 1);
} else {
}
else {
*dir_buffer = '\0';
}

snprintf(file_buffer, sizeof file_buffer, file_format, index);
strncat(dir_buffer, file_buffer, sizeof dir_buffer - strlen(dir_buffer) - 1);
strncat(dir_buffer, file_buffer,
sizeof dir_buffer - strlen(dir_buffer) - 1);
return dir_buffer;
}

int
main(int argc, const char * const argv[])
main(int argc, const char *const argv[])
{
int fd, fdout;
size_t read_size, block_size;
Expand Down Expand Up @@ -239,7 +241,7 @@ main(int argc, const char * const argv[])
max_size = atol_suffix(optarg);
break;
case 'o':
move_to(optarg);
record_chdir(optarg);
break;
case 'q':
quiet = 1;
Expand Down Expand Up @@ -278,6 +280,8 @@ main(int argc, const char * const argv[])
exit(1);
}

perform_chdirs();

page_size = getpagesize();
if (read_size % page_size || read_size < max_size) {
if (read_size < max_size) {
Expand Down
4 changes: 2 additions & 2 deletions recoverjpeg.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% RECOVERJPEG(1) Recoverjpeg User Manuals
% Samuel Tardieu <[email protected]>
% September 22, 2015
% November 12, 2016

# NAME
recoverjpeg - recover jpeg pictures from a filesystem image
Expand Down Expand Up @@ -66,7 +66,7 @@ it is discarded. The default is 6 MiB.
-o *directory*
: Change the working directory before restoring files. Use this option to
restore files into a directory with enough space instead of the current
directory.
directory. This option can be repeated.

-q
: Be quiet and do not display anything.
Expand Down
4 changes: 3 additions & 1 deletion recovermov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int main(int argc, char* const* argv) {
mov_index = atoi(optarg);
break;
case 'o':
move_to(optarg);
record_chdir(optarg);
break;
case 'V':
display_version_and_exit("recovermov");
Expand All @@ -130,6 +130,8 @@ int main(int argc, char* const* argv) {

std::ifstream infile(infilename, std::ios_base::in | std::ios_base::binary);

perform_chdirs();

size_t atom_size;
std::string atom_type;

Expand Down
4 changes: 2 additions & 2 deletions recovermov.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% RECOVERMOV(1) Recovermov User Manuals
% Jan Funke <[email protected]>
% September 22, 2015
% November 12, 2016

# NAME

Expand Down Expand Up @@ -49,7 +49,7 @@ for example) at the expense of a much longer running time.
-o *directory*
: Change the working directory before restoring files. Use this option to
restore files into a directory with enough space instead of the current
directory.
directory. This option can be repeated.

-V
: Display program version and exit.
Expand Down
40 changes: 35 additions & 5 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
#include <unistd.h>
#include "utils.h"

typedef struct move_s
{
const char *target;
struct move_s *next;
} move_t;

static move_t *chdirs = NULL;

size_t
atol_suffix(char *arg)
{
Expand Down Expand Up @@ -54,12 +62,34 @@ display_version_and_exit(const char *program_name)
}

void
move_to(const char *directory)
record_chdir(const char *directory)
{
if (chdir(directory) != 0) {
char buffer[128];
snprintf(buffer, sizeof buffer, "cannot change directory to `%s'", directory);
perror(buffer);
move_t **ptr = &chdirs;
while (*ptr != NULL) {
ptr = &(*ptr)->next;
}
*ptr = malloc(sizeof(move_t));
if (*ptr == NULL) {
perror("malloc");
exit(1);
}
(*ptr)->target = directory;
(*ptr)->next = NULL;
}

void
perform_chdirs()
{
move_t *p = chdirs;
while (p != NULL) {
if (chdir(p->target) != 0) {
char buffer[512];
snprintf(buffer, sizeof buffer, "cannot change directory to `%s'", p->target);
perror(buffer);
exit(1);
}
move_t *to_free = p;
p = p->next;
free(to_free);
}
}
4 changes: 3 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ extern "C"
void display_version_and_exit(const char *program_name)
__attribute__ ((noreturn));

void move_to(const char *directory);
void record_chdir(const char *directory);

void perform_chdirs();

#ifdef __cplusplus
}
Expand Down

0 comments on commit 4f62424

Please sign in to comment.