mirror of
https://github.com/wiiu-env/libfat.git
synced 2025-02-16 17:29:23 +01:00
Modified WinterMute's seek then write fix for elegance, removed
resetPosition
This commit is contained in:
parent
02d3e47079
commit
8d36ebe061
@ -52,6 +52,10 @@
|
|||||||
2007-11-04 - Chishm
|
2007-11-04 - Chishm
|
||||||
* file_extend_r renamed to _FAT_file_extend_r
|
* file_extend_r renamed to _FAT_file_extend_r
|
||||||
* A cluster is only allocated for a file when data is written, instead of when the file is opened
|
* A cluster is only allocated for a file when data is written, instead of when the file is opened
|
||||||
|
|
||||||
|
2008-05-12 - Chishm
|
||||||
|
* Modified WinterMute's seek then write fix for elegance
|
||||||
|
* Removed resetPosition
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@ -68,20 +72,6 @@
|
|||||||
#include "bit_ops.h"
|
#include "bit_ops.h"
|
||||||
#include "filetime.h"
|
#include "filetime.h"
|
||||||
|
|
||||||
static void _FAT_file_resetPosition (FILE_STRUCT* file) {
|
|
||||||
PARTITION* partition = file->partition;
|
|
||||||
|
|
||||||
if ( file->currentPosition < partition->bytesPerCluster ) {
|
|
||||||
file->rwPosition.cluster = file->startCluster;
|
|
||||||
file->rwPosition.sector = (file->currentPosition % partition->bytesPerCluster) / BYTES_PER_READ;
|
|
||||||
file->rwPosition.byte = file->currentPosition % BYTES_PER_READ;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
|
int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
|
||||||
PARTITION* partition = NULL;
|
PARTITION* partition = NULL;
|
||||||
bool fileExists;
|
bool fileExists;
|
||||||
@ -218,7 +208,7 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
|
|||||||
// Truncate the file if requested
|
// Truncate the file if requested
|
||||||
if ((flags & O_TRUNC) && file->write && (file->startCluster != 0)) {
|
if ((flags & O_TRUNC) && file->write && (file->startCluster != 0)) {
|
||||||
_FAT_fat_clearLinks (partition, file->startCluster);
|
_FAT_fat_clearLinks (partition, file->startCluster);
|
||||||
file->startCluster = 0;
|
file->startCluster = CLUSTER_FREE;
|
||||||
file->filesize = 0;
|
file->filesize = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,8 +216,14 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
|
|||||||
file->dirEntryStart = dirEntry.dataStart; // Points to the start of the LFN entries of a file, or the alias for no LFN
|
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;
|
||||||
|
|
||||||
|
// Reset read/write and append pointers
|
||||||
file->currentPosition = 0;
|
file->currentPosition = 0;
|
||||||
_FAT_file_resetPosition (file);
|
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
|
// Check if the end of the file is on the end of a cluster
|
||||||
if ( (file->filesize > 0) && ((file->filesize % partition->bytesPerCluster)==0) ){
|
if ( (file->filesize > 0) && ((file->filesize % partition->bytesPerCluster)==0) ){
|
||||||
@ -312,12 +308,12 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
|
|||||||
// Make sure we can actually read from the file
|
// Make sure we can actually read from the file
|
||||||
if ((file == NULL) || !file->inUse || !file->read) {
|
if ((file == NULL) || !file->inUse || !file->read) {
|
||||||
r->_errno = EBADF;
|
r->_errno = EBADF;
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't try to read if the read pointer is past the end of file
|
// Don't try to read if the read pointer is past the end of file
|
||||||
if (file->currentPosition >= file->filesize || file->startCluster == CLUSTER_FREE) {
|
if (file->currentPosition >= file->filesize || file->startCluster == CLUSTER_FREE) {
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't read past end of file
|
// Don't read past end of file
|
||||||
@ -573,7 +569,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
|
|||||||
// Make sure we can actually write to the file
|
// Make sure we can actually write to the file
|
||||||
if ((file == NULL) || !file->inUse || !file->write) {
|
if ((file == NULL) || !file->inUse || !file->write) {
|
||||||
r->_errno = EBADF;
|
r->_errno = EBADF;
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Short circuit cases where len is 0 (or less)
|
// Short circuit cases where len is 0 (or less)
|
||||||
@ -585,12 +581,21 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
|
|||||||
cache = file->partition->cache;
|
cache = file->partition->cache;
|
||||||
remain = len;
|
remain = len;
|
||||||
|
|
||||||
// Get a new cluster for the file if required
|
// Get a new cluster for the start of the file if required
|
||||||
if (file->startCluster == CLUSTER_FREE) {
|
if (file->startCluster == CLUSTER_FREE) {
|
||||||
file->startCluster = _FAT_fat_linkFreeCluster (partition, CLUSTER_FREE);
|
tempNextCluster = _FAT_fat_linkFreeCluster (partition, CLUSTER_FREE);
|
||||||
_FAT_file_resetPosition (file);
|
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
|
||||||
|
// Couldn't get a cluster, so abort immediately
|
||||||
|
r->_errno = ENOSPC;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
file->startCluster = tempNextCluster;
|
||||||
|
// Appending starts at the begining for a 0 byte file
|
||||||
|
file->appendPosition.cluster = file->startCluster;
|
||||||
|
file->appendPosition.sector = 0;
|
||||||
|
file->appendPosition.byte = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file->append) {
|
if (file->append) {
|
||||||
position = file->appendPosition;
|
position = file->appendPosition;
|
||||||
flagAppending = true;
|
flagAppending = true;
|
||||||
@ -598,7 +603,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
|
|||||||
// If the write pointer is past the end of the file, extend the file to that size
|
// If the write pointer is past the end of the file, extend the file to that size
|
||||||
if (file->currentPosition > file->filesize) {
|
if (file->currentPosition > file->filesize) {
|
||||||
if (!_FAT_file_extend_r (r, file)) {
|
if (!_FAT_file_extend_r (r, file)) {
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user