Home | History | Annotate | Line # | Download | only in gpt
destroy.c revision 1.2
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  *
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  *
     15  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  christos  */
     26  1.1  christos 
     27  1.1  christos #include <sys/cdefs.h>
     28  1.2  christos #ifdef __FBSDID
     29  1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
     30  1.2  christos #endif
     31  1.2  christos #ifdef __RCSID
     32  1.2  christos __RCSID("$NetBSD: destroy.c,v 1.2 2006/10/15 22:36:29 christos Exp $");
     33  1.2  christos #endif
     34  1.1  christos 
     35  1.1  christos #include <sys/types.h>
     36  1.1  christos 
     37  1.1  christos #include <err.h>
     38  1.1  christos #include <stddef.h>
     39  1.1  christos #include <stdio.h>
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #include <string.h>
     42  1.1  christos #include <unistd.h>
     43  1.1  christos 
     44  1.1  christos #include "map.h"
     45  1.1  christos #include "gpt.h"
     46  1.1  christos 
     47  1.1  christos static int recoverable;
     48  1.1  christos 
     49  1.1  christos static void
     50  1.1  christos usage_destroy(void)
     51  1.1  christos {
     52  1.1  christos 
     53  1.1  christos 	fprintf(stderr,
     54  1.1  christos 	    "usage: %s [-r] device ...\n", getprogname());
     55  1.1  christos 	exit(1);
     56  1.1  christos }
     57  1.1  christos 
     58  1.1  christos static void
     59  1.1  christos destroy(int fd)
     60  1.1  christos {
     61  1.1  christos 	map_t *pri_hdr, *sec_hdr;
     62  1.1  christos 
     63  1.1  christos 	pri_hdr = map_find(MAP_TYPE_PRI_GPT_HDR);
     64  1.1  christos 	sec_hdr = map_find(MAP_TYPE_SEC_GPT_HDR);
     65  1.1  christos 
     66  1.1  christos 	if (pri_hdr == NULL && sec_hdr == NULL) {
     67  1.1  christos 		warnx("%s: error: device doesn't contain a GPT", device_name);
     68  1.1  christos 		return;
     69  1.1  christos 	}
     70  1.1  christos 
     71  1.1  christos 	if (recoverable && sec_hdr == NULL) {
     72  1.1  christos 		warnx("%s: error: recoverability not possible", device_name);
     73  1.1  christos 		return;
     74  1.1  christos 	}
     75  1.1  christos 
     76  1.1  christos 	if (pri_hdr != NULL) {
     77  1.1  christos 		bzero(pri_hdr->map_data, secsz);
     78  1.1  christos 		gpt_write(fd, pri_hdr);
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos 	if (!recoverable && sec_hdr != NULL) {
     82  1.1  christos 		bzero(sec_hdr->map_data, secsz);
     83  1.1  christos 		gpt_write(fd, sec_hdr);
     84  1.1  christos 	}
     85  1.1  christos }
     86  1.1  christos 
     87  1.1  christos int
     88  1.1  christos cmd_destroy(int argc, char *argv[])
     89  1.1  christos {
     90  1.1  christos 	int ch, fd;
     91  1.1  christos 
     92  1.1  christos 	while ((ch = getopt(argc, argv, "r")) != -1) {
     93  1.1  christos 		switch(ch) {
     94  1.1  christos 		case 'r':
     95  1.1  christos 			recoverable = 1;
     96  1.1  christos 			break;
     97  1.1  christos 		default:
     98  1.1  christos 			usage_destroy();
     99  1.1  christos 		}
    100  1.1  christos 	}
    101  1.1  christos 
    102  1.1  christos 	if (argc == optind)
    103  1.1  christos 		usage_destroy();
    104  1.1  christos 
    105  1.1  christos 	while (optind < argc) {
    106  1.1  christos 		fd = gpt_open(argv[optind++]);
    107  1.1  christos 		if (fd == -1) {
    108  1.1  christos 			warn("unable to open device '%s'", device_name);
    109  1.1  christos 			continue;
    110  1.1  christos 		}
    111  1.1  christos 
    112  1.1  christos 		destroy(fd);
    113  1.1  christos 
    114  1.1  christos 		gpt_close(fd);
    115  1.1  christos 	}
    116  1.1  christos 
    117  1.1  christos 	return (0);
    118  1.1  christos }
    119