?? fat_out.c
字號:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2003, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
File : fat_out.c
Purpose : FAT12/FAT16/FAT32 Filesystem file write routines
----------------------------------------------------------------------
Known problems or limitations with current version
----------------------------------------------------------------------
None.
---------------------------END-OF-HEADER------------------------------
*/
/*********************************************************************
*
* #include Section
*
**********************************************************************
*/
#include "fs_conf.h"
#include "fs_port.h"
#ifndef FS_FARCHARPTR
#define FS_FARCHARPTR char *
#endif
#ifndef FS_FAT_FWRITE_UPDATE_DIR
#define FS_FAT_FWRITE_UPDATE_DIR 1
#endif
#include "fs_dev.h"
#include "fs_api.h"
#include "fs_fsl.h"
#include "fs_int.h"
#include "fs_os.h"
#include "fs_lbl.h"
#include "fs_fat.h"
#include "fs_clib.h"
/*********************************************************************
*
* Local functions
*
**********************************************************************
*/
/*********************************************************************
*
* _FS_fat_write_dentry
*
Description:
FS internal function. Write a directory entry.
Parameters:
Idx - Index of device in the device information table
referred by FS__pDevInfo.
Unit - Unit number.
FirstClust - First cluster of the file, which's directory entry
will be written.
pDirEntry - Pointer to an FS__fat_dentry_type structure, which
contains the new directory entry.
DirSec - Sector, which contains the directory entry.
pBuffer - Pointer to a buffer, which contains the sector with
the old directory entry.
Return value:
==1 - Directory entry has been written.
==0 - An error has occured.
*/
static int _FS_fat_write_dentry(int Idx, FS_u32 Unit, FS_u32 FirstClust, FS__fat_dentry_type *pDirEntry,
FS_u32 DirSec, char *pBuffer) {
FS__fat_dentry_type *s;
FS_u32 value;
int err;
if (DirSec == 0) {
return 0; /* Not a valid directory sector */
}
if (pBuffer == 0) {
return 0; /* No buffer */
}
/* Scan for the directory entry with FirstClust in the directory sector */
s = (FS__fat_dentry_type*)pBuffer;
while (1) {
if (s >= (FS__fat_dentry_type*)(pBuffer + FS_FAT_SEC_SIZE)) {
break; /* End of sector reached */
}
value = (FS_u32)s->data[26] + 0x100UL * s->data[27] + 0x10000UL * s->data[20] + 0x1000000UL * s->data[21];
if (value == FirstClust) {
break; /* Entry found */
}
s++;
}
if (s < (FS__fat_dentry_type*)(pBuffer + FS_FAT_SEC_SIZE)) {
if (pDirEntry) {
FS__CLIB_memcpy(s, pDirEntry, sizeof(FS__fat_dentry_type));
err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, DirSec, (void*)pBuffer);
if (err < 0) {
return 0;
}
}
return 1;
}
return 0;
}
/*********************************************************************
*
* _FS_fat_read_dentry
*
Description:
FS internal function. Read a directory entry.
Parameters:
Idx - Index of device in the device information table
referred by FS__pDevInfo.
Unit - Unit number.
FirstClust - First cluster of the file, which's directory entry
will be read.
DirStart - Start of directory, where to read the entry.
pDirEntry - Pointer to an FS__fat_dentry_type structure, which is
used to read the directory entry.
pDirSec - Pointer to an FS_u32, which is used to store the sector
number, in which the directory entry has been read.
pBuffer - Pointer to a buffer, which is used for reading the
directory.
Return value:
==1 - Directory entry has been read.
==0 - An error has occured.
*/
static int _FS_fat_read_dentry(int Idx, FS_u32 Unit, FS_u32 FirstClust,
FS_u32 DirStart, FS__fat_dentry_type *pDirEntry, FS_u32 *pDirSec, char *pBuffer) {
FS_u32 i;
FS_u32 dsize;
FS_u32 value;
FS__fat_dentry_type *s;
int err;
if (pBuffer == 0) {
return 0;
}
dsize = FS__fat_dir_size(Idx, Unit, DirStart);
/* Read the directory */
for (i = 0; i < dsize; i++) {
*pDirSec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
if (*pDirSec == 0) {
return 0; /* Unable to translate relative directory sector to absolute setor */
}
err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, *pDirSec, (void*)pBuffer);
if (err < 0) {
return 0;
}
/* Scan for entry with FirstClus in the sector */
s = (FS__fat_dentry_type*)pBuffer;
while (1) {
if (s >= (FS__fat_dentry_type*)(pBuffer + FS_FAT_SEC_SIZE)) {
break; /* End of sector reached */
}
value = (FS_u32)s->data[26] + 0x100UL * s->data[27] + 0x10000UL * s->data[20] + 0x1000000UL * s->data[21];
if (value == FirstClust) {
break; /* Entry found */
}
s++;
}
if (s < (FS__fat_dentry_type*)(pBuffer + FS_FAT_SEC_SIZE)) {
if (pDirEntry) {
/* Read the complete directory entry from the buffer */
FS__CLIB_memcpy(pDirEntry, s, sizeof(FS__fat_dentry_type));
}
return 1;
}
}
return 0;
}
/*********************************************************************
*
* Global functions
*
**********************************************************************
*/
/*********************************************************************
*
* FS__fat_fwrite
*
Description:
FS internal function. Write data to a file.
Parameters:
pData - Pointer to data, which will be written to the file.
Size - Size of an element to be transferred to a file.
N - Number of elements to be transferred to the file.
pFile - Pointer to a FS_FILE data structure.
Return value:
Number of elements written.
*/
FS_size_t FS__fat_fwrite(const void *pData, FS_size_t Size, FS_size_t N, FS_FILE *pFile) {
FS_size_t todo;
FS_u32 dstart;
FS_u32 dsize;
FS_u32 bytesperclus;
FS_u32 datastart;
FS_u32 fatsize;
FS_u32 fileclustnum;
FS_u32 diskclustnum;
FS_u32 prevclust;
FS_i32 last;
FS_i32 i;
FS_i32 j;
#if (FS_FAT_FWRITE_UPDATE_DIR)
FS__fat_dentry_type s;
FS_u32 dsec;
FS_u16 val;
#endif /* FS_FAT_FWRITE_UPDATE_DIR */
int err;
int lexp;
char *buffer;
if (!pFile) {
return 0;
}
/* Check if media is OK */
err = FS__lb_status(FS__pDevInfo[pFile->dev_index].devdriver, pFile->fileid_lo);
if (err == FS_LBL_MEDIACHANGED) {
pFile->error = FS_ERR_DISKCHANGED;
return 0;
}
else if (err < 0) {
pFile->error = FS_ERR_WRITEERROR;
return 0;
}
buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
if (!buffer) {
return 0;
}
fatsize = FS__FAT_aBPBUnit[pFile->dev_index][pFile->fileid_lo].FATSz16;
if (fatsize == 0) {
/* FAT32 */
fatsize = FS__FAT_aBPBUnit[pFile->dev_index][pFile->fileid_lo].FATSz32;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -