📄 fat16.c
字号:
return cluster_next;
} while(0);
/* No space left on device or writing error.
* Free up all clusters already allocated.
*/
Fat16_Free_Clusters(fs, cluster_next);
return 0;
#else
return 0;
#endif
}
/**********************************************************************
* 函数 Fat16_Free_Clusters --- 释放一个族。
* Frees a cluster chain, or a part thereof.
*
* Marks the specified cluster and all clusters which are sequentially
* referenced by it as free. They may then be used again for future
* file allocations.
*
* \note If this function is used for freeing just a part of a cluster
* chain, the new end of the chain is not correctly terminated
* within the FAT. Use Fat16_Terminate_Clusters() instead.
*
* \param[in] fs The filesystem on which to operate.
* \param[in] cluster_num The starting cluster of the chain which to free.
* \returns 0 on failure, 1 on success.
* \see Fat16_Terminate_Clusters
**********************************************************************/
u8
Fat16_Free_Clusters(const struct Fat16_FS_Struct* fs, u16 cluster_num)
{
#if FAT16_WRITE_SUPPORT
if(!fs || cluster_num < 2)
return 0;
u32 Fat_Offset = fs->header.Fat_Offset;
u8 buffer[2];
while(cluster_num)
{
if(!fs->partition->device_read(Fat_Offset + 2 * cluster_num, buffer, 2))
return 0;
/* get next cluster of current cluster before freeing current cluster */
u16 cluster_num_next = ((u16) buffer[0]) |
((u16) buffer[1] << 8);
if(cluster_num_next == FAT16_CLUSTER_FREE)
return 1;
if(cluster_num_next == FAT16_CLUSTER_BAD ||
(cluster_num_next >= FAT16_CLUSTER_RESERVED_MIN &&
cluster_num_next <= FAT16_CLUSTER_RESERVED_MAX
)
)
return 0;
if(cluster_num_next >= FAT16_CLUSTER_LAST_MIN /* && cluster_num_next <= FAT16_CLUSTER_LAST_MAX */)
cluster_num_next = 0;
/* free cluster */
buffer[0] = FAT16_CLUSTER_FREE & 0xff;
buffer[1] = (FAT16_CLUSTER_FREE >> 8) & 0xff;
fs->partition->device_write(Fat_Offset + 2 * cluster_num, buffer, 2);
/* We continue in any case here, even if freeing the cluster failed.
* The cluster is lost, but maybe we can still free up some later ones.
*/
cluster_num = cluster_num_next;
}
return 1;
#else
return 0;
#endif
}
/**********************************************************************
* 函数 Fat16_Terminate_Clusters --- 释放一部分族。
* Frees a part of a cluster chain and correctly terminates the rest.
*
* Marks the specified cluster as the new end of a cluster chain and
* frees all following clusters.
*
* \param[in] fs The filesystem on which to operate.
* \param[in] cluster_num The new end of the cluster chain.
* \returns 0 on failure, 1 on success.
* \see Fat16_Free_Clusters
**********************************************************************/
u8
Fat16_Terminate_Clusters(const struct Fat16_FS_Struct* fs, u16 cluster_num)
{
#if FAT16_WRITE_SUPPORT
if(!fs || cluster_num < 2)
return 0;
/* fetch next cluster before overwriting the cluster entry */
u16 cluster_num_next = Fat16_Get_Next_Cluster(fs, cluster_num);
/* mark cluster as the last one */
u8 buffer[2];
buffer[0] = FAT16_CLUSTER_LAST_MAX & 0xff;
buffer[1] = (FAT16_CLUSTER_LAST_MAX >> 8) & 0xff;
if(!fs->partition->device_write(fs->header.Fat_Offset + 2 * cluster_num, buffer, 2))
return 0;
/* free remaining clusters */
if(cluster_num_next)
return Fat16_Free_Clusters(fs, cluster_num_next);
else
return 1;
#else
return 0;
#endif
}
/**********************************************************************
* 函数 Fat16_Clear_Cluster --- 清除族中的数据(用 0 填充)。
*
* 输入参数 fs:The filesystem on which to operate.
* cluster_num:The cluster to clear
* 返回值 returns 0 on failure, 1 on success.
**********************************************************************/
u8
Fat16_Clear_Cluster(const struct Fat16_FS_Struct* fs, u16 cluster_num)
{
#if FAT16_WRITE_SUPPORT
if(cluster_num < 2)
return 0;
u32 cluster_offset = fs->header.Cluster_Zero_Offset +
(u32) (cluster_num - 2) * fs->header.Cluster_Size;
u8 zero[CLUSTER_BUFFER_SIZE];
return fs->partition->device_write_interval(cluster_offset,
zero,
fs->header.Cluster_Size,
Fat16_Clear_Cluster_CallBack,
0
);
#else
return 0;
#endif
}
/**********************************************************************
* 回调函数 Fat16_Clear_Cluster_CallBack
**********************************************************************/
u16
Fat16_Clear_Cluster_CallBack(u8* buffer, u32 offset, void* p)
{
#if FAT16_WRITE_SUPPORT
memset(buffer, 0, CLUSTER_BUFFER_SIZE);
return CLUSTER_BUFFER_SIZE;
#else
return 0;
#endif
}
/**********************************************************************
* 函数 Fat16_Open_File --- 打开一个文件。
*
* 输入参数 fs:The filesystem on which the file to open lies..
* dir_entry:The directory entry of the file to open
* 返回值 0 --- on failure,
* 或者 The file handle
**********************************************************************/
struct Fat16_File_Struct*
Fat16_Open_File(struct Fat16_FS_Struct* fs, const struct Fat16_Dir_Entry_Struct* dir_entry)
{
if(!fs || !dir_entry || (dir_entry->Attributes & FAT16_ATTRIB_DIR))
return 0;
#if USE_DYNAMIC_MEMORY
struct Fat16_File_Struct* fd = malloc(sizeof(*fd));
if(!fd)
return 0;
#else
struct Fat16_File_Struct* fd = fat16_file_handlers;
u8 i;
for(i = 0; i < FAT16_FILE_COUNT; ++i)
{
if(!fd->fs)
break;
++fd;
}
if(i >= FAT16_FILE_COUNT)
return 0;
#endif
memcpy(&fd->dir_entry, dir_entry, sizeof(*dir_entry));
fd->fs = fs;
fd->pos = 0;
fd->pos_cluster = dir_entry->Cluster;
return fd;
}
/**********************************************************************
* 函数 Fat16_Close_File --- 关闭一个文件。
*
* 输入参数 fd:The file handle of the file to close
* 返回值 无
**********************************************************************/
void
Fat16_Close_File(struct Fat16_File_Struct* fd)
{
if(fd)
#if USE_DYNAMIC_MEMORY
free(fd);
#else
fd->fs = 0;
#endif
}
/**********************************************************************
* 函数 Fat16_Read_File --- 读取文件数据。
*
* 输入参数 fd:The file handle of the file from which to read.
* buffer_len:The amount of data to read.
* 输出参数 buffer:The buffer into which to write
*
* 返回值 0 --- 失败
* 或者读取字节个数
**********************************************************************/
s16
Fat16_Read_File(struct Fat16_File_Struct* fd, u8* buffer, u16 buffer_len)
{
/* check arguments */
if(!fd || !buffer || buffer_len < 1)
return -1;
/* determine number of bytes to read */
if(fd->pos + buffer_len > fd->dir_entry.File_Size)
buffer_len = fd->dir_entry.File_Size - fd->pos;
if(buffer_len == 0)
return 0;
u16 Cluster_Size = fd->fs->header.Cluster_Size;
u16 cluster_num = fd->pos_cluster;
u16 buffer_left = buffer_len;
u16 first_cluster_offset = fd->pos % Cluster_Size;
/* find cluster in which to start reading */
if(!cluster_num)
{
cluster_num = fd->dir_entry.Cluster;
if(!cluster_num)
{
if(!fd->pos)
return 0;
else
return -1;
}
if(fd->pos)
{
u32 pos = fd->pos;
while(pos >= Cluster_Size)
{
pos -= Cluster_Size;
cluster_num = Fat16_Get_Next_Cluster(fd->fs, cluster_num);
if(!cluster_num)
return -1;
}
}
}
/* read data */
do
{
/* calculate data size to copy from cluster */
u32 cluster_offset = fd->fs->header.Cluster_Zero_Offset +
(u32) (cluster_num - 2) * Cluster_Size + first_cluster_offset;
u16 copy_length = Cluster_Size - first_cluster_offset;
if(copy_length > buffer_left)
copy_length = buffer_left;
/* read data */
if(!fd->fs->partition->device_read(cluster_offset, buffer, copy_length))
return buffer_len - buffer_left;
/* calculate new file position */
buffer += copy_length;
buffer_left -= copy_length;
fd->pos += copy_length;
if(first_cluster_offset + copy_length >= Cluster_Size)
{
/* we are on a cluster boundary, so get the next cluster */
if((cluster_num = Fat16_Get_Next_Cluster(fd->fs, cluster_num)))
{
first_cluster_offset = 0;
}
else
{
fd->pos_cluster = 0;
return buffer_len - buffer_left;
}
}
fd->pos_cluster = cluster_num;
} while(buffer_left > 0); /* check if we are done */
return buffer_len;
}
/**********************************************************************
* 函数 Fat16_Write_File --- 向文件写入数据。
*
* 输入参数 fd:The file handle of the file to which to write.
* buffer:The buffer from which to read the data to be written.
* buffer_len:The amount of data to write.
*
* 返回值 0 --- 失败,磁盘已满
* -1 --- 失败
* 或者写入的字节个数
**********************************************************************/
s16
Fat16_Write_File(struct Fat16_File_Struct* fd, const u8* buffer, u16 buffer_len)
{
#if FAT16_WRITE_SUPPORT
/* check arguments */
if(!fd || !buffer || buffer_len < 1)
return -1;
if(fd->pos > fd->dir_entry.File_Size)
return -1;
u16 Cluster_Size = fd->fs->header.Cluster_Size;
u16 cluster_num = fd->pos_cluster;
u16 buffer_left = buffer_len;
u16 first_cluster_offset = fd->pos % Cluster_Size;
/* find cluster in which to start writing */
if(!cluster_num)
{
cluster_num = fd->dir_entry.Cluster;
if(!cluster_num)
{
if(!fd->pos)
{
/* empty file */
fd->dir_entry.Cluster = cluster_num = Fat16_Append_Clusters(fd->fs, 0, 1);
if(!cluster_num)
return -1;
}
else
{
return -1;
}
}
if(fd->pos)
{
u32 pos = fd->pos;
u16 cluster_num_next;
while(pos >= Cluster_Size)
{
pos -= Cluster_Size;
cluster_num_next = Fat16_Get_Next_Cluster(fd->fs, cluster_num);
if(!cluster_num_next && pos == 0)
/* the file exactly ends on a cluster boundary, and we append to it */
cluster_num_next = Fat16_Append_Clusters(fd->fs, cluster_num, 1);
if(!cluster_num_next)
return -1;
cluster_num = cluster_num_next;
}
}
}
/* write data */
do
{
/* calculate data size to write to cluster */
u32 cluster_offset = fd->fs->header.Cluster_Zero_Offset +
(u32) (cluster_num - 2) * Cluster_Size + first_cluster_offset;
u16 write_length = Cluster_Size - first_cluster_offset;
if(write_length > buffer_left)
write_length = buffer_left;
/* write data which fits into the current cluster */
if(!fd->fs->partition->device_write(cluster_offset, buffer, write_length))
break;
/* calculate new file position */
buffer += write_length;
buffer_left -= write_length;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -