Simplify sysfs helpers.
authorMilan Broz <gmazyland@gmail.com>
Sun, 30 Jun 2013 07:04:00 +0000 (09:04 +0200)
committerMilan Broz <gmazyland@gmail.com>
Sun, 30 Jun 2013 07:05:43 +0000 (09:05 +0200)
lib/internal.h
lib/tcrypt/tcrypt.c
lib/utils_devpath.c
lib/utils_wipe.c

index 9ca5d48..e403db4 100644 (file)
@@ -92,8 +92,8 @@ struct device *crypt_data_device(struct crypt_device *cd);
 int crypt_confirm(struct crypt_device *cd, const char *msg);
 
 char *crypt_lookup_dev(const char *dev_id);
-int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
-int crypt_sysfs_get_partition(const char *dev_path, int *partition);
+int crypt_dev_is_rotational(int major, int minor);
+int crypt_dev_is_partition(const char *dev_path);
 
 ssize_t write_blockwise(int fd, int bsize, void *buf, size_t count);
 ssize_t read_blockwise(int fd, int bsize, void *_buf, size_t count);
index 6de62fa..70449a6 100644 (file)
@@ -565,7 +565,7 @@ int TCRYPT_read_phdr(struct crypt_device *cd,
 {
        struct device *device = crypt_metadata_device(cd);
        ssize_t hdr_size = sizeof(struct tcrypt_phdr);
-       int devfd = 0, r, bs, partition;
+       int devfd = 0, r, bs;
 
        assert(sizeof(struct tcrypt_phdr) == 512);
 
@@ -587,9 +587,7 @@ int TCRYPT_read_phdr(struct crypt_device *cd,
                if (lseek(devfd, TCRYPT_HDR_SYSTEM_OFFSET, SEEK_SET) >= 0 &&
                    read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size) {
                        r = TCRYPT_init_hdr(cd, hdr, params);
-                       if (r == -EPERM &&
-                           crypt_sysfs_get_partition(device_path(device), &partition) &&
-                           partition)
+                       if (r == -EPERM && crypt_dev_is_partition(device_path(device)))
                                log_std(cd, _("WARNING: device %s is a partition, for TCRYPT "
                                              "system encryption you usually need to use "
                                              "whole block device path.\n"), device_path(device));
index 7df2db5..36b6638 100644 (file)
@@ -193,18 +193,17 @@ static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *
        return 1;
 }
 
-int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
+int crypt_dev_is_rotational(int major, int minor)
 {
        uint64_t val;
 
        if (!_sysfs_get_uint64(major, minor, &val, "queue/rotational"))
-               return 0;
+               return 1; /* if failed, expect rotational disk */
 
-       *rotational = val ? 1 : 0;
-       return 1;
+       return val ? 1 : 0;
 }
 
-int crypt_sysfs_get_partition(const char *dev_path, int *partition)
+int crypt_dev_is_partition(const char *dev_path)
 {
        uint64_t val;
        struct stat st;
@@ -219,6 +218,5 @@ int crypt_sysfs_get_partition(const char *dev_path, int *partition)
                              &val, "partition"))
                return 0;
 
-       *partition = val ? 1 : 0;
-       return 1;
+       return val ? 1 : 0;
 }
index b9385b5..210c566 100644 (file)
@@ -124,7 +124,7 @@ int crypt_wipe(struct device *device,
 {
        struct stat st;
        char *buffer;
-       int devfd, flags, rotational, bsize;
+       int devfd, flags, bsize;
        ssize_t written;
 
        if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
@@ -139,14 +139,12 @@ int crypt_wipe(struct device *device,
        }
 
        if (type == CRYPT_WIPE_DISK && S_ISBLK(st.st_mode)) {
-               rotational = 0;
-               if (!crypt_sysfs_get_rotational(major(st.st_rdev),
-                                               minor(st.st_rdev),
-                                               &rotational))
-                       rotational = 1;
-               log_dbg("Rotational flag is %d.", rotational);
-               if (!rotational)
+               if (!crypt_dev_is_rotational(major(st.st_rdev),
+                                               minor(st.st_rdev))) {
                        type = CRYPT_WIPE_SSD;
+                       log_dbg("Non-rotational device, using SSD wipe mode.");
+               } else
+                       log_dbg("Rotational device, using normal wipe mode.");
        }
 
        bsize = device_block_size(device);