[BACK]Return to destroy.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / gpt

Annotation of src/sbin/gpt/destroy.c, Revision 1.4.20.2

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

CVSweb <webmaster@jp.NetBSD.org>