aboutsummaryrefslogtreecommitdiff
path: root/sdk-modifications/libsrc/fs/fatdir.c
blob: dc9b914acea92388213248be4042c9c7209ba506 (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
/*
 fatdir.c

 Functions used by the newlib disc stubs to interface with
 this library

 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-13 - Chishm
		* Moved all externally visible directory related functions to fatdir
		* Added _FAT_mkdir_r

	2006-08-14 - Chishm
		* Added directory iterator functions

	2006-08-19 - Chishm
		* Updated dirnext return values to return correctly

	2006-10-01 - Chishm
		* Now clears the whole cluster when creating a new directory, bug found by Hermes

	2007-01-10 - Chishm
		* Updated directory iterator functions for DevkitPro r20
*/

/*
	2010.06.30
		* Modify _FAT_diropen_r,
*/

#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/dir.h>

#include "fatdir.h"

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


int _FAT_stat_r (struct _reent *r, const char *path, struct stat *st) {
	PARTITION* partition = NULL;

	DIR_ENTRY dirEntry;

	// Get the partition this file is on
	partition = _FAT_partition_getPartitionFromPath (path);

	if (partition == NULL) {
		r->_errno = ENODEV;
		return -1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (path, ':') != NULL) {
		path = strchr (path, ':') + 1;
	}
	if (strchr (path, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}

	// Search for the file on the disc
	if (!_FAT_directory_entryFromPath (partition, &dirEntry, path, NULL)) {
		r->_errno = ENOENT;
		return -1;
	}

	// Fill in the stat struct
	_FAT_directory_entryStat (partition, &dirEntry, st);

	return 0;
}

int _FAT_getshortname_r (struct _reent *r, const char *name, char *outName) {
	PARTITION* partition = NULL;
	DIR_ENTRY DirEntry;
	u32 dirCluster;

	// Get the partition this directory is on
	partition = _FAT_partition_getPartitionFromPath (name);

	if (partition == NULL) {
		r->_errno = ENODEV;
		return 1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (name, ':') != NULL) {
		name = strchr (name, ':') + 1;
	}
	if (strchr (name, ':') != NULL) {
		r->_errno = EINVAL;
		return 1;
	}

	// Search for the file on the disc
	if (!_FAT_directory_entryFromPath (partition, &DirEntry, name, NULL)) {
		r->_errno = ENOENT;
		return 1;
	}

	strcpy(outName, DirEntry.entryData);
	return 1;
}

int _FAT_link_r (struct _reent *r, const char *existing, const char *newLink) {
	r->_errno = ENOTSUP;
	return -1;
}

int _FAT_unlink_r (struct _reent *r, const char *path) {
	PARTITION* partition = NULL;
	DIR_ENTRY dirEntry;
	DIR_ENTRY dirContents;
	u32 cluster;
	bool nextEntry;
	bool errorOccured = false;

	// Get the partition this directory is on
	partition = _FAT_partition_getPartitionFromPath (path);

	if (partition == NULL) {
		r->_errno = ENODEV;
		return -1;
	}

	// Make sure we aren't trying to write to a read-only disc
	if (partition->readOnly) {
		r->_errno = EROFS;
		return -1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (path, ':') != NULL) {
		path = strchr (path, ':') + 1;
	}
	if (strchr (path, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}

	// Search for the file on the disc
	if (!_FAT_directory_entryFromPath (partition, &dirEntry, path, NULL)) {
		r->_errno = ENOENT;
		return -1;
	}

	cluster = _FAT_directory_entryGetCluster (dirEntry.entryData);


	// If this is a directory, make sure it is empty
	if (_FAT_directory_isDirectory (&dirEntry)) {
		nextEntry = _FAT_directory_getFirstEntry (partition, &dirContents, cluster);

		while (nextEntry) {
			if (!_FAT_directory_isDot (&dirContents)) {
				// The directory had something in it that isn't a reference to itself or it's parent
				r->_errno = EPERM;
				return -1;
			}
			nextEntry = _FAT_directory_getNextEntry (partition, &dirContents);
		}
	}

	if (cluster != CLUSTER_FREE) {
		// Remove the cluster chain for this file
		if (!_FAT_fat_clearLinks (partition, cluster)) {
			r->_errno = EIO;
			errorOccured = true;
		}
	}

	// Remove the directory entry for this file
	if (!_FAT_directory_removeEntry (partition, &dirEntry)) {
		r->_errno = EIO;
		errorOccured = true;
	}

	// Flush any sectors in the disc cache
	if (!_FAT_cache_flush(partition->cache)) {
		r->_errno = EIO;
		errorOccured = true;
	}

	if (errorOccured) {
		return -1;
	} else {
		return 0;
	}
}

int _FAT_chdir_r (struct _reent *r, const char *path) {
	PARTITION* partition = NULL;

	// Get the partition this directory is on
	partition = _FAT_partition_getPartitionFromPath (path);

	if (partition == NULL) {
		r->_errno = ENODEV;
		return -1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (path, ':') != NULL) {
		path = strchr (path, ':') + 1;
	}
	if (strchr (path, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}

	// Set the default device to match this one
	if (!_FAT_partition_setDefaultPartition (partition)) {
		r->_errno = ENOENT;
		return -1;
	}

	// Try changing directory
	if (_FAT_directory_chdir (partition, path)) {
		// Successful
		return 0;
	} else {
		// Failed
		r->_errno = ENOTDIR;
		return -1;
	}
}

int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName) {
	PARTITION* partition = NULL;
	DIR_ENTRY oldDirEntry;
	DIR_ENTRY newDirEntry;
	const char *pathEnd;
	u32 dirCluster;

	// Get the partition this directory is on
	partition = _FAT_partition_getPartitionFromPath (oldName);

	if (partition == NULL) {
		r->_errno = ENODEV;
		return -1;
	}

	// Make sure the same partition is used for the old and new names
	if (partition != _FAT_partition_getPartitionFromPath (newName)) {
		r->_errno = EXDEV;
		return -1;
	}

	// Make sure we aren't trying to write to a read-only disc
	if (partition->readOnly) {
		r->_errno = EROFS;
		return -1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (oldName, ':') != NULL) {
		oldName = strchr (oldName, ':') + 1;
	}
	if (strchr (oldName, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}
	if (strchr (newName, ':') != NULL) {
		newName = strchr (newName, ':') + 1;
	}
	if (strchr (newName, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}

	// Search for the file on the disc
	if (!_FAT_directory_entryFromPath (partition, &oldDirEntry, oldName, NULL)) {
		r->_errno = ENOENT;
		return -1;
	}

	// Make sure there is no existing file / directory with the new name
	if (_FAT_directory_entryFromPath (partition, &newDirEntry, newName, NULL)) {
		r->_errno = EEXIST;
		return -1;
	}

	// Create the new file entry
	// Get the directory it has to go in
	pathEnd = strrchr (newName, DIR_SEPARATOR);
	if (pathEnd == NULL) {
		// No path was specified
		dirCluster = partition->cwdCluster;
		pathEnd = newName;
	} else {
		// Path was specified -- get the right dirCluster
		// Recycling newDirEntry, since it needs to be recreated anyway
		if (!_FAT_directory_entryFromPath (partition, &newDirEntry, newName, pathEnd) ||
			!_FAT_directory_isDirectory(&newDirEntry)) {
			r->_errno = ENOTDIR;
			return -1;
		}
		dirCluster = _FAT_directory_entryGetCluster (newDirEntry.entryData);
		// Move the pathEnd past the last DIR_SEPARATOR
		pathEnd += 1;
	}

	// Copy the entry data
	memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY));

	// Set the new name
	strncpy (newDirEntry.d_name, pathEnd, MAX_FILENAME_LENGTH - 1);

	// Write the new entry
	if (!_FAT_directory_addEntry (partition, &newDirEntry, dirCluster)) {
		r->_errno = ENOSPC;
		return -1;
	}

	// Remove the old entry
	if (!_FAT_directory_removeEntry (partition, &oldDirEntry)) {
		r->_errno = EIO;
		return -1;
	}

	// Flush any sectors in the disc cache
	if (!_FAT_cache_flush (partition->cache)) {
		r->_errno = EIO;
		return -1;
	}

	return 0;
}

int _FAT_hideAttrib_r (struct _reent *r, const char *name, u8 hide) {
	PARTITION* partition = NULL;
	DIR_ENTRY oldDirEntry;
	DIR_ENTRY newDirEntry;
	u32 dirCluster;

	// Get the partition this directory is on
	partition = _FAT_partition_getPartitionFromPath (name);

	if (partition == NULL) {
		r->_errno = ENODEV;
		return -1;
	}

	// Make sure we aren't trying to write to a read-only disc
	if (partition->readOnly) {
		r->_errno = EROFS;
		return -1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (name, ':') != NULL) {
		name = strchr (name, ':') + 1;
	}
	if (strchr (name, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}

	// Search for the file on the disc
	if (!_FAT_directory_entryFromPath (partition, &oldDirEntry, name, NULL)) {
		r->_errno = ENOENT;
		return -1;
	}

	// Create the new file entry
	// Get the directory it has to go in
	memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY));

	// No path was specified
	dirCluster = partition->cwdCluster;

	// Remove the old entry hopefully doesn't screw things up...
	if (!_FAT_directory_removeEntry (partition, &oldDirEntry)) {
		r->_errno = EIO;
		return -1;
	}

	// Set the new attribute
	if(hide)
		newDirEntry.entryData[DIR_ENTRY_attributes] |= ATTRIB_HID;
	else
		newDirEntry.entryData[DIR_ENTRY_attributes] &= ~ATTRIB_HID;

	// Write the new entry
	if (!_FAT_directory_addEntry (partition, &newDirEntry, dirCluster)) {
		r->_errno = ENOSPC;
		return -1;
	}

	// Flush any sectors in the disc cache
	if (!_FAT_cache_flush (partition->cache)) {
		r->_errno = EIO;
		return -1;
	}

	return 0;
}

int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
	PARTITION* partition = NULL;
	bool fileExists;
	DIR_ENTRY dirEntry;
	const char* pathEnd;
	u32 parentCluster, dirCluster;
	u8 newEntryData[DIR_ENTRY_DATA_SIZE];

	partition = _FAT_partition_getPartitionFromPath (path);
	if (partition == NULL) {
		r->_errno = ENODEV;
		return -1;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (path, ':') != NULL) {
		path = strchr (path, ':') + 1;
	}
	if (strchr (path, ':') != NULL) {
		r->_errno = EINVAL;
		return -1;
	}

	// Search for the file/directory on the disc
	fileExists = _FAT_directory_entryFromPath (partition, &dirEntry, path, NULL);

	// Make sure it doesn't exist
	if (fileExists) {
		r->_errno = EEXIST;
		return -1;
	}

	if (partition->readOnly) {
		// We can't write to a read-only partition
		r->_errno = EROFS;
		return -1;
	}

	// Get the directory it has to go in
	pathEnd = strrchr (path, DIR_SEPARATOR);
	if (pathEnd == NULL) {
		// No path was specified
		parentCluster = partition->cwdCluster;
		pathEnd = path;
	} else {
		// Path was specified -- get the right parentCluster
		// Recycling dirEntry, since it needs to be recreated anyway
		if (!_FAT_directory_entryFromPath (partition, &dirEntry, path, pathEnd) ||
			!_FAT_directory_isDirectory(&dirEntry)) {
			r->_errno = ENOTDIR;
			return -1;
		}
		parentCluster = _FAT_directory_entryGetCluster (dirEntry.entryData);
		// Move the pathEnd past the last DIR_SEPARATOR
		pathEnd += 1;
	}

	// Create the entry data
	strncpy (dirEntry.d_name, pathEnd, MAX_FILENAME_LENGTH - 1);
	memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);

	// Set the creation time and date
	dirEntry.entryData[DIR_ENTRY_cTime_ms] = 0;
	u16_to_u8array (dirEntry.entryData, DIR_ENTRY_cTime, _FAT_filetime_getTimeFromRTC());
	u16_to_u8array (dirEntry.entryData, DIR_ENTRY_cDate, _FAT_filetime_getDateFromRTC());

	// Set the directory attribute
	dirEntry.entryData[DIR_ENTRY_attributes] = ATTRIB_DIR;

	// Get a cluster for the new directory
	dirCluster = _FAT_fat_linkFreeClusterCleared (partition, CLUSTER_FREE);
	if (dirCluster == CLUSTER_FREE) {
		// No space left on disc for the cluster
		r->_errno = ENOSPC;
		return -1;
	}
	u16_to_u8array (dirEntry.entryData, DIR_ENTRY_cluster, dirCluster);
	u16_to_u8array (dirEntry.entryData, DIR_ENTRY_clusterHigh, dirCluster >> 16);

	// Write the new directory's entry to it's parent
	if (!_FAT_directory_addEntry (partition, &dirEntry, parentCluster)) {
		r->_errno = ENOSPC;
		return -1;
	}

	// Create the dot entry within the directory
	memset (newEntryData, 0, DIR_ENTRY_DATA_SIZE);
	memset (newEntryData, ' ', 11);
	newEntryData[DIR_ENTRY_name] = '.';
	newEntryData[DIR_ENTRY_attributes] = ATTRIB_DIR;
	u16_to_u8array (newEntryData, DIR_ENTRY_cluster, dirCluster);
	u16_to_u8array (newEntryData, DIR_ENTRY_clusterHigh, dirCluster >> 16);

	// Write it to the directory, erasing that sector in the process
	_FAT_cache_eraseWritePartialSector ( partition->cache, newEntryData,
		_FAT_fat_clusterToSector (partition, dirCluster), 0, DIR_ENTRY_DATA_SIZE);


	// Create the double dot entry within the directory
	newEntryData[DIR_ENTRY_name + 1] = '.';
	u16_to_u8array (newEntryData, DIR_ENTRY_cluster, parentCluster);
	u16_to_u8array (newEntryData, DIR_ENTRY_clusterHigh, parentCluster >> 16);

	// Write it to the directory
	_FAT_cache_writePartialSector ( partition->cache, newEntryData,
		_FAT_fat_clusterToSector (partition, dirCluster), DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);

	// Flush any sectors in the disc cache
	if (!_FAT_cache_flush(partition->cache)) {
		r->_errno = EIO;
		return -1;
	}

	return 0;
}

