aboutsummaryrefslogtreecommitdiff
path: root/sdk-modifications/libsrc/fs/directory.c
blob: 124daf86e69428793c2fd5e34d76c606127a429c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/*
 directory.c
 Reading, writing and manipulation of the directory structure on
 a FAT partition

 Copyright (c) 2006 Michael "Chishm" Chisholm

 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation and/or
     other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products derived
     from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	2006-08-14 - Chishm
		* entryFromPath correctly finds "" and "." now

	2006-08-17 - Chishm
		* entryFromPath doesn't look for "" anymore - use "." to refer to the current directory

	2006-08-19 - Chishm
		* Fixed entryFromPath bug when looking for "." in root directory

	2006-10-01 - Chishm
		* Now clears the whole new cluster when linking in more clusters for a directory

	2006-10-28 - Chishm
		* stat returns the hostType for the st_dev value
*/
//version 1.1
//Note: fix bug in _FAT_directory_isValidAlias()

#include <string.h>
#include <ctype.h>

#include "directory.h"
#include "fs_common.h"
#include "partition.h"
#include "file_allocation_table.h"
#include "bit_ops.h"
#include "filetime.h"
#include "fs_unicode.h"

// Directory entry codes
#define DIR_ENTRY_LAST 0x00
#define DIR_ENTRY_FREE 0xE5


// Long file name directory entry
enum LFN_offset {
	LFN_offset_ordinal = 0x00,	// Position within LFN
	LFN_offset_char0 = 0x01,
	LFN_offset_char1 = 0x03,
	LFN_offset_char2 = 0x05,
	LFN_offset_char3 = 0x07,
	LFN_offset_char4 = 0x09,
	LFN_offset_flag = 0x0B,	// Should be equal to ATTRIB_LFN
	LFN_offset_reserved1 = 0x0C,	// Always 0x00
	LFN_offset_checkSum = 0x0D,	// Checksum of short file name (alias)
	LFN_offset_char5 = 0x0E,
	LFN_offset_char6 = 0x10,
	LFN_offset_char7 = 0x12,
	LFN_offset_char8 = 0x14,
	LFN_offset_char9 = 0x16,
	LFN_offset_char10 = 0x18,
	LFN_offset_reserved2 = 0x1A,	// Always 0x0000
	LFN_offset_char11 = 0x1C,
	LFN_offset_char12 = 0x1E
};
const int LFN_offset_table[13]={0x01,0x03,0x05,0x07,0x09,0x0E,0x10,0x12,0x14,0x16,0x18,0x1C,0x1E};

#define LFN_END 0x40
#define LFN_DEL 0x80

static char* strupr(char* a)
{
	char *str;

	str = a;
	while(*str)
	{
		if(*str >= 'a' && *str <= 'z') *str -= 0x20;
		str += 1;
	}

	return a;
}

static char* strlwr(char *a){
	char *str;

	str = a;
	while(*str)
	{
		if((*str) >= 'A' && (*str) <= 'Z') (*str) = ((*str) -'A' + 'a');
		str++;
	}
	return a;
}

bool _FAT_directory_isValidLfn (const char* name) {
	u32 i;
	u32 nameLength;
	// Make sure the name is short enough to be valid
	if ( strnlen(name, MAX_FILENAME_LENGTH) >= MAX_FILENAME_LENGTH) {
		return false;
	}
	// Make sure it doesn't contain any invalid characters
	if (strpbrk (name, "\\/:*?\"<>|") != NULL) {
		return false;
	}

	nameLength = strnlen(name, MAX_FILENAME_LENGTH);
	// Make sure the name doesn't contain any control codes
	for (i = 0; i < nameLength; i++) {
		if ((unsigned char)name[i] < 0x20) {
			return false;
		}
	}
	// Otherwise it is valid
	return true;
}
bool _FAT_directory_isValidAlias (const char* name) {
	//return false;//disables this function to preserve file name casing

	u32 i;
	u32 nameLength;
	const char* dot;

	// Make sure the name is short enough to be valid
	if ( strnlen(name, MAX_ALIAS_LENGTH) >= MAX_ALIAS_LENGTH) {
		return false;
	}
	// Make sure it doesn't contain any invalid characters
	if (strpbrk (name, "\\/:;*?\"<>|&+,=[]") != NULL) {
		return false;
	}

	//
	if (strpbrk (name, " ") != NULL) {
		return false;
	}

	nameLength = strnlen(name, MAX_ALIAS_LENGTH);
	// Make sure the name doesn't contain any control codes
	//if name isn't all capitals, then it is not a valid short name
	for (i = 0; i < nameLength; i++) {
		if (name[i] > 0x5A && name[i]!= 0x20) {
			return false;
		}
	}

	dot = strchr ( name, '.');
	// Make sure there is only one '.'
	if ((dot != NULL) && (strrchr ( name, '.') != dot)) {
		return false;
	}
	// If there is a '.':
	if (dot != NULL) {
		// Make sure the filename portion is 1-8 characters long
		if (((dot - 1 - name) > 8) || ((dot - 1 - name) < 1)) {
			return false;
		}
		// Make sure the extension is 1-3 characters long, if it exists
		if ((strnlen(dot + 1, MAX_ALIAS_LENGTH) > 3) || (strnlen(dot + 1, MAX_ALIAS_LENGTH) < 1)) {
			return false;
		}
	} else {
		// Make sure the entire file name is 1-8 characters long
		if ((nameLength > 8) || (nameLength < 1)) {
			return false;
		}
	}

	// Since we made it through all those tests, it must be valid
	return true;
}

