/* ** Function used to init session structure ** ** IN: ** FTPSRV_SESSION_STRUCT* session - session structure pointer ** FTPSRV_STRUCT *server - pointer to server structure (needed for session parameters) ** const int sock - socket handle used for communication with client ** ** OUT: ** none ** ** Return Value: ** none */ static void ftpsrv_ses_init(FTPSRV_STRUCT *server, FTPSRV_SESSION_STRUCT *session, const int sock) { if (server && session) { /* Some buffer of arbitrary size so we can get filesystem pointer */ char dev_name[16] = {0}; /* Init session structure */ session->control_sock = sock; session->connected = TRUE; session->auth_tbl = server->params.auth_table; session->root_dir = (char*) server->params.root_dir; session->cur_dir = RTCS_mem_alloc_zero(sizeof("\\")); session->cur_dir[0] = '\\'; session->start_time = RTCS_time_get(); _io_get_dev_for_path(dev_name, NULL, 16, session->root_dir, NULL); session->fs_ptr = _io_get_fs_by_name(dev_name); session->msg_queue = RTCS_mem_alloc_zero(sizeof(LWMSGQ_STRUCT) + FTPSRV_NUM_MESSAGES*sizeof(FTPSRV_TRANSFER_MSG)*sizeof(_mqx_max_type)); if (session->msg_queue != NULL) { _lwmsgq_init(session->msg_queue, FTPSRV_NUM_MESSAGES, sizeof(FTPSRV_TRANSFER_MSG)/sizeof(_mqx_max_type)); } } }
/*FUNCTION*------------------------------------------------------------------- * convert relative to absolute path and extend by device name if not present * params: inputpath - pointer to parsed path * result - [out]pointer to place where abs. path will be stored * len - size of result * cur_dev - current device name * cirdir - current directory name * return value - [out]OK or error code *END*---------------------------------------------------------------------*/ int_32 _io_rel2abs(char_ptr result, char_ptr curdir, char_ptr inputpath, int_32 len, char_ptr cur_dev) { int_32 inputpathlen = strlen(inputpath); char_ptr path; char_ptr dest; char_ptr dirstart; int_32 length = 0, devlen = 0,dirlen = 0,error_code = 0; boolean relpathflag = FALSE, is_dev_in_path = FALSE; devlen = _io_get_dev_for_path(result,&is_dev_in_path,len,inputpath,cur_dev); // device name during path parsing will be skiped dest = result = result + devlen; if(is_dev_in_path == TRUE) { // there was device name in input path -> skip it in parsing inputpath = inputpath + devlen; } dest[0] = '\\'; dest[1] = '\0'; length = 1; // selected path begins with / or \, that means absolute path if(*inputpath == '/' || *inputpath == '\\') { path = inputpath; relpathflag = 0; } else { path = curdir+1; relpathflag = 1; } // browse path, convert to upper case and eliminate . and .. while ( path ) { if ( *path ) { dirstart = path; path = _parse_next_filename(path, &dirlen, len); if (path != NULL && dirstart[0] == '.' && dirstart[1] == '.' ) // ".." { if ( dest != result ) { while ( dest != result && *dest != '\\' ) { dest--; length--; } /* Erase the previous backslash, unless it is the ** first (root directory). */ if ( dest != result ) { *dest-- = '\0'; length--; } else { *(dest+1) = '\0'; } } else { /* ** The check for underflow is redundant, because the path ** has been found already. */ error_code = MQX_INVALID_PARAMETER; break; } } else if ( dirstart[0] && dirstart[0] != '.' ) // not dotted path { if ( *dest != '\\' ) { *(++dest) = '\\'; length++; } length += dirlen; if ( length < len ) { strncpy (++dest, dirstart, dirlen); dest[dirlen] = '\0'; dest = result + length - 1; } else { error_code = MQX_INVALID_PARAMETER; break; } } } // the end of parsed string check relative/absolute path else { if(relpathflag == 1) { // new path was relative(did not started with / or \), continue in process path = inputpath; relpathflag = 0; } else { // absolute path finish process break; } } } return error_code; }
int_32 Shell_cd(int_32 argc, char_ptr argv[] ) { /* Body */ boolean print_usage, shorthelp = FALSE, dev_in_path = FALSE; int_32 error = 0, return_code = SHELL_EXIT_SUCCESS; MQX_FILE_PTR fs_ptr; char_ptr abs_path = NULL; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); int_16 devlen = 0; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc != 2) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { devlen = _io_get_dev_for_path(abs_path, &dev_in_path, PATHNAME_SIZE, (char *)argv[1], Shell_get_current_filesystem_name(shell_ptr)); fs_ptr = _io_get_fs_by_name(abs_path); if (fs_ptr == NULL) { printf("Device \"%s\" not available\n", abs_path); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path, shell_ptr->CURRENT_DIR, (char *)argv[1], PATHNAME_SIZE, shell_ptr->CURRENT_DEVICE_NAME); if(!error) { // check if path exist error = ioctl(fs_ptr, IO_IOCTL_CHECK_DIR_EXIST,(pointer)abs_path ); } if (error) { printf("Error changing directory %s\n", argv[1]); } else { if(dev_in_path == TRUE) { // there is device name in input path //separate device name abs_path[devlen] = '\0'; Shell_set_current_filesystem_by_name(argv,abs_path); // add "\" back to the string abs_path[devlen] = '\\'; } // change shell current dir strcpy(shell_ptr->CURRENT_DIR,abs_path+devlen); } } MFS_free_path(abs_path); } } } if (print_usage) { if (shorthelp) { printf("%s <directory> \n", argv[0]); } else { printf("Usage: %s <directory>\n", argv[0]); printf(" <directory> = name of directory to change to\n"); } } return return_code; }
int32_t Shell_del(int32_t argc, char *argv[] ) { /* Body */ bool print_usage, shorthelp = FALSE, temp; int32_t error = 0, return_code = SHELL_EXIT_SUCCESS; MQX_FILE_PTR fs_ptr; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); char *abs_path; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc != 2) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } /* check if filesystem is mounted */ else if (NULL == Shell_get_current_filesystem(argv)) { printf("Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else { if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { _io_get_dev_for_path(abs_path,&temp,PATHNAME_SIZE,(char *)argv[1],Shell_get_current_filesystem_name(shell_ptr)); fs_ptr = _io_get_fs_by_name(abs_path); if (fs_ptr == NULL) { printf("Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,Shell_get_current_filesystem_name(shell_ptr)); if (!error) { error = ioctl(fs_ptr, IO_IOCTL_DELETE_FILE, (void *) abs_path); } if (error) { printf("Error deleting file %s\n", argv[1]); } } MFS_free_path(abs_path); } } } if (print_usage) { if (shorthelp) { printf("%s <file> \n", argv[0]); } else { printf("Usage: %s <file>\n", argv[0]); printf(" <file> = name of file to delete\n"); } } return return_code; }