DIR_STATE_STRUCT* _FAT_diropen_r(struct _reent *r, DIR_STATE_STRUCT *dirState, const char *path) {
	DIR_ENTRY dirEntry;
	DIR_STATE_STRUCT* state = dirState;
	bool fileExists;
	const char* root_path= "/";

	state->partition = _FAT_partition_getPartitionFromPath (path);
	if (state->partition == NULL) {
		r->_errno = ENODEV;
		return NULL;
	}

	// Move the path pointer to the start of the actual path
	if (strchr (path, ':') != NULL) {
		path = strchr (path, ':') + 1;
	}
	if (strchr (path, ':') != NULL) {
		r->_errno = EINVAL;
		return NULL;
	}

	if('\0' == path[0]) path = root_path;

	// Get the start cluster of the directory
	fileExists = _FAT_directory_entryFromPath (state->partition, &dirEntry, path, NULL);
	if (!fileExists) {
		r->_errno = ENOENT;
		return NULL;
	}

	// Make sure it is a directory
	if (! _FAT_directory_isDirectory (&dirEntry)) {
		r->_errno = ENOTDIR;
		return NULL;
	}

	// Save the start cluster for use when resetting the directory data
	state->startCluster = _FAT_directory_entryGetCluster (dirEntry.entryData);

	//Entry location pointer
	state->posEntry = 0;

//	// Get the first entry for use with a call to dirnext
//	state->validEntry =
//		_FAT_directory_getFirstEntry (state->partition, &(state->currentEntry), state->startCluster);
	state->validEntry = false;

	// We are now using this entry
	state->inUse = true;
	return state;
}

