summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/libfat/source/fatfile.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--c/src/lib/libbsp/arm/nds/libfat/source/fatfile.c210
1 files changed, 105 insertions, 105 deletions
diff --git a/c/src/lib/libbsp/arm/nds/libfat/source/fatfile.c b/c/src/lib/libbsp/arm/nds/libfat/source/fatfile.c
index c4c345d9e4..17151ab6fa 100644
--- a/c/src/lib/libbsp/arm/nds/libfat/source/fatfile.c
+++ b/c/src/lib/libbsp/arm/nds/libfat/source/fatfile.c
@@ -1,11 +1,11 @@
/*
fatfile.c
-
- Functions used by the newlib disc stubs to interface with
+
+ Functions used by the newlib disc stubs to interface with
this library
Copyright (c) 2006 Michael "Chishm" Chisholm
-
+
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
@@ -29,11 +29,11 @@
2006-07-11 - Chishm
* Original release
-
+
2006-07-17 - Chishm
* Made all path inputs const char*
* Added _FAT_rename_r
-
+
2006-08-02 - Chishm
* Fixed _FAT_seek_r
@@ -63,7 +63,7 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
u32 dirCluster;
FILE_STRUCT* file = (FILE_STRUCT*) fileStruct;
partition = _FAT_partition_getPartitionFromPath (path);
-
+
if (partition == NULL) {
r->_errno = ENODEV;
return -1;
@@ -77,7 +77,7 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
r->_errno = EINVAL;
return -1;
}
-
+
// Determine which mode the file is openned for
if ((flags & 0x03) == O_RDONLY) {
// Open the file for read-only access
@@ -97,24 +97,24 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
} else {
r->_errno = EACCES;
return -1;
- }
+ }
// Make sure we aren't trying to write to a read-only disc
if (file->write && partition->readOnly) {
r->_errno = EROFS;
return -1;
- }
+ }
// Search for the file on the disc
fileExists = _FAT_directory_entryFromPath (partition, &dirEntry, path, NULL);
-
+
// The file shouldn't exist if we are trying to create it
if ((flags & O_CREAT) && (flags & O_EXCL) && fileExists) {
r->_errno = EEXIST;
return -1;
}
-
- // It should not be a directory if we're openning a file,
+
+ // It should not be a directory if we're openning a file,
if (fileExists && _FAT_directory_isDirectory(&dirEntry)) {
r->_errno = EISDIR;
return -1;
@@ -127,9 +127,9 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
// We can't write to a read-only partition
r->_errno = EROFS;
return -1;
- }
+ }
// Create the file
- // Get the directory it has to go in
+ // Get the directory it has to go in
pathEnd = strrchr (path, DIR_SEPARATOR);
if (pathEnd == NULL) {
// No path was specified
@@ -150,12 +150,12 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
// Create the entry data
strncpy (dirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
-
+
// Set the creation time and date
dirEntry.entryData[DIR_ENTRY_cTime_ms] = 0;
u16_to_u8array (dirEntry.entryData, DIR_ENTRY_cTime, _FAT_filetime_getTimeFromRTC());
u16_to_u8array (dirEntry.entryData, DIR_ENTRY_cDate, _FAT_filetime_getDateFromRTC());
-
+
if (!_FAT_directory_addEntry (partition, &dirEntry, dirCluster)) {
r->_errno = ENOSPC;
return -1;
@@ -166,9 +166,9 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
return -1;
}
}
-
+
file->filesize = u8array_to_u32 (dirEntry.entryData, DIR_ENTRY_fileSize);
-
+
/* Allow LARGEFILEs with undefined results
// Make sure that the file size can fit in the available space
if (!(flags & O_LARGEFILE) && (file->filesize >= (1<<31))) {
@@ -176,16 +176,16 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
return -1;
}
*/
-
+
// Make sure we aren't trying to write to a read-only file
if (file->write && !_FAT_directory_isWritable(&dirEntry)) {
r->_errno = EROFS;
return -1;
- }
+ }
// Associate this file with a particular partition
file->partition = partition;
-
+
file->startCluster = _FAT_directory_entryGetCluster (dirEntry.entryData);
// Truncate the file if requested
@@ -199,21 +199,21 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
if (file->startCluster == 0) {
file->startCluster = _FAT_fat_linkFreeCluster (partition, CLUSTER_FREE);
}
-
+
// Remember the position of this file's directory entry
file->dirEntryStart = dirEntry.dataStart; // Points to the start of the LFN entries of a file, or the alias for no LFN
- file->dirEntryEnd = dirEntry.dataEnd;
-
+ file->dirEntryEnd = dirEntry.dataEnd;
+
file->currentPosition = 0;
-
+
file->rwPosition.cluster = file->startCluster;
file->rwPosition.sector = 0;
file->rwPosition.byte = 0;
-
+
file->appendPosition.cluster = _FAT_fat_lastCluster (partition, file->startCluster);
file->appendPosition.sector = (file->filesize % partition->bytesPerCluster) / BYTES_PER_READ;
file->appendPosition.byte = file->filesize % BYTES_PER_READ;
-
+
// Check if the end of the file is on the end of a cluster
if ( (file->filesize > 0) && ((file->filesize % partition->bytesPerCluster)==0) ){
// Set flag to allocate a new cluster
@@ -224,32 +224,32 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
if (flags & O_APPEND) {
file->append = true;
}
-
+
file->inUse = true;
-
+
partition->openFileCount += 1;
-
+
return (int) file;
}
int _FAT_close_r (struct _reent *r, int fd) {
FILE_STRUCT* file = (FILE_STRUCT*) fd;
u8 dirEntryData[DIR_ENTRY_DATA_SIZE];
-
+
if (!file->inUse) {
r->_errno = EBADF;
return -1;
}
if (file->write) {
// Load the old entry
- _FAT_cache_readPartialSector (file->partition->cache, dirEntryData,
+ _FAT_cache_readPartialSector (file->partition->cache, dirEntryData,
_FAT_fat_clusterToSector(file->partition, file->dirEntryEnd.cluster) + file->dirEntryEnd.sector,
file->dirEntryEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
// Write new data to the directory entry
// File size
u32_to_u8array (dirEntryData, DIR_ENTRY_fileSize, file->filesize);
-
+
// Start cluster
u16_to_u8array (dirEntryData, DIR_ENTRY_cluster, file->startCluster);
u16_to_u8array (dirEntryData, DIR_ENTRY_clusterHigh, file->startCluster >> 16);
@@ -257,12 +257,12 @@ int _FAT_close_r (struct _reent *r, int fd) {
// Modification time and date
u16_to_u8array (dirEntryData, DIR_ENTRY_mTime, _FAT_filetime_getTimeFromRTC());
u16_to_u8array (dirEntryData, DIR_ENTRY_mDate, _FAT_filetime_getDateFromRTC());
-
+
// Access date
u16_to_u8array (dirEntryData, DIR_ENTRY_aDate, _FAT_filetime_getDateFromRTC());
// Write the new entry
- _FAT_cache_writePartialSector (file->partition->cache, dirEntryData,
+ _FAT_cache_writePartialSector (file->partition->cache, dirEntryData,
_FAT_fat_clusterToSector(file->partition, file->dirEntryEnd.cluster) + file->dirEntryEnd.sector,
file->dirEntryEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
@@ -272,22 +272,22 @@ int _FAT_close_r (struct _reent *r, int fd) {
return -1;
}
}
-
- file->inUse = false;
+
+ file->inUse = false;
file->partition->openFileCount -= 1;
-
+
return 0;
}
int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
FILE_STRUCT* file = (FILE_STRUCT*) fd;
-
+
PARTITION* partition;
CACHE* cache;
-
+
FILE_POSITION position;
u32 tempNextCluster;
-
+
int tempVar;
u32 remain;
@@ -304,7 +304,7 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
if (file->currentPosition >= file->filesize) {
return 0;
}
-
+
// Don't read past end of file
if (len + file->currentPosition > file->filesize) {
r->_errno = EOVERFLOW;
@@ -314,7 +314,7 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
remain = len;
position = file->rwPosition;
-
+
partition = file->partition;
cache = file->partition->cache;
@@ -324,11 +324,11 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
tempVar = remain;
}
- if ((tempVar < BYTES_PER_READ) && flagNoError)
+ if ((tempVar < BYTES_PER_READ) && flagNoError)
{
_FAT_cache_readPartialSector ( cache, ptr, _FAT_fat_clusterToSector (partition, position.cluster) + position.sector,
position.byte, tempVar);
-
+
remain -= tempVar;
ptr += tempVar;
@@ -354,7 +354,7 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
remain -= tempVar * BYTES_PER_READ;
position.sector += tempVar;
}
-
+
// Move onto next cluster
// It should get to here without reading anything if a cluster is due to be allocated
if (position.sector >= partition->sectorsPerCluster) {
@@ -402,7 +402,7 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
// Last remaining sector
// Check if anything is left
if ((remain > 0) && flagNoError) {
- _FAT_cache_readPartialSector ( cache, ptr,
+ _FAT_cache_readPartialSector ( cache, ptr,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, 0, remain);
position.byte += remain;
remain = 0;
@@ -423,37 +423,37 @@ Extend a file so that the size is the same as the rwPosition
static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
PARTITION* partition = file->partition;
CACHE* cache = file->partition->cache;
-
+
FILE_POSITION position;
-
+
u32 remain;
-
- u8 zeroBuffer [BYTES_PER_READ] = {0};
-
+
+ u8 zeroBuffer [BYTES_PER_READ] = {0};
+
u32 tempNextCluster;
-
+
position.byte = file->filesize % BYTES_PER_READ;
position.sector = (file->filesize % partition->bytesPerCluster) / BYTES_PER_READ;
position.cluster = _FAT_fat_lastCluster (partition, file->startCluster);
-
+
remain = file->currentPosition - file->filesize;
-
-
+
+
// Only need to clear to the end of the sector
if (remain + position.byte < BYTES_PER_READ) {
- _FAT_cache_writePartialSector (cache, zeroBuffer,
+ _FAT_cache_writePartialSector (cache, zeroBuffer,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, position.byte, remain);
position.byte += remain;
} else {
if (position.byte > 0) {
- _FAT_cache_writePartialSector (cache, zeroBuffer,
+ _FAT_cache_writePartialSector (cache, zeroBuffer,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, position.byte,
BYTES_PER_READ - position.byte);
remain -= (BYTES_PER_READ - position.byte);
position.byte = 0;
position.sector ++;
}
-
+
while (remain >= BYTES_PER_READ) {
if (position.sector >= partition->sectorsPerCluster) {
position.sector = 0;
@@ -461,7 +461,7 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE)) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
- }
+ }
if (tempNextCluster == CLUSTER_FREE) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
@@ -470,21 +470,21 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
position.cluster = tempNextCluster;
}
}
-
- _FAT_disc_writeSectors (partition->disc,
+
+ _FAT_disc_writeSectors (partition->disc,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, 1, zeroBuffer);
-
+
remain -= BYTES_PER_READ;
position.sector ++;
}
-
+
if (position.sector >= partition->sectorsPerCluster) {
position.sector = 0;
tempNextCluster = _FAT_fat_nextCluster(partition, position.cluster);
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE)) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
- }
+ }
if (tempNextCluster == CLUSTER_FREE) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
@@ -493,14 +493,14 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
position.cluster = tempNextCluster;
}
}
-
+
if (remain > 0) {
- _FAT_cache_writePartialSector (cache, zeroBuffer,
+ _FAT_cache_writePartialSector (cache, zeroBuffer,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, 0, remain);
position.byte = remain;
}
}
-
+
file->rwPosition = position;
file->filesize = file->currentPosition;
return true;
@@ -509,13 +509,13 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
FILE_STRUCT* file = (FILE_STRUCT*) fd;
-
+
PARTITION* partition;
CACHE* cache;
-
+
FILE_POSITION position;
u32 tempNextCluster;
-
+
int tempVar;
u32 remain;
@@ -528,7 +528,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
r->_errno = EBADF;
return -1;
}
-
+
partition = file->partition;
cache = file->partition->cache;
remain = len;
@@ -543,16 +543,16 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
return 0;
}
}
-
+
// Write at current read pointer
position = file->rwPosition;
-
+
// If it is writing past the current end of file, set appending flag
if (len + file->currentPosition > file->filesize) {
flagAppending = true;
}
}
-
+
// Move onto next cluster if needed
if (position.sector >= partition->sectorsPerCluster) {
position.sector = 0;
@@ -560,7 +560,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE)) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
- }
+ }
if (tempNextCluster == CLUSTER_FREE) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
@@ -569,7 +569,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
position.cluster = tempNextCluster;
}
}
-
+
// Align to sector
tempVar = BYTES_PER_READ - position.byte;
if (tempVar > remain) {
@@ -578,14 +578,14 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
if ((tempVar < BYTES_PER_READ) && flagNoError) {
// Write partial sector to disk
- _FAT_cache_writePartialSector (cache, ptr,
+ _FAT_cache_writePartialSector (cache, ptr,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, position.byte, tempVar);
remain -= tempVar;
ptr += tempVar;
position.byte += tempVar;
-
+
// Move onto next sector
if (position.byte >= BYTES_PER_READ) {
position.byte = 0;
@@ -602,7 +602,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
}
if ((tempVar > 0) && flagNoError) {
- _FAT_disc_writeSectors (partition->disc,
+ _FAT_disc_writeSectors (partition->disc,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, tempVar, ptr);
ptr += tempVar * BYTES_PER_READ;
remain -= tempVar * BYTES_PER_READ;
@@ -615,7 +615,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE)) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
- }
+ }
if (tempNextCluster == CLUSTER_FREE) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
@@ -636,7 +636,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE)) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
- }
+ }
if (tempNextCluster == CLUSTER_FREE) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
@@ -653,27 +653,27 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
// Write remaining sectors
tempVar = remain / BYTES_PER_READ; // Number of sectors left
if ((tempVar > 0) && flagNoError) {
- _FAT_disc_writeSectors (partition->disc, _FAT_fat_clusterToSector (partition, position.cluster),
+ _FAT_disc_writeSectors (partition->disc, _FAT_fat_clusterToSector (partition, position.cluster),
tempVar, ptr);
ptr += tempVar * BYTES_PER_READ;
remain -= tempVar * BYTES_PER_READ;
position.sector += tempVar;
}
-
+
// Last remaining sector
if ((remain > 0) && flagNoError) {
if (flagAppending) {
- _FAT_cache_eraseWritePartialSector ( cache, ptr,
+ _FAT_cache_eraseWritePartialSector ( cache, ptr,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, 0, remain);
} else {
- _FAT_cache_writePartialSector ( cache, ptr,
+ _FAT_cache_writePartialSector ( cache, ptr,
_FAT_fat_clusterToSector (partition, position.cluster) + position.sector, 0, remain);
}
position.byte += remain;
remain = 0;
}
-
+
// Amount read is the originally requested amount minus stuff remaining
len = len - remain;
@@ -697,7 +697,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
int _FAT_seek_r (struct _reent *r, int fd, int pos, int dir) {
FILE_STRUCT* file = (FILE_STRUCT*) fd;
-
+
PARTITION* partition;
u32 cluster, nextCluster;
@@ -726,7 +726,7 @@ int _FAT_seek_r (struct _reent *r, int fd, int pos, int dir) {
r->_errno = EINVAL;
return -1;
}
-
+
if ((pos > 0) && (position < 0)) {
r->_errno = EOVERFLOW;
return -1;
@@ -736,15 +736,15 @@ int _FAT_seek_r (struct _reent *r, int fd, int pos, int dir) {
r->_errno = EINVAL;
return -1;
}
-
+
// Only change the read/write position if it is within the bounds of the current filesize
if (file->filesize > position) {
-
+
// Calculate the sector and byte of the current position,
// and store them
file->rwPosition.sector = (position % partition->bytesPerCluster) / BYTES_PER_READ;
file->rwPosition.byte = position % BYTES_PER_READ;
-
+
// Calculate where the correct cluster is
if (position >= file->currentPosition) {
clusCount = (position / partition->bytesPerCluster) - (file->currentPosition / partition->bytesPerCluster);
@@ -753,27 +753,27 @@ int _FAT_seek_r (struct _reent *r, int fd, int pos, int dir) {
clusCount = position / partition->bytesPerCluster;
cluster = file->startCluster;
}
-
+
nextCluster = _FAT_fat_nextCluster (partition, cluster);
while ((clusCount > 0) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF)) {
clusCount--;
cluster = nextCluster;
nextCluster = _FAT_fat_nextCluster (partition, cluster);
}
-
+
// Check if ran out of clusters, and the file is being written to
if ((clusCount > 0) && (file->write || file->append)) {
// Set flag to allocate a new cluster
file->rwPosition.sector = partition->sectorsPerCluster;
file->rwPosition.byte = 0;
}
-
+
file->rwPosition.cluster = cluster;
}
-
+
// Save position
file->currentPosition = position;
-
+
return position;
}
@@ -781,9 +781,9 @@ int _FAT_seek_r (struct _reent *r, int fd, int pos, int dir) {
int _FAT_fstat_r (struct _reent *r, int fd, struct stat *st) {
FILE_STRUCT* file = (FILE_STRUCT*) fd;
-
+
PARTITION* partition;
-
+
DIR_ENTRY fileEntry;
if ((file == NULL) || (file->inUse == false)) {
@@ -793,11 +793,11 @@ int _FAT_fstat_r (struct _reent *r, int fd, struct stat *st) {
}
partition = file->partition;
-
+
// Get the file's entry data
fileEntry.dataStart = file->dirEntryStart;
fileEntry.dataEnd = file->dirEntryEnd;
-
+
if (!_FAT_directory_entryFromPosition (partition, &fileEntry)) {
r->_errno = EIO;
return -1;
@@ -805,11 +805,11 @@ int _FAT_fstat_r (struct _reent *r, int fd, struct stat *st) {
// Fill in the stat struct
_FAT_directory_entryStat (partition, &fileEntry, st);
-
+
// Fix stats that have changed since the file was openned
st->st_ino = (ino_t)(file->startCluster); // The file serial number is the start cluster
- st->st_size = file->filesize; // File size
-
+ st->st_size = file->filesize; // File size
+
return 0;
}
-
+