static bool _FAT_directory_entryGetAlias (const u8* entryData, char* destName) {
	int i=0;
	int j=0;

	destName[0] = '\0';
	if (entryData[0] != DIR_ENTRY_FREE) {
		if (entryData[0] == '.') {
			destName[0] = '.';
			if (entryData[1] == '.') {
				destName[1] = '.';
				destName[2] = '\0';
			} else {
				destName[1] = '\0';
			}
		} else {
			// Copy the filename from the dirEntry to the string
			for (i = 0; (i < 8) && (entryData[DIR_ENTRY_name + i] != ' '); i++) {
				destName[i] = entryData[DIR_ENTRY_name + i];
			}
			// Copy the extension from the dirEntry to the string
			if (entryData[DIR_ENTRY_extension] != ' ') {
				destName[i++] = '.';
				for ( j = 0; (j < 3) && (entryData[DIR_ENTRY_extension + j] != ' '); j++) {
					destName[i++] = entryData[DIR_ENTRY_extension + j];
				}
			}
			destName[i] = '\0';
		}
	}

	return (destName[0] != '\0');
}

u32 _FAT_directory_entryGetCluster (const u8* entryData) {
	return u8array_to_u16(entryData,DIR_ENTRY_cluster) | (u8array_to_u16(entryData, DIR_ENTRY_clusterHigh) << 16);
}

static bool _FAT_directory_incrementDirEntryPosition (PARTITION* partition, DIR_ENTRY_POSITION* entryPosition, bool extendDirectory) {
	DIR_ENTRY_POSITION position;
	position = *entryPosition;
	u32 tempCluster;

	// Increment offset, wrapping at the end of a sector
	++ position.offset;
	if (position.offset == BYTES_PER_READ / DIR_ENTRY_DATA_SIZE) {
		position.offset = 0;
		// Increment sector when wrapping
		++ position.sector;
		// But wrap at the end of a cluster
		if ((position.sector == partition->sectorsPerCluster) && (position.cluster != FAT16_ROOT_DIR_CLUSTER)) {
			position.sector = 0;
			// Move onto the next cluster, making sure there is another cluster to go to
			tempCluster = _FAT_fat_nextCluster(partition, position.cluster);
			if (tempCluster == CLUSTER_EOF) {
				if (extendDirectory) {
					tempCluster = _FAT_fat_linkFreeClusterCleared (partition, position.cluster);
					if (tempCluster == CLUSTER_FREE) {
						return false;	// This will only happen if the disc is full
					}
				} else {
					return false;		// Got to the end of the directory, not extending it
				}
			}
			position.cluster = tempCluster;
		} else if ((position.cluster == FAT16_ROOT_DIR_CLUSTER) && (position.sector == (partition->dataStart - partition->rootDirStart))) {
			return false;	// Got to end of root directory, can't extend it
		}
	}
	*entryPosition = position;
	return true;
}

