Example #1
0
//! @brief This function delete a file or directory
//!
void ushell_cmd_rm( void )
{
   uint8_t u8_i = 0;
   Fs_index sav_index;

   if( g_s_arg[0][0] == 0 )
      return;

   // Save the position
   sav_index = nav_getindex();

   while( 1 )
   {
      // Restore the position
      nav_gotoindex(&sav_index);
      // Select file or directory
      if( !nav_setcwd( (FS_STRING)g_s_arg[0], true, false ) )
         break;
      // Delete file or directory
      if( !nav_file_del( false ) )
      {
         fputs(MSG_KO, stdout);
         break;
      }
      u8_i++;
   }
   printf( "%u file(s) deleted\n\r", u8_i );
}
int fb_iterator_init(Bool type){
	file_or_dir = type;
	fb_iterator_reset();

	// if no navigator available, return an error
	if ((iterator_navid = fsaccess_alloc_nav_id()) < 0) return ERROR_NO_NAV_ID;

	fsaccess_take_mutex();

	// select the navigator
	nav_select( iterator_navid );

	// navigate to folder
    if(nav_setcwd((FS_STRING)currentDirectory, FALSE, FALSE) == FALSE){
    	fsaccess_free_nav_id(iterator_navid);
    	return ERROR_NOT_A_DIRECTORY;
    }

    //select top of list
    if(nav_filelist_first(type)==FALSE){
    	fsaccess_free_nav_id(iterator_navid);
    	return ERROR_UNKNOWN;
    }

    nav_filelist_single_enable( type );

    filecount = nav_filelist_nb(file_or_dir);
    return 0;
}
Example #3
0
/*!
 * \brief Is a directory present?
 *
 * \param pcStringDirName Input. Directory name string.
 *
 * \return 1 if the directory exists, 0 if the directory doesn't exist, else -1
 */
