summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2021-05-01 20:38:26 +0200
committerPali Rohár <pali.rohar@gmail.com>2021-05-01 20:38:26 +0200
commitbbf438e1a87a98311044cb83ccedecac80d81a97 (patch)
treebe2593758d4b39e6686297cbe451131c6c9c4326
parent670db0d169672f5719663a2fc88e5aed39eb3f4f (diff)
download0xFFFF-bbf438e1a87a98311044cb83ccedecac80d81a97.tar.bz2
disk: Respect -s (simulate) flag for dump -e operation
-rw-r--r--src/disk.c36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/disk.c b/src/disk.c
index d0d2474..41d1823 100644
--- a/src/disk.c
+++ b/src/disk.c
@@ -118,7 +118,7 @@ int disk_open_dev(int maj, int min, int partition, int readonly) {
blkdev[len] = 0;
- fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL | O_NONBLOCK);
+ fd = open(blkdev, ((simulate || readonly) ? O_RDONLY : O_RDWR) | O_EXCL | O_NONBLOCK);
if ( fd < 0 ) {
ERROR_INFO("Cannot open block device %s", blkdev);
return -1;
@@ -144,13 +144,13 @@ int disk_open_dev(int maj, int min, int partition, int readonly) {
ERROR("Block device name is too long");
return -1;
}
- fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL);
+ fd = open(blkdev, ((simulate || readonly) ? O_RDONLY : O_RDWR) | O_EXCL);
if ( fd < 0 && errno == ENOENT ) {
if ( snprintf(blkdev+len, sizeof(blkdev)-len, "%d", partition) >= (int)(sizeof(blkdev)-len) ) {
ERROR("Block device name is too long");
return -1;
}
- fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL);
+ fd = open(blkdev, ((simulate || readonly) ? O_RDONLY : O_RDWR) | O_EXCL);
}
if ( fd < 0 && errno == ENOENT ) {
blkdev[len] = 0;
@@ -201,7 +201,7 @@ int disk_open_dev(int maj, int min, int partition, int readonly) {
int disk_dump_dev(int fd, const char * file) {
- int fd2;
+ int fd2 = -1;
int ret;
char * path;
uint64_t blksize;
@@ -253,11 +253,15 @@ int disk_dump_dev(int fd, const char * file) {
return -1;
}
- fd2 = creat(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if ( ! simulate ) {
+
+ fd2 = creat(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+ if ( fd2 < 0 ) {
+ ERROR_INFO("Cannot create file %s", file);
+ return -1;
+ }
- if ( fd2 < 0 ) {
- ERROR_INFO("Cannot create file %s", file);
- return -1;
}
sent = 0;
@@ -272,19 +276,23 @@ int disk_dump_dev(int fd, const char * file) {
break;
if ( size < 0 ) {
PRINTF_ERROR("Reading from block device failed");
- close(fd2);
+ if ( ! simulate )
+ close(fd2);
return -1;
}
- if ( write(fd2, global_buf, size) != size ) {
- PRINTF_ERROR("Dumping image failed");
- close(fd2);
- return -1;
+ if ( ! simulate ) {
+ if ( write(fd2, global_buf, size) != size ) {
+ PRINTF_ERROR("Dumping image failed");
+ close(fd2);
+ return -1;
+ }
}
sent += size;
printf_progressbar(sent, blksize);
}
- close(fd2);
+ if ( ! simulate )
+ close(fd2);
return 0;
}