bool _FAT_directory_getNextEntry (PARTITION* partition, DIR_ENTRY* entry) {
	DIR_ENTRY_POSITION entryStart;
	DIR_ENTRY_POSITION entryEnd;

	u8 entryData[0x20];

	bool notFound, found;
	u32 maxSectors;
	int lfnPos;
	u8 lfnChkSum, chkSum;
	char* filename;
	u16	unicodeFilename[256];
	bool lfnExists;

	int i;

	lfnChkSum = 0;

	entryStart = entry->dataEnd;

	// Make sure we are using the correct root directory, in case of FAT32
	if (entryStart.cluster == FAT16_ROOT_DIR_CLUSTER) {
		entryStart.cluster = partition->rootDirCluster;
	}

	entryEnd = entryStart;
	filename = entry->d_name;
	//unicodeFilename = entry->unicodeFilename;
	memset( unicodeFilename, 0, 512 );

	// Can only be FAT16_ROOT_DIR_CLUSTER if it is the root directory on a FAT12 or FAT16 partition
	if (entryStart.cluster == FAT16_ROOT_DIR_CLUSTER) {
		maxSectors = partition->dataStart - partition->rootDirStart;
	} else {
		maxSectors = partition->sectorsPerCluster;
	}

	lfnExists = false;

	found = false;
	notFound = false;

	while (!found && !notFound) {
		if (_FAT_directory_incrementDirEntryPosition (partition, &entryEnd, false) == false) {
			notFound = true;
		}

		_FAT_cache_readPartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, entryEnd.cluster) + entryEnd.sector, entryEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);

		if (entryData[DIR_ENTRY_attributes] == ATTRIB_LFN) {
			// It's an LFN
			if (entryData[LFN_offset_ordinal] & LFN_DEL) {
				lfnExists = false;
			} else if (entryData[LFN_offset_ordinal] & LFN_END) {
				// Last part of LFN, make sure it isn't deleted using previous if(Thanks MoonLight)
				entryStart = entryEnd;	// This is the start of a directory entry
				lfnExists = true;
				//filename[(entryData[LFN_offset_ordinal] & ~LFN_END) * 13] = '\0';	// Set end of lfn to null character
				unicodeFilename[(entryData[LFN_offset_ordinal] & ~LFN_END) * 13] = 0x0000;
				lfnChkSum = entryData[LFN_offset_checkSum];
			} if (lfnChkSum != entryData[LFN_offset_checkSum]) {
				lfnExists = false;
			}
			//unicodeFilename[0] = 0x0000;
			if (lfnExists) {
				lfnPos = ((entryData[LFN_offset_ordinal] & ~LFN_END) - 1) * 13;
				for (i = 0; i < 13; i++) {
					unicodeFilename[lfnPos + i] = u8array_to_u16( entryData, LFN_offset_table[i]);
					//	| entryData[LFN_offset_table[i]+1]<<8; // modify this for unicode support;
				}
			}
		} else if (entryData[DIR_ENTRY_attributes] & ATTRIB_VOL) {
			// This is a volume name, don't bother with it
		} else if (entryData[0] == DIR_ENTRY_LAST) {
			notFound = true;
		} else if ((entryData[0] != DIR_ENTRY_FREE) && (entryData[0] > 0x20) && !(entryData[DIR_ENTRY_attributes] & ATTRIB_VOL)) {
			if (lfnExists) {
				// Calculate file checksum
				chkSum = 0;
				for (i=0; i < 11; i++) {
					// NOTE: The operation is an unsigned char rotate right
					chkSum = ((chkSum & 1) ? 0x80 : 0) + (chkSum >> 1) + entryData[i];
				}
				if (chkSum != lfnChkSum) {
					lfnExists = false;
					//filename[0] = '\0';
					//unicodeFilename[0] = 0x0000; // move this line to below(1)
				}
			}
			//short name
			if (!lfnExists) {
				entryStart = entryEnd;
				unicodeFilename[0] = 0x0000; // (1)make sure clear previous search junk
				// get alias anyway
				_FAT_directory_entryGetAlias (entryData, filename);
				strlwr(filename);//convert to lowercase characters
			}
			//long name
			else
			{
				_FAT_unicode16_to_utf8 (unicodeFilename, filename);
			}

			found = true;
		}
	}

	// If no file is found, return false
	if (notFound) {
		return false;
	} else {
		// Fill in the directory entry struct
		entry->dataStart = entryStart;
		entry->dataEnd = entryEnd;
		memcpy (entry->entryData, entryData, DIR_ENTRY_DATA_SIZE);
		return true;
	}
}

bool _FAT_directory_getFirstEntry (PARTITION* partition, DIR_ENTRY* entry, u32 dirCluster) {
	entry->dataStart.cluster = dirCluster;
	entry->dataStart.sector = 0;
	entry->dataStart.offset = -1; // Start before the beginning of the directory

	entry->dataEnd = entry->dataStart;

	return _FAT_directory_getNextEntry (partition, entry);
}

bool _FAT_directory_getRootEntry (PARTITION* partition, DIR_ENTRY* entry) {
	entry->dataStart.cluster = 0;
	entry->dataStart.sector = 0;
	entry->dataStart.offset = 0;

	entry->dataEnd = entry->dataStart;

	memset (entry->d_name, '\0', MAX_FILENAME_LENGTH);
	entry->d_name[0] = '.';

	memset (entry->entryData, 0, DIR_ENTRY_DATA_SIZE);
	memset (entry->entryData, ' ', 11);
	entry->entryData[0] = '.';

	entry->entryData[DIR_ENTRY_attributes] = ATTRIB_DIR;

	u16_to_u8array (entry->entryData, DIR_ENTRY_cluster, partition->rootDirCluster);
	u16_to_u8array (entry->entryData, DIR_ENTRY_clusterHigh, partition->rootDirCluster >> 16);

	return true;
}

bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) {
	DIR_ENTRY_POSITION entryStart;
	DIR_ENTRY_POSITION entryEnd;
	entryStart = entry->dataStart;
	entryEnd = entry->dataEnd;
	bool entryStillValid;
	bool finished;

	int i;
	int lfnPos;

	u8 entryData[DIR_ENTRY_DATA_SIZE];

	memset (entry->d_name, '\0', MAX_FILENAME_LENGTH);

	// Create an empty directory entry to overwrite the old ones with
	for ( entryStillValid = true, finished = false;
		entryStillValid && !finished;
		entryStillValid = _FAT_directory_incrementDirEntryPosition (partition, &entryStart, false))
	{
		_FAT_cache_readPartialSector (partition->cache, entryData,
			_FAT_fat_clusterToSector(partition, entryStart.cluster) + entryStart.sector,
			entryStart.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);

		if ((entryStart.cluster == entryEnd.cluster)
			&& (entryStart.sector == entryEnd.sector)
			&& (entryStart.offset == entryEnd.offset)) {
			// Copy the entry data and stop, since this is the last section of the directory entry
			memcpy (entry->entryData, entryData, DIR_ENTRY_DATA_SIZE);
			finished = true;
		} else {
			// Copy the long file name data
			lfnPos = ((entryData[LFN_offset_ordinal] & ~LFN_END) - 1) * 13;
			for (i = 0; i < 13; i++) {
				entry->d_name[lfnPos + i] = entryData[LFN_offset_table[i]]; // modify this for unicode support;
			}
		}
	}

	if (!entryStillValid) {
		return false;
	}

	if ((entryStart.cluster == entryEnd.cluster)
		&& (entryStart.sector == entryEnd.sector)
		&& (entryStart.offset == entryEnd.offset)) {
		// Since the entry doesn't have a long file name, extract the short filename
		if (!_FAT_directory_entryGetAlias (entry->entryData, entry->d_name)) {
			return false;
		}
	}

	return true;
}

bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const char* path, const char* pathEnd) {
	size_t dirnameLength;
	const char* pathPosition;
	const char* nextPathPosition;

	//size_t uniDirnameLength;
	//u16 uniPath[MAX_FILENAME_LENGTH];
	//const u16 * uniPathPosition;
	//const u16 * uniNextPathPosition;

	u32 dirCluster;
	bool foundFile;

	//char alias[MAX_ALIAS_LENGTH];

	bool found, notFound;

	//_FAT_utf8_to_unicode16( path, uniPath );
	pathPosition = path;
	//uniPathPosition = uniPath;

	found = false;
	notFound = false;

	if (pathEnd == NULL) {
		// Set pathEnd to the end of the path string
		pathEnd = strchr (path, '\0');
	}

	if (pathPosition[0] == DIR_SEPARATOR) {
		// Start at root directory
		dirCluster = partition->rootDirCluster;
		// Consume separator(s)
		while (pathPosition[0] == DIR_SEPARATOR) {
			pathPosition++;
		}

		//while (uniPathPosition[0] == (unsigned short)DIR_SEPARATOR) {
		//	uniPathPosition++;
		//}

		if (pathPosition >= pathEnd)  {
			_FAT_directory_getRootEntry (partition, entry);
			found = true;
		}
	} else {
		// Start in current working directory
		dirCluster = partition->cwdCluster;
	}

	// If the path is only specifying a directory in the form "."
	// and this is the root directory, return it
	//if ((dirCluster == partition->rootDirCluster) && (strncasecmp(".", pathPosition, 2) == 0)) {
	if ((dirCluster == partition->rootDirCluster) && strlen(pathPosition) == 1 && (strcasecmp(".", pathPosition) == 0)) {
		_FAT_directory_getRootEntry (partition, entry);
		found = true;
	}

	while (!found && !notFound) {
		// Get the name of the next required subdirectory within the path
		nextPathPosition = strchr (pathPosition, DIR_SEPARATOR);
		//uniNextPathPosition = _unistrchr( uniPathPosition, (unsigned short)DIR_SEPARATOR );
		if (nextPathPosition != NULL) {
			dirnameLength = nextPathPosition - pathPosition;
		} else {
			dirnameLength = strlen(pathPosition);
		}
		//if (uniNextPathPosition != 0x0000) {
		//	uniDirnameLength = uniNextPathPosition - uniPathPosition;
		//} else {
		//	uniDirnameLength = _unistrnlen(uniPathPosition, MAX_FILENAME_LENGTH);
		//}

		if (dirnameLength > MAX_FILENAME_LENGTH) {
			// The path is too long to bother with
			return false;
		}
		//if( uniDirnameLength > MAX_FILENAME_LENGTH ) {
		//	return false;
		//}

		// Look for the directory within the path
		foundFile = _FAT_directory_getFirstEntry (partition, entry, dirCluster);

		while (foundFile && !found && !notFound) {			// It hasn't already found the file
			// Check if the filename matches
			//if ((uniDirnameLength == _unistrnlen(entry->unicodeFilename, MAX_FILENAME_LENGTH))
			//	&& (_unistrncmp(entry->unicodeFilename, uniPathPosition, uniDirnameLength) == 0)) {
			//		found = true;
			//}

			//if(!strncasecmp(entry->d_name, pathPosition, dirnameLength))
			if(strlen(entry->d_name) == dirnameLength  && !strncasecmp(entry->d_name, pathPosition, dirnameLength))
				found = true;

			// Check if the alias matches
			//_FAT_directory_entryGetAlias (entry->entryData, alias);
			//if ((dirnameLength == strnlen(alias, MAX_ALIAS_LENGTH))
			//	&& (strncasecmp(alias, pathPosition, dirnameLength) == 0)) {
			//		found = true;
			//}

			if (found && !(entry->entryData[DIR_ENTRY_attributes] & ATTRIB_DIR) && (nextPathPosition != NULL)) {
				// Make sure that we aren't trying to follow a file instead of a directory in the path
				found = false;
			}

			if (!found) {
				foundFile = _FAT_directory_getNextEntry (partition, entry);
			}
		}

		if (!foundFile) {
			// Check that the search didn't get to the end of the directory
			notFound = true;
			found = false;
		} else if ((nextPathPosition == NULL) || (nextPathPosition >= pathEnd)) {
			// Check that we reached the end of the path
			found = true;
		} else if (entry->entryData[DIR_ENTRY_attributes] & ATTRIB_DIR) {
			dirCluster = _FAT_directory_entryGetCluster (entry->entryData);
			pathPosition = nextPathPosition;
			//uniPathPosition = uniNextPathPosition;
			// Consume separator(s)
			while (pathPosition[0] == DIR_SEPARATOR) {
				pathPosition++;
			}
			//while (uniPathPosition[0] == (unsigned short)DIR_SEPARATOR) {
			//	uniPathPosition++;
			//}
			// The requested directory was found
			if (pathPosition >= pathEnd)  {
				found = true;
			} else {
				found = false;
			}
		}
	}

	if (found && !notFound) {
		return true;
	} else {
		return false;
	}
}

