Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
From: Karel Zak <kzak@redhat.com>
To: Lukas Czerner <lczerner@redhat.com>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH] blkdiscard: Refuse to proceed if signatures are found
Date: Thu, 18 Jun 2020 12:06:26 +0200
Message-ID: <20200618100626.f5uyuxomfhbnhibn@ws.net.home> (raw)
In-Reply-To: <20200618092916.12191-1-lczerner@redhat.com>

On Thu, Jun 18, 2020 at 11:29:16AM +0200, Lukas Czerner wrote:
> With this commit blkdiscard will check for existing signatures on the
> device and refuse to continue if any are found unless the operation is
> forced with the -f option.

Good idea.

> +/*
> + * Check existing signature on the open fd
> + * Returns	0  if no signature was found
> + * 		1  if a signature was found

this is not true, 0 means detected, 1 not found

> + * 		<0 on error
> + */
> +static int device_empty(int fd, char *path)

This is difficult for to read, at first glance it seems according
to function name that 1 means "yes, it's empty".
      
Maybe rename it to probe_device().

> +{
> +	const char *type;
> +	blkid_probe pr = NULL;
> +	int ret = -1;
> +
> +	pr = blkid_new_probe();
> +	if (!pr || blkid_probe_set_device(pr, fd, 0, 0))
> +		return ret;
> +
> +	blkid_probe_enable_superblocks(pr, TRUE);
> +	blkid_probe_enable_partitions(pr, TRUE);
> +
> +	ret = blkid_do_fullprobe(pr);
> +	if (ret)
> +		return ret;

yes, blkid_do_fullprobe() returns: 0 on success, 1 if nothing is detected or -1 on case of error.

> +
> +	if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
> +		warnx("%s contains existing file system (%s).",path ,type);
> +	} else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
> +		warnx("%s contains existing partition (%s).",path ,type);
> +	} else {
> +		warnx("%s contains existing signature.", path);
> +	}
> +
> +	blkid_free_probe(pr);
> +	return ret;

This is always 0.

> +}
>  
>  int main(int argc, char **argv)
>  {
>  	char *path;
> -	int c, fd, verbose = 0, secsize, force = 0;
> +	int c, fd, ret, verbose = 0, secsize, force = 0;
>  	uint64_t end, blksize, step, range[2], stats[2];
>  	struct stat sb;
>  	struct timeval now, last;
> @@ -184,7 +219,7 @@ int main(int argc, char **argv)
>  		errtryhelp(EXIT_FAILURE);
>  	}
>  
> -	fd = open(path, O_WRONLY | (force ? 0 : O_EXCL));
> +	fd = open(path, O_RDWR | (force ? 0 : O_EXCL));
>  	if (fd < 0)
>  		err(EXIT_FAILURE, _("cannot open %s"), path);
>  
> @@ -217,6 +252,21 @@ int main(int argc, char **argv)
>  		errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned "
>  			 "to sector size %i"), path, range[1], secsize);
>  
> +	 /* Check for existing signatures on the device */
> +	if ((ret = device_empty(fd, path)) == 0) {

 What about:

 switch (probe_device(fd, path)) {
 case 0: /* signature detected */
> +		/*
> +		 * Only require force in interactive mode to avoid
> +		 * breaking existing scripts
> +		 */
> +		if (!force && isatty(STDIN_FILENO)) {
> +			errx(EXIT_FAILURE,
> +			     _("This is destructive operation, data will " \
> +			       "be lost! Use the -f option to override."));
> +		}
> +		warnx(_("Operation forced, data will be lost!"));
        break;

 case 1: /* no signature */
        break;

 default: /* error */
> +		err(EXIT_FAILURE, _("failed to probe the device"));
        break;
 }


I think it's more readable ;-)

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


  reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-18  9:29 Lukas Czerner
2020-06-18 10:06 ` Karel Zak [this message]
2020-06-18 10:33   ` Lukas Czerner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200618100626.f5uyuxomfhbnhibn@ws.net.home \
    --to=kzak@redhat.com \
    --cc=lczerner@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org
	public-inbox-index util-linux

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git