| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include "fat.h" |
|---|
| 20 | |
|---|
| 21 | #include <console.h> |
|---|
| 22 | #include <error.h> |
|---|
| 23 | #include <typedefs.h> |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | int FatGetDirEntry(struct VNode* vNode,struct FatDirEntry* dirEntry,int currPos) |
|---|
| 40 | { |
|---|
| 41 | struct Buffer* buff; |
|---|
| 42 | int offset; |
|---|
| 43 | |
|---|
| 44 | if (!vNode || !vNode->superBlock) |
|---|
| 45 | return -EFAULT; |
|---|
| 46 | |
|---|
| 47 | buff=FatBlockRead(vNode,currPos/BYTES_PER_SECTOR(vNode->superBlock)); |
|---|
| 48 | if (!buff) |
|---|
| 49 | return -EIO; |
|---|
| 50 | |
|---|
| 51 | offset=currPos & (BYTES_PER_SECTOR(vNode->superBlock)-1); |
|---|
| 52 | memcpy((char*)dirEntry,(char*)((DWORD)buff->data+offset),sizeof(struct FatDirEntry)); |
|---|
| 53 | |
|---|
| 54 | BlockFree(buff); |
|---|
| 55 | return 0; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | int FatGetLongName(struct VNode* vNode,int* pos,char* longName,struct FatDirEntry* currDirEntry) |
|---|
| 74 | { |
|---|
| 75 | struct FatDirSlot* slot=(struct FatDirSlot*)currDirEntry; |
|---|
| 76 | DWORD numSlots=FAT_LONG_ENTRIES(slot->id); |
|---|
| 77 | BYTE checkSum=slot->checkSum,shortCheckSum; |
|---|
| 78 | int offset,i; |
|---|
| 79 | |
|---|
| 80 | if (!(FAT_IS_LONG(slot->id)) || !numSlots || numSlots > 20) |
|---|
| 81 | return -EINVAL; |
|---|
| 82 | |
|---|
| 83 | while (1) |
|---|
| 84 | { |
|---|
| 85 | numSlots--; |
|---|
| 86 | offset=numSlots*13; |
|---|
| 87 | |
|---|
| 88 | for (i=0; i<5; i++) |
|---|
| 89 | longName[offset+i]=slot->name[i*2]; |
|---|
| 90 | |
|---|
| 91 | for (i=0; i<6; i++) |
|---|
| 92 | longName[offset+5+i]=slot->name2[i*2]; |
|---|
| 93 | |
|---|
| 94 | for (i=0; i<2; i++) |
|---|
| 95 | longName[offset+11+i]=slot->name3[i*2]; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | if (slot->id & 0x40) |
|---|
| 99 | longName[offset+13]='\0'; |
|---|
| 100 | |
|---|
| 101 | FatGetDirEntry(vNode,currDirEntry,FAT_DIR_POS(*pos)); |
|---|
| 102 | |
|---|
| 103 | ++(*pos); |
|---|
| 104 | |
|---|
| 105 | if (!numSlots) |
|---|
| 106 | break; |
|---|
| 107 | |
|---|
| 108 | slot=(struct FatDirSlot*)currDirEntry; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | if (slot->attr != ATTR_EXT || (FAT_LONG_ENTRIES(slot->id)) != numSlots || (slot->checkSum != checkSum)) |
|---|
| 112 | return -EINVAL; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | shortCheckSum=FatCheckSum(currDirEntry->fileName); |
|---|
| 118 | |
|---|
| 119 | if (UNLIKELY(shortCheckSum != checkSum)) |
|---|
| 120 | return -EINVAL; |
|---|
| 121 | else |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | int FatScanDir(struct VNode* vNode,const char* name,int nameLength,struct FatDirEntry* retVal,DWORD* vNum,DWORD* dirPos) |
|---|
| 142 | { |
|---|
| 143 | int pos=0,sec,offset,found=0,isLongName=0; |
|---|
| 144 | struct FatDirEntry currDirEntry; |
|---|
| 145 | char longName[255]; |
|---|
| 146 | char fatName[12]; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | if (!FatIsLong(name,nameLength)) |
|---|
| 157 | if (FatCreateShortName(vNode,fatName,(char*)name,nameLength)) |
|---|
| 158 | return -EINVAL; |
|---|
| 159 | |
|---|
| 160 | while (pos < vNode->size) |
|---|
| 161 | { |
|---|
| 162 | |
|---|
| 163 | if (FatGetDirEntry(vNode,&currDirEntry,FAT_DIR_POS(pos))) |
|---|
| 164 | break; |
|---|
| 165 | |
|---|
| 166 | FatPosToSecOffset(vNode,pos,&sec,&offset); |
|---|
| 167 | ++pos; |
|---|
| 168 | |
|---|
| 169 | if (!currDirEntry.fileName[0]) |
|---|
| 170 | break; |
|---|
| 171 | |
|---|
| 172 | if (!FAT_VALID_DE(&currDirEntry)) |
|---|
| 173 | continue; |
|---|
| 174 | |
|---|
| 175 | if (currDirEntry.attribute == ATTR_EXT) |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | if (FatGetLongName(vNode,&pos,longName,&currDirEntry)) |
|---|
| 179 | continue; |
|---|
| 180 | |
|---|
| 181 | FatPosToSecOffset(vNode,pos-1,&sec,&offset); |
|---|
| 182 | isLongName=1; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if (isLongName) |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | if (nameLength == strlen(longName) && !strnicmp(name,longName,strlen(longName))) |
|---|
| 189 | found=1; |
|---|
| 190 | |
|---|
| 191 | isLongName=0; |
|---|
| 192 | }else |
|---|
| 193 | |
|---|
| 194 | if (!strncmp((const char*)currDirEntry.fileName,fatName,11)) |
|---|
| 195 | found=1; |
|---|
| 196 | |
|---|
| 197 | if (found) |
|---|
| 198 | { |
|---|
| 199 | if (retVal) |
|---|
| 200 | memcpy(retVal,&currDirEntry,sizeof(struct FatDirEntry)); |
|---|
| 201 | |
|---|
| 202 | if (vNum) |
|---|
| 203 | *vNum=SS_TO_VNUM(sec,offset); |
|---|
| 204 | |
|---|
| 205 | if (dirPos) |
|---|
| 206 | *dirPos=--pos; |
|---|
| 207 | |
|---|
| 208 | return 0; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | return -ENOENT; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | int FatReadDir(struct File* file,void* dirEntries) |
|---|
| 229 | { |
|---|
| 230 | struct FatDirEntry currDirEntry; |
|---|
| 231 | int sec,offset; |
|---|
| 232 | char* fillName; |
|---|
| 233 | char newName[12]=""; |
|---|
| 234 | char longName[256]; |
|---|
| 235 | int isLongName=0; |
|---|
| 236 | |
|---|
| 237 | while (file->position < file->vNode->size) |
|---|
| 238 | { |
|---|
| 239 | if (FatGetDirEntry(file->vNode,&currDirEntry,FAT_DIR_POS(file->position))) |
|---|
| 240 | break; |
|---|
| 241 | |
|---|
| 242 | FatPosToSecOffset(file->vNode,file->position,&sec,&offset); |
|---|
| 243 | |
|---|
| 244 | ++file->position; |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | if (!currDirEntry.fileName[0]) |
|---|
| 248 | break; |
|---|
| 249 | |
|---|
| 250 | if (!FAT_VALID_DE(&currDirEntry)) |
|---|
| 251 | continue; |
|---|
| 252 | |
|---|
| 253 | if ((currDirEntry.attribute & ATTR_LABEL) && currDirEntry.attribute != ATTR_EXT) |
|---|
| 254 | continue; |
|---|
| 255 | |
|---|
| 256 | if (currDirEntry.attribute == ATTR_EXT) |
|---|
| 257 | { |
|---|
| 258 | if (FatGetLongName(file->vNode,&file->position,longName,&currDirEntry)) |
|---|
| 259 | continue; |
|---|
| 260 | |
|---|
| 261 | FatPosToSecOffset(file->vNode,file->position-1,&sec,&offset); |
|---|
| 262 | isLongName=1; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | if (isLongName) |
|---|
| 266 | { |
|---|
| 267 | fillName=longName; |
|---|
| 268 | isLongName=0; |
|---|
| 269 | }else{ |
|---|
| 270 | FatNameToStr(currDirEntry.fileName,newName); |
|---|
| 271 | fillName=newName; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | if (FillDir(dirEntries,fillName,strlen(fillName),SS_TO_VNUM(sec,offset))) |
|---|
| 275 | break; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | return 0; |
|---|
| 279 | } |
|---|