| 1 |
/* |
| 2 |
* Copyright (C) 2017 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vratislav Podzimek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <blockdev/utils.h> |
| 21 |
#include <check_deps.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
#include "ntfs.h" |
| 26 |
#include "fs.h" |
| 27 |
#include "common.h" |
| 28 |
|
| 29 |
static volatile guint avail_deps = 0; |
| 30 |
static GMutex deps_check_lock; |
| 31 |
|
| 32 |
G_GNUC_INTERNAL |
| 33 |
void _fs_ntfs_reset_avail_deps (void) { |
| 34 |
g_atomic_int_set (&avail_deps, 0); |
| 35 |
} |
| 36 |
|
| 37 |
#define DEPS_MKNTFS 0 |
| 38 |
#define DEPS_MKNTFS_MASK (1 << DEPS_MKNTFS) |
| 39 |
#define DEPS_NTFSFIX 1 |
| 40 |
#define DEPS_NTFSFIX_MASK (1 << DEPS_NTFSFIX) |
| 41 |
#define DEPS_NTFSRESIZE 2 |
| 42 |
#define DEPS_NTFSRESIZE_MASK (1 << DEPS_NTFSRESIZE) |
| 43 |
#define DEPS_NTFSLABEL 3 |
| 44 |
#define DEPS_NTFSLABEL_MASK (1 << DEPS_NTFSLABEL) |
| 45 |
#define DEPS_NTFSINFO 4 |
| 46 |
#define DEPS_NTFSINFO_MASK (1 << DEPS_NTFSINFO) |
| 47 |
|
| 48 |
#define DEPS_LAST 5 |
| 49 |
|
| 50 |
static const UtilDep deps[DEPS_LAST] = { |
| 51 |
{"mkntfs", NULL, NULL, NULL}, |
| 52 |
{"ntfsfix", NULL, NULL, NULL}, |
| 53 |
{"ntfsresize", NULL, NULL, NULL}, |
| 54 |
{"ntfslabel", NULL, NULL, NULL}, |
| 55 |
{"ntfsinfo", NULL, NULL, NULL}, |
| 56 |
}; |
| 57 |
|
| 58 |
static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = { |
| 59 |
DEPS_MKNTFS_MASK, /* mkfs */ |
| 60 |
0, /* wipe */ |
| 61 |
DEPS_NTFSFIX_MASK, /* check */ |
| 62 |
DEPS_NTFSFIX_MASK, /* repair */ |
| 63 |
DEPS_NTFSLABEL_MASK, /* set-label */ |
| 64 |
DEPS_NTFSINFO_MASK, /* query */ |
| 65 |
DEPS_NTFSRESIZE_MASK, /* resize */ |
| 66 |
DEPS_NTFSLABEL_MASK /* set-uuid */ |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* bd_fs_ntfs_is_tech_avail: |
| 72 |
* @tech: the queried tech |
| 73 |
* @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech |
| 74 |
* @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available) |
| 75 |
* |
| 76 |
* Returns: whether the @tech-@mode combination is available -- supported by the |
| 77 |
* plugin implementation and having all the runtime dependencies available |
| 78 |
*/ |
| 79 |
G_GNUC_INTERNAL gboolean |
| 80 |
bd_fs_ntfs_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) { |
| 81 |
guint32 required = 0; |
| 82 |
guint i = 0; |
| 83 |
for (i = 0; i <= BD_FS_MODE_LAST; i++) |
| 84 |
if (mode & (1 << i)) |
| 85 |
required |= fs_mode_util[i]; |
| 86 |
|
| 87 |
return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* bd_fs_ntfs_info_copy: (skip) |
| 92 |
* @data: (nullable): %BDFSNtfsInfo to copy |
| 93 |
* |
| 94 |
* Creates a new copy of @data. |
| 95 |
*/ |
| 96 |
BDFSNtfsInfo* bd_fs_ntfs_info_copy (BDFSNtfsInfo *data) { |
| 97 |
if (data == NULL) |
| 98 |
return NULL; |
| 99 |
|
| 100 |
BDFSNtfsInfo *ret = g_new0 (BDFSNtfsInfo, 1); |
| 101 |
|
| 102 |
ret->label = g_strdup (data->label); |
| 103 |
ret->uuid = g_strdup (data->uuid); |
| 104 |
ret->size = data->size; |
| 105 |
ret->free_space = data->free_space; |
| 106 |
|
| 107 |
return ret; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* bd_fs_ntfs_info_free: (skip) |
| 112 |
* @data: (nullable): %BDFSNtfsInfo to free |
| 113 |
* |
| 114 |
* Frees @data. |
| 115 |
*/ |
| 116 |
void bd_fs_ntfs_info_free (BDFSNtfsInfo *data) { |
| 117 |
if (data == NULL) |
| 118 |
return; |
| 119 |
|
| 120 |
g_free (data->label); |
| 121 |
g_free (data->uuid); |
| 122 |
g_free (data); |
| 123 |
} |
| 124 |
|
| 125 |
G_GNUC_INTERNAL BDExtraArg ** |
| 126 |
bd_fs_ntfs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) { |
| 127 |
GPtrArray *options_array = g_ptr_array_new (); |
| 128 |
const BDExtraArg **extra_p = NULL; |
| 129 |
|
| 130 |
if (options->label && g_strcmp0 (options->label, "") != 0) |
| 131 |
g_ptr_array_add (options_array, bd_extra_arg_new ("-L", options->label)); |
| 132 |
|
| 133 |
if (options->dry_run) |
| 134 |
g_ptr_array_add (options_array, bd_extra_arg_new ("-n", "")); |
| 135 |
|
| 136 |
if (extra) { |
| 137 |
for (extra_p = extra; *extra_p; extra_p++) |
| 138 |
g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p)); |
| 139 |
} |
| 140 |
|
| 141 |
g_ptr_array_add (options_array, NULL); |
| 142 |
|
| 143 |
return (BDExtraArg **) g_ptr_array_free (options_array, FALSE); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* bd_fs_ntfs_mkfs: |
| 148 |
* @device: the device to create a new ntfs fs on |
| 149 |
* @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now |
| 150 |
* passed to the 'mkntfs' utility) |
| 151 |
* @error: (out) (optional): place to store error (if any) |
| 152 |
* |
| 153 |
* Returns: whether a new NTFS fs was successfully created on @device or not |
| 154 |
* |
| 155 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_MKFS |
| 156 |
*/ |
| 157 |
gboolean bd_fs_ntfs_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) { |
| 158 |
const gchar *args[5] = {"mkntfs", "-f", "-F", device, NULL}; |
| 159 |
|
| 160 |
if (!check_deps (&avail_deps, DEPS_MKNTFS_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 161 |
return FALSE; |
| 162 |
|
| 163 |
return bd_utils_exec_and_report_error (args, extra, error); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* bd_fs_ntfs_check: |
| 168 |
* @device: the device containing the file system to check |
| 169 |
* @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now |
| 170 |
* passed to the 'ntfsfix' utility) |
| 171 |
* @error: (out) (optional): place to store error (if any) |
| 172 |
* |
| 173 |
* Returns: whether an ntfs file system on the @device is clean or not |
| 174 |
* |
| 175 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_CHECK |
| 176 |
*/ |
| 177 |
gboolean bd_fs_ntfs_check (const gchar *device, const BDExtraArg **extra, GError **error) { |
| 178 |
const gchar *args[4] = {"ntfsfix", "-n", device, NULL}; |
| 179 |
gint status = 0; |
| 180 |
gboolean ret = FALSE; |
| 181 |
|
| 182 |
if (!check_deps (&avail_deps, DEPS_NTFSFIX_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 183 |
return FALSE; |
| 184 |
|
| 185 |
ret = bd_utils_exec_and_report_status_error (args, extra, &status, error); |
| 186 |
if (!ret && (status == 1)) { |
| 187 |
/* no error should be reported for exit code 1 -- Recoverable errors have been detected */ |
| 188 |
g_clear_error (error); |
| 189 |
} |
| 190 |
return ret; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* bd_fs_ntfs_repair: |
| 195 |
* @device: the device containing the file system to repair |
| 196 |
* @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now |
| 197 |
* passed to the 'ntfsfix' utility) |
| 198 |
* @error: (out) (optional): place to store error (if any) |
| 199 |
* |
| 200 |
* Returns: whether an NTFS file system on the @device was successfully repaired |
| 201 |
* (if needed) or not (error is set in that case) |
| 202 |
* |
| 203 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_REPAIR |
| 204 |
*/ |
| 205 |
gboolean bd_fs_ntfs_repair (const gchar *device, const BDExtraArg **extra, GError **error) { |
| 206 |
const gchar *args[4] = {"ntfsfix", "-d", device, NULL}; |
| 207 |
|
| 208 |
if (!check_deps (&avail_deps, DEPS_NTFSFIX_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 209 |
return FALSE; |
| 210 |
|
| 211 |
return bd_utils_exec_and_report_error (args, extra, error); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* bd_fs_ntfs_set_label: |
| 216 |
* @device: the device containing the file system to set the label for |
| 217 |
* @label: label to set |
| 218 |
* @error: (out) (optional): place to store error (if any) |
| 219 |
* |
| 220 |
* Returns: whether the label of the NTFS file system on the @device was |
| 221 |
* successfully set or not |
| 222 |
* |
| 223 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_SET_LABEL |
| 224 |
*/ |
| 225 |
gboolean bd_fs_ntfs_set_label (const gchar *device, const gchar *label, GError **error) { |
| 226 |
const gchar *args[4] = {"ntfslabel", device, label, NULL}; |
| 227 |
|
| 228 |
if (!check_deps (&avail_deps, DEPS_NTFSLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 229 |
return FALSE; |
| 230 |
|
| 231 |
return bd_utils_exec_and_report_error (args, NULL, error); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* bd_fs_ntfs_check_label: |
| 236 |
* @label: label to check |
| 237 |
* @error: (out) (optional): place to store error |
| 238 |
* |
| 239 |
* Returns: whether @label is a valid label for the ntfs file system or not |
| 240 |
* (reason is provided in @error) |
| 241 |
* |
| 242 |
* Tech category: always available |
| 243 |
*/ |
| 244 |
gboolean bd_fs_ntfs_check_label (const gchar *label, GError **error) { |
| 245 |
if (strlen (label) > 128) { |
| 246 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID, |
| 247 |
"Label for NTFS filesystem must be at most 128 characters long."); |
| 248 |
return FALSE; |
| 249 |
} |
| 250 |
|
| 251 |
return TRUE; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* bd_fs_ntfs_set_uuid: |
| 256 |
* @device: the device containing the file system to set the UUID (serial number) for |
| 257 |
* @uuid: (nullable): UUID to set or %NULL to generate a new one |
| 258 |
* @error: (out) (optional): place to store error (if any) |
| 259 |
* |
| 260 |
* Returns: whether the UUID of the NTFS file system on the @device was |
| 261 |
* successfully set or not |
| 262 |
* |
| 263 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_SET_UUID |
| 264 |
*/ |
| 265 |
gboolean bd_fs_ntfs_set_uuid (const gchar *device, const gchar *uuid, GError **error) { |
| 266 |
gboolean ret = FALSE; |
| 267 |
const gchar *args[4] = {"ntfslabel", device, NULL, NULL}; |
| 268 |
|
| 269 |
if (!check_deps (&avail_deps, DEPS_NTFSLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 270 |
return FALSE; |
| 271 |
|
| 272 |
if (!uuid) |
| 273 |
args[2] = g_strdup ("--new-serial"); |
| 274 |
else if (strlen (uuid) == 16) |
| 275 |
args[2] = g_strdup_printf ("--new-serial=%s", uuid); |
| 276 |
else if (strlen (uuid) == 8) |
| 277 |
args[2] = g_strdup_printf ("--new-half-serial=%s", uuid); |
| 278 |
else { |
| 279 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 280 |
"Invalid format of UUID/serial number for NTFS filesystem."); |
| 281 |
return FALSE; |
| 282 |
} |
| 283 |
|
| 284 |
ret = bd_utils_exec_and_report_error (args, NULL, error); |
| 285 |
|
| 286 |
g_free ((gchar *) args[2]); |
| 287 |
return ret; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* bd_fs_ntfs_check_uuid: |
| 292 |
* @uuid: UUID to check |
| 293 |
* @error: (out) (optional): place to store error |
| 294 |
* |
| 295 |
* Returns: whether @uuid is a valid UUID for the ntfs file system or not |
| 296 |
* (reason is provided in @error) |
| 297 |
* |
| 298 |
* Tech category: always available |
| 299 |
*/ |
| 300 |
gboolean bd_fs_ntfs_check_uuid (const gchar *uuid, GError **error) { |
| 301 |
size_t len = 0; |
| 302 |
|
| 303 |
len = strlen (uuid); |
| 304 |
if (len != 8 && len != 16) { |
| 305 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID, |
| 306 |
"UUID for NTFS filesystem must be either 8 or 16 characters long."); |
| 307 |
return FALSE; |
| 308 |
} |
| 309 |
|
| 310 |
for (size_t i = 0; i < len; i++) { |
| 311 |
if (!g_ascii_isxdigit (uuid[i])) { |
| 312 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID, |
| 313 |
"UUID for NTFS filesystem must be a hexadecimal number."); |
| 314 |
return FALSE; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
return TRUE; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/** |
| 323 |
* bd_fs_ntfs_resize: |
| 324 |
* @device: the device the file system of which to resize |
| 325 |
* @new_size: new requested size for the file system in bytes (if 0, the file system |
| 326 |
* is adapted to the underlying block device) |
| 327 |
* @error: (out) (optional): place to store error (if any) |
| 328 |
* |
| 329 |
* Returns: whether the file system on @device was successfully resized or not |
| 330 |
* |
| 331 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_RESIZE |
| 332 |
*/ |
| 333 |
gboolean bd_fs_ntfs_resize (const gchar *device, guint64 new_size, GError **error) { |
| 334 |
const gchar *args[6] = {"ntfsresize", "--no-progress-bar", NULL, NULL, NULL, NULL}; |
| 335 |
gboolean ret = FALSE; |
| 336 |
|
| 337 |
if (!check_deps (&avail_deps, DEPS_NTFSRESIZE_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 338 |
return FALSE; |
| 339 |
|
| 340 |
if (new_size != 0) { |
| 341 |
args[2] = "-s"; |
| 342 |
args[3] = g_strdup_printf ("%"G_GUINT64_FORMAT, new_size); |
| 343 |
args[4] = device; |
| 344 |
} else { |
| 345 |
args[2] = device; |
| 346 |
} |
| 347 |
ret = bd_utils_exec_and_report_error (args, NULL, error); |
| 348 |
|
| 349 |
g_free ((gchar *) args[3]); |
| 350 |
return ret; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* bd_fs_ntfs_get_info: |
| 355 |
* @device: the device containing the file system to get info for (device must |
| 356 |
not be mounted, trying to get info for a mounted device will result |
| 357 |
in an error) |
| 358 |
* @error: (out) (optional): place to store error (if any) |
| 359 |
* |
| 360 |
* Returns: (transfer full): information about the file system on @device or |
| 361 |
* %NULL in case of error |
| 362 |
* |
| 363 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_QUERY |
| 364 |
*/ |
| 365 |
BDFSNtfsInfo* bd_fs_ntfs_get_info (const gchar *device, GError **error) { |
| 366 |
const gchar *args[4] = {"ntfsinfo", "-m", device, NULL}; |
| 367 |
gboolean success = FALSE; |
| 368 |
gchar *output = NULL; |
| 369 |
BDFSNtfsInfo *ret = NULL; |
| 370 |
gchar **lines = NULL; |
| 371 |
gchar **line_p = NULL; |
| 372 |
gchar *val_start = NULL; |
| 373 |
g_autofree gchar* mountpoint = NULL; |
| 374 |
GError *l_error = NULL; |
| 375 |
size_t cluster_size = 0; |
| 376 |
|
| 377 |
if (!check_deps (&avail_deps, DEPS_NTFSINFO_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 378 |
return NULL; |
| 379 |
|
| 380 |
mountpoint = bd_fs_get_mountpoint (device, &l_error); |
| 381 |
if (mountpoint != NULL) { |
| 382 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 383 |
"Can't get NTFS file system information for '%s': Device is mounted.", device); |
| 384 |
return NULL; |
| 385 |
} else { |
| 386 |
if (l_error != NULL) { |
| 387 |
g_propagate_prefixed_error (error, l_error, "Error when trying to get mountpoint for '%s': ", device); |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
ret = g_new0 (BDFSNtfsInfo, 1); |
| 393 |
|
| 394 |
success = get_uuid_label (device, &(ret->uuid), &(ret->label), error); |
| 395 |
if (!success) { |
| 396 |
/* error is already populated */ |
| 397 |
bd_fs_ntfs_info_free (ret); |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
success = bd_utils_exec_and_capture_output (args, NULL, &output, error); |
| 402 |
if (!success) { |
| 403 |
/* error is already populated */ |
| 404 |
bd_fs_ntfs_info_free (ret); |
| 405 |
return NULL; |
| 406 |
} |
| 407 |
|
| 408 |
lines = g_strsplit (output, "\n", 0); |
| 409 |
g_free (output); |
| 410 |
line_p = lines; |
| 411 |
|
| 412 |
/* find the beginning of the (data) section we are interested in */ |
| 413 |
while (line_p && *line_p && !strstr (*line_p, "Cluster Size:")) |
| 414 |
line_p++; |
| 415 |
if (!line_p || !(*line_p)) { |
| 416 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse NTFS file system information"); |
| 417 |
g_strfreev (lines); |
| 418 |
bd_fs_ntfs_info_free (ret); |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
/* extract data from something like this: "Cluster Size: 4096" */ |
| 423 |
val_start = strchr (*line_p, ':'); |
| 424 |
val_start++; |
| 425 |
cluster_size = g_ascii_strtoull (val_start, NULL, 0); |
| 426 |
|
| 427 |
while (line_p && *line_p && !strstr (*line_p, "Volume Size in Clusters:")) |
| 428 |
line_p++; |
| 429 |
if (!line_p || !(*line_p)) { |
| 430 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse NTFS file system information"); |
| 431 |
g_strfreev (lines); |
| 432 |
bd_fs_ntfs_info_free (ret); |
| 433 |
return NULL; |
| 434 |
} |
| 435 |
|
| 436 |
/* extract data from something like this: "Volume Size in Clusters: 15314943" */ |
| 437 |
val_start = strchr (*line_p, ':'); |
| 438 |
val_start++; |
| 439 |
ret->size = g_ascii_strtoull (val_start, NULL, 0) * cluster_size; |
| 440 |
|
| 441 |
while (line_p && *line_p && !strstr (*line_p, "Free Clusters:")) |
| 442 |
line_p++; |
| 443 |
if (!line_p || !(*line_p)) { |
| 444 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse NTFS file system information"); |
| 445 |
g_strfreev (lines); |
| 446 |
bd_fs_ntfs_info_free (ret); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
|
| 450 |
/* extract data from something like this: "Free Clusters: 7812655 (51,0%)" */ |
| 451 |
val_start = strchr (*line_p, ':'); |
| 452 |
val_start++; |
| 453 |
ret->free_space = g_ascii_strtoull (val_start, NULL, 0) * cluster_size; |
| 454 |
|
| 455 |
g_strfreev (lines); |
| 456 |
|
| 457 |
return ret; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* bd_fs_ntfs_get_min_size: |
| 462 |
* @device: the device containing the file system to get min size for |
| 463 |
* @error: (out) (optional): place to store error (if any) |
| 464 |
* |
| 465 |
* Returns: smallest shrunken filesystem size as reported by ntfsresize |
| 466 |
* in case of error 0 is returned and @error is set |
| 467 |
* |
| 468 |
* Tech category: %BD_FS_TECH_NTFS-%BD_FS_TECH_MODE_RESIZE |
| 469 |
*/ |
| 470 |
guint64 bd_fs_ntfs_get_min_size (const gchar *device, GError **error) { |
| 471 |
const gchar *args[5] = {"ntfsresize", "--no-progress-bar", "--info", device, NULL}; |
| 472 |
gboolean success = FALSE; |
| 473 |
gchar *output = NULL; |
| 474 |
gchar **lines = NULL; |
| 475 |
gchar **line_p = NULL; |
| 476 |
guint64 min_size = 0; |
| 477 |
gint scanned = 0; |
| 478 |
|
| 479 |
if (!check_deps (&avail_deps, DEPS_NTFSRESIZE_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 480 |
return FALSE; |
| 481 |
|
| 482 |
success = bd_utils_exec_and_capture_output (args, NULL, &output, error); |
| 483 |
if (!success) |
| 484 |
/* error is already populated */ |
| 485 |
return 0; |
| 486 |
|
| 487 |
lines = g_strsplit (output, "\n", 0); |
| 488 |
g_free (output); |
| 489 |
|
| 490 |
for (line_p=lines; *line_p; line_p++) { |
| 491 |
if (g_str_has_prefix (*line_p, "You might resize at")) { |
| 492 |
scanned = sscanf (*line_p, "You might resize at %" G_GUINT64_FORMAT " bytes %*s.", |
| 493 |
&min_size); |
| 494 |
if (scanned != 1) { |
| 495 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 496 |
"Failed to get minimum size for '%s'", device); |
| 497 |
g_strfreev (lines); |
| 498 |
return 0; |
| 499 |
} else { |
| 500 |
g_strfreev (lines); |
| 501 |
return min_size; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 507 |
"Failed to get minimum size for '%s'", device); |
| 508 |
g_strfreev (lines); |
| 509 |
return 0; |
| 510 |
} |