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

Annotation of src/sbin/gpt/remove.c, Revision 1.18

1.1       christos    1: /*-
                      2:  * Copyright (c) 2004 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.14      christos   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/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
1.2       christos   34: #endif
                     35: #ifdef __RCSID
1.18    ! christos   36: __RCSID("$NetBSD: remove.c,v 1.17 2015/11/29 00:14:46 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.18    ! christos   50: #include "gpt_private.h"
1.1       christos   51:
                     52: static int all;
1.16      christos   53: static gpt_uuid_t type;
1.1       christos   54: static off_t block, size;
                     55: static unsigned int entry;
1.13      jnemeth    56: static uint8_t *label;
1.1       christos   57:
1.18    ! christos   58: const char removemsg1[] = "remove -a";
1.13      jnemeth    59: const char removemsg2[] = "remove [-b blocknr] [-i index] [-L label] "
1.18    ! christos   60:        "[-s sectors] [-t type]";
1.4       riz        61:
1.18    ! christos   62: static int
1.1       christos   63: usage_remove(void)
                     64: {
                     65:
                     66:        fprintf(stderr,
1.4       riz        67:            "usage: %s %s\n"
                     68:            "       %s %s\n",
                     69:            getprogname(), removemsg1, getprogname(), removemsg2);
1.18    ! christos   70:        return -1;
1.1       christos   71: }
                     72:
1.18    ! christos   73: static int
        !            74: rem(gpt_t gpt)
1.1       christos   75: {
1.18    ! christos   76:        map_t m;
1.1       christos   77:        struct gpt_ent *ent;
                     78:        unsigned int i;
                     79:
1.18    ! christos   80:        if (gpt_hdr(gpt) == NULL)
        !            81:                return -1;
1.1       christos   82:
                     83:        /* Remove all matching entries in the map. */
1.18    ! christos   84:        for (m = map_first(gpt); m != NULL; m = m->map_next) {
1.1       christos   85:                if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
                     86:                        continue;
                     87:                if (entry > 0 && entry != m->map_index)
                     88:                        continue;
                     89:                if (block > 0 && block != m->map_start)
                     90:                        continue;
                     91:                if (size > 0 && size != m->map_size)
                     92:                        continue;
                     93:
                     94:                i = m->map_index - 1;
                     95:
1.18    ! christos   96:                ent = gpt_ent_primary(gpt, i);
1.13      jnemeth    97:
                     98:                if (label != NULL)
                     99:                        if (strcmp((char *)label,
                    100:                            (char *)utf16_to_utf8(ent->ent_name)) != 0)
                    101:                                continue;
                    102:
1.16      christos  103:                if (!gpt_uuid_is_nil(type) &&
                    104:                    !gpt_uuid_equal(type, ent->ent_type))
1.1       christos  105:                        continue;
                    106:
                    107:                /* Remove the primary entry by clearing the partition type. */
1.16      christos  108:                gpt_uuid_copy(ent->ent_type, gpt_uuid_nil);
1.1       christos  109:
1.18    ! christos  110:                if (gpt_write_primary(gpt) == -1)
        !           111:                        return -1;
        !           112:
        !           113:                ent = gpt_ent_backup(gpt, i);
1.1       christos  114:
1.7       jakllsch  115:                /* Remove the secondary entry. */
1.16      christos  116:                gpt_uuid_copy(ent->ent_type, gpt_uuid_nil);
1.1       christos  117:
1.18    ! christos  118:                if (gpt_write_backup(gpt) == -1)
        !           119:                        return -1;
        !           120:                gpt_msg(gpt, "partition %d removed", m->map_index);
1.1       christos  121:        }
1.18    ! christos  122:        return 0;
1.1       christos  123: }
                    124:
                    125: int
1.18    ! christos  126: cmd_remove(gpt_t gpt, int argc, char *argv[])
1.1       christos  127: {
                    128:        char *p;
1.18    ! christos  129:        int ch;
1.10      jnemeth   130:        int64_t human_num;
1.1       christos  131:
                    132:        /* Get the remove options */
1.13      jnemeth   133:        while ((ch = getopt(argc, argv, "ab:i:L:s:t:")) != -1) {
1.1       christos  134:                switch(ch) {
                    135:                case 'a':
                    136:                        if (all > 0)
1.18    ! christos  137:                                return usage_remove();
1.1       christos  138:                        all = 1;
                    139:                        break;
                    140:                case 'b':
                    141:                        if (block > 0)
1.18    ! christos  142:                                return usage_remove();
1.10      jnemeth   143:                        if (dehumanize_number(optarg, &human_num) < 0)
1.18    ! christos  144:                                return usage_remove();
1.10      jnemeth   145:                        block = human_num;
1.12      jnemeth   146:                        if (block < 1)
1.18    ! christos  147:                                return usage_remove();
1.1       christos  148:                        break;
                    149:                case 'i':
                    150:                        if (entry > 0)
1.18    ! christos  151:                                return usage_remove();
1.3       riz       152:                        entry = strtoul(optarg, &p, 10);
1.1       christos  153:                        if (*p != 0 || entry < 1)
1.18    ! christos  154:                                return usage_remove();
1.1       christos  155:                        break;
1.13      jnemeth   156:                case 'L':
                    157:                        if (label != NULL)
1.18    ! christos  158:                                return usage_remove();
1.13      jnemeth   159:                        label = (uint8_t *)strdup(optarg);
                    160:                        break;
1.1       christos  161:                case 's':
                    162:                        if (size > 0)
                    163:                                usage_remove();
                    164:                        size = strtoll(optarg, &p, 10);
                    165:                        if (*p != 0 || size < 1)
1.18    ! christos  166:                                return usage_remove();
1.1       christos  167:                        break;
                    168:                case 't':
1.16      christos  169:                        if (!gpt_uuid_is_nil(type))
1.18    ! christos  170:                                return usage_remove();
1.16      christos  171:                        if (gpt_uuid_parse(optarg, type) != 0)
1.18    ! christos  172:                                return usage_remove();
1.1       christos  173:                        break;
                    174:                default:
1.18    ! christos  175:                        return usage_remove();
1.1       christos  176:                }
                    177:        }
                    178:
                    179:        if (!all ^
1.13      jnemeth   180:            (block > 0 || entry > 0 || label != NULL || size > 0 ||
1.16      christos  181:            !gpt_uuid_is_nil(type)))
1.18    ! christos  182:                return usage_remove();
1.1       christos  183:
1.18    ! christos  184:        if (argc != optind)
        !           185:                return usage_remove();
1.1       christos  186:
1.18    ! christos  187:        return rem(gpt);
1.1       christos  188: }

CVSweb <webmaster@jp.NetBSD.org>