From 413bfd6b969407941728655bb3895d033edb37b8 Mon Sep 17 00:00:00 2001
From: Oliver Schmidt
Date: Sun, 4 Feb 2018 20:17:23 +0100
Subject: [PATCH] Added check for success of file operations.
Note: As the drive 2 disk image filename is optional all access to drive 2 should trigger an I/O error on the client side.
---
server/main.cpp | 44 +++++++++++++++++++++++++++++++++++++-------
1 file changed, 37 insertions(+), 7 deletions(-)
diff --git a/server/main.cpp b/server/main.cpp
index 2b970ab..0f8fff9 100644
--- a/server/main.cpp
+++ b/server/main.cpp
@@ -102,14 +102,33 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
- char * apFileData[2];
- for (int iDrive=0; iDrive<2; iDrive++)
+ char *apFileData[2] = {0, 0};
+ for (int iDrive = 0; iDrive < 2; iDrive++)
{
+ if (apFilename[iDrive] == 0)
+ continue;
+
int fd = open(apFilename[iDrive], O_RDWR, S_IWRITE | S_IREAD);
+ if (fd == -1)
+ {
+ printf("Err: %s - %s\n", apFilename[iDrive], strerror(errno));
+ return EXIT_FAILURE;
+ }
+
struct stat stats;
- fstat(fd, &stats);
+ if (fstat(fd, &stats) == -1)
+ {
+ printf("Err: %s - %s\n", apFilename[iDrive], strerror(errno));
+ return EXIT_FAILURE;
+ }
unsigned int uFilesize = (unsigned int)stats.st_size;
- apFileData[iDrive] = (char*) mmap(0, uFilesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+
+ apFileData[iDrive] = (char*)mmap(0, uFilesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (apFileData[iDrive] == MAP_FAILED)
+ {
+ printf("Err: %s - %s\n", apFilename[iDrive], strerror(errno));
+ return EXIT_FAILURE;
+ }
}
enum sp_return eResult;
@@ -252,7 +271,18 @@ int main(int argc, char *argv[])
// If 4x bytes were read and the checksum passed, process request
int iBlock = auReadBuf[1] | (auReadBuf[2] << 8);
- char *pDriveData = apFileData[(auReadBuf[0] >> 2) & 1];
+ int iDrive = (auReadBuf[0] >> 2) & 1;
+ char *pDriveData = apFileData[iDrive];
+ if (pDriveData == 0)
+ {
+ if (bLog)
+ {
+ printf("%d\tN\n", iDrive);
+ fflush(stdout);
+ }
+ // TODO: Make this an I/O error on the client side
+ continue;
+ }
pDriveData += iBlock << 9;
// Read block
@@ -281,7 +311,7 @@ int main(int argc, char *argv[])
if (bLog)
{
- printf("%d\tR %5d\r", (auReadBuf[0] >> 2) & 1, iBlock);
+ printf("%d\tR %5d\r", iDrive, iBlock);
fflush(stdout);
}
}
@@ -308,7 +338,7 @@ int main(int argc, char *argv[])
if (bLog)
{
- printf("%d\t\t\t\tW %5d\r", (auReadBuf[0] >> 2) & 1, iBlock);
+ printf("%d\t\t\t\tW %5d\r", iDrive, iBlock);
fflush(stdout);
}
}