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