Fix signit if reencryption not yet started.
[platform/upstream/cryptsetup.git] / lib / utils_devpath.c
index 2844f35..31289ee 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <string.h>
@@ -32,6 +32,7 @@
 
 char *crypt_lookup_dev(const char *dev_id);
 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size);
+int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
 
 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
 {
@@ -169,7 +170,7 @@ char *crypt_lookup_dev(const char *dev_id)
 
 static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor)
 {
-       char path[PATH_MAX], tmp[64];
+       char path[PATH_MAX], tmp[64] = {0};
        int fd, r = 0;
 
        if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0)
@@ -180,7 +181,11 @@ static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor
        r = read(fd, tmp, sizeof(tmp));
        close(fd);
 
-       if (r <= 0 || sscanf(tmp, "%d:%d", major, minor) != 2)
+       if (r <= 0)
+               return 0;
+
+       tmp[63] = '\0';
+       if (sscanf(tmp, "%d:%d", major, minor) != 2)
                return 0;
 
        return 1;
@@ -238,3 +243,26 @@ int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_
 
        return r;
 }
+
+int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
+{
+       char path[PATH_MAX], tmp[64] = {0};
+       int fd, r;
+
+       if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
+                    major, minor) < 0)
+               return 0;
+
+       if ((fd = open(path, O_RDONLY)) < 0)
+               return 0;
+       r = read(fd, tmp, sizeof(tmp));
+       close(fd);
+
+       if (r <= 0)
+               return 0;
+
+        if (sscanf(tmp, "%d", rotational) != 1)
+               return 0;
+
+       return 1;
+}