⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat.c

📁 MP3播放器详细设计方案
💻 C
📖 第 1 页 / 共 5 页
字号:
          fat_update_fat_sector(sector_number);
          sector_number++;
          Fat_load_fat_sector(sector_number);
          i = 0;
        }
        cluster += ((fat_buf_sector[i] & 0x0F) << 8) ;
        fat_buf_sector[i] &= 0xF0;
      }
      else
      {
        cluster = (fat_buf_sector[i] & 0xF0) >> 4;
        fat_buf_sector[i] &=  0x0F;
        i++;
        if (i == 512)
        {
          fat_update_fat_sector(sector_number);
          sector_number++;
          Fat_load_fat_sector(sector_number);
          i = 0;
        }
        cluster += (fat_buf_sector[i] << 4);
        fat_buf_sector[i] = 0x00;
      }
    }
    while (cluster != 0xFFF);
    fat_update_fat_sector(sector_number);

  }
}

/*F**************************************************************************
* NAME: fat_fdelete
*----------------------------------------------------------------------------
* PARAMS:
*    
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Delete a selected file
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function works only on a root directory entry
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_fdelete (void)
{
Uint16 i;
xdata Uint32 dir_sector; 

  if (fat_check_ext() != FILE_DIR)
  {

    fat_seek_entry_record();    /* Re-fetch the entry <-> fat_dir_list_index */
  
    Hard_read_close();
  
    dir_sector = fat_dir_current_sect;
    fat_root_entry = fat_dclust_byte_count / 32;
    fat_file_size.l = fat_cache.current.size.l;
  
  
    Hard_read_open(dir_sector);
    for (i = 0; i < SECTOR_SIZE; i++)
      fat_buf_sector[i] = Hard_read_byte();     /* read entry's sector */
    
    i = (fat_root_entry % 16) * 32 ;            /* position of entry in the sector */
    Hard_read_close();
    while (fat_buf_sector[i+11] == ATTR_LFN_ENTRY)
    {
      /* mark file as deleted */
      fat_buf_sector[i] = FILE_DELETED;
      i+=32;
  
      if (!dir_is_root)
        fat_dclust_byte_count += 32;
  
      if (i == SECTOR_SIZE)
      {
        Hard_write_open(dir_sector);
        for (i = 0; i < SECTOR_SIZE; i++)
          Hard_write_byte(fat_buf_sector[i]);
        Hard_write_close();
  
        if (!dir_is_root)                         /* sub-directory */
        {
          /* check if we are at the end of a cluster */
          if ((((Byte*)&fat_dclust_byte_count)[1] == 0x00) &&
            ((((Byte*)&fat_dclust_byte_count)[0] & fat_cluster_mask) == 0x00))
          {
            /* extract if necessary the next cluster from the allocation list */
            if (dclusters[fat_dchain_index].number == fat_dchain_nb_clust)
            { /* new fragment */
              fat_dchain_index++;
              fat_dchain_nb_clust = 1;
              dir_sector = (fat_ptr_data + ((Uint32)(dclusters[fat_dchain_index].cluster) * fat_cluster_size));
            }
            else
            {
              fat_dchain_nb_clust++;                /* one more cluster read */
              dir_sector++;                         /* Contiguous cluster    */
            }
          }
          else
          { /* Don't change the cluster */
            dir_sector++;
          }
        }
        else
        { /* Root directory is linear */
          dir_sector++;
        }
  
        Hard_read_open(dir_sector);
        for (i = 0; i < SECTOR_SIZE; i++)
          fat_buf_sector[i] = Hard_read_byte();   /* read entry's sector */
        Hard_read_close();
        i = 0;
      }
  
    }
    fat_buf_sector[i] = FILE_DELETED;
    Hard_write_open(dir_sector);
    for (i = 0; i < SECTOR_SIZE; i++)
      Hard_write_byte(fat_buf_sector[i]);
    Hard_write_close();
  
    /* FAT update */
    fat_fclust_byte_count = 0;            /* byte 0 of cluster */
    /* reset the allocation list variable */
    fat_fchain_index = 0;
    fat_fchain_nb_clust = 0;              /* start on first contiguous cl */
    /* get file allocation list */
    fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER);
    /* Clear fat cluster */
    fat_clear_fat();
    /* NF correction */
    for (i = 0; i < 256; i++)
      gl_buffer[i] = 0x00;
    return OK;
  }
  else
  {
    return KO;
  }

}


