add destructor to close fd to open device one time, and make dir be const char* type
This commit is contained in:
parent
0442a53fbf
commit
d379f6d7f0
@ -8,21 +8,20 @@
|
|||||||
|
|
||||||
class RawDisk{
|
class RawDisk{
|
||||||
|
|
||||||
char* dir;
|
int fd;
|
||||||
|
const char* dir;
|
||||||
off_t numSectors;
|
off_t numSectors;
|
||||||
off_t diskSize;
|
off_t diskSize;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RawDisk(char *directory){
|
RawDisk(const char *directory){
|
||||||
dir = directory;
|
dir = directory;
|
||||||
/*dir = strdup("/dev/vdc");
|
/*dir = strdup("/dev/vdc");
|
||||||
numSectors = 62914560;
|
numSectors = 62914560;
|
||||||
diskSize = 32212254720;*/
|
diskSize = 32212254720;*/
|
||||||
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
// Open the block device (replace /dev/sdX with the actual device)
|
// 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) {
|
if (fd == -1) {
|
||||||
perror("Error opening device");
|
perror("Error opening device");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -41,58 +40,41 @@ public:
|
|||||||
printf("====Initializing RawDisk====\n");
|
printf("====Initializing RawDisk====\n");
|
||||||
printf("Number of sectors: %llu\n", numSectors);
|
printf("Number of sectors: %llu\n", numSectors);
|
||||||
printf("Disk size (in bytes): %llu\n", diskSize);
|
printf("Disk size (in bytes): %llu\n", diskSize);
|
||||||
|
}
|
||||||
|
|
||||||
close(fd);
|
~RawDisk() {
|
||||||
|
if (fd != -1) {
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int rawdisk_read(off_t blockNumber, char *buffer){
|
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
|
// Calculate the offset in bytes
|
||||||
off_t offset = blockNumber * 512;
|
off_t offset = blockNumber * 512;
|
||||||
|
|
||||||
// Move the file pointer to the desired block
|
// Move the file pointer to the desired block
|
||||||
if (lseek(fd, offset, SEEK_SET) == -1) {
|
if (lseek(fd, offset, SEEK_SET) == -1) {
|
||||||
perror("Error seeking to block");
|
perror("Error seeking to block");
|
||||||
close(fd);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a block of data
|
// Read a 512 byte-size block of data
|
||||||
ssize_t bytesRead = read(fd, buffer, 512);
|
ssize_t bytesRead = read(fd, buffer, 512);
|
||||||
if (bytesRead == -1) {
|
if (bytesRead == -1) {
|
||||||
perror("Error reading from device");
|
perror("Error reading from device");
|
||||||
close(fd);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the data (e.g., data recovery or analysis)
|
return 0;
|
||||||
close(fd);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int rawdisk_write(off_t blockNumber, char *buffer){
|
int rawdisk_write(off_t blockNumber, char *buffer){
|
||||||
int fd;
|
|
||||||
|
|
||||||
fd = open(dir, O_WRONLY);
|
|
||||||
if (fd == -1) {
|
|
||||||
perror("Error opening device");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate the offset in bytes
|
// Calculate the offset in bytes
|
||||||
off_t offset = blockNumber * 512;
|
off_t offset = blockNumber * 512;
|
||||||
|
|
||||||
// Move the file pointer to the desired block
|
// Move the file pointer to the desired block
|
||||||
if (lseek(fd, offset, SEEK_SET) == -1) {
|
if (lseek(fd, offset, SEEK_SET) == -1) {
|
||||||
perror("Error seeking to block");
|
perror("Error seeking to block");
|
||||||
close(fd);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,12 +82,9 @@ public:
|
|||||||
ssize_t bytesWrite = write(fd, buffer, 512);
|
ssize_t bytesWrite = write(fd, buffer, 512);
|
||||||
if (bytesWrite == -1) {
|
if (bytesWrite == -1) {
|
||||||
perror("Error writing from device");
|
perror("Error writing from device");
|
||||||
close(fd);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the data (e.g., data recovery or analysis)
|
|
||||||
close(fd);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,12 +4,7 @@
|
|||||||
#include "rawdisk.h"
|
#include "rawdisk.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
char *d = NULL;
|
const char* d = (argc < 2) ? "/dev/vdc" : argv[1];
|
||||||
if(argc < 2){
|
|
||||||
d = strdup("/dev/vdc");
|
|
||||||
}else{
|
|
||||||
d = argv[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
RawDisk *H = new RawDisk(d);
|
RawDisk *H = new RawDisk(d);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user