summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@google.com>2014-06-16 17:49:36 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2014-06-16 15:49:03 +0000
commit9c82554ee3284b57df9d8dfdf11467066ab0877a (patch)
tree1e73835bae21a35c79a391e8d8f4b956fe3cb13d
parent13e929c11c07e5cbae5db0d5887845ec6c6d08a4 (diff)
parenteb5fcc3e932a8ccac1b580788a213c7782aff31b (diff)
downloadextras-9c82554ee3284b57df9d8dfdf11467066ab0877a.tar.gz
Merge "Ignore wipe operation on non-block devices."
-rw-r--r--ext4_utils/ext4_utils.c14
-rw-r--r--ext4_utils/ext4_utils.h1
-rw-r--r--ext4_utils/make_ext4fs.c3
-rw-r--r--ext4_utils/wipe.c19
-rw-r--r--ext4_utils/wipe.h9
5 files changed, 42 insertions, 4 deletions
diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c
index 3755fee1..bb6c8637 100644
--- a/ext4_utils/ext4_utils.c
+++ b/ext4_utils/ext4_utils.c
@@ -393,6 +393,20 @@ u64 get_block_device_size(int fd)
return size;
}
+int is_block_device_fd(int fd)
+{
+#ifdef USE_MINGW
+ return 0;
+#else
+ struct stat st;
+ int ret = fstat(fd, &st);
+ if (ret < 0)
+ return 0;
+
+ return S_ISBLK(st.st_mode);
+#endif
+}
+
u64 get_file_size(int fd)
{
struct stat buf;
diff --git a/ext4_utils/ext4_utils.h b/ext4_utils/ext4_utils.h
index 447d4161..c6f52b5b 100644
--- a/ext4_utils/ext4_utils.h
+++ b/ext4_utils/ext4_utils.h
@@ -140,6 +140,7 @@ void ext4_create_journal_inode(void);
void ext4_update_free(void);
void ext4_queue_sb(void);
u64 get_block_device_size(int fd);
+int is_block_device_fd(int fd);
u64 get_file_size(int fd);
u64 parse_num(const char *arg);
void ext4_parse_sb_info(struct ext4_super_block *sb);
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index f164883e..6a0f8758 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -623,8 +623,9 @@ int make_ext4fs_internal(int fd, const char *_directory,
aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
aux_info.sb->s_blocks_count_lo);
- if (wipe)
+ if (wipe && WIPE_IS_SUPPORTED) {
wipe_block_device(fd, info.len);
+ }
write_ext4_image(fd, gzip, sparse, crc);
diff --git a/ext4_utils/wipe.c b/ext4_utils/wipe.c
index 3bd33e5b..5766632e 100644
--- a/ext4_utils/wipe.c
+++ b/ext4_utils/wipe.c
@@ -17,6 +17,8 @@
#include "ext4_utils.h"
#include "wipe.h"
+#if WIPE_IS_SUPPORTED
+
#if defined(__linux__)
#include <linux/fs.h>
@@ -35,6 +37,11 @@ int wipe_block_device(int fd, s64 len)
u64 range[2];
int ret;
+ if (!is_block_device_fd(fd)) {
+ // Wiping only makes sense on a block device.
+ return 0;
+ }
+
range[0] = 0;
range[1] = len;
ret = ioctl(fd, BLKSECDISCARD, &range);
@@ -53,11 +60,17 @@ int wipe_block_device(int fd, s64 len)
return 0;
}
-#else
+
+#else /* __linux__ */
+#error "Missing block device wiping implementation for this platform!"
+#endif
+
+#else /* WIPE_IS_SUPPORTED */
+
int wipe_block_device(int fd, s64 len)
{
- error("wipe not supported on non-linux platforms");
+ /* Wiping is not supported on this platform. */
return 1;
}
-#endif
+#endif /* WIPE_IS_SUPPORTED */
diff --git a/ext4_utils/wipe.h b/ext4_utils/wipe.h
index ecde9c14..bd119e35 100644
--- a/ext4_utils/wipe.h
+++ b/ext4_utils/wipe.h
@@ -19,6 +19,15 @@
#include "ext4_utils.h"
+/* Set WIPE_IS_SUPPORTED to 1 if the current platform supports
+ * wiping of block devices. 0 otherwise. For now, only Linux does.
+ */
+#ifdef __linux__
+# define WIPE_IS_SUPPORTED 1
+#else
+# define WIPE_IS_SUPPORTED 0
+#endif
+
int wipe_block_device(int fd, s64 len);
#endif