Merge remote-tracking branch 'origin/victortung'

This commit is contained in:
Victor 2023-11-06 17:46:24 -08:00
commit 94a1496127
2 changed files with 24 additions and 59 deletions

View File

@ -8,21 +8,20 @@
class RawDisk{
char* dir;
int fd;
const char* dir;
off_t numSectors;
off_t diskSize;
public:
RawDisk(char *directory){
RawDisk(const char *directory) : fd(-1), dir(nullptr), numSectors(0), diskSize(0) {
dir = directory;
/*dir = strdup("/dev/vdc");
numSectors = 62914560;
diskSize = 32212254720;*/
int fd;
// Open the block device (replace /dev/sdX with the actual device)
fd = open(dir, O_RDONLY);
fd = open(dir, O_RDWR); // Allow read and write
if (fd == -1) {
perror("Error opening device");
exit(1);
@ -41,71 +40,42 @@ public:
printf("====Initializing RawDisk====\n");
printf("Number of sectors: %llu\n", numSectors);
printf("Disk size (in bytes): %llu\n", diskSize);
close(fd);
}
int rawdisk_read(off_t blockNumber, char *buffer){
int fd;
fd = open(dir, O_RDONLY);
if (fd == -1) {
perror("Error opening device");
return -1;
}
// Calculate the offset in bytes
off_t offset = blockNumber * 512;
// Move the file pointer to the desired block
if (lseek(fd, offset, SEEK_SET) == -1) {
perror("Error seeking to block");
~RawDisk() {
if (fd != -1) {
close(fd);
}
}
int rawdisk_read(off_t offset, char *buffer, size_t length) {
if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
perror("Error seeking to offset");
return -1;
}
// Read a block of data
ssize_t bytesRead = read(fd, buffer, 512);
ssize_t bytesRead = read(fd, buffer, length);
if (bytesRead == -1) {
perror("Error reading from device");
close(fd);
return -1;
}
// Process the data (e.g., data recovery or analysis)
close(fd);
return 0;
}
int rawdisk_write(off_t blockNumber, char *buffer){
int fd;
fd = open(dir, O_WRONLY);
if (fd == -1) {
perror("Error opening device");
// Write a specified number of bytes at a given byte offset
int rawdisk_write(off_t offset, char *buffer, size_t length) {
if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
perror("Error seeking to offset");
return -1;
}
// Calculate the offset in bytes
off_t offset = blockNumber * 512;
// Move the file pointer to the desired block
if (lseek(fd, offset, SEEK_SET) == -1) {
perror("Error seeking to block");
close(fd);
ssize_t bytesWritten = write(fd, buffer, length);
if (bytesWritten == -1) {
perror("Error writing to device");
return -1;
}
// Write a block of data
ssize_t bytesWrite = write(fd, buffer, 512);
if (bytesWrite == -1) {
perror("Error writing from device");
close(fd);
return -1;
}
// Process the data (e.g., data recovery or analysis)
close(fd);
return 0;
}

View File

@ -4,12 +4,7 @@
#include "rawdisk.h"
int main(int argc, char *argv[]) {
char *d = NULL;
if(argc < 2){
d = strdup("/dev/vdc");
}else{
d = argv[1];
}
const char* d = (argc < 2) ? "/dev/vdc" : argv[1];
RawDisk *H = new RawDisk(d);
@ -20,11 +15,11 @@ int main(int argc, char *argv[]) {
//use number to substitute H->getnumSector(), getnumSectors() are not yest implemented
for(off_t i = 0; i < 10; i++) {
H->rawdisk_write(i, buf);
H->rawdisk_write(i*512, buf, strlen(buf));//Change write_API
}
//use number to substitute H->getnumSector(), getnumSectors() are not yest implemented
for(off_t i = 0; i < 10; i++) {
H->rawdisk_read(i, readBuffer);
H->rawdisk_read(i*512, readBuffer, sizeof(readBuffer));//Change read_API
assert(strncmp(readBuffer, buf, strlen(buf)) == 0);
}