Copyright Tristan Aubrey-Jones and Dale Caffull December 2006.
#include
#include
#include "myfs.h"
#define SECTOR_SIZE 512
#define MAX_SECTOR 2879
/* === SIMULATED DISK === */
/* The two functions below provide dummy implementations of a disk
Note that these implementations never fail for valid sector numbers;
a rigorous test environment would include random failures */
static char disk[SECTOR_SIZE*(MAX_SECTOR+1)];
int myfs_read_sector(char buffer[512], int sector_number) {
/* If the sector number lies in the range 0...2879 inclusive, attempt
to read the numbered sector into the buffer. A return of MYFS_SUCCESS
indicates success. Any other return code, or failure to return, will
not not change the disk contents. */
int i;
if (sector_number<0 || sector_number > MAX_SECTOR)
return MYFS_DISK_FAILURE;
for (i=0; i MAX_SECTOR)
return MYFS_DISK_FAILURE;
for (i=0; i= 0)
{
sprintf(filename, "%011d", i);
r = myfs_read_file(filename, buffer, BUFFER_SIZE, &file_size);
if (r != MYFS_SUCCESS) printf("File %d failed read with %d\n", i, r);
else if (file_size != FILE_SIZE) printf("File %d has wrong size %d", i, file_size);
else
{
for (j = 0; j < file_size; j++) {
if (buffer[j] != j % 127)
printf("File %d corrupt at %d", i, j);
}
}
i--;
}
/* corrupt the FAT table */
disk[23] = 245;
/* delete them */
for (i = 0; i < count; i++)
{
sprintf(filename, "%011d", i);
r = myfs_delete_file(filename);
if (r != MYFS_SUCCESS) printf("Deleting file %d failed with %d", i, r);
else
{
r = myfs_read_file(filename, buffer, BUFFER_SIZE, &file_size);
if (r != MYFS_FILE_NOT_FOUND) printf("File %d should have been deleted, but read returns %d", i, r);
}
}
printf("Deleted files\n");
/* try writing new ones */
file_size = FILE_SIZE * 1.5;
i = 0; r = MYFS_SUCCESS;
while (r == MYFS_SUCCESS)
{
sprintf(filename, "%011d", i);
r = myfs_write_file(filename, buffer, file_size);
i++;
}
i--;
count = i;
printf("Max 3kB files: %d\n", count);
/* test reads */
for (i = 0; i < count; i++)
{
sprintf(filename, "%011d", i);
r = myfs_read_file(filename, buffer, BUFFER_SIZE, &file_size);
if (r != MYFS_SUCCESS) printf("File %d failed read with %d\n", i, r);
else if (file_size != FILE_SIZE * 1.5) printf("File %d has wrong size %d", i, file_size);
else
{
for (j = 0; j < file_size; j++) {
if (buffer[j] != j % 127)
printf("File %d corrupt at %d", i, j);
}
}
}
/* delete half of them */
for (i = 0; i < count / 3; i++)
{
sprintf(filename, "%011d", i);
r = myfs_delete_file(filename);
if (r != MYFS_SUCCESS) printf("Deleting file %d failed with %d", i, r);
else
{
r = myfs_read_file(filename, buffer, BUFFER_SIZE, &file_size);
if (r != MYFS_FILE_NOT_FOUND) printf("File %d should have been deleted, but read returns %d", i, r);
}
}
printf("Deleted %d files\n", i);
/* write some more */
file_size = FILE_SIZE * 1.5;
i = 0; r = MYFS_SUCCESS;
while (r == MYFS_SUCCESS)
{
sprintf(filename, "%011d", i);
r = myfs_write_file(filename, buffer, file_size);
i++;
}
i--;
count = i;
printf("Added 3kB files: %d\n", count);
/* test reads */
i = 0;
r = MYFS_SUCCESS;
while (r == MYFS_SUCCESS)
{
sprintf(filename, "%011d", i);
r = myfs_read_file(filename, buffer, BUFFER_SIZE, &file_size);
if (r != MYFS_SUCCESS) printf("File %d failed read with %d\n", i, r);
else
{
for (j = 0; j < file_size; j++) {
if (buffer[j] != j % 127)
printf("File %d corrupt at %d", i, j);
}
}
i++;
}
system("PAUSE");
return 0;
}