bool _FAT_directory_removeEntry (PARTITION* partition, DIR_ENTRY* entry) {
	DIR_ENTRY_POSITION entryStart;
	DIR_ENTRY_POSITION entryEnd;
	entryStart = entry->dataStart;
	entryEnd = entry->dataEnd;
	bool entryStillValid;
	bool finished;

	u8 entryData[DIR_ENTRY_DATA_SIZE];

	// Create an empty directory entry to overwrite the old ones with
	for ( entryStillValid = true, finished = false;
		entryStillValid && !finished;
		entryStillValid = _FAT_directory_incrementDirEntryPosition (partition, &entryStart, false))
	{
		_FAT_cache_readPartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, entryStart.cluster) + entryStart.sector, entryStart.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
		entryData[0] = DIR_ENTRY_FREE;
		_FAT_cache_writePartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, entryStart.cluster) + entryStart.sector, entryStart.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
		if ((entryStart.cluster == entryEnd.cluster) && (entryStart.sector == entryEnd.sector) && (entryStart.offset == entryEnd.offset)) {
			finished = true;
		}
	}

	if (!entryStillValid) {
		return false;
	}

	return true;
}

static bool _FAT_directory_findEntryGap (PARTITION* partition, DIR_ENTRY* entry, u32 dirCluster, u32 size) {
	DIR_ENTRY_POSITION gapStart;
	DIR_ENTRY_POSITION gapEnd;

	u8 entryData[DIR_ENTRY_DATA_SIZE];

	u32 dirEntryRemain;

	bool endOfDirectory, entryStillValid;

	// Scan Dir for free entry
	gapEnd.offset = 0;
	gapEnd.sector = 0;
	gapEnd.cluster = dirCluster;

	gapStart = gapEnd;

	entryStillValid = true;
	dirEntryRemain = size;
	endOfDirectory = false;

	while (entryStillValid && !endOfDirectory && (dirEntryRemain > 0)) {
		_FAT_cache_readPartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, gapEnd.cluster) + gapEnd.sector, gapEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
		if (entryData[0] == DIR_ENTRY_LAST) {
			gapStart = gapEnd;
			-- dirEntryRemain;
			endOfDirectory = true;
		} else if (entryData[0] == DIR_ENTRY_FREE) {
			if (dirEntryRemain == size) {
				gapStart = gapEnd;
			}
			-- dirEntryRemain;
		} else {
			dirEntryRemain = size;
		}

		if (!endOfDirectory && (dirEntryRemain > 0)) {
			entryStillValid = _FAT_directory_incrementDirEntryPosition (partition, &gapEnd, true);
		}
	}

	// Make sure the scanning didn't fail
	if (!entryStillValid) {
		return false;
	}

	// Save the start entry, since we know it is valid
	entry->dataStart = gapStart;

	if (endOfDirectory) {
		memset (entryData, DIR_ENTRY_LAST, DIR_ENTRY_DATA_SIZE);
		dirEntryRemain += 1;	// Increase by one to take account of End Of Directory Marker
		while ((dirEntryRemain > 0) && entryStillValid) {
			// Get the gapEnd before incrementing it, so the second to last one is saved
			entry->dataEnd = gapEnd;
			// Increment gapEnd, moving onto the next entry
			entryStillValid = _FAT_directory_incrementDirEntryPosition (partition, &gapEnd, true);
			-- dirEntryRemain;
			// Fill the entry with blanks
			_FAT_cache_writePartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, gapEnd.cluster) + gapEnd.sector, gapEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
		}
		if (!entryStillValid) {
			return false;
		}
	} else {
		entry->dataEnd = gapEnd;
	}

	return true;
}

