diff --git a/include/fat.h b/include/fat.h index 605f9ec..ad1eba0 100644 --- a/include/fat.h +++ b/include/fat.h @@ -99,6 +99,21 @@ Get Volume Label */ extern void fatGetVolumeLabel (const char* name, char *label); +// File attributes +#define ATTR_ARCHIVE 0x20 // Archive +#define ATTR_DIRECTORY 0x10 // Directory +#define ATTR_VOLUME 0x08 // Volume +#define ATTR_SYSTEM 0x04 // System +#define ATTR_HIDDEN 0x02 // Hidden +#define ATTR_READONLY 0x01 // Read only + +/* +Methods to modify DOS File Attributes +*/ +int FAT_getAttr(const char *file); +int FAT_setAttr(const char *file, int attr ); + + #ifdef __cplusplus } #endif diff --git a/source/fatfile.c b/source/fatfile.c index f1e6d43..99d0e76 100644 --- a/source/fatfile.c +++ b/source/fatfile.c @@ -69,9 +69,54 @@ int FAT_getAttr(const char *file) { } int FAT_setAttr(const char *file, int attr) { - DIR_ENTRY dirEntry; - if (!_FAT_findEntry(file,&dirEntry)) return -1; + + // Defines... + DIR_ENTRY_POSITION entryEnd; + PARTITION *partition = NULL; + DIR_ENTRY* dirEntry = NULL; + + // Get Partition + partition = _FAT_partition_getPartitionFromPath( file ); + + // Check Partition + if( !partition ) + return -1; + // Move the path pointer to the start of the actual path + if (strchr (file, ':') != NULL) + file = strchr (file, ':') + 1; + if (strchr (file, ':') != NULL) + return -1; + + // Get DIR_ENTRY + if( !_FAT_directory_entryFromPath (partition, dirEntry, file, NULL) ) + return -1; + + // Get Entry-End + entryEnd = dirEntry->dataEnd; + + // Lock Partition + _FAT_lock(&partition->lock); + + + // Write Data + _FAT_cache_writePartialSector ( + partition->cache // Cache to write + , &attr // Value to be written + , _FAT_fat_clusterToSector( partition , entryEnd.cluster ) + entryEnd.sector // cluster + , entryEnd.offset * DIR_ENTRY_DATA_SIZE + DIR_ENTRY_attributes // offset + , 1 // Size in bytes + ); + + // Flush any sectors in the disc cache + if ( !_FAT_cache_flush( partition->cache ) ) { + _FAT_unlock(&partition->lock); // Unlock Partition + return -1; + } + + // Unlock Partition + _FAT_unlock(&partition->lock); + return 0; }