_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); }
_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); }
/*! * \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; }
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 */
/*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); }
MFS_DIR_ENTRY_PTR MFS_Find_entry_on_disk ( MFS_DRIVE_STRUCT_PTR drive_ptr, /*[IN] drive context */ char *pathname, /*[IN] pathname of the file */ _mfs_error_ptr error_ptr, /*[IN/OUT] resulting error code is written to this address*/ uint32_t *dir_cluster_ptr, /*[OUT] the cluster in which the dir entry is located */ uint32_t *dir_index_ptr, /*[OUT] the index within the dir cluster where the entry is located */ uint32_t *prev_cluster_ptr /*[OUT] the cluster previous to dir cluster */ ) { MFS_DIR_ENTRY_PTR dir_entry_ptr; char *temp_dirname; char *temp_filename; uint32_t first_cluster; *error_ptr = MFS_alloc_2paths(&temp_dirname,&temp_filename); if ( *error_ptr != MFS_NO_ERROR ) { return( NULL ); } *error_ptr = MFS_NO_ERROR; dir_entry_ptr = NULL; if ( pathname ) { MFS_Parse_pathname(temp_dirname, temp_filename, pathname); /* ** Is this a relative or an absolute file search ? */ if ( *pathname == '\\' || *pathname == '/' ) { first_cluster = ROOT_CLUSTER(drive_ptr); } else { first_cluster = drive_ptr->CUR_DIR_CLUSTER; } /* first_cluster will now store the current_cluster # */ first_cluster = MFS_Find_directory(drive_ptr, temp_dirname, first_cluster); *prev_cluster_ptr = CLUSTER_INVALID; if ( first_cluster == CLUSTER_INVALID ) { *error_ptr = MFS_PATH_NOT_FOUND; } else { *dir_index_ptr = 0; *dir_cluster_ptr = first_cluster; /* ** Checking for the superset of filename chars will allow ** finding something like \mydir\.. */ if ( !MFS_lfn_dirname_valid(temp_filename) ) { dir_entry_ptr = NULL; *error_ptr = MFS_INVALID_PARAMETER; } else { dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, temp_filename, dir_cluster_ptr, dir_index_ptr, prev_cluster_ptr, MFS_ATTR_ANY, error_ptr); if ( dir_entry_ptr == NULL && !*error_ptr ) { *error_ptr = MFS_FILE_NOT_FOUND; } } } } else { *error_ptr = MFS_INVALID_PARAMETER; } MFS_free_path(temp_dirname); MFS_free_path(temp_filename); return(dir_entry_ptr); }
MFS_DIR_ENTRY_PTR MFS_Create_entry_slave ( MFS_DRIVE_STRUCT_PTR drive_ptr, /*[IN] the drive on which to operate */ uchar attr, /*[IN] attribute to be given to the new file */ char_ptr pathname, /*[IN] directory and file name to be given to the new file */ uint_32_ptr cluster_ptr, /*[OUT] cluster # in the directory where the entry was created */ uint_32_ptr index_ptr, /*[OUT] index # in the directory where the entry was created*/ _mfs_error_ptr error_ptr, /*[IN/OUT] error code is written to this address */ boolean overwrite /*[IN] if TRUE, we will overwrite an existing entry w/the same name */ ) { MFS_DIR_ENTRY_PTR dir_entry_ptr; char_ptr temp_dirname; char_ptr temp_filename; uchar at; uint_32 dir_cluster; uint_32 dir_index; uint_32 vol_cluster, vol_index; _mfs_error error_code; uint_32 prev_cluster; #if MFSCFG_READ_ONLY_CHECK if (MFS_is_read_only (NULL, drive_ptr)) { if ( error_ptr ) { *error_ptr = MFS_DISK_IS_WRITE_PROTECTED; } return NULL; } #endif dir_index = 0; dir_cluster = drive_ptr->CUR_DIR_CLUSTER; dir_entry_ptr = NULL; error_code = MFS_alloc_2paths(&temp_dirname,&temp_filename); if ( error_code ) { return( NULL ); } error_code = MFS_Parse_pathname(temp_dirname, temp_filename, pathname); if ( error_code == MFS_NO_ERROR ) { dir_cluster = MFS_Find_directory(drive_ptr, temp_dirname, dir_cluster); if ( dir_cluster == CLUSTER_INVALID ) { error_code = MFS_PATH_NOT_FOUND; } else { if ( MFS_is_valid_lfn(temp_filename) ) { if ( attr & MFS_ATTR_VOLUME_NAME ) { /* ** If creating a volume label, it must be in the root ** directory. */ if ( dir_cluster == 0 ) { vol_cluster = 0; vol_index = 0; dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, NULL, &vol_cluster, &vol_index, &prev_cluster, MFS_ATTR_VOLUME_NAME, &error_code); if ( dir_entry_ptr ) { error_code = MFS_ALREADY_ASSIGNED; } } else { error_code = MFS_ACCESS_DENIED; } } if ( error_code == MFS_NO_ERROR ) { dir_entry_ptr = MFS_Create_directory_entry(drive_ptr, temp_filename, attr, &dir_cluster, &dir_index, &error_code); /* if dir_entry_ptr == NULL ** we couldn't create a new entry. ** if not NULL, it may be a brand new entry (MFS_NO_ERROR) or ** an old one (MFS_FILE_EXISTS) */ if ( dir_entry_ptr != NULL ) { at = dtohc(dir_entry_ptr->ATTRIBUTE); if ( (error_code != MFS_NO_ERROR) && (overwrite == FALSE) ) { dir_entry_ptr = NULL; } else if ( (error_code == MFS_FILE_EXISTS) && ((at & MFS_ATTR_DIR_NAME) != (attr & MFS_ATTR_DIR_NAME)) || ((at & MFS_ATTR_VOLUME_NAME) != (attr & MFS_ATTR_VOLUME_NAME)) ) { error_code = MFS_WRITE_FAULT; dir_entry_ptr = NULL; } else if ( at & MFS_ATTR_READ_ONLY && error_code != MFS_NO_ERROR ) { /* ** If not freshly created */ error_code = MFS_ACCESS_DENIED; dir_entry_ptr = NULL; } } *cluster_ptr = dir_cluster; *index_ptr = dir_index; } } else { error_code = MFS_INVALID_PARAMETER; dir_entry_ptr = NULL; } } } if ( error_ptr ) { *error_ptr = error_code; } MFS_free_path(temp_filename); MFS_free_path(temp_dirname); return(dir_entry_ptr); }
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 */
_mfs_error MFS_Create_subdir ( MFS_DRIVE_STRUCT_PTR drive_ptr, char *pathname /*[IN] pathname of the directory to be created */ ) { MFS_DIR_ENTRY_PTR dir_entry_ptr; uint32_t dir_cluster; uint32_t parent_cluster; uint32_t free_cluster; uint32_t dir_index; _mfs_error error_code; char *temp_dirname; char *temp_filename; if ( (pathname == NULL) || (*pathname == '\0') ) { return MFS_INVALID_PARAMETER; } #if MFSCFG_READ_ONLY_CHECK if (MFS_is_read_only (drive_ptr)) { return MFS_DISK_IS_WRITE_PROTECTED; } #endif error_code = MFS_alloc_2paths(&temp_dirname,&temp_filename); 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(temp_dirname); MFS_free_path(temp_filename); return error_code; } MFS_Parse_pathname (temp_dirname, temp_filename, pathname); dir_cluster = drive_ptr->CUR_DIR_CLUSTER; parent_cluster = MFS_Find_directory (drive_ptr, temp_dirname, dir_cluster); dir_cluster = parent_cluster; if ( MFS_is_valid_lfn(temp_filename) ) { if ( dir_cluster != CLUSTER_INVALID ) { /* ** We'll obtain a cluster for the new directory first. If we ** cannot create the directory afterwards, it is easier to re-free ** the cluster than to remove the new entry. */ free_cluster = MFS_Find_unused_cluster_from(drive_ptr, drive_ptr->NEXT_FREE_CLUSTER); if ( free_cluster != CLUSTER_INVALID ) { error_code = MFS_Clear_cluster(drive_ptr, free_cluster); if ( error_code ) { MFS_unlock(drive_ptr,TRUE); MFS_free_path(temp_dirname); MFS_free_path(temp_filename); return( error_code ); } error_code = MFS_Put_fat(drive_ptr, free_cluster, CLUSTER_EOF); dir_entry_ptr = MFS_Create_directory_entry(drive_ptr, temp_filename, MFS_ATTR_DIR_NAME, &dir_cluster, &dir_index, &error_code); if ( error_code == MFS_NO_ERROR ) { clustod(dir_entry_ptr->HFIRST_CLUSTER, dir_entry_ptr->LFIRST_CLUSTER, free_cluster); drive_ptr->DIR_SECTOR_DIRTY = TRUE; /* ** We shall now create the "." and ".." entries. */ dir_cluster = free_cluster; dir_entry_ptr = MFS_Create_directory_entry(drive_ptr,".", MFS_ATTR_DIR_NAME, &dir_cluster, &dir_index, &error_code); if ( error_code == MFS_NO_ERROR ) { clustod(dir_entry_ptr->HFIRST_CLUSTER, dir_entry_ptr->LFIRST_CLUSTER, free_cluster); drive_ptr->DIR_SECTOR_DIRTY = TRUE; dir_entry_ptr = MFS_Create_directory_entry(drive_ptr,"..", MFS_ATTR_DIR_NAME, &dir_cluster, &dir_index, &error_code); if ( error_code == MFS_NO_ERROR ) { if ( drive_ptr->FAT_TYPE == MFS_FAT32 ) { if ( drive_ptr->BPB32.ROOT_CLUSTER == parent_cluster ) { /* ** Even though the FAT32 root sector can be ** anywhere, it is identified as 0 when referenced ** through a directory entry */ parent_cluster = 0; } } clustod(dir_entry_ptr->HFIRST_CLUSTER, dir_entry_ptr->LFIRST_CLUSTER, parent_cluster); drive_ptr->DIR_SECTOR_DIRTY = TRUE; } } } else { MFS_Put_fat(drive_ptr, free_cluster, CLUSTER_UNUSED); } } else { error_code = MFS_DISK_FULL; } } else { error_code = MFS_PATH_NOT_FOUND; } } else if ( MFS_lfn_dirname_valid(temp_filename) ) { if ( dir_cluster ) { error_code = MFS_FILE_EXISTS; } else { error_code = MFS_CANNOT_CREATE_DIRECTORY; } } else { error_code = MFS_INVALID_PARAMETER; } MFS_free_path(temp_dirname); MFS_free_path(temp_filename); MFS_unlock(drive_ptr,TRUE); return(error_code); }
_mfs_error MFS_Change_current_dir ( MFS_DRIVE_STRUCT_PTR drive_ptr, char *pathname /*[IN] pathname of the directory to become the current dir */ ) { uint32_t dir_cluster; uint16_t length; _mfs_error error_code; char *directory; char *temp_cur; register char *src, *dest, *dest_start; if ( (pathname == NULL) || (*pathname == '\0') ) { return MFS_INVALID_PARAMETER; } error_code = MFS_alloc_2paths(&directory,&temp_cur); 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); MFS_free_path(temp_cur); return error_code; } dir_cluster = drive_ptr->CUR_DIR_CLUSTER; dir_cluster = MFS_Find_directory(drive_ptr, pathname, dir_cluster); if ( dir_cluster != CLUSTER_INVALID ) { src = pathname; dest = temp_cur; if ( *src == '\\' || *src == '/' ) { temp_cur[0] = '\\'; temp_cur[1] = '\0'; src++; } else { strcpy (temp_cur, drive_ptr->CURRENT_DIR); dest = temp_cur + strlen(temp_cur) - 1; } dest_start = temp_cur; length = strlen(dest_start); while ( src ) { if ( *src ) { src = MFS_Parse_next_filename(src, directory); if ( src != NULL ) { MFS_strupr(directory); } if ( directory[0] == '.' && directory[1] == '.' ) { if ( dest != dest_start ) { while ( dest != dest_start && *dest != '\\' ) { dest--; } /* Erase the previous backslash, unless it is the ** first (root directory). */ if ( dest != dest_start ) { *dest-- = '\0'; } else { *(dest+1) = '\0'; } } else { /* ** The check for underflow is redundant, because the path ** has been found already. */ error_code = MFS_PATH_NOT_FOUND; break; } } else if ( directory[0] && directory[0] != '.' ) { if ( *dest != '\\' ) { *(++dest) = '\\'; length++; } length += strlen(directory); if ( length < sizeof (drive_ptr->CURRENT_DIR) ) { strcpy (++dest, directory); dest = temp_cur + length - 1; } else { error_code = MFS_INVALID_LENGTH_IN_DISK_OPERATION; break; } } } else { break; } } if ( error_code == MFS_NO_ERROR ) { strcpy (drive_ptr->CURRENT_DIR, dest_start); drive_ptr->CUR_DIR_CLUSTER = dir_cluster; } } else { error_code = MFS_PATH_NOT_FOUND; } MFS_free_path(directory); MFS_free_path(temp_cur); MFS_unlock(drive_ptr,FALSE); return(error_code); }
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); }
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; }
_mfs_error MFS_Find_first_file ( MFS_DRIVE_STRUCT_PTR drive_ptr, unsigned char attribute, /*[IN] type of file to find, Search attributes */ char *pathname, /*[IN] optionally the directory and filename to search for */ MFS_SEARCH_DATA_PTR transfer_ptr /*[IN] address of search data block into which the results of the search are put */ ) { MFS_INTERNAL_SEARCH_PTR internal_search_ptr; char *temp_dirname; char *temp_filename; uint32_t current_cluster; _mfs_error error_code; uint32_t i; char c; if ( (pathname==NULL) || (*pathname=='\0') ) { return MFS_INVALID_PARAMETER; } if ( transfer_ptr == NULL ) { return MFS_INVALID_MEMORY_BLOCK_ADDRESS; } error_code = MFS_alloc_2paths(&temp_dirname,&temp_filename); 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(temp_dirname); MFS_free_path(temp_filename); return error_code; } _mem_zero(transfer_ptr, sizeof (MFS_SEARCH_DATA)); transfer_ptr->DRIVE_PTR = drive_ptr; MFS_Parse_pathname(temp_dirname, temp_filename, pathname); current_cluster = drive_ptr->CUR_DIR_CLUSTER; current_cluster = MFS_Find_directory(drive_ptr, temp_dirname, current_cluster); if ( current_cluster == CLUSTER_INVALID ) { error_code = MFS_PATH_NOT_FOUND; } else { /* ** The internal search is only initialised if the directory exists. */ internal_search_ptr = &transfer_ptr->INTERNAL_SEARCH_DATA; internal_search_ptr->CURRENT_CLUSTER = current_cluster; internal_search_ptr->PREV_CLUSTER = CLUSTER_INVALID; internal_search_ptr->DIR_ENTRY_INDEX = 0; MFS_Expand_wildcard(temp_filename, internal_search_ptr->FILENAME); internal_search_ptr->FULLNAME = temp_filename; i = _strnlen(pathname, PATHNAME_SIZE + FILENAME_SIZE + 1); c = pathname[--i]; while ( (c != '\\') && (c != '/') && (c != ':') && i ) { i--; c = pathname[i]; } if ( i || c == '\\' || c == '/' ) { i++; } internal_search_ptr->SRC_PTR = pathname + i; internal_search_ptr->ATTRIBUTE = attribute; error_code = MFS_Find_next_slave(drive_ptr, transfer_ptr); } MFS_free_path(temp_dirname); MFS_free_path(temp_filename); MFS_unlock(drive_ptr,FALSE); 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; }
/*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, ¤t_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); }
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); }
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 */