/*F**************************************************************************
* NAME: fat_set_clusters
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   - OK: allocation done
*   - KO: allocation cannot be done : no free cluster
*----------------------------------------------------------------------------
* PURPOSE:
*   Prepare a list of the free clusters:
*     chain[n].cluster contains the starting cluster number of a fragment
*     chain[n].number contains the number of contiguous clusters in fragment
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   Free cluster list is limited by the nb_frag parameter.
*   If memory is too much fragmented, created file may be limited in size.
*   Last list item always has single cluster
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit fat_set_clusters (void)
{
bit     cluster_free;
bit     fat12_parity;
Byte    temp;
Byte    index;
Uint16  cluster;
Uint16  max_cluster;
Union16 tmp_cluster;

  max_cluster = fat_count_of_clusters + 1;
  cluster = 0;
  cluster_free = FALSE;
  index = 0;
  Hard_read_open(fat_ptr_fats);

  if (fat_is_fat16)     /* FAT16 management */
  {
    /* search the first free cluster in fat */
    Hard_read_byte();                       /* skip first 2 clusters */
    Hard_read_byte();
    Hard_read_byte();
    Hard_read_byte();
    do                                      /* search for first free cluster */
    {
      tmp_cluster.b[1] = Hard_read_byte();
      tmp_cluster.b[0] = Hard_read_byte();
      if (tmp_cluster.w == 0x0000)
        cluster_free = TRUE;
      else 
        cluster++;
    }
    while ((cluster != max_cluster) && (!cluster_free));
     
    if (!cluster_free)
    {
      Hard_read_close();
      return KO;                            /* no free cluster found */
    }
  
    fclusters[0].number = 1;  
    fclusters[0].cluster = cluster;         /* store first cluster */
    cluster++;

    if (cluster != max_cluster)
    {
      do                                      /* construct the list */
      {
        cluster_free = FALSE;
        tmp_cluster.b[1] = Hard_read_byte();
        tmp_cluster.b[0] = Hard_read_byte();
        if (tmp_cluster.w == 0x0000)
          cluster_free = TRUE;

        if (cluster_free)
        { /* free cluster: add it to the list */
          if (fclusters[index].number == MAX_CL_PER_FRAG) 
          {
            index++;
            fclusters[index].number = 1;
            fclusters[index].cluster = cluster;
            cluster++;
          }
          else
          {
            fclusters[index].number++;
            cluster++;
          }
        }
        else
        { /* cluster already used */
          do                                  /* search for next free fragment */
          {
            if (Hard_read_byte() == 0x00)
            {
              if (Hard_read_byte() == 0x00)
              {
                cluster_free = TRUE;          /* free cluster found */
                break;                        /* exit while loop */
              }
              else
                cluster++;
            }
            else
            {
              Hard_read_byte();
              cluster++;
            }        
          }
          while (cluster != max_cluster);
  
          cluster++;
          if (!cluster_free)
          {
            if (fclusters[index].number == 1)
            { /* last cluster is the current one */
              fat_last_clust_index = index;
              fclusters[fat_last_clust_index].number = 0; /* end of list marker */
            }
            else
            {
              fat_last_clust_index = index + 1;
              fclusters[index].number--;
              fclusters[fat_last_clust_index].cluster = fclusters[index].cluster + fclusters[index].number;
              fclusters[fat_last_clust_index].number = 0; /* end of list marker */
            }
            Hard_read_close();
            return OK;                        /* end of partition reached */
          }
  
          index++;
          fclusters[index].number = 1;
          fclusters[index].cluster = cluster;
          cluster++;
        }
      }
      while ((index != (MAX_FILE_FRAGMENT_NUMBER - 1)) && (cluster < max_cluster));
    }
  
    /* Mark end of list -> last record number = 0 */
    /* Be careful : 0 value is just a marker      */
    /*              Real value is in fact 1!!!    */
    if (index == MAX_FILE_FRAGMENT_NUMBER - 1)    /* End of chain cluster -> maximum file size */
    {
      /* The last index must have the end of list marker */
      fat_last_clust_index = index;
      fclusters[index-1].number--;
      fclusters[index].cluster = fclusters[index - 1].cluster + fclusters[index-1].number;
      fclusters[fat_last_clust_index].number = 1;   /* end of list marker */
    }
    else                                            /* max cluster -> end of media */
    {
      if (fclusters[index].number == 1)
      { /* last cluster is the current one */
        fat_last_clust_index = index;
        fclusters[fat_last_clust_index].number = 1; /* end of list marker */
      }
      else
      {
        fat_last_clust_index = index + 1;
        fclusters[index].number--;
        fclusters[fat_last_clust_index].cluster = fclusters[index].cluster + fclusters[index].number;
        fclusters[fat_last_clust_index].number = 1; /* end of list marker */
      }
    }
    Hard_read_close();
    return OK;
  }
  else      /* FAT12 management */
  {
    fat12_parity = 1;
    Hard_read_byte();                       /* skip first 2 clusters */
    Hard_read_byte();
    Hard_read_byte();
    do                                      /* search for first free cluster */
    {
      if (fat12_parity)
      {
        fat12_parity = 0;
        if (Hard_read_byte() == 0x00)
        {
          temp = Hard_read_byte();
          if ((temp & 0x0F) == 0x00)
          {
            cluster_free = TRUE;            /* free cluster found */
            break;                          /* exit while loop */
          }
          else
          {
            cluster++;
          }
        }
        else
        {
          temp = Hard_read_byte();
          cluster++;
        }
      }
      else
      {
        fat12_parity = 1;
        if ((temp & 0xF0) == 0x00)
        {
          if (Hard_read_byte() == 0x00)
          {
            cluster_free = TRUE;            /* free cluster found */
            break;                          /* exit while loop */
          }
          else
          {
            cluster++;
          }
        }
        else
        {
          Hard_read_byte();
          cluster++;
        }
      }
    }
    while (cluster != max_cluster);

    if (!cluster_free)
    {
      Hard_read_close();
      return KO;                            /* no free cluster found */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -