better check for devoptab name
[libfat.git] / source / libfat.c
blob9f8dd0146bbb0299a2f251c0a5859a41c8cf7613
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>
32 #include <stdio.h>
34 #include "common.h"
35 #include "partition.h"
36 #include "fatfile.h"
37 #include "fatdir.h"
38 #include "lock.h"
39 #include "mem_allocate.h"
40 #include "disc.h"
42 static const devoptab_t dotab_fat = {
43 "fat",
44 sizeof (FILE_STRUCT),
45 _FAT_open_r,
46 _FAT_close_r,
47 _FAT_write_r,
48 _FAT_read_r,
49 _FAT_seek_r,
50 _FAT_fstat_r,
51 _FAT_stat_r,
52 _FAT_link_r,
53 _FAT_unlink_r,
54 _FAT_chdir_r,
55 _FAT_rename_r,
56 _FAT_mkdir_r,
57 sizeof (DIR_STATE_STRUCT),
58 _FAT_diropen_r,
59 _FAT_dirreset_r,
60 _FAT_dirnext_r,
61 _FAT_dirclose_r,
62 _FAT_statvfs_r,
63 _FAT_ftruncate_r,
64 _FAT_fsync_r,
65 NULL, /* Device data */
66 NULL,
67 NULL
70 bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSector, uint32_t cacheSize, uint32_t SectorsPerPage) {
71 PARTITION* partition;
72 devoptab_t* devops;
73 char* nameCopy;
75 if(!name || strlen(name) > 8 || !interface)
76 return false;
78 if(!interface->startup())
79 return false;
81 if(!interface->isInserted())
82 return false;
84 char devname[10];
85 sprintf(devname, "%s:", name);
86 if(FindDevice(devname) >= 0)
87 return true;
89 devops = _FAT_mem_allocate (sizeof(devoptab_t) + strlen(name) + 1);
90 if (!devops) {
91 return false;
93 // Use the space allocated at the end of the devoptab struct for storing the name
94 nameCopy = (char*)(devops+1);
96 // Initialize the file system
97 partition = _FAT_partition_constructor (interface, cacheSize, SectorsPerPage, startSector);
98 if (!partition) {
99 _FAT_mem_free (devops);
100 return false;
103 // Add an entry for this device to the devoptab table
104 memcpy (devops, &dotab_fat, sizeof(dotab_fat));
105 strcpy (nameCopy, name);
106 devops->name = nameCopy;
107 devops->deviceData = partition;
109 AddDevice (devops);
111 return true;
114 bool fatMountSimple (const char* name, const DISC_INTERFACE* interface) {
115 return fatMount (name, interface, 0, DEFAULT_CACHE_PAGES, DEFAULT_SECTORS_PAGE);
118 void fatUnmount (const char* name) {
119 devoptab_t *devops;
120 PARTITION* partition;
122 if(!name)
123 return;
125 devops = (devoptab_t*)GetDeviceOpTab (name);
126 if (!devops) {
127 return;
130 // Perform a quick check to make sure we're dealing with a libfat controlled device
131 if (devops->open_r != dotab_fat.open_r) {
132 return;
135 if (RemoveDevice (name) == -1) {
136 return;
139 partition = (PARTITION*)devops->deviceData;
140 _FAT_partition_destructor (partition);
141 _FAT_mem_free (devops);
144 bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice) {
145 int i;
146 int defaultDevice = -1;
147 const DISC_INTERFACE *disc;
149 for (i = 0;
150 _FAT_disc_interfaces[i].name != NULL && _FAT_disc_interfaces[i].getInterface != NULL;
151 i++)
153 disc = _FAT_disc_interfaces[i].getInterface();
154 if (fatMount (_FAT_disc_interfaces[i].name, disc, 0, cacheSize, DEFAULT_SECTORS_PAGE)) {
155 // The first device to successfully mount is set as the default
156 if (defaultDevice < 0) {
157 defaultDevice = i;
162 if (defaultDevice < 0) {
163 // None of our devices mounted
164 return false;
167 if (setAsDefaultDevice) {
168 char filePath[MAXPATHLEN * 2];
169 strcpy (filePath, _FAT_disc_interfaces[defaultDevice].name);
170 strcat (filePath, ":/");
171 #ifdef ARGV_MAGIC
172 if ( __system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1 && strrchr( __system_argv->argv[0], '/' )!=NULL ) {
173 // Check the app's path against each of our mounted devices, to see
174 // if we can support it. If so, change to that path.
175 for (i = 0;
176 _FAT_disc_interfaces[i].name != NULL && _FAT_disc_interfaces[i].getInterface != NULL;
177 i++)
179 if ( !strncasecmp( __system_argv->argv[0], _FAT_disc_interfaces[i].name,
180 strlen(_FAT_disc_interfaces[i].name)))
182 char *lastSlash;
183 strcpy(filePath, __system_argv->argv[0]);
184 lastSlash = strrchr( filePath, '/' );
186 if ( NULL != lastSlash) {
187 if ( *(lastSlash - 1) == ':') lastSlash++;
188 *lastSlash = 0;
193 #endif
194 chdir (filePath);
197 return true;
200 bool fatInitDefault (void) {
201 return fatInit (DEFAULT_CACHE_PAGES, true);
204 void fatGetVolumeLabel (const char* name, char *label) {
205 devoptab_t *devops;
206 PARTITION* partition;
207 char *buf;
208 int namelen,i;
210 if(!name || !label)
211 return;
213 namelen = strlen(name);
214 buf=(char*)_FAT_mem_allocate(sizeof(char)*namelen+2);
215 strcpy(buf,name);
217 if (name[namelen-1] == '/') {
218 buf[namelen-1]='\0';
219 namelen--;
222 if (name[namelen-1] != ':') {
223 buf[namelen]=':';
224 buf[namelen+1]='\0';
227 devops = (devoptab_t*)GetDeviceOpTab(buf);
229 for(i=0;buf[i]!='\0' && buf[i]!=':';i++);
230 if (!devops || strncasecmp(buf,devops->name,i)) {
231 _FAT_mem_free(buf);
232 return;
235 _FAT_mem_free(buf);
237 // Perform a quick check to make sure we're dealing with a libfat controlled device
238 if (devops->open_r != dotab_fat.open_r) {
239 return;
242 partition = (PARTITION*)devops->deviceData;
244 if(!_FAT_directory_getVolumeLabel(partition, label)) {
245 strncpy(label,partition->label,11);
246 label[11]='\0';
248 if(!strncmp(label, "NO NAME", 7)) label[0]='\0';