Skip to content

Commit e177cbe

Browse files
author
Ian Seyler
committed
bmfslite - calculate and write correct starting block
1 parent 2f63753 commit e177cbe

File tree

1 file changed

+8
-55
lines changed

1 file changed

+8
-55
lines changed

src/bmfslite.c

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* BareMetal File System Lite Utility */
22
/* Written by Ian Seyler of Return Infinity */
3-
/* v1.0 (2024 11 19) */
3+
/* v1.0 (2024 11 21) */
44

55
/* Global includes */
66
#include <stdio.h>
@@ -45,7 +45,6 @@ char s_initialize[] = "initialize";
4545
char s_create[] = "create";
4646
char s_read[] = "read";
4747
char s_write[] = "write";
48-
char s_delete[] = "delete";
4948
struct BMFSEntry entry;
5049
void *pentry = &entry;
5150
char *BlockMap;
@@ -60,7 +59,6 @@ int bmfs_initialize(char *diskname, char *size);
6059
void bmfs_create(char *filename, unsigned long long maxsize);
6160
void bmfs_read(char *filename);
6261
void bmfs_write(char *filename);
63-
void bmfs_delete(char *filename);
6462

6563
/* Program code */
6664
int main(int argc, char *argv[])
@@ -72,7 +70,7 @@ int main(int argc, char *argv[])
7270
printf("Written by Ian Seyler @ Return Infinity ([email protected])\n\n");
7371
printf("Usage: bmfs disk function file\n\n");
7472
printf("Disk: the name of the disk file\n");
75-
printf("Function: list, read, write, create, delete, format, initialize\n");
73+
printf("Function: list, read, write, create, format, initialize\n");
7674
printf("File: (if applicable)\n");
7775
exit(EXIT_SUCCESS);
7876
}
@@ -111,7 +109,7 @@ int main(int argc, char *argv[])
111109
{
112110
fseek(disk, 0, SEEK_END);
113111
disksize = ftell(disk); // Disk size in Bytes
114-
fseek(disk, 0, SEEK_SET); // Seek 4KiB in for directory
112+
fseek(disk, 0, SEEK_SET); // Seek to start for directory
115113
retval = fread(Directory, 4096, 1, disk); // Read 4096 bytes to the Directory buffer
116114
rewind(disk);
117115
}
@@ -120,6 +118,7 @@ int main(int argc, char *argv[])
120118
{
121119
bmfs_list();
122120
}
121+
/*
123122
else if (strcasecmp(s_format, command) == 0)
124123
{
125124
if (argc > 3)
@@ -138,6 +137,7 @@ int main(int argc, char *argv[])
138137
printf("Format aborted!\n");
139138
}
140139
}
140+
*/
141141
else if (strcasecmp(s_create, command) == 0)
142142
{
143143
if (filename == NULL)
@@ -178,10 +178,6 @@ int main(int argc, char *argv[])
178178
{
179179
bmfs_write(filename);
180180
}
181-
else if (strcasecmp(s_delete, command) == 0)
182-
{
183-
bmfs_delete(filename);
184-
}
185181
else
186182
{
187183
printf("bmfs error: Unknown command\n");
@@ -456,20 +452,15 @@ void bmfs_create(char *filename, unsigned long long maxsize)
456452
struct BMFSEntry tempentry;
457453
int slot;
458454

459-
if (maxsize % 2 != 0)
460-
maxsize++;
461-
462455
if (bmfs_find(filename, &tempentry, &slot) == 0)
463456
{
464457
unsigned long long blocks_requested = maxsize / 2; // how many blocks to allocate
465-
unsigned long long num_blocks = disksize / 2; // number of blocks in the disk
466458
char dir_copy[4096]; // copy of directory
467459
int num_used_entries = 0; // how many entries of Directory are either used or deleted
468460
int first_free_entry = -1; // where to put new entry
469461
int tint;
470462
struct BMFSEntry *pEntry;
471463
unsigned long long new_file_start = 0;
472-
unsigned long long prev_file_end = 1;
473464

474465
if(strlen(filename) > 31)
475466
{
@@ -514,33 +505,15 @@ void bmfs_create(char *filename, unsigned long long maxsize)
514505
// between the end of the previous file (initially == 1)
515506
// and the beginning of the current file (or the last data block if there are no more files).
516507

517-
unsigned long long this_file_start;
518508
pEntry = (struct BMFSEntry *)(dir_copy + tint * 64); // points to the current directory entry
519509

520-
if (tint == num_used_entries || pEntry->FileName[0] == 0x01)
521-
this_file_start = num_blocks - 1; // index of the last block
522-
else
523-
this_file_start = pEntry->StartingBlock;
524-
525-
if (this_file_start - prev_file_end >= blocks_requested)
526-
{ // fits here
527-
new_file_start = prev_file_end;
528-
break;
529-
}
530-
531-
if (tint < num_used_entries)
532-
prev_file_end = pEntry->StartingBlock + pEntry->ReservedBlocks;
533-
}
534-
535-
if (new_file_start == 0)
536-
{
537-
printf("bmfs error: Cannot create file of size %lld bytes.\n", maxsize);
538-
return;
510+
new_file_start += pEntry->ReservedBlocks;
539511
}
512+
new_file_start += 4; // Skip the directory
540513

541514
// Add file record to Directory
542515
pEntry = (struct BMFSEntry *)(Directory + first_free_entry * 64);
543-
pEntry->StartingBlock = new_file_start + 3;
516+
pEntry->StartingBlock = new_file_start;
544517
pEntry->ReservedBlocks = blocks_requested;
545518
pEntry->FileSize = 0;
546519
strcpy(pEntry->FileName, filename);
@@ -715,24 +688,4 @@ void bmfs_write(char *filename)
715688
}
716689

717690

718-
void bmfs_delete(char *filename)
719-
{
720-
struct BMFSEntry tempentry;
721-
char delmarker = 0x01;
722-
int slot;
723-
724-
if (0 == bmfs_find(filename, &tempentry, &slot))
725-
{
726-
printf("bmfs error: File not found in BMFS.\n");
727-
}
728-
else
729-
{
730-
// Update directory
731-
memcpy(Directory+(slot*64), &delmarker, 1);
732-
fseek(disk, 4096, SEEK_SET); // Seek 4KiB in for directory
733-
fwrite(Directory, 4096, 1, disk); // Write new directory to disk
734-
}
735-
}
736-
737-
738691
/* EOF */

0 commit comments

Comments
 (0)