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

Add 3 new fonctionnalities to amrecover #93

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions recover-src/amrecover.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static char *errstr = NULL;
char *authopt;
int amindexd_alive = 0;
security_handle_t *gsech;
char *listing_date = NULL;

static struct {
const char *name;
Expand Down Expand Up @@ -606,6 +607,7 @@ main(

g_printf(_("Setting restore date to today (%s)\n"), dump_date);
line = g_strdup_printf("DATE %s", dump_date);
listing_date = g_strdup(dump_date);
if (converse(line) == -1) {
aclose(server_socket);
exit(1);
Expand Down
4 changes: 4 additions & 0 deletions recover-src/amrecover.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ extern am_feature_t *tapesrv_features;
extern pid_t extract_restore_child_pid;
extern proplist_t proplist;
extern gboolean translate_mode;
extern char *listing_date;

extern void free_dir_item(DIR_ITEM *item);

Expand Down Expand Up @@ -106,6 +107,9 @@ extern void show_mode(void);
extern void set_property_name(char *name, int append);
extern void add_property_value(char *value);
extern void list_property(void);
extern void list_all_file(char *dir, char *name, int re);
extern void find_file(char *name, char *dir, int re);
extern void list_file_history(char* name);

extern void add_storage_value(char *storage);
extern void set_storage(void);
Expand Down
253 changes: 253 additions & 0 deletions recover-src/display_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "amanda.h"
#include "amrecover.h"
#include "amutil.h"
#include "match.h"

gboolean translate_mode = TRUE;

Expand All @@ -50,6 +51,7 @@ static int add_dir_list_item(char *date,
void list_disk_history(void);
void suck_dir_list_from_server(void);
void list_directory(void);
char *convert_name(char *name);

static DIR_ITEM *dir_list = NULL;

Expand All @@ -69,6 +71,257 @@ get_next_dir_item(
}


void list_file_history(char* name)
{
size_t i;
DIR_ITEM *item;
char *quoted;
char *l;
char *s;
char *date;
int ch;
int ind = 0;
int size = 1;
char *next_date;
char *cmd;
char **list_date;
list_date = g_malloc0(sizeof(char *) * size);

if (disk_path == NULL) {
g_printf(_("Must select a disk before listing file history; use the setdisk command.\n"));
return;
}

send_command("DHST");
get_reply_line();

while (get_reply_line() != 0)
{
s = reply_line();
l = s;
if (strncmp_const_skip(l, "201-", s, ch) != 0) {
g_printf("bad reply: not 201-");
continue;
}
ch = *s++;
skip_whitespace(s, ch);
if(ch == '\0') {
g_printf("bad reply: missing date field");
continue;
}
date = s - 1;
skip_non_whitespace(s, ch);
*(s - 1) = '\0';
list_date[ind] = g_strdup(date);
ind++;
if (ind == size) {
size *= 2;
list_date = g_realloc(list_date, sizeof(char *) * size);
for (int j = size/2; j < size; j++) {
list_date[j] = NULL;
}
}
}
g_printf("History of file: %s\n", name);
i = strlen(disk_tpath);
if (i != 1)
i++;
ind = 0;
next_date = g_strdup(list_date[ind]);
while ((ind < size) && (list_date[ind] != NULL)) {
if (!(strcmp(list_date[ind], next_date) > 0))
{

clear_dir_list();
cmd = g_strconcat("DATE ", list_date[ind], NULL);
if (exchange(cmd) == -1)
exit(1);
if (server_happy())
{
suck_dir_list_from_server();
} else {
continue;
}
for (item = get_dir_list(); item != NULL; item = get_next_dir_item(item)) {
quoted = quote_string(item->tpath + i);
if (strcmp(quoted, name) == 0) {
if (strcmp(item->date, list_date[ind]) == 0) {
g_printf("%s %s\n", item->date, quoted);
amfree(quoted);
break;
} else {
amfree(next_date);
next_date = g_strdup(item->date);
}
}
amfree(quoted);
}
}
amfree(list_date[ind]);
ind++;
}
amfree(list_date);
cmd = g_strconcat("DATE ", listing_date, NULL);
if (exchange(cmd) == -1)
exit(1);
if (server_happy())
{
suck_dir_list_from_server();
}
amfree(cmd);
}


void
list_all_file(char *dir, char *name, int re)
{
DIR_ITEM *item;
char *quote;
int size = 5;
int nb_folder = 0;
char **dir_list;
char *next_dir;
int i;
int len;
int dir_len;
int printed = 0;

if (disk_path == NULL) {
g_printf(_("Must select a disk before listing files; use the setdisk command.\n"));
return;
}

dir_list = g_malloc0(sizeof(char *) * size);

if (set_directory(dir, 0) == 1) {
dir_len = strlen(disk_tpath);
if (dir_len == 1)
dir_len = 0;
for (item = get_dir_list(); item != NULL; item = get_next_dir_item(item)) {
quote = quote_string(item->tpath + 1);
i = 0;
if (quote[strlen(quote) - 1] == '\"') {
quote++;
quote[strlen(quote) - 1] = '\0';
i = 1;
}
len = strlen(quote);
if (quote[len - 1] == '/') {
if (dir[strlen(dir) - 1] == '/') {
next_dir = g_strconcat(dir, &quote[dir_len], NULL);
} else {
next_dir = g_strconcat(dir, "/", &quote[dir_len], NULL);
}
dir_list[nb_folder] = next_dir;
nb_folder++;
if (nb_folder == size) {
size *= 2;
dir_list = g_realloc(dir_list, sizeof(char *) * size);
for (int j = size/2; j < size; j++) {
dir_list[j] = NULL;
}
}
} else {
if ((re && (match(name, &quote[dir_len]) == 1)) || (!re && (strcmp(name, &quote[dir_len]) == 0))) {
if (!printed) {
printed = 1;
g_printf("\nFolder: %s\n", dir);
}
g_printf("%s: %s\n", item->date, quote);
}
}
quote -= i;
amfree(quote);
}
i = 0;
while ((i < size) && (dir_list[i] != NULL)) {
list_all_file(dir_list[i], name, re);
amfree(dir_list[i]);
i++;
}
set_directory(dir, 0);
}
amfree(dir_list);
}


void
find_file(char *name, char *dir, int re)
{
char *error = NULL;
char *regex = NULL;

if (disk_path == NULL) {
g_printf(_("Must select a disk before search for a file; use the setdisk command.\n"));
return;
}

if ((strlen(dir) != 1) && (dir[0] == '\"') && (dir[strlen(dir) - 1] == '\"')) {
dir = &dir[1];
dir[strlen(dir) - 1] = '\0';
}
if (!g_str_has_prefix(dir, mount_point)) {
if (dir[0] != '/') {
if (disk_tpath[strlen(disk_tpath) - 1] == '/') {
dir = g_strconcat(mount_point, disk_tpath, dir, NULL);
} else {
dir = g_strconcat(mount_point, disk_tpath, "/", dir, NULL);
}
} else {
dir = g_strconcat(mount_point, dir, NULL);
}
}

name = convert_name(name);
if (re == 1) {
error = validate_regexp(name);
if (error == NULL) {
regex = g_strconcat("^", name, "$", NULL);

g_printf("Search regex : %s in %s\n", regex, dir);

list_all_file(dir, regex, re);
} else {
g_printf("Regex error: %s\n", error);
}
} else {
g_printf("Search %s in %s\n", name, dir);

list_all_file(dir, name, re);
}
amfree(name);
}


char *
convert_name(char *name)
{
char *new_name;
char *tmp;
int i;

if ((name[0] == '\"') && (name[strlen(name) - 1] == '\"')) {
new_name = g_strdup(name + 1);
new_name[strlen(new_name) - 1] = '\0';
} else {
new_name = g_strdup(name);
}
i = 0;
while (new_name[i] != '\0') {
if (new_name[i] == '/') {
new_name[i] = '\0';
tmp = g_strconcat(new_name, "\342\201\204", &new_name[i + 1], NULL);
amfree(new_name);
new_name = tmp;
i += 2;
}
i++;
}
return new_name;
}



void
clear_dir_list(void)
{
Expand Down
6 changes: 6 additions & 0 deletions recover-src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ help_list(void)
g_printf(_("deletex path1 ... - delete from extraction list (regular expressions)\n"));
g_printf(_("extract - extract selected files from tapes\n"));
g_printf(_("exit\n"));
g_printf(_("find file - find file in current directory and subdirectories\n"));
g_printf(_("find path file - find file in path and path subdirectories\n"));
g_printf(_("findx filename - find file in current directory and subdirectories (regular expressions)\n"));
g_printf(_("findx path file - find file in path and path subdirectories (regular expressions)\n"));
g_printf(_("help\n"));
g_printf(_("history - show dump history of disk\n"));
g_printf(_("list [filename] - show extraction list, optionally writing to file\n"));
g_printf(_("lcd directory - change cwd on local file system\n"));
g_printf(_("ls - list directory on virtual file system\n"));
g_printf(_("ls-all - list all directories and file on virtual file system\n"));
g_printf(_("lpwd - show cwd on local file system\n"));
g_printf(_("mode - show the method used to extract SMB shares\n"));
g_printf(_("pwd - show cwd on virtual file system\n"));
Expand All @@ -71,5 +76,6 @@ help_list(void)
g_printf(_("settranslate [on|off] - set/unset translation of non-ASCII characters\n"));
g_printf(_("storage [storage-name]* - the name of the storage in order of preference\n"));
g_printf(_(" use 'HOLDING' for the holding disk\n"));
g_printf(_("version filename - show file history\n"));
g_printf("\n");
}
5 changes: 4 additions & 1 deletion recover-src/set_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ set_date(
{
char *cmd = NULL;
char *qdisk_path;


amfree(listing_date);
listing_date = g_strdup(date);

clear_dir_list();

cmd = g_strconcat("DATE ", date, NULL);
Expand Down
17 changes: 15 additions & 2 deletions recover-src/uparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ extern char * yytext;

%token LISTHOST LISTDISK LISTPROPERTY
%token SETHOST SETDISK SETDATE SETTAPE SETMODE SETDEVICE SETPROPERTY
%token CD CDX QUIT DHIST LS ADD ADDX EXTRACT DASH_H
%token CD CDX QUIT DHIST LS LSALL ADD ADDX EXTRACT DASH_H
%token LIST DELETE DELETEX PWD CLEAR HELP LCD LPWD MODE SMB TAR
%token APPEND PRIORITY SETTRANSLATE STORAGE
%token APPEND PRIORITY SETTRANSLATE STORAGE FIND FINDX FILEHISTORY
%token NL

/* typed tokens */
Expand Down Expand Up @@ -129,6 +129,17 @@ set_command:
| SETMODE invalid_string { yyerror("Invalid argument"); }
| SETMODE NL { yyerror("Argument required"); }
| STORAGE storage_value { set_storage(); }
| FIND STRING STRING NL { find_file($3, $2, 0); amfree($2); amfree($3); }
| FIND STRING NL { find_file($2, "", 0); amfree($2); }
| FIND NL { yyerror("Argument required"); }
| FIND STRING STRING invalid_string { yyerror("Invalid argument"); amfree($2); amfree($3); }
| FINDX STRING STRING NL { find_file($3, $2, 1); amfree($2); amfree($3); }
| FINDX STRING NL { find_file($2, "", 1); amfree($2); }
| FINDX NL { yyerror("Argument required"); }
| FINDX STRING STRING invalid_string { yyerror("Invalid argument"); amfree($2); amfree($3); }
| FILEHISTORY STRING NL { list_file_history($2); amfree($2); }
| FILEHISTORY STRING invalid_string { yyerror("Invalid argument"); amfree($2); }
| FILEHISTORY NL { yyerror("Argument required"); }
;

setdate_command:
Expand Down Expand Up @@ -186,6 +197,8 @@ display_command:
| DHIST invalid_string { yyerror("Invalid argument"); }
| LS NL { list_directory(); }
| LS invalid_string { yyerror("Invalid argument"); }
| LSALL NL { list_all_file(mount_point, ".*", 1); }
| LSALL invalid_string { yyerror("Invalid argument"); }
| LIST STRING NL { display_extract_list($2); amfree($2); }
| LIST NL { display_extract_list(NULL); }
| LIST STRING invalid_string { yyerror("Invalid argument"); }
Expand Down
Loading