2009-11-15 22:30:44 +01:00
|
|
|
/*
|
|
|
|
fatdir.c
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
Functions used by the newlib disc stubs to interface with
|
2009-11-15 22:30:44 +01:00
|
|
|
this library
|
|
|
|
|
|
|
|
Copyright (c) 2006 Michael "Chishm" Chisholm
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-11-15 22:30:44 +01:00
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
3. The name of the author may not be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
2009-11-15 22:30:44 +01:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2010-09-24 02:48:03 +02:00
|
|
|
*/
|
2009-11-15 22:30:44 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/dir.h>
|
|
|
|
|
|
|
|
#include "fatdir.h"
|
|
|
|
|
2010-09-18 01:28:01 +02:00
|
|
|
#include "fat_cache.h"
|
2009-11-15 22:30:44 +01:00
|
|
|
#include "file_allocation_table.h"
|
|
|
|
#include "partition.h"
|
|
|
|
#include "directory.h"
|
|
|
|
#include "bit_ops.h"
|
|
|
|
#include "filetime.h"
|
|
|
|
#include "lock.h"
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_stat_r(struct _reent *r, const char *path, struct stat *st)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
PARTITION* partition = NULL;
|
|
|
|
DIR_ENTRY dirEntry;
|
|
|
|
|
|
|
|
// Get the partition this file is on
|
2010-09-24 02:48:03 +02:00
|
|
|
partition = _FAT_partition_getPartitionFromPath(path);
|
|
|
|
if (partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the path pointer to the start of the actual path
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
path = strchr(path, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Search for the file on the disc
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_entryFromPath(partition, &dirEntry, path, NULL))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the stat struct
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_directory_entryStat(partition, &dirEntry, st);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_link_r(struct _reent *r, const char *existing, const char *newLink)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENOTSUP;
|
|
|
|
return -1;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_unlink_r(struct _reent *r, const char *path)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
PARTITION* partition = NULL;
|
|
|
|
DIR_ENTRY dirEntry;
|
|
|
|
DIR_ENTRY dirContents;
|
|
|
|
uint32_t cluster;
|
|
|
|
bool nextEntry;
|
|
|
|
bool errorOccured = false;
|
|
|
|
|
|
|
|
// Get the partition this directory is on
|
2010-09-24 02:48:03 +02:00
|
|
|
partition = _FAT_partition_getPartitionFromPath(path);
|
|
|
|
if (partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we aren't trying to write to a read-only disc
|
2010-09-24 02:48:03 +02:00
|
|
|
if (partition->readOnly)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the path pointer to the start of the actual path
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
path = strchr(path, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Search for the file on the disc
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_entryFromPath(partition, &dirEntry, path, NULL))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
cluster = _FAT_directory_entryGetCluster(partition, dirEntry.entryData);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// If this is a directory, make sure it is empty
|
2010-09-24 02:48:03 +02:00
|
|
|
if (_FAT_directory_isDirectory(&dirEntry))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
nextEntry = _FAT_directory_getFirstEntry(partition, &dirContents, cluster);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
while (nextEntry)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_isDot(&dirContents))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// The directory had something in it that isn't a reference to itself or it's parent
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EPERM;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
nextEntry = _FAT_directory_getNextEntry(partition, &dirContents);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (_FAT_fat_isValidCluster(partition, cluster))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// Remove the cluster chain for this file
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_fat_clearLinks(partition, cluster))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EIO;
|
|
|
|
errorOccured = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the directory entry for this file
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_removeEntry(partition, &dirEntry))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EIO;
|
|
|
|
errorOccured = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush any sectors in the disc cache
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_cache_flush(partition->cache))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EIO;
|
|
|
|
errorOccured = true;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
|
|
|
if (errorOccured)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_chdir_r(struct _reent *r, const char *path)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
PARTITION* partition = NULL;
|
|
|
|
|
|
|
|
// Get the partition this directory is on
|
2010-09-24 02:48:03 +02:00
|
|
|
partition = _FAT_partition_getPartitionFromPath(path);
|
|
|
|
if (partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the path pointer to the start of the actual path
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
path = strchr(path, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Try changing directory
|
2010-09-24 02:48:03 +02:00
|
|
|
if (_FAT_directory_chdir(partition, path))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// Successful
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Failed
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_rename_r(struct _reent *r, const char *oldName, const char *newName)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
PARTITION* partition = NULL;
|
|
|
|
DIR_ENTRY oldDirEntry;
|
|
|
|
DIR_ENTRY newDirEntry;
|
|
|
|
const char *pathEnd;
|
|
|
|
uint32_t dirCluster;
|
|
|
|
|
|
|
|
// Get the partition this directory is on
|
2010-09-24 02:48:03 +02:00
|
|
|
partition = _FAT_partition_getPartitionFromPath(oldName);
|
|
|
|
if (partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Make sure the same partition is used for the old and new names
|
2010-09-24 02:48:03 +02:00
|
|
|
if (partition != _FAT_partition_getPartitionFromPath(newName))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EXDEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we aren't trying to write to a read-only disc
|
2010-09-24 02:48:03 +02:00
|
|
|
if (partition->readOnly)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the path pointer to the start of the actual path
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(oldName, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
oldName = strchr(oldName, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(oldName, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(newName, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
newName = strchr(newName, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(newName, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the file on the disc
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_entryFromPath(partition, &oldDirEntry, oldName, NULL))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is no existing file / directory with the new name
|
2010-09-24 02:48:03 +02:00
|
|
|
if (_FAT_directory_entryFromPath(partition, &newDirEntry, newName, NULL))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EEXIST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new file entry
|
|
|
|
// Get the directory it has to go in
|
2010-09-24 02:48:03 +02:00
|
|
|
pathEnd = strrchr(newName, DIR_SEPARATOR);
|
|
|
|
if (pathEnd == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// No path was specified
|
|
|
|
dirCluster = partition->cwdCluster;
|
|
|
|
pathEnd = newName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Path was specified -- get the right dirCluster
|
|
|
|
// Recycling newDirEntry, since it needs to be recreated anyway
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_entryFromPath(partition, &newDirEntry, newName, pathEnd) || !_FAT_directory_isDirectory(
|
|
|
|
&newDirEntry))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
dirCluster = _FAT_directory_entryGetCluster(partition, newDirEntry.entryData);
|
2010-09-19 01:16:05 +02:00
|
|
|
// Move the pathEnd past the last DIR_SEPARATOR
|
|
|
|
pathEnd += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the entry data
|
2010-09-24 02:48:03 +02:00
|
|
|
memcpy(&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY));
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Set the new name
|
2010-09-24 02:48:03 +02:00
|
|
|
strncpy(newDirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Write the new entry
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_addEntry(partition, &newDirEntry, dirCluster))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOSPC;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old entry
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_removeEntry(partition, &oldDirEntry))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush any sectors in the disc cache
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_cache_flush(partition->cache))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_mkdir_r(struct _reent *r, const char *path, int mode)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
PARTITION* partition = NULL;
|
|
|
|
bool fileExists;
|
|
|
|
DIR_ENTRY dirEntry;
|
|
|
|
const char* pathEnd;
|
|
|
|
uint32_t parentCluster, dirCluster;
|
|
|
|
uint8_t newEntryData[DIR_ENTRY_DATA_SIZE];
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
partition = _FAT_partition_getPartitionFromPath(path);
|
|
|
|
if (partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the path pointer to the start of the actual path
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
path = strchr(path, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Search for the file/directory on the disc
|
2010-09-24 02:48:03 +02:00
|
|
|
fileExists = _FAT_directory_entryFromPath(partition, &dirEntry, path, NULL);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Make sure it doesn't exist
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fileExists)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EEXIST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (partition->readOnly)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// We can't write to a read-only partition
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the directory it has to go in
|
2010-09-24 02:48:03 +02:00
|
|
|
pathEnd = strrchr(path, DIR_SEPARATOR);
|
|
|
|
if (pathEnd == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// No path was specified
|
|
|
|
parentCluster = partition->cwdCluster;
|
|
|
|
pathEnd = path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Path was specified -- get the right parentCluster
|
|
|
|
// Recycling dirEntry, since it needs to be recreated anyway
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_entryFromPath(partition, &dirEntry, path, pathEnd)
|
|
|
|
|| !_FAT_directory_isDirectory(&dirEntry))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
parentCluster = _FAT_directory_entryGetCluster(partition, dirEntry.entryData);
|
2010-09-19 01:16:05 +02:00
|
|
|
// Move the pathEnd past the last DIR_SEPARATOR
|
|
|
|
pathEnd += 1;
|
|
|
|
}
|
|
|
|
// Create the entry data
|
2010-09-24 02:48:03 +02:00
|
|
|
strncpy(dirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
|
|
|
|
memset(dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Set the creation time and date
|
|
|
|
dirEntry.entryData[DIR_ENTRY_cTime_ms] = 0;
|
2010-09-24 02:48:03 +02:00
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_cTime, _FAT_filetime_getTimeFromRTC());
|
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_cDate, _FAT_filetime_getDateFromRTC());
|
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_mTime, _FAT_filetime_getTimeFromRTC());
|
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_mDate, _FAT_filetime_getDateFromRTC());
|
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_aDate, _FAT_filetime_getDateFromRTC());
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Set the directory attribute
|
|
|
|
dirEntry.entryData[DIR_ENTRY_attributes] = ATTRIB_DIR;
|
|
|
|
|
|
|
|
// Get a cluster for the new directory
|
2010-09-24 02:48:03 +02:00
|
|
|
dirCluster = _FAT_fat_linkFreeClusterCleared(partition, CLUSTER_FREE);
|
|
|
|
if (!_FAT_fat_isValidCluster(partition, dirCluster))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// No space left on disc for the cluster
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOSPC;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_cluster, dirCluster);
|
|
|
|
u16_to_u8array(dirEntry.entryData, DIR_ENTRY_clusterHigh, dirCluster >> 16);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Write the new directory's entry to it's parent
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_addEntry(partition, &dirEntry, parentCluster))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOSPC;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the dot entry within the directory
|
2010-09-24 02:48:03 +02:00
|
|
|
memset(newEntryData, 0, DIR_ENTRY_DATA_SIZE);
|
|
|
|
memset(newEntryData, ' ', 11);
|
2010-09-19 01:16:05 +02:00
|
|
|
newEntryData[DIR_ENTRY_name] = '.';
|
|
|
|
newEntryData[DIR_ENTRY_attributes] = ATTRIB_DIR;
|
2010-09-24 02:48:03 +02:00
|
|
|
u16_to_u8array(newEntryData, DIR_ENTRY_cluster, dirCluster);
|
|
|
|
u16_to_u8array(newEntryData, DIR_ENTRY_clusterHigh, dirCluster >> 16);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Write it to the directory, erasing that sector in the process
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_cache_eraseWritePartialSector(partition->cache, newEntryData, _FAT_fat_clusterToSector(partition, dirCluster),
|
|
|
|
0, DIR_ENTRY_DATA_SIZE);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Create the double dot entry within the directory
|
|
|
|
|
|
|
|
// if ParentDir == Rootdir then ".."" always link to Cluster 0
|
2010-09-24 02:48:03 +02:00
|
|
|
if (parentCluster == partition->rootDirCluster) parentCluster = FAT16_ROOT_DIR_CLUSTER;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
newEntryData[DIR_ENTRY_name + 1] = '.';
|
2010-09-24 02:48:03 +02:00
|
|
|
u16_to_u8array(newEntryData, DIR_ENTRY_cluster, parentCluster);
|
|
|
|
u16_to_u8array(newEntryData, DIR_ENTRY_clusterHigh, parentCluster >> 16);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Write it to the directory
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_cache_writePartialSector(partition->cache, newEntryData, _FAT_fat_clusterToSector(partition, dirCluster),
|
|
|
|
DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Flush any sectors in the disc cache
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_cache_flush(partition->cache))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_statvfs_r(struct _reent *r, const char *path, struct statvfs *buf)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
PARTITION* partition = NULL;
|
|
|
|
unsigned int freeClusterCount;
|
|
|
|
|
|
|
|
// Get the partition of the requested path
|
2010-09-24 02:48:03 +02:00
|
|
|
partition = _FAT_partition_getPartitionFromPath(path);
|
|
|
|
if (partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
freeClusterCount = _FAT_fat_freeClusterCount(partition);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// FAT clusters = POSIX blocks
|
2010-09-24 02:48:03 +02:00
|
|
|
buf->f_bsize = partition->bytesPerCluster; // File system block size.
|
2010-09-19 01:16:05 +02:00
|
|
|
buf->f_frsize = partition->bytesPerCluster; // Fundamental file system block size.
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
buf->f_blocks = partition->fat.lastCluster - CLUSTER_FIRST + 1; // Total number of blocks on file system in units of f_frsize.
|
|
|
|
buf->f_bfree = freeClusterCount; // Total number of free blocks.
|
|
|
|
buf->f_bavail = freeClusterCount; // Number of free blocks available to non-privileged process.
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Treat requests for info on inodes as clusters
|
2010-09-24 02:48:03 +02:00
|
|
|
buf->f_files = partition->fat.lastCluster - CLUSTER_FIRST + 1; // Total number of file serial numbers.
|
|
|
|
buf->f_ffree = freeClusterCount; // Total number of free file serial numbers.
|
|
|
|
buf->f_favail = freeClusterCount; // Number of file serial numbers available to non-privileged process.
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// File system ID. 32bit ioType value
|
2010-09-24 02:48:03 +02:00
|
|
|
buf->f_fsid = _FAT_disc_hostType(partition->disc);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Bit mask of f_flag values.
|
|
|
|
buf->f_flag = ST_NOSUID /* No support for ST_ISUID and ST_ISGID file mode bits */
|
2010-09-24 02:48:03 +02:00
|
|
|
| (partition->readOnly ? ST_RDONLY /* Read only file system */: 0);
|
2010-09-19 01:16:05 +02:00
|
|
|
// Maximum filename length.
|
|
|
|
buf->f_namemax = MAX_FILENAME_LENGTH;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
DIR_ITER* _FAT_diropen_r(struct _reent *r, DIR_ITER *dirState, const char *path)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
DIR_ENTRY dirEntry;
|
2010-09-24 02:48:03 +02:00
|
|
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) (dirState->dirStruct);
|
2010-09-19 01:16:05 +02:00
|
|
|
bool fileExists;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
state->partition = _FAT_partition_getPartitionFromPath(path);
|
|
|
|
if (state->partition == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = ENODEV;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the path pointer to the start of the actual path
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
path = strchr(path, ':') + 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strchr(path, ':') != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
r->_errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Get the start cluster of the directory
|
2010-09-24 02:48:03 +02:00
|
|
|
fileExists = _FAT_directory_entryFromPath(state->partition, &dirEntry, path, NULL);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!fileExists)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOENT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is a directory
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!_FAT_directory_isDirectory(&dirEntry))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOTDIR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the start cluster for use when resetting the directory data
|
2010-09-24 02:48:03 +02:00
|
|
|
state->startCluster = _FAT_directory_entryGetCluster(state->partition, dirEntry.entryData);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Get the first entry for use with a call to dirnext
|
2010-09-24 02:48:03 +02:00
|
|
|
state->validEntry = _FAT_directory_getFirstEntry(state->partition, &(state->currentEntry), state->startCluster);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// We are now using this entry
|
|
|
|
state->inUse = true;
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
|
|
|
return (DIR_ITER*) state;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_dirreset_r(struct _reent *r, DIR_ITER *dirState)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) (dirState->dirStruct);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&state->partition->lock);
|
2010-02-09 11:59:55 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Make sure we are still using this entry
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!state->inUse)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Get the first entry for use with a call to dirnext
|
2010-09-24 02:48:03 +02:00
|
|
|
state->validEntry = _FAT_directory_getFirstEntry(state->partition, &(state->currentEntry), state->startCluster);
|
2010-02-09 11:59:55 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_dirnext_r(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) (dirState->dirStruct);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// Make sure we are still using this entry
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!state->inUse)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is another file to report on
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!state->validEntry)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
r->_errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the filename
|
2010-09-24 02:48:03 +02:00
|
|
|
strncpy(filename, state->currentEntry.filename, MAX_FILENAME_LENGTH);
|
2010-09-19 01:16:05 +02:00
|
|
|
// Get the stats, if requested
|
2010-09-24 02:48:03 +02:00
|
|
|
if (filestat != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_directory_entryStat(state->partition, &(state->currentEntry), filestat);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the next entry for use next time
|
2010-09-24 02:48:03 +02:00
|
|
|
state->validEntry = _FAT_directory_getNextEntry(state->partition, &(state->currentEntry));
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int _FAT_dirclose_r(struct _reent *r, DIR_ITER *dirState)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) (dirState->dirStruct);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
// We are no longer using this entry
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_lock(&state->partition->lock);
|
2010-09-19 01:16:05 +02:00
|
|
|
state->inUse = false;
|
2010-09-24 02:48:03 +02:00
|
|
|
_FAT_unlock(&state->partition->lock);
|
2010-02-09 11:59:55 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-11-15 22:30:44 +01:00
|
|
|
}
|