add new elements for devkitARM r29, use internal deallocaters
[libfat.git] / source / libfat.c
blob09aae704a8971d290ee8c9e4075ba93873514bca
1 /*
2 libfat.c
3 Simple functionality for startup, mounting and unmounting of FAT-based devices.
5 Copyright (c) 2006 Michael "Chishm" Chisholm
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution.
15 3. The name of the author may not be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/iosupport.h>
30 #include <unistd.h>
31 #include <string.h>
33 #include "common.h"
34 #include "partition.h"
35 #include "fatfile.h"
36 #include "fatdir.h"
37 #include "lock.h"
38 #include "mem_allocate.h"
39 #include "disc.h"
41 static const devoptab_t dotab_fat = {
42 "fat",
43 sizeof (FILE_STRUCT),
44 _FAT_open_r,
45 _FAT_close_r,
46 _FAT_write_r,
47 _FAT_read_r,
48 _FAT_seek_r,
49 _FAT_fstat_r,
50 _FAT_stat_r,
51 _FAT_link_r,
52 _FAT_unlink_r,
53 _FAT_chdir_r,
54 _FAT_rename_r,
55 _FAT_mkdir_r,
56 sizeof (DIR_STATE_STRUCT),
57 _FAT_diropen_r,
58 _FAT_dirreset_r,
59 _FAT_dirnext_r,
60 _FAT_dirclose_r,
61 _FAT_statvfs_r,
62 _FAT_ftruncate_r,
63 _FAT_fsync_r,
64 NULL, /* Device data */
65 NULL,
66 NULL
69 bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSector, uint32_t cacheSize, uint32_t SectorsPerPage) {
70 PARTITION* partition;
71 devoptab_t* devops;
72 char* nameCopy;
74 if(!name || !interface)
75 return false;
77 if(!interface->startup())
78 return false;
80 if(!interface->isInserted())
81 return false;
83 devops = _FAT_mem_allocate (sizeof(devoptab_t) + strlen(name) + 1);
84 if (!devops) {
85 return false;
87 // Use the space allocated at the end of the devoptab struct for storing the name
88 nameCopy = (char*)(devops+1);
90 // Initialize the file system
91 partition = _FAT_partition_constructor (interface, cacheSize, SectorsPerPage, startSector);
92 if (!partition) {
93 _FAT_mem_free (devops);
94 return false;
97 // Add an entry for this device to the devoptab table
98 memcpy (devops, &dotab_fat, sizeof(dotab_fat));
99 strcpy (nameCopy, name);
100 devops->name = nameCopy;
101 devops->deviceData = partition;
103 AddDevice (devops);
105 return true;
108 bool fatMountSimple (const char* name, const DISC_INTERFACE* interface) {
109 return fatMount (name, interface, 0, DEFAULT_CACHE_PAGES, DEFAULT_SECTORS_PAGE);
112 void fatUnmount (const char* name) {
113 devoptab_t *devops;
114 PARTITION* partition;
116 if(!name)
117 return;
119 devops = (devoptab_t*)GetDeviceOpTab (name);
120 if (!devops) {
121 return;
124 // Perform a quick check to make sure we're dealing with a libfat controlled device
125 if (devops->open_r != dotab_fat.open_r) {
126 return;
129 if (RemoveDevice (name) == -1) {
130 return;
133 partition = (PARTITION*)devops->deviceData;
134 _FAT_partition_destructor (partition);
135 _FAT_mem_free (devops);
138 bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice) {
139 int i;
140 int defaultDevice = -1;
141 const DISC_INTERFACE *disc;
143 for (i = 0;
144 _FAT_disc_interfaces[i].name != NULL && _FAT_disc_interfaces[i].getInterface != NULL;
145 i++)
147 disc = _FAT_disc_interfaces[i].getInterface();
148 if (fatMount (_FAT_disc_interfaces[i].name, disc, 0, cacheSize, DEFAULT_SECTORS_PAGE)) {
149 // The first device to successfully mount is set as the default
150 if (defaultDevice < 0) {
151 defaultDevice = i;
156 if (defaultDevice < 0) {
157 // None of our devices mounted
158 return false;
161 if (setAsDefaultDevice) {
162 char filePath[MAXPATHLEN * 2];
163 strcpy (filePath, _FAT_disc_interfaces[defaultDevice].name);
164 strcat (filePath, ":/");
165 #ifdef ARGV_MAGIC
166 if ( __system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1 && strrchr( __system_argv->argv[0], '/' )!=NULL ) {
167 // Check the app's path against each of our mounted devices, to see
168 // if we can support it. If so, change to that path.
169 for (i = 0;
170 _FAT_disc_interfaces[i].name != NULL && _FAT_disc_interfaces[i].getInterface != NULL;
171 i++)
173 if ( !strncasecmp( __system_argv->argv[0], _FAT_disc_interfaces[i].name,
174 strlen(_FAT_disc_interfaces[i].name)))
176 char *lastSlash;
177 strcpy(filePath, __system_argv->argv[0]);
178 lastSlash = strrchr( filePath, '/' );
180 if ( NULL != lastSlash) {
181 if ( *(lastSlash - 1) == ':') lastSlash++;
182 *lastSlash = 0;
187 #endif
188 chdir (filePath);
191 return true;
194 bool fatInitDefault (void) {
195 return fatInit (DEFAULT_CACHE_PAGES, true);
198 void fatGetVolumeLabel (const char* name, char *label) {
199 devoptab_t *devops;
200 PARTITION* partition;
201 char *buf;
202 int namelen,i;
204 if(!name || !label)
205 return;
207 namelen = strlen(name);
208 buf=(char*)_FAT_mem_allocate(sizeof(char)*namelen+2);
209 strcpy(buf,name);
211 if (name[namelen-1] == '/') {
212 buf[namelen-1]='\0';
213 namelen--;
216 if (name[namelen-1] != ':') {
217 buf[namelen]=':';
218 buf[namelen+1]='\0';
221 devops = (devoptab_t*)GetDeviceOpTab(buf);
223 for(i=0;buf[i]!='\0' && buf[i]!=':';i++);
224 if (!devops || strncasecmp(buf,devops->name,i)) {
225 _FAT_mem_free(buf);
226 return;
229 _FAT_mem_free(buf);
231 // Perform a quick check to make sure we're dealing with a libfat controlled device
232 if (devops->open_r != dotab_fat.open_r) {
233 return;
236 partition = (PARTITION*)devops->deviceData;
238 if(!_FAT_directory_getVolumeLabel(partition, label)) {
239 strncpy(label,partition->label,11);
240 label[11]='\0';
242 if(!strncmp(label, "NO NAME", 7)) label[0]='\0';