RetroArch
directory.h
Go to the documentation of this file.
1 /*
2  directory.h
3  Reading, writing and manipulation of the directory structure on
4  a FAT partition
5 
6  Copyright (c) 2006 Michael "Chishm" Chisholm
7 
8  Redistribution and use in source and binary forms, with or without modification,
9  are permitted provided that the following conditions are met:
10 
11  1. Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation and/or
15  other materials provided with the distribution.
16  3. The name of the author may not be used to endorse or promote products derived
17  from this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
22  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef _DIRECTORY_H
31 #define _DIRECTORY_H
32 
33 #include <sys/stat.h>
34 #include <sys/syslimits.h>
35 
36 #include "common.h"
37 #include "partition.h"
38 
39 #define DIR_ENTRY_DATA_SIZE 0x20
40 #define MAX_LFN_LENGTH 256
41 #define MAX_ALIAS_LENGTH 13
42 #define LFN_ENTRY_LENGTH 13
43 #define ALIAS_ENTRY_LENGTH 11
44 #define MAX_ALIAS_EXT_LENGTH 3
45 #define MAX_ALIAS_PRI_LENGTH 8
46 #define MAX_NUMERIC_TAIL 999999
47 #define FAT16_ROOT_DIR_CLUSTER 0
48 
49 #define DIR_SEPARATOR '/'
50 
51 // File attributes
52 #define ATTRIB_ARCH 0x20 // Archive
53 #define ATTRIB_DIR 0x10 // Directory
54 #define ATTRIB_LFN 0x0F // Long file name
55 #define ATTRIB_VOL 0x08 // Volume
56 #define ATTRIB_SYS 0x04 // System
57 #define ATTRIB_HID 0x02 // Hidden
58 #define ATTRIB_RO 0x01 // Read only
59 
60 #define CASE_LOWER_EXT 0x10 // WinNT lowercase extension
61 #define CASE_LOWER_BASE 0x08 // WinNT lowercase basename
62 
63 typedef enum {FT_DIRECTORY, FT_FILE} FILE_TYPE;
64 
65 typedef struct {
66  uint32_t cluster;
67  sec_t sector;
70 
71 typedef struct {
72  uint8_t entryData[DIR_ENTRY_DATA_SIZE];
73  DIR_ENTRY_POSITION dataStart; // Points to the start of the LFN entries of a file, or the alias for no LFN
74  DIR_ENTRY_POSITION dataEnd; // Always points to the file/directory's alias entry
75  char filename[NAME_MAX];
76 } DIR_ENTRY;
77 
78 // Directory entry offsets
93 };
94 
95 /*
96 Returns true if the file specified by entry is a directory
97 */
98 static inline bool _FAT_directory_isDirectory (DIR_ENTRY* entry) {
99  return ((entry->entryData[DIR_ENTRY_attributes] & ATTRIB_DIR) != 0);
100 }
101 
102 static inline bool _FAT_directory_isWritable (DIR_ENTRY* entry) {
103  return ((entry->entryData[DIR_ENTRY_attributes] & ATTRIB_RO) == 0);
104 }
105 
106 static inline bool _FAT_directory_isDot (DIR_ENTRY* entry) {
107  return ((entry->filename[0] == '.') && ((entry->filename[1] == '\0') ||
108  ((entry->filename[1] == '.') && entry->filename[2] == '\0')));
109 }
110 
111 /*
112 Reads the first directory entry from the directory starting at dirCluster
113 Places result in entry
114 entry will be destroyed even if no directory entry is found
115 Returns true on success, false on failure
116 */
118 
119 /*
120 Reads the next directory entry after the one already pointed to by entry
121 Places result in entry
122 entry will be destroyed even if no directory entry is found
123 Returns true on success, false on failure
124 */
126 
127 /*
128 Gets the directory entry corrsponding to the supplied path
129 entry will be destroyed even if no directory entry is found
130 pathEnd specifies the end of the path string, for cutting strings short if needed
131  specify NULL to use the full length of path
132  pathEnd is only a suggestion, and the path string will be searched up until the next PATH_SEPARATOR
133  after pathEND.
134 Returns true on success, false on failure
135 */
136 bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const char* path, const char* pathEnd);
137 
138 /*
139 Changes the current directory to the one specified by path
140 Returns true on success, false on failure
141 */
142 bool _FAT_directory_chdir (PARTITION* partition, const char* path);
143 
144 /*
145 Removes the directory entry specified by entry
146 Assumes that entry is valid
147 Returns true on success, false on failure
148 */
150 
151 /*
152 Add a directory entry to the directory specified by dirCluster
153 The fileData, dataStart and dataEnd elements of the DIR_ENTRY struct are
154 updated with the new directory entry position and alias.
155 Returns true on success, false on failure
156 */
157 bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t dirCluster);
158 
159 /*
160 Get the start cluster of a file from it's entry data
161 */
163 
164 /*
165 Fill in the file name and entry data of DIR_ENTRY* entry.
166 Assumes that the entry's dataStart and dataEnd are correct
167 Returns true on success, false on failure
168 */
170 
171 /*
172 Fill in a stat struct based on a file entry
173 */
174 void _FAT_directory_entryStat (PARTITION* partition, DIR_ENTRY* entry, struct stat *st);
175 
176 /*
177 Get volume label
178 */
180 
181 #endif // _DIRECTORY_H
unsigned char uint8_t
Definition: stdint.h:124
#define ATTRIB_RO
Definition: directory.h:58
Definition: directory.h:63
Definition: partition.h:52
FILE_TYPE
Definition: directory.h:63
GLsizei const GLchar ** path
Definition: glext.h:7901
signed int int32_t
Definition: stdint.h:123
Definition: directory.h:91
Definition: directory.h:84
static bool _FAT_directory_isDot(DIR_ENTRY *entry)
Definition: directory.h:106
bool _FAT_directory_addEntry(PARTITION *partition, DIR_ENTRY *entry, uint32_t dirCluster)
Definition: directory.c:939
Definition: directory.h:71
#define DIR_ENTRY_DATA_SIZE
Definition: directory.h:39
uint8_t entryData[DIR_ENTRY_DATA_SIZE]
Definition: directory.h:72
static bool _FAT_directory_isDirectory(DIR_ENTRY *entry)
Definition: directory.h:98
char filename[NAME_MAX]
Definition: directory.h:75
uint32_t sec_t
Definition: iosuhax_disc_interface.h:40
static IdxT partition(lua_State *L, IdxT lo, IdxT up)
Definition: ltablib.c:310
Definition: directory.h:63
Definition: directory.h:65
Definition: directory.h:87
Definition: directory.h:85
Definition: directory.h:83
bool _FAT_directory_getVolumeLabel(PARTITION *partition, char *label)
Definition: directory.c:448
bool _FAT_directory_chdir(PARTITION *partition, const char *path)
Definition: directory.c:1132
unsigned int uint32_t
Definition: stdint.h:126
bool _FAT_directory_entryFromPosition(PARTITION *partition, DIR_ENTRY *entry)
Definition: directory.c:494
Definition: directory.h:92
GLintptr offset
Definition: glext.h:6560
#define ATTRIB_DIR
Definition: directory.h:53
Definition: directory.h:80
GLuint GLsizei const GLchar * label
Definition: glext.h:8583
bool _FAT_directory_getNextEntry(PARTITION *partition, DIR_ENTRY *entry)
Definition: directory.c:303
bool _FAT_directory_removeEntry(PARTITION *partition, DIR_ENTRY *entry)
Definition: directory.c:683
DIR_ENTRY_offset
Definition: directory.h:79
Definition: directory.h:82
uint32_t _FAT_directory_entryGetCluster(PARTITION *partition, const uint8_t *entryData)
Definition: directory.c:257
void _FAT_directory_entryStat(PARTITION *partition, DIR_ENTRY *entry, struct stat *st)
Definition: directory.c:1147
static bool _FAT_directory_isWritable(DIR_ENTRY *entry)
Definition: directory.h:102
Definition: directory.h:81
Definition: directory.h:88
bool _FAT_directory_entryFromPath(PARTITION *partition, DIR_ENTRY *entry, const char *path, const char *pathEnd)
Definition: directory.c:561
Definition: directory.h:89
bool _FAT_directory_getFirstEntry(PARTITION *partition, DIR_ENTRY *entry, uint32_t dirCluster)
Definition: directory.c:414
Definition: directory.h:86
Definition: directory.h:90