Sun Nov 29 00:15:12 2015 UTC ()
- use snprintf
- fix bogus logic on map_type


(christos)
diff -r1.7 -r1.8 src/sbin/gpt/map.c

cvs diff -r1.7 -r1.8 src/sbin/gpt/map.c (expand / switch to unified diff)

--- src/sbin/gpt/map.c 2014/09/29 20:28:57 1.7
+++ src/sbin/gpt/map.c 2015/11/29 00:15:12 1.8
@@ -23,93 +23,103 @@ @@ -23,93 +23,103 @@
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 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. 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27#if HAVE_NBTOOL_CONFIG_H 27#if HAVE_NBTOOL_CONFIG_H
28#include "nbtool_config.h" 28#include "nbtool_config.h"
29#endif 29#endif
30 30
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32#ifdef __FBSDID 32#ifdef __FBSDID
33__FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $"); 33__FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
34#endif 34#endif
35#ifdef __RCSID 35#ifdef __RCSID
36__RCSID("$NetBSD: map.c,v 1.7 2014/09/29 20:28:57 christos Exp $"); 36__RCSID("$NetBSD: map.c,v 1.8 2015/11/29 00:15:12 christos Exp $");
37#endif 37#endif
38 38
39#include <sys/types.h> 39#include <sys/types.h>
40#include <err.h> 40#include <err.h>
41#include <stdio.h> 41#include <stdio.h>
42#include <stdlib.h> 42#include <stdlib.h>
43 43
44#include "map.h" 44#include "map.h"
 45#include "gpt.h"
45 46
46int lbawidth; 47int lbawidth;
47 48
48static map_t *mediamap; 49static map_t *mediamap;
49 50
50static map_t * 51static map_t *
51mkmap(off_t start, off_t size, int type) 52mkmap(off_t start, off_t size, int type)
52{ 53{
53 map_t *m; 54 map_t *m;
54 55
55 m = malloc(sizeof(*m)); 56 m = calloc(1, sizeof(*m));
56 if (m == NULL) 57 if (m == NULL)
57 return (NULL); 58 return (NULL);
58 m->map_start = start; 59 m->map_start = start;
59 m->map_size = size; 60 m->map_size = size;
60 m->map_next = m->map_prev = NULL; 61 m->map_next = m->map_prev = NULL;
61 m->map_type = type; 62 m->map_type = type;
62 m->map_index = 0; 63 m->map_index = 0;
63 m->map_data = NULL; 64 m->map_data = NULL;
64 return (m); 65 return (m);
65} 66}
66 67
67map_t * 68map_t *
68map_add(off_t start, off_t size, int type, void *data) 69map_add(off_t start, off_t size, int type, void *data)
69{ 70{
70 map_t *m, *n, *p; 71 map_t *m, *n, *p;
71 72
72 n = mediamap; 73 n = mediamap;
73 while (n != NULL && n->map_start + n->map_size <= start) 74 while (n != NULL && n->map_start + n->map_size <= start)
74 n = n->map_next; 75 n = n->map_next;
75 if (n == NULL) 76 if (n == NULL) {
 77 if (!quiet)
 78 warnx("Can't find map");
76 return (NULL); 79 return (NULL);
 80 }
77 81
78 if (n->map_start + n->map_size < start + size) { 82 if (n->map_start + n->map_size < start + size) {
79 warnx("error: map entry doesn't fit media"); 83 if (!quiet)
 84 warnx("map entry doesn't fit media");
80 return (NULL); 85 return (NULL);
81 } 86 }
82 87
83 if (n->map_start == start && n->map_size == size) { 88 if (n->map_start == start && n->map_size == size) {
84 if (n->map_type != MAP_TYPE_UNUSED) { 89 if (n->map_type != MAP_TYPE_UNUSED) {
85 if (n->map_type != MAP_TYPE_MBR_PART || 90 if (n->map_type != MAP_TYPE_MBR_PART ||
86 type != MAP_TYPE_GPT_PART) { 91 type != MAP_TYPE_GPT_PART) {
87 warnx("warning: partition(%llu,%llu) mirrored", 92 if (!quiet)
88 (long long)start, (long long)size); 93 warnx("partition(%ju,%ju) mirrored",
 94 (uintmax_t)start, (uintmax_t)size);
89 } 95 }
90 } 96 }
91 n->map_type = type; 97 n->map_type = type;
92 n->map_data = data; 98 n->map_data = data;
93 return (n); 99 return (n);
94 } 100 }
95 101
96 if (n->map_type != MAP_TYPE_UNUSED) { 102
97 if (n->map_type != MAP_TYPE_MBR_PART || 103 switch (n->map_type) {
98 type != MAP_TYPE_GPT_PART) { 104 case MAP_TYPE_MBR_PART:
99 warnx("error: bogus map"); 105 case MAP_TYPE_GPT_PART:
100 return (0); 
101 } 
102 n->map_type = MAP_TYPE_UNUSED; 106 n->map_type = MAP_TYPE_UNUSED;
 107 break;
 108 case MAP_TYPE_UNUSED:
 109 break;
 110 default:
 111 warnx("bogus map %#x", n->map_type);
 112 return (NULL);
103 } 113 }
104 114
105 m = mkmap(start, size, type); 115 m = mkmap(start, size, type);
106 if (m == NULL) 116 if (m == NULL)
107 return (NULL); 117 return (NULL);
108 118
109 m->map_data = data; 119 m->map_data = data;
110 120
111 if (start == n->map_start) { 121 if (start == n->map_start) {
112 m->map_prev = n->map_prev; 122 m->map_prev = n->map_prev;
113 m->map_next = n; 123 m->map_next = n;
114 if (m->map_prev != NULL) 124 if (m->map_prev != NULL)
115 m->map_prev->map_next = m; 125 m->map_prev->map_next = m;
@@ -324,17 +334,17 @@ map_free(off_t start, off_t size) @@ -324,17 +334,17 @@ map_free(off_t start, off_t size)
324 if (m == NULL || m->map_type != MAP_TYPE_UNUSED) 334 if (m == NULL || m->map_type != MAP_TYPE_UNUSED)
325 return (0LL); 335 return (0LL);
326 if (size) 336 if (size)
327 return ((m->map_start + m->map_size >= start + size) ? 1 : 0); 337 return ((m->map_start + m->map_size >= start + size) ? 1 : 0);
328 return (m->map_size - (start - m->map_start)); 338 return (m->map_size - (start - m->map_start));
329} 339}
330 340
331void 341void
332map_init(off_t size) 342map_init(off_t size)
333{ 343{
334 char buf[32]; 344 char buf[32];
335 345
336 mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED); 346 mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED);
337 lbawidth = sprintf(buf, "%llu", (long long)size); 347 lbawidth = snprintf(buf, sizeof(buf), "%ju", (uintmax_t)size);
338 if (lbawidth < 5) 348 if (lbawidth < 5)
339 lbawidth = 5; 349 lbawidth = 5;
340} 350}