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 addstorage/rmstorage commands #61

Merged
merged 3 commits into from
Apr 1, 2021
Merged
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
1 change: 1 addition & 0 deletions inc/mtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void mtp_set_usb_handle(mtp_ctx * ctx, void * handle, uint32_t max_packet_size);
int mtp_load_config_file(mtp_ctx * context, const char * conffile);

uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_t flags);
int mtp_remove_storage(mtp_ctx * ctx, char * name);
int mtp_get_storage_index_by_name(mtp_ctx * ctx, char * name);
uint32_t mtp_get_storage_id_by_name(mtp_ctx * ctx, char * name);
char * mtp_get_storage_description(mtp_ctx * ctx, uint32_t storage_id);
Expand Down
2 changes: 2 additions & 0 deletions inc/mtp_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@
#include "mtp.h"

int mtp_create_storage(mtp_ctx * context);
void mtp_add_storage_from_line(mtp_ctx *context, char *line, int idx);
int mtp_remove_storage_from_line(mtp_ctx * context, char * name, int idx);
#endif
9 changes: 9 additions & 0 deletions src/msgqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <errno.h>

#include "mtp.h"
#include "mtp_cfg.h"
#include "mtp_helpers.h"
#include "mtp_constant.h"
#include "mtp_datasets.h"
Expand Down Expand Up @@ -89,6 +90,14 @@ void* msgqueue_thread( void* arg )
{
PRINT_DEBUG("msgqueue_thread : New message received : %s",msg_buf.mesg_text);

if (!strncmp(msg_buf.mesg_text,"addstorage:", 11)) {
mtp_add_storage_from_line(ctx, &msg_buf.mesg_text[11], 0);
}

if (!strncmp(msg_buf.mesg_text,"rmstorage:", 10)) {
mtp_remove_storage_from_line(ctx, &msg_buf.mesg_text[10], 0);
}

if(!strncmp((char*)&msg_buf.mesg_text,"mount:",6))
{
store_index = mtp_get_storage_index_by_name(ctx, (char*)&msg_buf.mesg_text + 6);
Expand Down
21 changes: 21 additions & 0 deletions src/mtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_

PRINT_DEBUG("mtp_add_storage : %s", path );

if (mtp_get_storage_id_by_name(ctx, description) != 0xFFFFFFFF)
return 0x00000000;

i = 0;
while(i < MAX_STORAGE_NB)
{
Expand Down Expand Up @@ -692,6 +695,24 @@ uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_
return 0x00000000;
}

int mtp_remove_storage(mtp_ctx * ctx, char * name)
{
int index = mtp_get_storage_index_by_name(ctx, name);

if (index < 0)
return index;

free(ctx->storages[index].root_path);
free(ctx->storages[index].description);

ctx->storages[index].root_path = NULL;
ctx->storages[index].description = NULL;
ctx->storages[index].flags = 0x00000000;
ctx->storages[index].storage_id = 0x00000000;

return 0;
}

uint32_t mtp_get_storage_id_by_name(mtp_ctx * ctx, char * name)
{
int i;
Expand Down
34 changes: 25 additions & 9 deletions src/mtp_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,15 @@ static int get_param_offset(char * line, int param)
offs = 0;
offs = get_next_word(line, offs);

param_cnt = 0;
do
for (param_cnt = 0; param_cnt < param; param_cnt++)
{
offs = copy_param(NULL, line, offs);

offs = get_next_word( line, offs );

if(line[offs] == 0 || line[offs] == '#')
return -1;

param_cnt++;
}while( param_cnt < param );
}

return offs;
}
Expand Down Expand Up @@ -244,21 +241,35 @@ int test_flag(char * str, char * flag)
return 0;
}

static int get_storage_params(mtp_ctx * context, char * line,int cmd)
int mtp_remove_storage_from_line(mtp_ctx * context, char * name, int idx)
{
char storagename[MAX_CFG_STRING_SIZE];
int i;

i = get_param(name, idx, storagename);
if (i < 0)
return i;

PRINT_MSG("Remove storage %s", storagename);

return mtp_remove_storage(context, storagename);
}

void mtp_add_storage_from_line(mtp_ctx * context, char * line, int idx)
{
int i, j, k;
char storagename[MAX_CFG_STRING_SIZE];
char storagepath[MAX_CFG_STRING_SIZE];
char options[MAX_CFG_STRING_SIZE];
uint32_t flags;

i = get_param(line, 2,storagename);
j = get_param(line, 1,storagepath);
i = get_param(line, idx + 1,storagename);
j = get_param(line, idx,storagepath);
flags = UMTP_STORAGE_READWRITE;

if( i >= 0 && j >= 0 )
{
k = get_param(line, 3,options);
k = get_param(line, idx + 2,options);
if( k >= 0 )
{
if(test_flag(options, "ro"))
Expand All @@ -281,6 +292,11 @@ static int get_storage_params(mtp_ctx * context, char * line,int cmd)

mtp_add_storage(context, storagepath, storagename, flags);
}
}

static int get_storage_params(mtp_ctx * context, char * line,int cmd)
{
mtp_add_storage_from_line(context, line, 1);

return 0;
}
Expand Down