static bool _FAT_directory_entryExists (PARTITION* partition, const char* name, u32 dirCluster) {
	DIR_ENTRY tempEntry;
	bool foundFile;
	//char alias[MAX_ALIAS_LENGTH];
	u32 dirnameLength;
	//u16 unicodeName[MAX_FILENAME_LENGTH];

	dirnameLength = strnlen(name, MAX_FILENAME_LENGTH);

	if (dirnameLength >= MAX_FILENAME_LENGTH) {
		return false;
	}

	//_FAT_utf8_to_unicode16( name, unicodeName );

	// Make sure the entry doesn't already exist
	foundFile = _FAT_directory_getFirstEntry (partition, &tempEntry, dirCluster);

	while (foundFile) {			// It hasn't already found the file
		// Check if the filename matches
		//if (/*(dirnameLength == _unistrnlen(tempEntry.unicodeFilename, MAX_FILENAME_LENGTH))*/
		//	//&& (strcasecmp(tempEntry.filename, name) == 0)) {
		//	/*&&*/ (_unistrncmp( unicodeName, tempEntry.unicodeFilename, MAX_FILENAME_LENGTH ) == 0 ))
		//{
		//		return true;
		//}
		//if(!strncasecmp(name, tempEntry.d_name, dirnameLength))
		if(!strcasecmp(name, tempEntry.d_name))
			return true;

		// Check if the alias matches
		//_FAT_directory_entryGetAlias (tempEntry.entryData, alias);
		//if ((dirnameLength == strnlen(alias, MAX_ALIAS_LENGTH))
		//	&& (strcasecmp(alias, name) == 0)) {
		//		return true;
		//}
		foundFile = _FAT_directory_getNextEntry (partition, &tempEntry);
	}
	return false;
}

//a fix for checking if a short file name is already in use.
static bool _FAT_directory_entryExistsSFN (PARTITION* partition, const char* name, u32 dirCluster) {
	DIR_ENTRY tempEntry;
	bool foundFile;
	char alias[MAX_ALIAS_LENGTH];
	u32 dirnameLength;

	dirnameLength = strnlen(name, MAX_FILENAME_LENGTH);

	if (dirnameLength >= MAX_FILENAME_LENGTH) {
		return false;
	}


	// Make sure the entry doesn't already exist
	foundFile = _FAT_directory_getFirstEntry (partition, &tempEntry, dirCluster);

	while (foundFile) {			// It hasn't already found the file
		if(!strcasecmp(name, tempEntry.d_name))
			return true;

		// Check if the alias matches
		_FAT_directory_entryGetAlias (tempEntry.entryData, alias);
		if ((dirnameLength == strnlen(alias, MAX_ALIAS_LENGTH))
			&& (strcasecmp(alias, name) == 0)) {
				return true;
		}
		foundFile = _FAT_directory_getNextEntry (partition, &tempEntry);
	}
	return false;
}

bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, u32 dirCluster) {
	u32 entrySize;
	u8 lfnEntry[DIR_ENTRY_DATA_SIZE];
	s32 i,j; // Must be signed for use when decrementing in for loop
	char *tmpCharPtr;
	DIR_ENTRY_POSITION curEntryPos;
	bool entryStillValid;
	u8 aliasCheckSum = 0;
	char alias [MAX_ALIAS_LENGTH];
	u16 unicodeFilename[256];

	// Make sure the filename is not 0 length
	if (strnlen (entry->d_name, MAX_FILENAME_LENGTH) < 1) {
		return false;
	}
	// Make sure the filename is at least a valid LFN
	if ( !(_FAT_directory_isValidLfn (entry->d_name))) {
		return false;
	}

	// Remove trailing spaces
	for (i = strlen (entry->d_name) - 1; (i > 0) && (entry->d_name[i] == ' '); --i) {
		entry->d_name[i] = '\0';
	}
	// Remove leading spaces
	for (i = 0; (i < strlen (entry->d_name)) && (entry->d_name[i] == ' '); ++i) ;
	if (i > 0) {
		memmove (entry->d_name, entry->d_name + i, strlen (entry->d_name + i));
	}

	// Remove junk in filename
	i = strlen (entry->d_name);
	memset (entry->d_name + i, '\0', MAX_FILENAME_LENGTH - i);

	// Make sure the entry doesn't already exist
	if (_FAT_directory_entryExists (partition, entry->d_name, dirCluster)) {
		return false;
	}

	// Clear out alias, so we can generate a new one
	memset (entry->entryData, ' ', 11);

	if ( strncmp(entry->d_name, ".", MAX_FILENAME_LENGTH) == 0) {
		// "." entry
		entry->entryData[0] = '.';
		entrySize = 1;
	} else if ( strncmp(entry->d_name, "..", MAX_FILENAME_LENGTH) == 0) {
		// ".." entry
		entry->entryData[0] = '.';
		entry->entryData[1] = '.';
		entrySize = 1;
	}else if (_FAT_directory_isValidAlias (entry->d_name)) {
		entrySize = 1;
		// Copy into alias
		for (i = 0, j = 0; (j < 8) && (entry->d_name[i] != '.') && (entry->d_name[i] != '\0'); i++, j++) {
			entry->entryData[j] = entry->d_name[i];
		}
		while (j < 8) {
			entry->entryData[j] = ' ';
			++ j;
		}
		if (entry->d_name[i] == '.') {
			// Copy extension
			++ i;
			while ((entry->d_name[i] != '\0') && (j < 11)) {
				entry->entryData[j] = entry->d_name[i];
				++ i;
				++ j;
			}
		}
		while (j < 11) {
			entry->entryData[j] = ' ';
			++ j;
		}
		// Short filename
		strupr (entry->entryData);
	}else {
		// Long filename needed
		//memset( entry->unicodeFilename, 0, 512 );
		//_FAT_utf8_to_unicode16( (const char*)entry->d_name, entry->unicodeFilename );
		memset( unicodeFilename, 0, 512 );
		_FAT_utf8_to_unicode16( (const char*)entry->d_name, unicodeFilename );

		//entrySize = ((strnlen (entry->filename, MAX_FILENAME_LENGTH) + LFN_ENTRY_LENGTH - 1) / LFN_ENTRY_LENGTH) + 1;
		//entrySize = ((_unistrnlen( entry->unicodeFilename, MAX_FILENAME_LENGTH ) + LFN_ENTRY_LENGTH - 1) / LFN_ENTRY_LENGTH) + 1;
		entrySize = ((_unistrnlen( unicodeFilename, MAX_FILENAME_LENGTH ) + LFN_ENTRY_LENGTH - 1) / LFN_ENTRY_LENGTH) + 1;

		// Generate alias
		tmpCharPtr = strrchr (entry->d_name, '.');
		if (tmpCharPtr == NULL) {
			tmpCharPtr = strrchr (entry->d_name, '\0');
		}
		for (i = 0, j = 0; (j < 6) && (entry->d_name + i < tmpCharPtr); i++) {
			if ( _uniisalnum((u8)(entry->d_name[i]))) {
				alias[j] = entry->d_name[i];
				++ j;
			}
		}
		while (j < 8) {
			alias[j] = '_';
			++ j;
		}
		tmpCharPtr = strrchr (entry->d_name, '.');
		if (tmpCharPtr != NULL) {
			alias[8] = '.';
			// Copy extension
			while ((tmpCharPtr != '\0') && (j < 12)) {
				alias[j] = tmpCharPtr[0];
				++ tmpCharPtr;
				++ j;
			}
			alias[j] = '\0';
		} else {
			for (j = 8; j < MAX_ALIAS_LENGTH; j++) {
				alias[j] = '\0';
			}
		}

		// Get a valid tail number
		alias[5] = '~';
		i = 0;
		do {
			i++;
			alias[6] = '0' + ((i / 10) % 10);	// 10's digit
			alias[7] = '0' + (i % 10);	// 1's digit
		} while (_FAT_directory_entryExistsSFN (partition, alias, dirCluster) && (i < 100));
		if (i == 100) {
			// Couldn't get a tail number
			return false;
		}

		// Make it upper case
		strupr (alias);

		// Now copy it into the directory entry data
		memcpy (entry->entryData, alias, 8);
		memcpy (entry->entryData + 8, alias + 9, 3);
		for (i = 0; i < 10; i++) {
			if (entry->entryData[i] < 0x20) {
				// Replace null and control characters with spaces
				entry->entryData[i] = 0x20;
			}
		}
		// Generate alias checksum
		for (i=0; i < 11; i++)
		{
			// NOTE: The operation is an unsigned char rotate right
			aliasCheckSum = ((aliasCheckSum & 1) ? 0x80 : 0) + (aliasCheckSum >> 1) + entry->entryData[i];
		}

	}

	// Find or create space for the entry
	if (_FAT_directory_findEntryGap (partition, entry, dirCluster, entrySize) == false) {
		return false;
	}

	// Write out directory entry
	curEntryPos = entry->dataStart;

	for (entryStillValid = true, i = entrySize; entryStillValid && i > 0;
		entryStillValid = _FAT_directory_incrementDirEntryPosition (partition, &curEntryPos, false), -- i )
	{
		if (i > 1) {
			// Long filename entry
			lfnEntry[LFN_offset_ordinal] = (i - 1) | (i == entrySize ? LFN_END : 0);
			for (j = 0; j < 13; j++) {
				//if (entry->unicodeFilename[(i - 2) * 13 + j] == '\0') {
				if (unicodeFilename[(i - 2) * 13 + j] == '\0') {
					//if ((j > 1) && (entry->unicodeFilename[(i - 2) * 13 + (j-1)] == '\0')) {
					if ((j > 1) && (unicodeFilename[(i - 2) * 13 + (j-1)] == '\0')) {
						u16_to_u8array (lfnEntry, LFN_offset_table[j], 0xffff);		// Padding
					} else {
						u16_to_u8array (lfnEntry, LFN_offset_table[j], 0x0000);		// Terminating null character
					}
				} else {
					//u16_to_u8array (lfnEntry, LFN_offset_table[j], entry->unicodeFilename[(i - 2) * 13 + j]);
					u16_to_u8array (lfnEntry, LFN_offset_table[j], unicodeFilename[(i - 2) * 13 + j]);
				}
			}

			lfnEntry[LFN_offset_checkSum] = aliasCheckSum;
			lfnEntry[LFN_offset_flag] = ATTRIB_LFN;
			lfnEntry[LFN_offset_reserved1] = 0;
			u16_to_u8array (lfnEntry, LFN_offset_reserved2, 0);
			_FAT_cache_writePartialSector (partition->cache, lfnEntry, _FAT_fat_clusterToSector(partition, curEntryPos.cluster) + curEntryPos.sector, curEntryPos.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
		} else {
			// Alias & file data
			_FAT_cache_writePartialSector (partition->cache, entry->entryData, _FAT_fat_clusterToSector(partition, curEntryPos.cluster) + curEntryPos.sector, curEntryPos.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
		}
	}

	return true;
}

bool _FAT_directory_chdir (PARTITION* partition, const char* path) {
	DIR_ENTRY entry;

	if (!_FAT_directory_entryFromPath (partition, &entry, path, NULL)) {
		return false;
	}

	if (!(entry.entryData[DIR_ENTRY_attributes] & ATTRIB_DIR)) {
		return false;
	}

	partition->cwdCluster = _FAT_directory_entryGetCluster (entry.entryData);

	return true;
}

void _FAT_directory_entryStat (PARTITION* partition, DIR_ENTRY* entry, struct stat *st) {
	// Fill in the stat struct
	// Some of the values are faked for the sake of compatibility
	st->st_dev = _FAT_disc_hostType(partition->disc);					// The device is the 32bit ioType value
  	st->st_ino = (ino_t)(_FAT_directory_entryGetCluster(entry->entryData));		// The file serial number is the start cluster
	st->st_mode = (_FAT_directory_isDirectory(entry) ? S_IFDIR : S_IFREG) |
		(S_IRUSR | S_IRGRP | S_IROTH) |
		(_FAT_directory_isWritable (entry) ? (S_IWUSR | S_IWGRP | S_IWOTH) : 0) |
		(_FAT_directory_isHidden (entry) ? S_IHIDDEN : 0);		// Mode bits based on dirEntry ATTRIB byte
	st->st_nlink = 1;								// Always one hard link on a FAT file
	st->st_uid = 1;									// Faked for FAT
	st->st_gid = 2;									// Faked for FAT
	st->st_rdev = st->st_dev;
	st->st_size = u8array_to_u32 (entry->entryData, DIR_ENTRY_fileSize);		// File size
	st->st_atime = _FAT_filetime_to_time_t (
		0,
		u8array_to_u16 (entry->entryData, DIR_ENTRY_aDate)
	);
	//st->st_spare1 = _FAT_directory_isHidden (entry);
	st->st_mtime = _FAT_filetime_to_time_t (
		u8array_to_u16 (entry->entryData, DIR_ENTRY_mTime),
		u8array_to_u16 (entry->entryData, DIR_ENTRY_mDate)
	);
	//st->st_spare2 = 0;
	st->st_ctime = _FAT_filetime_to_time_t (
		u8array_to_u16 (entry->entryData, DIR_ENTRY_cTime),
		u8array_to_u16 (entry->entryData, DIR_ENTRY_cDate)
	);
	//st->st_spare3 = 0;
	st->st_blksize = BYTES_PER_READ;				// Prefered file I/O block size
	st->st_blocks = (st->st_size + BYTES_PER_READ - 1) / BYTES_PER_READ;	// File size in blocks
//	st->st_spare4[0] = 0;
//	st->st_spare4[1] = 0;
}