Skip to content

Commit

Permalink
Adding support to TargetFileName field. If this field is filled in a …
Browse files Browse the repository at this point in the history
…Download requisition, the file is created if the name in this field.
  • Loading branch information
carlos authored and carlos committed Feb 2, 2017
1 parent 0731288 commit 551bd48
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
20 changes: 16 additions & 4 deletions src/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ int backup_remove_transfer_complete(mxml_node_t *node)
return 0;
}

mxml_node_t *backup_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password)
mxml_node_t *backup_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password, char *target_file_name)
{
mxml_node_t *tree, *data, *b, *n;
char time_execute[16];
Expand Down Expand Up @@ -307,12 +307,16 @@ mxml_node_t *backup_add_download(char *key, int delay, char *file_size, char *do
if (!n) return NULL;
n = mxmlNewText(n, 0, file_size);
if (!n) return NULL;

n = mxmlNewElement(b, "time_execute");
if (!n) return NULL;
n = mxmlNewText(n, 0, time_execute);
if (!n) return NULL;

n = mxmlNewElement(b, "target_file_name");
if (!n) return NULL;
n = mxmlNewText(n, 0, target_file_name);
if (!n) return NULL;

backup_save_file();
return b;
}
Expand All @@ -324,7 +328,8 @@ int backup_load_download(void)
mxml_node_t *data, *b, *c;
char *download_url = NULL, *file_size = NULL,
*command_key = NULL, *file_type = NULL,
*username = NULL, *password = NULL;
*username = NULL, *password = NULL,
*target_file_name = NULL;

data = mxmlFindElement(backup_tree, backup_tree, "cwmp", NULL, NULL, MXML_DESCEND);
if (!data) return -1;
Expand Down Expand Up @@ -381,7 +386,14 @@ int backup_load_download(void)
else
file_type = strdup("");

cwmp_add_download(command_key, delay, file_size, download_url, file_type, username, password, b);
c = mxmlFindElement(b, b, "target_file_name",NULL, NULL, MXML_DESCEND);
if (!c)return -1;
if(c->child)
target_file_name = c->child->value.text.string;
else
target_file_name = "";

cwmp_add_download(command_key, delay, file_size, download_url, file_type, username, password, target_file_name, b);
FREE(file_type);
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ mxml_node_t *backup_check_transfer_complete(void);
mxml_node_t *backup_tree_init(void);
mxml_node_t *backup_add_transfer_complete(char *command_key, int fault_code, char *start_time, int method_id);
mxml_node_t *backup_add_event(int code, char *key, int method_id);
mxml_node_t * backup_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password);
mxml_node_t * backup_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password, char *target_file_name);
#endif
14 changes: 8 additions & 6 deletions src/cwmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ static inline void cwmp_free_download(struct download *d)
free(d->key);
free(d->password);
free(d->username);
free(d->target_file_name);
free(d);
}

Expand All @@ -435,16 +436,16 @@ void cwmp_download_launch(struct uloop_timeout *timeout)

d = container_of(timeout, struct download, handler_timer);

log_message(NAME, L_NOTICE, "start download url = %s, FileType = '%s', CommandKey = '%s'\n",
d->download_url, d->file_type, d->key);
log_message(NAME, L_NOTICE, "start download url = %s, FileType = '%s', CommandKey = '%s, TargetFileName = '%s'\n",
d->download_url, d->file_type, d->key, d->target_file_name);

if (external_init()) {
D("external scripts initialization failed\n");
return;
}

start_time = mix_get_time();
external_action_download_execute(d->download_url, d->file_type, d->file_size, d->username, d->password);
external_action_download_execute(d->download_url, d->file_type, d->file_size, d->username, d->password, d->target_file_name);
external_action_handle(json_handle_method_status);
backup_remove_download(d->backup_node);
list_del(&d->list);
Expand Down Expand Up @@ -497,7 +498,7 @@ end_fault :
free(fault);
}

void cwmp_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password, mxml_node_t *node)
void cwmp_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password, char *target_file_name, mxml_node_t *node)
{
struct download *d = NULL;

Expand All @@ -511,12 +512,13 @@ void cwmp_add_download(char *key, int delay, char *file_size, char *download_url
d->file_type = file_type ? strdup(file_type) : NULL;
d->username = username ? strdup(username) : NULL;
d->password = password ? strdup(password) : NULL;
d->target_file_name = target_file_name ? strdup(target_file_name) : NULL;
d->handler_timer.cb = cwmp_download_launch;
d->backup_node = node;
d->time_execute = time(NULL) + delay;
list_add_tail(&d->list, &cwmp->downloads);
log_message(NAME, L_NOTICE, "add download: delay = %d sec, url = %s, FileType = '%s', CommandKey = '%s'\n",
delay, d->download_url, d->file_type, d->key);
log_message(NAME, L_NOTICE, "add download: delay = %d sec, url = %s, FileType = '%s', CommandKey = '%s', TargetFileName = '%s'\n",
delay, d->download_url, d->file_type, d->key, d->target_file_name);

uloop_timeout_set(&d->handler_timer, SECDTOMSEC * delay);
}
Expand Down
3 changes: 2 additions & 1 deletion src/cwmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ struct download {
char *file_type;
char *username;
char *password;
char *target_file_name;
time_t time_execute;
mxml_node_t *backup_node;
};
Expand Down Expand Up @@ -136,7 +137,7 @@ static inline int rpc_get_rpc_methods(void);
static inline int rpc_transfer_complete(mxml_node_t *node, int *method_id);

void cwmp_add_scheduled_inform(char *key, int delay);
void cwmp_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password, mxml_node_t *node);
void cwmp_add_download(char *key, int delay, char *file_size, char *download_url, char *file_type, char *username, char *password, char *target_file_name, mxml_node_t *node);
void cwmp_download_launch(struct uloop_timeout *timeout);
void cwmp_init(void);
void cwmp_connection_request(int code);
Expand Down
3 changes: 2 additions & 1 deletion src/external.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ int external_action_simple_execute(char *command, char *class, char *arg)
return 0;
}

int external_action_download_execute(char *url, char *file_type, char *file_size, char *user_name, char *password)
int external_action_download_execute(char *url, char *file_type, char *file_size, char *user_name, char *password, char *target_file_name)
{
log_message(NAME, L_NOTICE, "external: execute download\n", url);

Expand All @@ -283,6 +283,7 @@ int external_action_download_execute(char *url, char *file_type, char *file_size
if (file_size) external_add_json_obj(json_obj_out, "file_size", file_size);
if (user_name) external_add_json_obj(json_obj_out, "user_name", user_name);
if (password) external_add_json_obj(json_obj_out, "password", password);
if (target_file_name) external_add_json_obj(json_obj_out, "target_file_name", target_file_name);
external_write_pipe(json_object_to_json_string(json_obj_out));
json_object_put(json_obj_out);

Expand Down
2 changes: 1 addition & 1 deletion src/external.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void external_add_obj_resp (char *status, char *instance, char *fault);
void external_fetch_add_obj_resp (char **status, char **instance, char **fault);
int external_action_parameter_execute(char *command, char *class, char *name, char *arg);
int external_action_simple_execute(char *command, char *class, char *arg);
int external_action_download_execute(char *url, char *file_type, char *file_size, char *user_name, char *password);
int external_action_download_execute(char *url, char *file_type, char *file_size, char *user_name, char *password, char *target_file_name);
int external_action_handle (int (*json_handle)(char *));
int external_init();
void external_exit();
Expand Down
13 changes: 10 additions & 3 deletions src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ static int xml_handle_download(mxml_node_t *body_in,
mxml_node_t *n, *t, *b = body_in, *body_out;
char *download_url = NULL, *file_size = NULL,
*command_key = NULL, *file_type = NULL, *username = NULL,
*password = NULL, r;
*password = NULL, *target_file_name = NULL, r;
int delay = -1, code = FAULT_9002;

body_out = mxmlFindElement(tree_out, tree_out, "soap_env:Body", NULL, NULL, MXML_DESCEND);
Expand Down Expand Up @@ -1337,6 +1337,13 @@ static int xml_handle_download(mxml_node_t *body_in,
!b->child) {
file_size = "0";
}
if (b && b->type == MXML_TEXT &&
b->value.text.string &&
b->parent->type == MXML_ELEMENT &&
!strcmp(b->parent->value.element.name, "TargetFileName")) {
FREE(target_file_name);
target_file_name = xml_get_value_with_whitespace(&b, body_in);
}
if (b && b->type == MXML_TEXT &&
b->value.text.string &&
b->parent->type == MXML_ELEMENT &&
Expand All @@ -1358,8 +1365,8 @@ static int xml_handle_download(mxml_node_t *body_in,
code = FAULT_9004;
goto fault_out;
}
n = backup_add_download(command_key, delay, file_size, download_url, file_type, username, password);
cwmp_add_download(command_key, delay, file_size, download_url, file_type, username, password, n);
n = backup_add_download(command_key, delay, file_size, download_url, file_type, username, password, target_file_name);
cwmp_add_download(command_key, delay, file_size, download_url, file_type, username, password, target_file_name, n);
FREE(file_type);
FREE(command_key);
FREE(username);
Expand Down

0 comments on commit 551bd48

Please sign in to comment.