From: Milan Broz Date: Fri, 19 Jun 2009 17:03:22 +0000 (+0000) Subject: Use better error messages if device doesn't exist X-Git-Tag: upstream/1.6~769 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=91f1e7b59ab112034aeaccbf6a596b8fffe94cb7;p=platform%2Fupstream%2Fcryptsetup.git Use better error messages if device doesn't exist or is already used by other mapping. git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@51 36d66b0a-2a48-0410-832c-cd162a569da5 --- diff --git a/lib/setup.c b/lib/setup.c index be39f13..81b45ea 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -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); diff --git a/luks/keymanage.c b/luks/keymanage.c index f6f8d1f..193a97b 100644 --- a/luks/keymanage.c +++ b/luks/keymanage.c @@ -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: