Skip to content

Commit 26b0edd

Browse files
author
Farzeen
committed
Allow specifying path to disk.xfs
Provide `--disk-file /path/to/disk.xfs` command line option. Default value is `disk.xfs` for backwards compatibility.
1 parent 56b8023 commit 26b0edd

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

disk.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ Interface to access disk file.
1010
#include <sys/types.h>
1111

1212
BLOCK *disk;
13+
/* path to disk.xfs */
14+
static const char* pathToDisk = NULL;
15+
16+
void setPathToDisk(const char* path) {
17+
pathToDisk = path;
18+
}
19+
1320

1421
/* Reads an entire block from fileBlockNumber on the disk to virtBlockNumber on the memory copy of the disk */
1522
int readFromDisk(int virtBlockNumber, int fileBlockNumber)
@@ -39,7 +46,7 @@ int writeToDisk(int virtBlockNumber, int fileBlockNumber)
3946
/* Opens the disk file and returns the file descriptor */
4047
int openDiskFile(int access)
4148
{
42-
int fd = open(DISK_NAME, access, 0666);
49+
int fd = open(pathToDisk, access, 0666);
4350
if (fd < 0)
4451
exception_throwException(EXCEPTION_CANT_OPEN_DISK);
4552

@@ -52,9 +59,9 @@ void createDiskFile(int format)
5259
int fd;
5360

5461
if (format == DISK_FORMAT)
55-
fd = open(DISK_NAME, O_CREAT | O_TRUNC | O_SYNC, 0666);
62+
fd = open(pathToDisk, O_CREAT | O_TRUNC | O_SYNC, 0666);
5663
else
57-
fd = open(DISK_NAME, O_CREAT, 0666);
64+
fd = open(pathToDisk, O_CREAT, 0666);
5865

5966
if (fd < 0)
6067
exception_throwException(EXCEPTION_CANT_CREATE_DISK);
@@ -66,4 +73,4 @@ void createDiskFile(int format)
6673
void diskCheckFileExists()
6774
{
6875
close(openDiskFile(O_RDONLY));
69-
}
76+
}

disk.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "fileSystem.h"
66
#include "exception.h"
77

8-
#define DISK_NAME "disk.xfs"
98

109
#define DISK_NO_FORMAT 0
1110
#define DISK_FORMAT 1
@@ -15,10 +14,11 @@ typedef struct
1514
char word[BLOCK_SIZE][WORD_SIZE];
1615
} BLOCK;
1716

17+
void setPathToDisk(const char* path);
1818
int readFromDisk(int virtBlockNumber, int fileBlockNumber);
1919
int writeToDisk(int virtBlockNumber, int fileBlockNumber);
2020
int openDiskFile(int access);
2121
void createDiskFile(int format);
2222
void diskCheckFileExists();
2323

24-
#endif
24+
#endif

interface.c

+26-2
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,31 @@ int main(int argc, char **argv)
560560

561561
disk_init();
562562

563-
fd = open(DISK_NAME, O_RDONLY, 0666);
563+
const char* disk_file = DEFAULT_DISK_NAME;
564+
565+
if(argc>1 && strcmp(argv[1], "--disk-file")==0)
566+
{
567+
argc --;
568+
argv ++;
569+
if(argc>1)
570+
{
571+
disk_file = argv[1];
572+
argc --;
573+
argv ++;
574+
}
575+
else
576+
{
577+
printf("--disk-file option requires a file name.\n");
578+
printf("\n");
579+
printf("Syntax: --disk-file /path/to/disk.xfs\n");
580+
printf("Specifies the path to disk.xfs to use.\n");
581+
exit(-1);
582+
}
583+
}
584+
585+
setPathToDisk(disk_file);
586+
587+
fd = open(disk_file, O_RDONLY, 0666);
564588
if (fd > 0)
565589
{
566590
close(fd);
@@ -569,4 +593,4 @@ int main(int argc, char **argv)
569593

570594
cli(argc, argv);
571595
return 0;
572-
}
596+
}

interface.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define DO_NOT_FORMAT 0
99
#define FORMAT 1
1010

11+
#define DEFAULT_DISK_NAME "disk.xfs"
12+
1113
void cli(int argc, char **argv);
1214
void runCommand(char command[]);
1315
char *xfs_cli_stripwhite(char *str);

0 commit comments

Comments
 (0)