Use better error messages if device doesn't exist
authorMilan Broz <gmazyland@gmail.com>
Fri, 19 Jun 2009 17:03:22 +0000 (17:03 +0000)
committerMilan Broz <gmazyland@gmail.com>
Fri, 19 Jun 2009 17:03:22 +0000 (17:03 +0000)
or is already used by other mapping.

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@51 36d66b0a-2a48-0410-832c-cd162a569da5

lib/setup.c
luks/keymanage.c

index be39f13..81b45ea 100644 (file)
@@ -299,7 +299,7 @@ static int __crypt_create_device(int reload, struct setup_backend *backend,
                        return r;
        } else {
                if (r >= 0) {
-                       set_error("Device already exists");
+                       set_error("Device %s already exists.", options->name);
                        return -EEXIST;
                }
                if (r != -ENODEV)
@@ -440,10 +440,8 @@ static int __crypt_luks_format(int arg, struct setup_backend *backend, struct cr
        int PBKDF2perSecond;
         int keyIndex;
 
-       if (!LUKS_device_ready(options->device, O_RDWR | O_EXCL)) {
-               set_error("Can not access device");
-               r = -ENOTBLK; goto out;
-       }
+       if (!LUKS_device_ready(options->device, O_RDWR | O_EXCL))
+               return -ENOTBLK;
 
        mk = LUKS_generate_masterkey(options->key_size);
        if(NULL == mk) return -ENOMEM; // FIXME This may be misleading, since we don't know what went wrong
@@ -521,14 +519,12 @@ static int __crypt_luks_open(int arg, struct setup_backend *backend, struct cryp
 
        r = backend->status(0, &tmp, NULL);
        if (r >= 0) {
-               set_error("Device already exists");
+               set_error("Device %s already exists.", options->name);
                return -EEXIST;
        }
 
-       if (!LUKS_device_ready(options->device, O_RDONLY | excl)) {
-               set_error("Can not access device");
+       if (!LUKS_device_ready(options->device, O_RDONLY | excl))
                return -ENOTBLK;
-       }
 
        if (get_device_infos(options->device, &infos) < 0) {
                set_error("Can't get device information.\n");
@@ -601,11 +597,9 @@ static int __crypt_luks_add_key(int arg, struct setup_backend *backend, struct c
         unsigned int keyIndex;
        const char *device = options->device;
        int r;
-       
-       if (!LUKS_device_ready(options->device, O_RDWR)) {
-               set_error("Can not access device");
-               r = -ENOTBLK; goto out;
-       }
+
+       if (!LUKS_device_ready(options->device, O_RDWR))
+               return -ENOTBLK;
 
        r = LUKS_read_phdr(device, &hdr);
        if(r < 0) return r;
@@ -671,10 +665,9 @@ static int luks_remove_helper(int arg, struct setup_backend *backend, struct cry
        int keyIndex;
        int openedIndex;
        int r;
-       if (!LUKS_device_ready(options->device, O_RDWR)) {
-           set_error("Can not access device");
-           r = -ENOTBLK; goto out;
-       }
+
+       if (!LUKS_device_ready(options->device, O_RDWR))
+           return -ENOTBLK;
 
        if(supply_it) {
            get_key("Enter LUKS passphrase to be deleted: ",&password,&passwordLen, 0, options->new_key_file, options->passphrase_fd, options->timeout, options->flags);
index f6f8d1f..193a97b 100644 (file)
@@ -487,13 +487,24 @@ int LUKS_benchmarkt_iterations()
 
 int LUKS_device_ready(const char *device, int mode)
 {
-        int devfd = open(device, mode | O_DIRECT | O_SYNC);
-        if(devfd < 0) {
-                set_error(_("Can't open device for %s%saccess: %s\n"), (mode & O_EXCL)?_("exclusive "):"", (mode & O_RDWR)?_("writable "):"read-only ", device);
-                return 0;
-        }
-        close(devfd);
-        return 1;
+       int devfd;
+       struct stat st;
+
+       if(stat(device, &st) < 0) {
+               set_error(_("Device %s doesn't exist or access denied."), device);
+               return 0;
+       }
+
+       devfd = open(device, mode | O_DIRECT | O_SYNC);
+       if(devfd < 0) {
+               set_error(_("Can't open device %s for %s%saccess."), device,
+                         (mode & O_EXCL)?_("exclusive "):"",
+                         (mode & O_RDWR)?_("writable "):"read-only ");
+               return 0;
+       }
+       close(devfd);
+
+       return 1;
 }
 
 // Local Variables: