Beispiel #1
0
/*!
 * \brief
 *
 * \param path1_ptr_ptr
 * \param path2_ptr_ptr
 *
 * \return _mfs_error
 */
_mfs_error MFS_alloc_2paths(char **path1_ptr_ptr, char **path2_ptr_ptr)
{
    _mfs_error error_code;
    char *path1_ptr, *path2_ptr;

    error_code = MFS_alloc_path(&path1_ptr);
    if (error_code == MFS_NO_ERROR)
    {
        error_code = MFS_alloc_path(&path2_ptr);
        if (error_code == MFS_NO_ERROR)
        {
            *path1_ptr_ptr = path1_ptr;
            *path2_ptr_ptr = path2_ptr;
        }
        else
        {
            MFS_free_path(path1_ptr);
        }
    }

    return error_code;
}
_mfs_error MFS_Check_dir_exist
    (
        MFS_DRIVE_STRUCT_PTR drive_ptr,

        char               *pathname     /*[IN] pathname of the directory to become the current dir */
    )
{
    uint32_t                 dir_cluster;
    _mfs_error              error_code;
    char                *directory;

    // for empty string return error
    if ( (pathname == NULL) || (*pathname == '\0') )
    {
        return MFS_INVALID_PARAMETER;
    }

    // allocate memory for directory
    error_code = MFS_alloc_path(&directory);
    if ( error_code != MFS_NO_ERROR )
    {
        return( error_code );
    }

    error_code = MFS_lock_dos_disk( drive_ptr );
    if ( error_code != MFS_NO_ERROR )
    {
        MFS_free_path(directory);
        return error_code;
    }

    dir_cluster = ROOT_CLUSTER(drive_ptr);
    dir_cluster = MFS_Find_directory(drive_ptr, pathname, dir_cluster);

    if ( dir_cluster == CLUSTER_INVALID )
    {
        error_code = MFS_PATH_NOT_FOUND;
    }  

    MFS_free_path(directory);
    MFS_unlock(drive_ptr,FALSE);

    return(error_code);
}
Beispiel #3
0
_mfs_error MFS_Check_dir_exist
    (
    MQX_FILE_PTR            mfs_fd_ptr, /*[IN] pointer to the file struct returned by fopen("MFS...:",..); */
    char_ptr               pathname     /*[IN] pathname of the directory to become the current dir */
    )
{
    MFS_DRIVE_STRUCT_PTR    drive_ptr;
    uint_32                 dir_cluster;
    _mfs_error              error_code;
    char_ptr                directory;

    // for empty string return error
    if ( (pathname == NULL) || (*pathname == '\0') )
    {
        return MFS_INVALID_PARAMETER;
    }

    // allocate memory for directory
    error_code = MFS_alloc_path(&directory);
    if ( error_code != MFS_NO_ERROR )
    {
        return( error_code );
    }

    error_code = MFS_lock_dos_disk( mfs_fd_ptr, &drive_ptr );
    if ( error_code != MFS_NO_ERROR )
    {
        MFS_free_path(directory);
        return error_code;
    }

    dir_cluster = ROOT_CLUSTER(drive_ptr);
    dir_cluster = MFS_Find_directory(drive_ptr, pathname, dir_cluster);

    if ( dir_cluster == CLUSTER_INVALID )
    {
        error_code = MFS_PATH_NOT_FOUND;
    }  

    MFS_free_path(directory);
    MFS_unlock(drive_ptr,FALSE);

    return(error_code);
}
void  *MFS_Create_temp_file
    (
        MFS_DRIVE_STRUCT_PTR drive_ptr,
        
        
        unsigned char       attr,           /*[IN] attribute to be given to the file when it is created */
        char                *pathname,       /*[IN] provides the directory path where the file is to be created,
                                            **[OUT] the file name is appended to the directory name */
        _mfs_error_ptr      error_ptr    /*[OUT] Pointer for the MFS ERROR CODE location */
    )
{
    MFS_HANDLE_PTR       handle;
    MFS_DIR_ENTRY_PTR       dir_entry_ptr;
    _mfs_error              error_code;
    uint32_t                 dir_cluster, dir_index;
    uint16_t                 trial;
    char                *last_char;
    uint16_t                 pathlen;
    unsigned char                   access;
    char                *temp_file;

#if MFSCFG_READ_ONLY_CHECK
    if (MFS_is_read_only (drive_ptr))
    {
        *error_ptr = MFS_DISK_IS_WRITE_PROTECTED;
        return NULL;
    }
#endif

    trial = 0;
    error_code = MFS_FILE_NOT_FOUND;
    handle = NULL;

    if ( (pathname == NULL) || (*pathname == '\0') )
    {
        *error_ptr = MFS_INVALID_PARAMETER;
        return( NULL );
    }

    *error_ptr  = MFS_alloc_path(&temp_file);
    if ( *error_ptr )
    {
        return( NULL );
    }

    *error_ptr = MFS_lock_dos_disk( drive_ptr );
    if ( *error_ptr != MFS_NO_ERROR )
    {
        MFS_free_path(temp_file);
        return NULL;
    }

    attr &= (MFS_ATTR_READ_ONLY | MFS_ATTR_HIDDEN_FILE | MFS_ATTR_SYSTEM_FILE | MFS_ATTR_ARCHIVE | MFS_ATTR_VOLUME_NAME);
    attr |= MFS_ATTR_ARCHIVE;
    access = (attr & MFS_ATTR_READ_ONLY) ? MFS_ACCESS_READ_ONLY : MFS_ACCESS_READ_WRITE;

    dir_cluster = drive_ptr->CUR_DIR_CLUSTER;
    dir_cluster = MFS_Find_directory(drive_ptr, pathname, dir_cluster);

    if ( dir_cluster == CLUSTER_INVALID )
    {
        error_code = MFS_PATH_NOT_FOUND;
    }
    else
    {
        pathlen = strlen (pathname);
        last_char = pathname + pathlen;
        if ( pathlen )
        {
            last_char--;
        }

        do
        {
            sprintf(temp_file, (*last_char == '\\' || *last_char == '/' || pathlen == 0) ?
                "%sTMP%05.5lu.@@@" : "%s\\TMP%05.5lu.@@@", pathname, (uint32_t) trial);
            dir_entry_ptr = MFS_Create_entry_slave(drive_ptr, attr, temp_file, &dir_cluster, &dir_index, &error_code, FALSE);
            trial++;
        } while ( (error_code == MFS_FILE_EXISTS) && (trial < MFSCFG_FIND_TEMP_TRIALS) );

        if ( error_code == MFS_NO_ERROR )
        {
            handle = MFS_Get_handle(drive_ptr,dir_entry_ptr);
            if ( handle )
            {
                _mem_copy (temp_file, pathname, strlen(temp_file) + 1);
                handle->DIR_CLUSTER = dir_cluster;
                handle->DIR_INDEX = dir_index;
                handle->ACCESS = access;
                handle->CURRENT_CLUSTER = 0;
                handle->PREVIOUS_CLUSTER = 0;
            }
            else
            {
                error_code = MFS_INSUFFICIENT_MEMORY;
            }  
        }
    }  

    MFS_free_path(temp_file);
    MFS_unlock(drive_ptr,FALSE);

    if ( error_ptr )
    {
        *error_ptr = error_code;
    }

    return((void *) handle);
}  
Beispiel #5
0
int_32  Shell_type(int_32 argc, char_ptr argv[] )
{ /* Body */
   boolean              print_usage, shorthelp = FALSE;
   int_32               return_code = SHELL_EXIT_SUCCESS;
   MQX_FILE_PTR         fd;
   char_ptr             abs_path;   
   _mqx_int             c;
   SHELL_CONTEXT_PTR    shell_ptr = Shell_get_context( argv );
   int_32               error = 0;   
   

   print_usage = Shell_check_help_request(argc, argv, &shorthelp );

   if (!print_usage)  {
      if (argc == 2)  {
         /* check if filesystem is mounted */ 
         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 {
            error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME);
            if(!error)
            {
               fd = fopen(abs_path, "r");
            }
            MFS_free_path(abs_path);
            if (fd && !error)  {
               do {
                  c = fgetc(fd);
                  if (c!= IO_EOF) {
                     fputc((char)c, stdout);
                  }
               } while (c!=IO_EOF);
            
               fclose(fd);
               printf("\n");
            } else  {
               printf("Error, unable to open file %s.\n", argv[1] );
               return_code = SHELL_EXIT_ERROR;
            }
         }
      } else  {
         printf("Error, %s invoked with incorrect number of arguments\n", argv[0]);
         return_code = SHELL_EXIT_ERROR;
         print_usage = TRUE;
      }
   }
   
   if (print_usage)  {
      if (shorthelp)  {
         printf("%s <filename>\n", argv[0]);
      } else  {
         printf("Usage: %s <filename>\n", argv[0]);
         printf("   <filename>   = filename to display\n");
      }
   }
   return return_code;
} /* Endbody */
Beispiel #6
0
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;
}
Beispiel #7
0
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;
}
Beispiel #8
0
int32_t  Shell_create(int32_t argc, char *argv[] )
{ /* Body */
   bool           print_usage, shorthelp = FALSE;
   int32_t            return_code = SHELL_EXIT_SUCCESS;
   char          *mode;
   int fd = -1;
   char          *abs_path;
   SHELL_CONTEXT_PTR    shell_ptr = Shell_get_context( argv );
   int32_t               error = 0;


   print_usage = Shell_check_help_request(argc, argv, &shorthelp );

   if (!print_usage)  {
      if ((argc < 2) || (argc > 3)) {
         fprintf(shell_ptr->STDOUT, "Error, invalid number of parameters\n");
         return_code = SHELL_EXIT_ERROR;
         print_usage = TRUE;
      }
      /* check if filesystem is mounted */
      else if (0 > Shell_get_current_filesystem(argv))
      {
         fprintf(shell_ptr->STDOUT, "Error, file system not mounted\n" );
         return_code = SHELL_EXIT_ERROR;
      }
      else
      {
         /*if (argc != 3)  {
            mode = "a";
         } else {
            mode = argv[2];
         }
          */

         if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) {
            fprintf(shell_ptr->STDOUT, "Error, unable to allocate memory for paths\n" );
            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)
            {
               fd = open(abs_path, O_WRONLY | O_CREAT);
            }
            MFS_free_path(abs_path);
            if (0 <= fd && !error)  {
               close(fd);
            } else  {
               fprintf(shell_ptr->STDOUT, "Error, unable to open file %s.\n", argv[1] );
               return_code = SHELL_EXIT_ERROR;
            }
         }
      }
   }

   if (print_usage)  {
      if (shorthelp)  {
         fprintf(shell_ptr->STDOUT, "%s <filename> [<mode>] \n", argv[0]);
      } else  {
         fprintf(shell_ptr->STDOUT, "Usage: %s <filename> [<mode>]\n", argv[0]);
         fprintf(shell_ptr->STDOUT, "   <filename>   = filename to create\n");
         fprintf(shell_ptr->STDOUT, "   <bytes>      = open mode\n");
      }
   }
   return return_code;
} /* Endbody */
int32_t  Shell_compare(int32_t argc, char *argv[] )
{ /* Body */
   bool      print_usage, shorthelp = FALSE;
   int32_t      size1, size2, return_code = SHELL_EXIT_SUCCESS;
   MQX_FILE_PTR in_fd_1=NULL, in_fd_2=NULL;
   char         *file1=NULL, *file2=NULL;
   SHELL_CONTEXT_PTR    shell_ptr = Shell_get_context( argv );
   int32_t               error = 0;      

   print_usage = Shell_check_help_request(argc, argv, &shorthelp );

   if (!print_usage)  {
      if (argc != 3)  {
         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(&file1) != MFS_NO_ERROR) {
         printf("Error, unable to allocate memory for paths\n" );
         return_code = SHELL_EXIT_ERROR;
      } else {
         error = _io_rel2abs(file1,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME);
         if(!error)
         {
            in_fd_1 = fopen(file1, "r");
         }      

         error = _io_rel2abs(file1,shell_ptr->CURRENT_DIR,(char *) argv[2],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME);
         if(!error)
         {
            in_fd_2 = fopen(file1, "r");
         }  

         MFS_free_path(file1);         

         if (in_fd_1 == NULL)  {
             printf("Error, unable to open file %s\n", argv[1] );
             return_code = SHELL_EXIT_ERROR;
         } else  if (in_fd_2 == NULL)  {
             printf("Error, unable to open file %s\n", argv[2] );
             return_code = SHELL_EXIT_ERROR;
         } else {
            file1 = _mem_alloc_zero(COMPARE_BLOCK_SIZE);
            file2 = _mem_alloc_zero(COMPARE_BLOCK_SIZE);
            if ((file1==NULL) || (file2==NULL)) {
               printf("Error, unable to allocate buffer space" );
               return_code = SHELL_EXIT_ERROR;
            } else {
               do {
                  size1 = read(in_fd_1, file1, COMPARE_BLOCK_SIZE);
                  size2 = read(in_fd_2, file2, COMPARE_BLOCK_SIZE);
                  if (size1!=size2) {
                     printf("Compare failed, files have different sizes\n" );
                     return_code = SHELL_EXIT_ERROR;
                  } else if (size1 > 0) {
                     if (memcmp(file1,file2,COMPARE_BLOCK_SIZE)!=0) {
                        printf("Compare failed, files are different\n" );
                        return_code = SHELL_EXIT_ERROR;
                     }                     
                  }
               } while ((size1>0) && (return_code == SHELL_EXIT_SUCCESS));
               if (return_code == SHELL_EXIT_SUCCESS) {
                  printf("The files are identical\n" );
               }
            }
            if (file1) _mem_free(file1);
            if (file2) _mem_free(file2);
         }
         if (in_fd_1) fclose(in_fd_1); 
         if (in_fd_2) fclose(in_fd_2); 
      }
   }
      
      
   if (print_usage)  {
      if (shorthelp)  {
         printf("%s <file1> <file2> \n", argv[0]);
      } else  {
         printf("Usage: %s <file1> <file2>\n", argv[0]);
         printf("   <file1> = first file to compare\n");
         printf("   <file2> = second file to compare\n");
      }
   }
   return return_code;
} /* Endbody */
Beispiel #10
0
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    :  MFS_Parse_pathname
* Returned Value   :  error code
* Comments  :
*      Take a pathname and convert it into its directory name and
*      filename components.
*END*---------------------------------------------------------------------*/
_mfs_error MFS_Parse_pathname
(
    char        *dirname_ptr,    /*[IN/OUT] pointer to buffer where directory is to be written */
    char        *filename_ptr,   /*[IN/OUT] pointer to buffer where filename is to be written */
    char        *pathname_ptr    /*[IN] pathname include '\', not including drive specification */
)
{
    _mfs_error error_code;
    char   *src, *p, *temp;

    if ( pathname_ptr )
    {
        error_code = MFS_alloc_path(&temp);
        if ( error_code!=MFS_NO_ERROR )
        {
            return( error_code );
        }

        src           = pathname_ptr;
        *filename_ptr = '\0';
        temp[0]       = '\0';
        error_code    = MFS_NO_ERROR;

        if ( *src == '\\' || *src == '/' )
        {
            *dirname_ptr++ = '\\';
            src++;
        }

        *dirname_ptr = '\0';
        p = dirname_ptr;

        while ( *src )
        {
            src = MFS_Parse_next_filename (src, temp);
            if ( src == NULL )
            {
                /*
                ** if the next filename was not a valid filename
                */
                error_code = MFS_PATH_NOT_FOUND;
                break;
            }
            else if ( *src )
            {
                if ( *dirname_ptr )
                {
                    /*
                    ** if this is not the first filename in the path,
                    */
                    strcat (dirname_ptr, "\\");
                }
                p = p + strlen(p);
                strcat (dirname_ptr, temp);
                /*
                ** If any of the directory_names is invalid, return.
                */
                if ( MFS_lfn_dirname_valid(p) == FALSE )
                {
                    src = NULL;
                    error_code = MFS_PATH_NOT_FOUND;
                    break;
                }
            }
        }

        /*
        ** last read word shall be a filename
        */
        if ( src )
        {
            strcpy(filename_ptr, temp);
        }
        MFS_free_path(temp);
    }
    else
    {
        error_code = MFS_INVALID_PARAMETER;
    }

    return(error_code);
}
_mfs_error MFS_Find_next_slave
    (
    MFS_DRIVE_STRUCT_PTR  drive_ptr,    /*[IN] drive context */
    void     *search_next_ptr           /*[IN] address of search data block indicating the current criteria and the results 
                                        ** of the last search results of this search are placed in this data block */
    )
{
    MFS_SEARCH_DATA_PTR        transfer_ptr;
    MFS_INTERNAL_SEARCH_PTR    internal_search_ptr;
    MFS_DIR_ENTRY_PTR          dir_entry_ptr;
    _mfs_error                 error_code;
    uint32_t                    len;
    bool                    found;
    bool                    match_all;
    bool                    eightdotthree = FALSE;
    char                   *lfn;
    char                       sname[SFILENAME_SIZE+1]; 
    uint32_t                    dotfile = 0;
    MFS_DIR_ENTRY  saved_dir_entry;  

    transfer_ptr = (MFS_SEARCH_DATA_PTR) search_next_ptr;
    error_code = MFS_NO_ERROR;
    found = FALSE;

    if ( transfer_ptr )
    {
        internal_search_ptr = &transfer_ptr->INTERNAL_SEARCH_DATA;

        if ( MFS_alloc_path(&lfn)!= MFS_NO_ERROR )
        {
            return MFS_INSUFFICIENT_MEMORY;
        }

        match_all = MFS_Check_search_string_for_all(internal_search_ptr->SRC_PTR);
        if ( !match_all )
        {
            dotfile = MFS_Is_dot_directory(internal_search_ptr->SRC_PTR);
            if ( dotfile == 0 )
            {
                eightdotthree = MFS_Check_search_string_for_8dot3( internal_search_ptr->SRC_PTR);
            }
        }
        do
        {
            dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, NULL, &internal_search_ptr->CURRENT_CLUSTER, &internal_search_ptr->DIR_ENTRY_INDEX,
                &internal_search_ptr->PREV_CLUSTER, internal_search_ptr->ATTRIBUTE & (~ATTR_EXCLUSIVE), &error_code);

            if ( dir_entry_ptr == NULL && !error_code )
            {
                error_code = MFS_FILE_NOT_FOUND;
                break;
            }

            if ( internal_search_ptr->CURRENT_CLUSTER == CLUSTER_INVALID )
            {
                error_code = MFS_FILE_NOT_FOUND;
                break;
            }

            if ( dir_entry_ptr == NULL )
            {
                break;
            }
            saved_dir_entry=*dir_entry_ptr; 

            /* Make sure the entry is not an LFN entry */
            if ( *dir_entry_ptr->ATTRIBUTE != MFS_ATTR_LFN )
            {
                MFS_Compress_nondotfile (dir_entry_ptr->NAME, sname);

                if ( match_all )
                {
                    found = TRUE;
                }
                else
                {
                    if ( dotfile != 0 )
                    {
                        if ( dotfile == 1 )
                        {
                            found = internal_search_ptr->FULLNAME[0] == dir_entry_ptr->NAME[0];
                        }
                        else if ( dotfile == 2 )
                        {
                            found = (internal_search_ptr->FULLNAME[0] == dir_entry_ptr->NAME[0]) &&
                                    (internal_search_ptr->FULLNAME[1] == dir_entry_ptr->NAME[1]);
                        }
                        else
                        {
                            found = FALSE; /* This shouldn't happen */
                        }
                    }
                    else if ( eightdotthree )
                    {
                        found = MFS_Wildcard_match(internal_search_ptr->FILENAME, dir_entry_ptr->NAME);
                    }
                    else
                    {
                        if ( MFS_get_lfn_dir_cluster(drive_ptr, search_next_ptr, sname, lfn) == MFS_NO_ERROR )
                        {
                            found = MFS_lfn_match(internal_search_ptr->SRC_PTR, lfn, 0);
                        }
                        else
                        {
                            found = MFS_lfn_match(internal_search_ptr->SRC_PTR, sname, 0);
                        }  
                    }  
                }  
            }

            if ( !error_code )
            {
                error_code = MFS_Increment_dir_index(drive_ptr, &internal_search_ptr->CURRENT_CLUSTER,
                    &internal_search_ptr->DIR_ENTRY_INDEX, &internal_search_ptr->PREV_CLUSTER);  
            }

        } while ( (error_code == MFS_NO_ERROR) && (found  == FALSE) );

        if ( error_code == MFS_NO_ERROR )
        {
            transfer_ptr->ATTRIBUTE = mqx_dtohc(saved_dir_entry.ATTRIBUTE);
            transfer_ptr->TIME      = mqx_dtohs(saved_dir_entry.TIME);
            transfer_ptr->DATE      = mqx_dtohs(saved_dir_entry.DATE);
            transfer_ptr->FILE_SIZE = mqx_dtohl(saved_dir_entry.FILE_SIZE);

            /* Transfer the filename */
            len = _strnlen(sname,13);
            if ( sname[len-1] == '.' )
            {
                sname[len-1] = '\0';
            }
            strncpy(transfer_ptr->NAME, sname, 13);
        }

        MFS_free_path((void *)lfn);
    }
    else
    {
        error_code = MFS_INVALID_MEMORY_BLOCK_ADDRESS;
    }  

    return(error_code);
}  
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    :  MFS_Find_directory
* Returned Value   :  starting_cluster of the specified directory
* Comments  :  Search through the file structure to find the specified
*  directory.  Will search either from the current directory or from the
*  root directory.  Returns the starting cluster number of the specified
*  directory.  If the specified directory cannot be found, then 
*  CLUSTER_INVALID  is returned.
*
*END*---------------------------------------------------------------------*/
uint32_t MFS_Find_directory
    (
    MFS_DRIVE_STRUCT_PTR  drive_ptr,    /*[IN] drive context */
    char              *path_ptr,     /*[IN] specific directory to search for */
    uint32_t               first_cluster /*[IN] start searching in this cluster, used for a relative search*/
    )
{
    MFS_DIR_ENTRY_PTR     dir_entry_ptr;
    char              *directory;
    uint32_t               current_cluster;
    uint32_t               dir_index;
    _mfs_error            error_code;
    bool               flag;
    uint32_t               prev_cluster = CLUSTER_INVALID; 
    unsigned char                 attribute;


    if ( MFS_alloc_path(&directory)!= MFS_NO_ERROR )
    {
        return( CLUSTER_INVALID );
    }

    if ( *path_ptr == '\\' || *path_ptr == '/' )
    {
        current_cluster = ROOT_CLUSTER(drive_ptr);
        path_ptr++;
    }
    else
    {
        current_cluster = first_cluster;
    }  

    while ( path_ptr )
    {
        if ( *path_ptr )
        {
            path_ptr = MFS_Parse_next_filename(path_ptr, directory);

            if ( !MFS_lfn_dirname_valid(directory) )
            {
                current_cluster = CLUSTER_INVALID;
                break;
            }

            flag = FALSE;
            if ( current_cluster == 0 )  flag = TRUE;
            if ( drive_ptr->FAT_TYPE == MFS_FAT32 )
            {
                if ( current_cluster == drive_ptr->BPB32.ROOT_CLUSTER )
                {
                    flag = TRUE;
                }
            }

            if ( flag )
            {
                /*
                ** Special treatment for '.' and '..' in root directory
                */
                dir_index = MFS_Is_dot_directory(directory);
                if ( dir_index == 1 )
                {
                    /* Return the value of the root cluster */
                    MFS_free_path(directory);
                    return ROOT_CLUSTER(drive_ptr);
                }
                else if ( dir_index == 2 )
                {
                    MFS_free_path(directory);
                    return(CLUSTER_INVALID);
                }
            }

            dir_index = 0;

            dir_entry_ptr = MFS_Find_directory_entry( drive_ptr, directory,
                &current_cluster, &dir_index,  &prev_cluster, (~ATTR_EXCLUSIVE),  
                &error_code);

            if ( dir_entry_ptr == NULL )
            {
                current_cluster = CLUSTER_INVALID;
                break;
            }
            else
            {
                attribute = mqx_dtohc(dir_entry_ptr->ATTRIBUTE);

                if ( attribute & MFS_ATTR_DIR_NAME )
                {
                    current_cluster = clustoh(dir_entry_ptr->HFIRST_CLUSTER, dir_entry_ptr->LFIRST_CLUSTER);
                    if ( current_cluster == 0 && drive_ptr->FAT_TYPE == MFS_FAT32 )
                    {
                        current_cluster = drive_ptr->BPB32.ROOT_CLUSTER;
                    }
                }
                else
                {
                    // Found an entry, but it is not a directory
                    MFS_free_path(directory);
                    return(CLUSTER_INVALID);
                }
            }  
        }
        else
        {
            break;
        }  
    }  
    MFS_free_path(directory);

    return(current_cluster);
}  
Beispiel #13
0
int_32  Shell_write_binary(int_32 argc, char_ptr argv[] ,uint_32 num,uchar_ptr data)
{ /* Body */
   boolean           print_usage, shorthelp = FALSE;
   int_32            return_code = SHELL_EXIT_SUCCESS;
   uint_32           count=0, i;
   int_32            offset=0;
   int_32            seek_mode=0;
   char              c;
   MQX_FILE_PTR      fd = NULL;
   char_ptr          abs_path;   
   boolean           cache_enabled=TRUE;
   SHELL_CONTEXT_PTR    shell_ptr = Shell_get_context( argv );
   char                 buf[SHELL_BLOCK_SIZE];
   char *               argv5_p;
   int_32               error = 0;
   _mqx_int             bi;   

   /* wk @130514 -->  */
   uint_32 nums;uchar left;
   /* wk @130514 -->  */
   print_usage = Shell_check_help_request(argc, argv, &shorthelp ); // wk @130405 --> 简化程序减小时间

   if (!print_usage)  {
      if ((argc < 3)) {
         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  {
         count = 0;
         offset = 0;
         seek_mode = IO_SEEK_CUR; // 默认模式
         if (argc >= 4)  {
           
//            if (! Shell_parse_uint_32(argv[2], &count ))  {// 检查输入长度
//               printf("Error, invalid length\n");
//               return_code = SHELL_EXIT_ERROR;
//               print_usage=TRUE;
//            } else  
           
            {
               if (argc >= 4)  {
                  if (strcmp(argv[2], "begin") == 0) {
                     seek_mode = IO_SEEK_SET;
                  } else if (strcmp(argv[2], "end") == 0) { 
                     seek_mode = IO_SEEK_END;
                  } else if (strcmp(argv[2], "current") == 0) { 
                     seek_mode = IO_SEEK_CUR;
                  } else { 
                     printf("Error, invalid seek type\n");
                     return_code = SHELL_EXIT_ERROR;
                     print_usage=TRUE;
                  }
                  
                  if (return_code == SHELL_EXIT_SUCCESS)  {
                     if (! Shell_parse_int_32(argv[3], &offset ))  {
                        printf("Error, invalid seek value\n");
                        return_code = SHELL_EXIT_ERROR;
                        print_usage=TRUE;
                     }
                  }
               }
            }
         }

      }
     
      if (return_code == SHELL_EXIT_SUCCESS)  {
         if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) {
            printf("Error, unable to allocate memory for paths\n" );
            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)
             {
                fd = fopen(abs_path, "a");
             }
             if (fd && !error)  {
                if (fseek(fd, offset, seek_mode ) != IO_ERROR)  {
               
               // generate data to buf
//               argv5_p = (char*)argv[5];// by dx
//               for (i = 0, c = '0', bi = 0; i < count;i++) {// 写入txt,从字符0到z
//                  
//                  buf[bi++] = c;
//                  
//                  if (bi > SHELL_BLOCK_SIZE - 1) {
//                     write(fd, buf, SHELL_BLOCK_SIZE);
//                     bi = 0;
//                  }
//                  
//                  if (c == 'z')
//                     c='0';
//                  else
//                     c++;
//               }
//               
//               if (bi)                  
//                  count = ( strlen(argv5_p)<count )?( strlen(argv5_p) ):(count);// 修改长度,若超过最大长度则用最大长度
//                  write(fd, argv5_p, count);// 写入SD,相应路径
  
                  /* wk @130405 --> 写我二进制数据 */
//                  for(i = 0; i < num; i++)
//                  {
////                    write(fd,data+i,1);
//                  }
                  
                   /* wk @130514 --> */
                   nums=num>>7;
                   left=(uchar)num&0x7f;
                   for(uint_32 i=0;i<nums;i++)
                     write(fd,&data[i<<7],SHELL_BLOCK_SIZE);
                   write(fd,&data[nums<<7],left);
                   /* wk @130514 --> end*/
                   
//                   write(fd,data,num);
                   /* wk @130405 --> 写二进制数据 <--END */
                  
                  fclose(fd);
               } else  {
                  printf("Error, unable to seek file %s.\n", argv[1] );
                  return_code = SHELL_EXIT_ERROR;
               }
            } else  {
                printf("Error, unable to open file %s.\n", argv[1] );
                return_code = SHELL_EXIT_ERROR;
            }
            MFS_free_path(abs_path);
         }
Beispiel #14
0
int_32  Shell_create(int_32 argc, char_ptr argv[] )
{ /* Body */
   boolean           print_usage, shorthelp = FALSE;
   int_32            return_code = SHELL_EXIT_SUCCESS;
   char_ptr          mode;
   MQX_FILE_PTR      fd = NULL;
   char_ptr          abs_path;   
   SHELL_CONTEXT_PTR    shell_ptr = Shell_get_context( argv );
   int_32               error = 0;   
  

   print_usage = Shell_check_help_request(argc, argv, &shorthelp );

   if (!print_usage)  {
      if ((argc < 2) || (argc > 3)) {
         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 (argc != 3)  {
            mode = "a";
         } else {
            mode = argv[2];
         }
         if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) {
            printf("Error, unable to allocate memory for paths\n" );
            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)
            {
               fd = fopen(abs_path, mode);
            }
            MFS_free_path(abs_path);
            if (fd && !error)  {
               fclose(fd);
            } else  {
               printf("Error, unable to open file %s.\n", argv[1] );
               return_code = SHELL_EXIT_ERROR;
            }
         }
      }
   }
   
   if (print_usage)  {
      if (shorthelp)  {
         printf("%s <filename> [<mode>] \n", argv[0]);
      } else  {
         printf("Usage: %s <filename> [<mode>]\n", argv[0]);
         printf("   <filename>   = filename to create\n");
         printf("   <bytes>      = open mode\n");
      }
   }
   return return_code;
} /* Endbody */