source: mainline/uspace/lib/fdisk/src/cap.c@ 2dab624

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2dab624 was 2dab624, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Fix parsing capacity without decimal separator as zero. Fix non-fs partition content display when selecting partition to delete. Fix capacity of new partition. Fix getting partition content, from volsrv.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2015 Jiri Svoboda
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 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libfdisk
30 * @{
31 */
32/**
33 * @file Disk management library.
34 */
35
36#include <errno.h>
37#include <fdisk.h>
38#include <imath.h>
39#include <stdio.h>
40#include <str.h>
41
42/** Simplified capacity parameters */
43enum {
44 /** Simplified capacity maximum integer digits */
45 scap_max_idig = 3,
46 /** Simplified capacity maximum significant digits */
47 scap_max_sdig = 4
48};
49
50static const char *cu_str[] = {
51 [cu_byte] = "B",
52 [cu_kbyte] = "kB",
53 [cu_mbyte] = "MB",
54 [cu_gbyte] = "GB",
55 [cu_tbyte] = "TB",
56 [cu_pbyte] = "PB",
57 [cu_ebyte] = "EB",
58 [cu_zbyte] = "ZB",
59 [cu_ybyte] = "YB"
60};
61
62void fdisk_cap_from_blocks(uint64_t nblocks, size_t block_size,
63 fdisk_cap_t *cap)
64{
65 uint64_t tsize;
66
67 tsize = nblocks * block_size;
68 cap->m = tsize;
69 cap->dp = 0;
70 cap->cunit = cu_byte;
71}
72
73/** Convert capacity to blocks.
74 *
75 * If the value of bytes is not integer, it is properly rounded. If the number
76 * of bytes is not divisible by the number of blocks, it is rounded
77 * up to an integer number of blocks.
78 *
79 * A capacity value entails precision, i.e. it corresponds to a range
80 * of values. @a cvsel selects the value to return. @c fcv_nom gives
81 * the nominal (middle) value, @c fcv_min gives the minimum value
82 * and @c fcv_max gives the maximum value.
83 */
84int fdisk_cap_to_blocks(fdisk_cap_t *cap, fdisk_cvsel_t cvsel,
85 size_t block_size, uint64_t *rblocks)
86{
87 int exp;
88 uint64_t bytes;
89 uint64_t f;
90 uint64_t adj;
91 uint64_t blocks;
92 uint64_t rem;
93 int rc;
94
95 exp = cap->cunit * 3 - cap->dp;
96 if (exp < 0) {
97 rc = ipow10_u64(-exp, &f);
98 if (rc != EOK)
99 return ERANGE;
100 bytes = (cap->m + (f / 2)) / f;
101 if (bytes * f - (f / 2) != cap->m)
102 return ERANGE;
103 } else {
104 rc = ipow10_u64(exp, &f);
105 if (rc != EOK)
106 return ERANGE;
107
108 adj = 0;
109 switch (cvsel) {
110 case fcv_nom:
111 adj = 0;
112 break;
113 case fcv_min:
114 adj = -(f / 2);
115 break;
116 case fcv_max:
117 adj = f / 2 - 1;
118 break;
119 }
120
121 bytes = cap->m * f + adj;
122 if ((bytes - adj) / f != cap->m)
123 return ERANGE;
124 }
125
126 rem = bytes % block_size;
127 if ((bytes + rem) < bytes)
128 return ERANGE;
129
130 blocks = (bytes + rem) / block_size;
131
132 *rblocks = blocks;
133 return EOK;
134}
135
136/** Simplify and round capacity to a human-friendly form.
137 *
138 * Change unit and round the number so that we have at most three integer
139 * digits and at most two fractional digits, e.g abc.xy <unit>.
140 */
141void fdisk_cap_simplify(fdisk_cap_t *cap)
142{
143 uint64_t div;
144 uint64_t maxv;
145 unsigned sdig;
146 unsigned rdig;
147 int rc;
148
149 /* Change units so that we have at most @c scap_max_idig integer digits */
150 rc = ipow10_u64(scap_max_idig, &maxv);
151 assert(rc == EOK);
152
153 rc = ipow10_u64(cap->dp, &div);
154 assert(rc == EOK);
155
156 while (cap->m / div >= maxv) {
157 ++cap->cunit;
158 cap->dp += 3;
159 div = div * 1000;
160 }
161
162 /* Round the number so that we have at most @c scap_max_sdig significant digits */
163 sdig = 1 + ilog10_u64(cap->m); /* number of significant digits */
164 if (sdig > scap_max_sdig) {
165 /* Number of digits to remove */
166 rdig = sdig - scap_max_sdig;
167 if (rdig > cap->dp)
168 rdig = cap->dp;
169
170 rc = ipow10_u64(rdig, &div);
171 assert(rc == EOK);
172
173 cap->m = (cap->m + (div / 2)) / div;
174 cap->dp -= rdig;
175 }
176}
177
178int fdisk_cap_format(fdisk_cap_t *cap, char **rstr)
179{
180 int rc;
181 const char *sunit;
182 uint64_t ipart;
183 uint64_t fpart;
184 uint64_t div;
185
186 sunit = NULL;
187
188 if (cap->cunit < 0 || cap->cunit >= CU_LIMIT)
189 assert(false);
190
191 rc = ipow10_u64(cap->dp, &div);
192 if (rc != EOK)
193 return rc;
194
195 ipart = cap->m / div;
196 fpart = cap->m % div;
197
198 sunit = cu_str[cap->cunit];
199 if (cap->dp > 0) {
200 rc = asprintf(rstr, "%" PRIu64 ".%0*" PRIu64 " %s", ipart,
201 (int)cap->dp, fpart, sunit);
202 } else {
203 rc = asprintf(rstr, "%" PRIu64 " %s", ipart, sunit);
204 }
205 if (rc < 0)
206 return ENOMEM;
207
208 return EOK;
209}
210
211static int fdisk_digit_val(char c, int *val)
212{
213 switch (c) {
214 case '0': *val = 0; break;
215 case '1': *val = 1; break;
216 case '2': *val = 2; break;
217 case '3': *val = 3; break;
218 case '4': *val = 4; break;
219 case '5': *val = 5; break;
220 case '6': *val = 6; break;
221 case '7': *val = 7; break;
222 case '8': *val = 8; break;
223 case '9': *val = 9; break;
224 default:
225 return EINVAL;
226 }
227
228 return EOK;
229}
230
231int fdisk_cap_parse(const char *str, fdisk_cap_t *cap)
232{
233 const char *eptr;
234 const char *p;
235 int d;
236 int dp;
237 unsigned long m;
238 int i;
239
240 m = 0;
241
242 eptr = str;
243 while (fdisk_digit_val(*eptr, &d) == EOK) {
244 m = m * 10 + d;
245 ++eptr;
246 }
247
248 if (*eptr == '.') {
249 ++eptr;
250 dp = 0;
251 while (fdisk_digit_val(*eptr, &d) == EOK) {
252 m = m * 10 + d;
253 ++dp;
254 ++eptr;
255 }
256 } else {
257 dp = 0;
258 }
259
260 while (*eptr == ' ')
261 ++eptr;
262
263 if (*eptr == '\0') {
264 cap->cunit = cu_byte;
265 } else {
266 for (i = 0; i < CU_LIMIT; i++) {
267 if (str_lcasecmp(eptr, cu_str[i],
268 str_length(cu_str[i])) == 0) {
269 p = eptr + str_size(cu_str[i]);
270 while (*p == ' ')
271 ++p;
272 if (*p == '\0')
273 goto found;
274 }
275 }
276
277 return EINVAL;
278found:
279 cap->cunit = i;
280 }
281
282 cap->m = m;
283 cap->dp = dp;
284 return EOK;
285}
286
287/** @}
288 */
Note: See TracBrowser for help on using the repository browser.