int _FAT_dirreset_r (struct _reent *r, DIR_STATE_STRUCT *dirState) {
	DIR_STATE_STRUCT* state = dirState;

	// Make sure we are still using this entry
	if (!state->inUse) {
		r->_errno = EBADF;
		return -1;
	}

	// Get the first entry for use with a call to dirnext
//	state->validEntry =
//		_FAT_directory_getFirstEntry (state->partition, &(state->currentEntry), state->startCluster);

	state->posEntry = 0;

	return 0;
}

int _FAT_dirnext_r (struct _reent *r, DIR_STATE_STRUCT *dirState, struct stat *filestat) {
	DIR_STATE_STRUCT* state = dirState;

	// Make sure we are still using this entry
	if (!state->inUse) {
		r->_errno = EBADF;
		return -1;
	}

	// Make sure there is another file to report on
//	if (!state->validEntry) {
//		r->_errno = ENOENT;
//		return -1;
//	}

	if(0 == state->posEntry) {
		state->validEntry = _FAT_directory_getFirstEntry (state->partition, &(state->currentEntry), state->startCluster);
	}
	else
		state->validEntry = _FAT_directory_getNextEntry (state->partition, &(state->currentEntry));

	state->posEntry += 1;

	if(!state->validEntry)
	{
		r->_errno = ENOENT;
		return -1;
	}

	if (filestat != NULL) {
		_FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat);
	}

	return 0;
/*
	// Get the filename
	strncpy (filename, state->currentEntry.d_name, MAX_FILENAME_LENGTH);
	// Get the stats, if requested
	if (filestat != NULL) {
		_FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat);
	}

	// Look for the next entry for use next time
	state->validEntry =
		_FAT_directory_getNextEntry (state->partition, &(state->currentEntry));

	return 0;
*/
}

int _FAT_dirclose_r (struct _reent *r, DIR_STATE_STRUCT *dirState) {
	DIR_STATE_STRUCT* state = dirState;

	// We are no longer using this entry
	state->inUse = false;

	return 0;
}