int8_t fsaccess_IsDirPresent( const char *pcStringDirName )
{
    signed short TempoNavId;
    int8_t           RetVal;


    fsaccess_take_mutex(); // Take the mutex on the file system access.

    TempoNavId = fsaccess_alloc_nav_id(); // Get a navigator.
    if( -1 == TempoNavId ) // No navigator.
    {
        // give the mutex for nav access
        fsaccess_give_mutex();
        return( -1 );
    }

    // select the navigator
    nav_select( TempoNavId );

    // Try to enter in the directory.
    RetVal = (int8_t)nav_setcwd( (FS_STRING)pcStringDirName, true, false );

    fsaccess_free_nav_id( TempoNavId ); // mark NavId as free
    fsaccess_give_mutex(); // give the mutex for nav access

    return( RetVal );
}
int fb_ls_ext(const char *ext){
	int CurrentNavId = -1;

	// if no navigator available, return an error
	if ((CurrentNavId = fsaccess_alloc_nav_id()) < 0) return ERROR_NO_NAV_ID;

	// select the navigator
	nav_select( CurrentNavId );

	// navigate to folder
    if(nav_setcwd((FS_STRING)currentDirectory, FALSE, FALSE) == FALSE){
    	fsaccess_free_nav_id(CurrentNavId);
    	return ERROR_NOT_A_DIRECTORY;
    }
    if(nav_filelist_first(FS_FILE)==FALSE){
    	fsaccess_free_nav_id(CurrentNavId);
    	return ERROR_UNKNOWN;
    }
    char filename[255];
    if(nav_file_getname(filename,sizeof(filename))==FALSE){
    	fsaccess_free_nav_id(CurrentNavId);
    	return ERROR_UNKNOWN;
    }


    seprintf("Contents of folder %s:\n\n",currentDirectory);

    nav_filelist_single_enable( FS_DIR );
    int filecount = nav_filelist_nb(FS_DIR);

    short a;
    for(a=0; a < filecount; a++){
    	if(nav_filelist_goto(a)==FALSE)return ERROR_UNKNOWN;
        char filename[255];
        if(nav_file_getname(filename,sizeof(filename))==FALSE){
        	fsaccess_free_nav_id(CurrentNavId);
        	return ERROR_UNKNOWN;
        }

        seprintf("%d: %s\n",a,filename);
    }
    nav_filelist_single_enable( FS_FILE );
    filecount = nav_filelist_nb(FS_FILE);
    for(a=0; a < filecount; a++){
    	if(nav_filelist_goto(a)==FALSE)return ERROR_UNKNOWN;
    	if(!nav_file_checkext(ext))continue;
        char filename[255];
        if(nav_file_getname(filename,sizeof(filename))==FALSE){
        	fsaccess_free_nav_id(CurrentNavId);
        	return ERROR_UNKNOWN;
        }

        seprintf("%d: %s\n",a,filename);
    }

    fsaccess_free_nav_id(CurrentNavId);
    return 0;
}
Example #5
0
//! @brief This function enter in a directory
//!
void ushell_cmd_cd( void )
{
   if( g_s_arg[0][0] == 0 )
      return;

   // Add '\' at the end of path, else the nav_setcwd select the directory but don't enter into.
   ushell_path_valid_syntac( g_s_arg[0] );

   // Call file system routine
   if( nav_setcwd((FS_STRING)g_s_arg[0],true,false) == false )
   {
      fputs(MSG_ER_UNKNOWN_FILE, stdout);
   }
}
Example #6
0
//! @brief Minimalist file editor to append char to a file
//!
//! @verbatim
//! hit ^q to exit and save file
//! @endverbatim
//!
void ushell_cmd_append_file( void )
{
   int c_key;

   if( g_s_arg[0][0] == 0 )
      return;

   // Select file or directory
   if( !nav_setcwd( (FS_STRING)g_s_arg[0], true, false ) )
   {
      fputs(MSG_ER_UNKNOWN_FILE, stdout);
      return;
   }
   // Open file
   if( !file_open(FOPEN_MODE_APPEND) )
   {
      fputs(MSG_KO, stdout);
      return;
   }

   // Append file
   fputs(MSG_APPEND_WELCOME, stdout);
   while( 1 )
   {
      usart_reset_status(SHL_USART);
      while(usart_read_char(SHL_USART, &c_key) != USART_SUCCESS);

      if( c_key == ASCII_CTRL_Q )
         break;   // ^q to quit

      putchar( c_key );
      file_putc( c_key );
      if( c_key == ASCII_CR )
      {
         putchar(ASCII_LF);
         file_putc(ASCII_LF);
      }
   }

   // Close file
   file_close();
   putchar(ASCII_CR);putchar(ASCII_LF);
}
Example #7
0
//! @brief This function renames a file or a directory
//!
void ushell_cmd_rename( void )
{
   if( g_s_arg[0][0] == 0 )
      return;
   if( g_s_arg[1][0] == 0 )
      return;

   // Select source file
   if( !nav_setcwd( (FS_STRING)g_s_arg[0], true, false ) )
   {
      fputs(MSG_ER_UNKNOWN_FILE, stdout);
      return;
   }
   // Rename file or directory
   if( !nav_file_rename( (FS_STRING)g_s_arg[1] ) )
   {
      fputs(MSG_KO, stdout);
      return;
   }
}
Example #8
0
//! @brief Manage cat command
//!
//! @param b_more   enable the '|more' management
//!
//! @todo more management not fully functional with file without CR
//!
void ushell_cmd_cat(bool b_more)
{
   char c_file_character;
   uint8_t n_line=0;

   if( g_s_arg[0][0] == 0 )
      return;

   // Select file
   if( !nav_setcwd((FS_STRING)g_s_arg[0],true,false) )
   {
      fputs(MSG_ER_UNKNOWN_FILE, stdout);
      return;
   }

   // Open file
   file_open(FOPEN_MODE_R);
   while (file_eof()==false)
   {
      // Check 'b_more' option
      if( b_more && (n_line >= USHELL_NB_LINE))
      {
         n_line = 0;
         if( !ushell_more_wait() )
            break;   // Stop cat command
      }

      // Display a character
      c_file_character = file_getc();
      putchar( c_file_character );

      // Count the line number
      if (c_file_character==ASCII_LF)
         n_line++;
   }
   file_close();

   // Jump in a new line
   putchar(ASCII_CR);putchar(ASCII_LF);
}
Example #9
0
//! @brief Synchronize a path with an other path
//!
//! @return true if success
//!
bool ushell_cmd_sync( void )
{
   Fs_index sav_index;
   uint8_t u8_folder_level = 0;

   if( g_s_arg[0][0] == 0 )
      return false;
   if( g_s_arg[1][0] == 0 )
      return false;
   // Add '\' at the end of path, else the nav_setcwd select the directory but don't enter into.
   ushell_path_valid_syntac( g_s_arg[0] );
   ushell_path_valid_syntac( g_s_arg[1] );

   printf("Synchronize folders:\n\r");
   sav_index = nav_getindex();   // Save the position

   // Select source directory in COPYFILE navigator handle
   nav_select( FS_NAV_ID_COPYFILE );
   printf("Select source directory\n\r");
   if( !nav_setcwd( (FS_STRING)g_s_arg[0], true, false ) )
      goto ushell_cmd_sync_error;
   nav_filelist_reset();

   // Select destination directory in USHELL navigator handle
   nav_select( FS_NAV_ID_USHELL_CMD );
   printf("Select destination directory\n\r");
   if( !nav_setcwd( (FS_STRING)g_s_arg[1], true, true ) )
      goto ushell_cmd_sync_error;
   nav_filelist_reset();

   // loop to scan and create ALL folders and files
   while(1)
   {
      while(1)
      {
         // Loop to Search files or directories
         // Reselect Source
         nav_select( FS_NAV_ID_COPYFILE );
         if( nav_filelist_set( 0 , FS_FIND_NEXT ) )
            break;   // a next file and directory is found

         // No other dir or file in current dir then go to parent dir on Source and Destination disk
         if( 0 == u8_folder_level )
         {
            // end of update folder
            //********* END OF COPY **************
            goto ushell_cmd_sync_finish;
         }

         printf("Go to parent\n\r");
         // Remark, nav_dir_gotoparent() routine go to in parent dir and select the children dir in list
         u8_folder_level--;
         if( !nav_dir_gotoparent() )
            goto ushell_cmd_sync_error;
         // Select Destination navigator and go to the same dir of Source
         nav_select( FS_NAV_ID_USHELL_CMD );
         if( !nav_dir_gotoparent() )
            goto ushell_cmd_sync_error;
      } // end of while (1)

      if( nav_file_isdir())
      {
         printf("Dir found - create dir: ");
         //** here, a new directory is found and is selected
         // Get name of current selection (= dir name on Source)
         if( !nav_file_name( (FS_STRING)g_s_arg[0], USHELL_SIZE_CMD_LINE, FS_NAME_GET, false ))
            goto ushell_cmd_sync_error;
         // Enter in dir (on Source)
         if( !nav_dir_cd())
            goto ushell_cmd_sync_error;
         u8_folder_level++;
         // Select Destination disk
         nav_select( FS_NAV_ID_USHELL_CMD );
         // Create folder in Destination disk
         printf((char*)g_s_arg[0]);
         printf("\n\r");
         if( !nav_dir_make( (FS_STRING )g_s_arg[0] ))
         {
            if( FS_ERR_FILE_EXIST != fs_g_status )
               goto ushell_cmd_sync_error;
            // here, error the name exist
         }
         // Here the navigator have selected the folder on Destination
         if( !nav_dir_cd())
         {
            if( FS_ERR_NO_DIR == fs_g_status )
            {
               // FYC -> Copy impossible, because a file have the same name of folder
            }
            goto ushell_cmd_sync_error;
         }
         // here, the folder is created and the navigator is entered in this dir
      }
      else
      {
         printf("File found - copy file: ");
         //** here, a new file is found and is selected
         // Get name of current selection (= file name on Source)
         if( !nav_file_name( (FS_STRING)g_s_arg[0], USHELL_SIZE_CMD_LINE, FS_NAME_GET, false ))
            goto ushell_cmd_sync_error;
         printf((char*)g_s_arg[0]);
         printf("\n\r");
         if( !nav_file_copy())
            goto ushell_cmd_sync_error;

         // Paste file in current dir of Destination disk
         nav_select( FS_NAV_ID_USHELL_CMD );
         while( !nav_file_paste_start( (FS_STRING)g_s_arg[0] ) )
         {
            // Error
            if( fs_g_status != FS_ERR_FILE_EXIST )
               goto ushell_cmd_sync_error;
            // File exists then deletes this one
            printf("File exists then deletes this one.\n\r");
            if( !nav_file_del( true ) )
               goto ushell_cmd_sync_error;
            // here, retry PASTE
         }
         // Copy running
         {
         uint8_t status;
         do{
            status = nav_file_paste_state(false);
         }while( COPY_BUSY == status );

         if( COPY_FINISH != status )
            goto ushell_cmd_sync_error;
         }
      } // if dir OR file
   } // end of first while(1)

ushell_cmd_sync_error:
   // Restore the position
   nav_select( FS_NAV_ID_USHELL_CMD );
   nav_gotoindex(&sav_index);
   printf("!!!Copy fail\n\r");
   return false;

ushell_cmd_sync_finish:
   // Restore the position
   nav_select( FS_NAV_ID_USHELL_CMD );
   nav_gotoindex(&sav_index);
   printf("End of copy\n\r");
   return true;
}
Example #10
0
//! @brief This function copies a file to other location
//!
void ushell_cmd_copy( void )
{
   Fs_index sav_index;
   uint8_t u8_status_copy;

   if( g_s_arg[0][0] == 0 )
      return;

   // Save the position
   sav_index = nav_getindex();

   // Select source file
   if( !nav_setcwd( (FS_STRING)g_s_arg[0], true, false ) )
   {
      fputs(MSG_ER_UNKNOWN_FILE, stdout);
      return;
   }
   // Get name of source to be used as same destination name
   nav_file_name( (FS_STRING)g_s_arg[0], MAX_FILE_PATH_LENGTH, FS_NAME_GET, true );
   // Mark this selected file like source file
   if( !nav_file_copy())
   {
      fputs(MSG_KO, stdout);
      goto cp_end;
   }

   // Select destination
   if( g_s_arg[1][0]==0 )
   {
      // g_s_arg[1] is NULL, using mark
      if( !nav_gotoindex(&g_mark_index) )
         goto cp_end;
   }
   else
   {
      // g_s_arg[1] exists, then go to this destination
      if( !nav_setcwd( (FS_STRING)g_s_arg[1], true, false ) )
      {
         fputs(MSG_ER_UNKNOWN_FILE, stdout);
         goto cp_end;
      }
   }

   // Set the name destination and start paste
   if( !nav_file_paste_start((FS_STRING)g_s_arg[0]) )
   {
      fputs(MSG_ER_PASTE, stdout);
      goto cp_end;
   }

   // Performs copy
   do
   {
      u8_status_copy = nav_file_paste_state( false );
   }while( u8_status_copy == COPY_BUSY );

   // Check status of copy action
   if( u8_status_copy == COPY_FAIL )
   {
      fputs(MSG_ER_PASTE, stdout);
      goto cp_end;
   }

cp_end:
   // Restore the position
   nav_gotoindex(&sav_index);
}
Example #11
0
//! This function reads and checks the next line
//!
//! @param opt         PL_MAIN_READLINE_OPT_CHECKLINE to check the next line in the file.\n
//!                     PL_MAIN_READLINE_OPT_READSTRING to read the string in the text file.\n
//!                     PL_MAIN_READLINE_OPT_GOTOPATH to read, check and goto the path.
//! @param id_nav       ID navigator to update with the selected file (ignore if b_gotopatch == false)
//! @param sz_path      Address of the string returned. It is used only if PL_MAIN_READLINE_OPT_READSTRING or
//!                     PL_MAIN_READLINE_OPT_GOTOPATH options are specified.
//! @param u16_size_line  The length of the path. If this parameter is NULL, then the length is not returned.
//!
//! @return    true  if a line with a correct path
//!            false line ignored or end of file
//!
bool  pl_main_readline( readline_opt_t opt, uint8_t id_nav, FS_STRING *sz_path, uint16_t *u16_size_line )
{
   uint16_t line_beg[8];           // Contains first characters of the line
   uint32_t u32_file_pos;
   uint16_t u16_alt_length = 0;
   uint8_t  u8_pos_path_in_line = 0;

   // Set output to NULL to specify if there is an error
   *sz_path = NULL;

   // Read the 3 first char in the line
   u32_file_pos = file_getpos();
   if( E_PL_M3U == pl_g_list_type )
   {
      // M3U path lines = "path\file"
      // M3U comment lines = "#..."
      u16_alt_length = reader_txt_get_line( true, (FS_STRING)line_beg, 2 );
      if( (2 > u16_alt_length)   // The line size is not correct
      ||  ('#' == line_beg[0]) ) // It is a comment line
      {
         return false;
      }
      u8_pos_path_in_line=0;
   }
   if( (E_PL_PLS == pl_g_list_type) || (E_PL_SMP == pl_g_list_type) )
   {
      // PLS path lines = "Filexxx=path\file"
      // SMP path lines = "File=path\file"
      u16_alt_length = reader_txt_get_line( true, (FS_STRING)line_beg, 8 );
      if(6 > u16_alt_length)     // The line size can't contain a path line
         return false;
      // Check if it is a path line
      if( ('F' != line_beg[0])
      ||  ('i' != line_beg[1])
      ||  ('l' != line_beg[2])
      ||  ('e' != line_beg[3]) )
      {
         return false;
      }
      u8_pos_path_in_line=5;
      if('=' != line_beg[4])
      {
         u8_pos_path_in_line++;
         if('=' != line_beg[5])
         {
            u8_pos_path_in_line++;
            if('=' != line_beg[6])
            {
               u8_pos_path_in_line++;
               if('=' != line_beg[7])
                  return false;
            }
         }
      }
   }
   // Here, the line contains a path

   // Store the length of the line if a buffer is set
   if (u16_size_line)
     *u16_size_line = u16_alt_length - u8_pos_path_in_line;
   if (opt == PL_MAIN_READLINE_OPT_CHECKLINE)
      return true;
   // else go to path

   // Read all characters in line
   file_seek( u32_file_pos, FS_SEEK_SET );
   *sz_path = PLAYLIST_BUF_ALLOC( u16_alt_length * (Is_unicode? 2 : 1 ) );
   if( NULL == *sz_path )
      return false;   // no enough memory, impossible to store the path but path present in line
   if( 0 == reader_txt_get_line( (FS_UNICODE==true), *sz_path, u16_alt_length ) )
      return false;  // Error during the read
   if( 0 != u8_pos_path_in_line )
   {
      // Shift the path to the beginning of the buffer
      memmove(*sz_path, *sz_path + u8_pos_path_in_line * ((FS_UNICODE==true) ? 2 : 1),
              (u16_alt_length - u8_pos_path_in_line) * ((FS_UNICODE==true) ? 2 : 1));
   }
   if (opt == PL_MAIN_READLINE_OPT_READSTRING)
     return true;

   // Update the navigator with the path of play list file, because the path is relative at this one
   nav_copy( id_nav );
   nav_select( id_nav );
   // Go to the path included in the line
   if( !nav_setcwd( (FS_STRING) *sz_path , false, false ) )
   {
      // path no found then path is dead -> reset list to deselect a file
      nav_filelist_reset();
   }
   nav_select( FS_NAV_ID_PLAYLIST );

   return true;
}
Example #12
0
//!
//! This function opens a file.
//!
//! @param pathname   path of the file to open.
//! @param flags      flags to give file access rights
//!                   should be O_CREAT  : create file if not exist
//!                             O_APPEND : add data to the end of file
//!                             O_RDONLY : Read Only
//!                             O_WRONLY : Write Only
//!                             O_RDWR   : Read/Write
//! @return int : file descriptor (>= 0 if OK (NavID), -1 otherwise)
//!
int open(const char *pathname, int flags, ...)
{
    int CurrentNavId = -1;


    // take the mutex for nav access
    fsaccess_take_mutex();

    // if no navigator available, return an error
    if ((CurrentNavId = fsaccess_alloc_nav_id()) < 0) goto error_exit;

    // select the navigator
    nav_select( CurrentNavId );

    // the filesystem is now at the root directory
    if ((flags & O_CREAT) == O_CREAT)
    {
        // try to create, the flag is set
        if(nav_setcwd((FS_STRING)pathname, false, true) == false)
        {
            goto error_free_nav;
        }
    }
    else
    {
        // do not try to create, if it doesn't exist, error
        if(nav_setcwd((FS_STRING)pathname, false, false) == false)
        {
            goto error_free_nav;
        }
    }

    // if user wants to append in file
    if ((flags & O_APPEND) == O_APPEND)
    {
        // open the file
        if (file_open(FOPEN_MODE_APPEND) == false) goto error_free_nav;
    }
    else if ((flags & O_RDWR) == O_RDWR)
    {
        // open as read/write
        if (file_open(FOPEN_MODE_R_PLUS) == false) goto error_free_nav;
    }
    else if ((flags & O_WRONLY) == O_WRONLY)
    {
        // open as write
        if (file_open(FOPEN_MODE_W) == false) goto error_free_nav;
    }
    else
    {
        // open as read
        if (file_open(FOPEN_MODE_R) == false) goto error_free_nav;
    }

    // give the mutex for nav access
    fsaccess_give_mutex();

    return (CurrentNavId);

error_free_nav:
    // mark NavId as free
    fsaccess_free_nav_id(CurrentNavId);
error_exit:
    // give the mutex for nav access
    fsaccess_give_mutex();
    return(-1);
}