| 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 <glib.h> |
| 21 |
#include <glib/gstdio.h> |
| 22 |
#include <blkid.h> |
| 23 |
#include <string.h> |
| 24 |
#include <sys/types.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <sys/ioctl.h> |
| 27 |
#include <linux/fs.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
#include <blockdev/utils.h> |
| 32 |
|
| 33 |
#include "generic.h" |
| 34 |
#include "mount.h" |
| 35 |
#include "fs.h" |
| 36 |
#include "common.h" |
| 37 |
#include "ext.h" |
| 38 |
#include "xfs.h" |
| 39 |
#include "vfat.h" |
| 40 |
#include "ntfs.h" |
| 41 |
#include "f2fs.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
typedef enum { |
| 46 |
BD_FS_MKFS, |
| 47 |
BD_FS_RESIZE, |
| 48 |
BD_FS_REPAIR, |
| 49 |
BD_FS_CHECK, |
| 50 |
BD_FS_LABEL, |
| 51 |
BD_FS_LABEL_CHECK, |
| 52 |
BD_FS_GET_SIZE, |
| 53 |
BD_FS_UUID, |
| 54 |
BD_FS_UUID_CHECK, |
| 55 |
BD_FS_GET_FREE_SPACE, |
| 56 |
BD_FS_GET_INFO, |
| 57 |
BD_FS_GET_MIN_SIZE, |
| 58 |
} BDFSOpType; |
| 59 |
|
| 60 |
static const BDFSFeatures fs_features[BD_FS_LAST_FS] = { |
| 61 |
/* padding for BD_FS_TECH_GENERIC and MOUNT to make accessing the FS techs simpler */ |
| 62 |
{ 0 }, { 0 }, |
| 63 |
/* EXT2 */ |
| 64 |
{ .resize = BD_FS_ONLINE_GROW | BD_FS_OFFLINE_GROW | BD_FS_OFFLINE_SHRINK, |
| 65 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID | BD_FS_MKFS_DRY_RUN | BD_FS_MKFS_NODISCARD | |
| 66 |
BD_FS_MKFS_FORCE, |
| 67 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 68 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 69 |
.features = BD_FS_FEATURE_OWNERS, |
| 70 |
.partition_id = "0x83", |
| 71 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 72 |
.min_size = 1 MiB, |
| 73 |
.max_size = 8 TiB }, |
| 74 |
/* EXT3 */ |
| 75 |
{ .resize = BD_FS_ONLINE_GROW | BD_FS_OFFLINE_GROW | BD_FS_OFFLINE_SHRINK, |
| 76 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID | BD_FS_MKFS_DRY_RUN | BD_FS_MKFS_NODISCARD | |
| 77 |
BD_FS_MKFS_FORCE, |
| 78 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 79 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 80 |
.features = BD_FS_FEATURE_OWNERS, |
| 81 |
.partition_id = "0x83", |
| 82 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 83 |
.min_size = 1 MiB, |
| 84 |
.max_size = 16 TiB }, |
| 85 |
/* EXT4 */ |
| 86 |
{ .resize = BD_FS_ONLINE_GROW | BD_FS_OFFLINE_GROW | BD_FS_OFFLINE_SHRINK, |
| 87 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID | BD_FS_MKFS_DRY_RUN | BD_FS_MKFS_NODISCARD | |
| 88 |
BD_FS_MKFS_FORCE, |
| 89 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 90 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 91 |
.features = BD_FS_FEATURE_OWNERS, |
| 92 |
.partition_id = "0x83", |
| 93 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 94 |
.min_size = 1 MiB, |
| 95 |
.max_size = 1 EiB }, |
| 96 |
/* XFS */ |
| 97 |
{ .resize = BD_FS_ONLINE_GROW | BD_FS_OFFLINE_GROW, |
| 98 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID | BD_FS_MKFS_DRY_RUN | BD_FS_MKFS_NODISCARD | |
| 99 |
BD_FS_MKFS_FORCE, |
| 100 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 101 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 102 |
.features = BD_FS_FEATURE_OWNERS, |
| 103 |
.partition_id = "0x83", |
| 104 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 105 |
.min_size = 300 MiB, |
| 106 |
.max_size = 16 EiB - 1 }, |
| 107 |
/* VFAT */ |
| 108 |
{ .resize = BD_FS_OFFLINE_GROW | BD_FS_OFFLINE_SHRINK, |
| 109 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID | BD_FS_MKFS_FORCE | BD_FS_MKFS_NOPT, |
| 110 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 111 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 112 |
.features = BD_FS_FEATURE_PARTITION_TABLE, |
| 113 |
.partition_id = "0x0c", |
| 114 |
.partition_type = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", |
| 115 |
.min_size = 1 MiB, |
| 116 |
.max_size = 1 TiB }, |
| 117 |
/* NTFS */ |
| 118 |
{ .resize = BD_FS_OFFLINE_GROW | BD_FS_OFFLINE_SHRINK, |
| 119 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_DRY_RUN, |
| 120 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 121 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 122 |
.features = 0, |
| 123 |
.partition_id = "0x07", |
| 124 |
.partition_type = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", |
| 125 |
.min_size = 1 MiB, |
| 126 |
.max_size = 16 TiB }, |
| 127 |
/* F2FS */ |
| 128 |
{ .resize = BD_FS_OFFLINE_GROW | BD_FS_OFFLINE_SHRINK, |
| 129 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_NODISCARD | BD_FS_MKFS_FORCE, |
| 130 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 131 |
.configure = 0, |
| 132 |
.features = BD_FS_FEATURE_OWNERS, |
| 133 |
.partition_id = "0x83", |
| 134 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 135 |
.min_size = 1 MiB, |
| 136 |
.max_size = 16 TiB }, |
| 137 |
/* NILFS2 */ |
| 138 |
{ .resize = BD_FS_ONLINE_GROW | BD_FS_ONLINE_SHRINK, |
| 139 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_DRY_RUN | BD_FS_MKFS_NODISCARD | BD_FS_MKFS_FORCE, |
| 140 |
.fsck = 0, |
| 141 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 142 |
.features = BD_FS_FEATURE_OWNERS, |
| 143 |
.partition_id = "0x83", |
| 144 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 145 |
.min_size = 1 MiB, |
| 146 |
.max_size = 16 TiB }, |
| 147 |
/* EXFAT */ |
| 148 |
{ .resize = 0, |
| 149 |
.mkfs = BD_FS_MKFS_LABEL, |
| 150 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 151 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 152 |
.features = 0, |
| 153 |
.partition_id = "0x07", |
| 154 |
.partition_type = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", |
| 155 |
.min_size = 3 MiB, |
| 156 |
.max_size = 512 TiB }, |
| 157 |
/* BTRFS */ |
| 158 |
{ .resize = BD_FS_ONLINE_GROW | BD_FS_ONLINE_SHRINK, |
| 159 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID | BD_FS_MKFS_NODISCARD | BD_FS_MKFS_FORCE, |
| 160 |
.fsck = BD_FS_FSCK_CHECK | BD_FS_FSCK_REPAIR, |
| 161 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 162 |
.features = BD_FS_FEATURE_OWNERS, |
| 163 |
.partition_id = "0x83", |
| 164 |
.partition_type = "0fc63daf-8483-4772-8e79-3d69d8477de4", |
| 165 |
.min_size = 256 MiB, |
| 166 |
.max_size = 16 EiB - 1 }, |
| 167 |
/* UDF */ |
| 168 |
{ .resize = 0, |
| 169 |
.mkfs = BD_FS_MKFS_LABEL | BD_FS_MKFS_UUID, |
| 170 |
.fsck = 0, |
| 171 |
.configure = BD_FS_SUPPORT_SET_LABEL | BD_FS_SUPPORT_SET_UUID, |
| 172 |
.features = BD_FS_FEATURE_OWNERS | BD_FS_FEATURE_PARTITION_TABLE, |
| 173 |
.partition_id = "0x07", |
| 174 |
.partition_type = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", |
| 175 |
.min_size = 2 MiB, |
| 176 |
.max_size = 16 TiB }, |
| 177 |
}; |
| 178 |
|
| 179 |
/** |
| 180 |
* BDFSInfo: |
| 181 |
* @type: filesystem identifier, must be present |
| 182 |
* @mkfs_util: required utility for filesystem creation, "" if not needed and NULL for no support |
| 183 |
* @check_util: required utility for consistency checking, "" if not needed and NULL for no support |
| 184 |
* @repair_util: required utility for repair, "" if not needed and NULL for no support |
| 185 |
* @resize_util: required utility for resize, "" if not needed and NULL for no support |
| 186 |
* @label_util: required utility for labelling, "" if not needed and NULL for no support |
| 187 |
* @info_util: required utility for getting information about the filesystem, "" if not needed and NULL for no support |
| 188 |
* @uuid_util: required utility for setting UUID, "" if not needed and NULL for no support |
| 189 |
*/ |
| 190 |
typedef struct BDFSInfo { |
| 191 |
const gchar *type; |
| 192 |
const gchar *mkfs_util; |
| 193 |
const gchar *check_util; |
| 194 |
const gchar *repair_util; |
| 195 |
const gchar *resize_util; |
| 196 |
const gchar *minsize_util; |
| 197 |
const gchar *label_util; |
| 198 |
const gchar *info_util; |
| 199 |
const gchar *uuid_util; |
| 200 |
} BDFSInfo; |
| 201 |
|
| 202 |
const BDFSInfo fs_info[BD_FS_LAST_FS] = { |
| 203 |
/* padding for BD_FS_TECH_GENERIC and MOUNT to make accessing the FS techs simpler */ |
| 204 |
{ 0 }, { 0 }, |
| 205 |
/* EXT2 */ |
| 206 |
{ .type = "ext2", |
| 207 |
.mkfs_util = "mkfs.ext2", |
| 208 |
.check_util = "e2fsck", |
| 209 |
.repair_util = "e2fsck", |
| 210 |
.resize_util = "resize2fs", |
| 211 |
.minsize_util = "resize2fs", |
| 212 |
.label_util = "tune2fs", |
| 213 |
.info_util = "dumpe2fs", |
| 214 |
.uuid_util = "tune2fs" }, |
| 215 |
/* EXT3 */ |
| 216 |
{ .type = "ext3", |
| 217 |
.mkfs_util = "mkfs.ext3", |
| 218 |
.check_util = "e2fsck", |
| 219 |
.repair_util = "e2fsck", |
| 220 |
.resize_util = "resize2fs", |
| 221 |
.minsize_util = "resize2fs", |
| 222 |
.label_util = "tune2fs", |
| 223 |
.info_util = "dumpe2fs", |
| 224 |
.uuid_util = "tune2fs" }, |
| 225 |
/* EXT4 */ |
| 226 |
{ .type = "ext4", |
| 227 |
.mkfs_util = "mkfs.ext4", |
| 228 |
.check_util = "e2fsck", |
| 229 |
.repair_util = "e2fsck", |
| 230 |
.resize_util = "resize2fs", |
| 231 |
.minsize_util = "resize2fs", |
| 232 |
.label_util = "tune2fs", |
| 233 |
.info_util = "dumpe2fs", |
| 234 |
.uuid_util = "tune2fs" }, |
| 235 |
/* XFS */ |
| 236 |
{ .type = "xfs", |
| 237 |
.mkfs_util = "mkfs.xfs", |
| 238 |
.check_util = "xfs_db", |
| 239 |
.repair_util = "xfs_repair", |
| 240 |
.resize_util = "xfs_growfs", |
| 241 |
.minsize_util = NULL, |
| 242 |
.label_util = "xfs_admin", |
| 243 |
.info_util = "xfs_admin", |
| 244 |
.uuid_util = "xfs_admin" }, |
| 245 |
/* VFAT */ |
| 246 |
{ .type = "vfat", |
| 247 |
.mkfs_util = "mkfs.vfat", |
| 248 |
.check_util = "fsck.vfat", |
| 249 |
.repair_util = "fsck.vfat", |
| 250 |
.resize_util = "vfat-resize", |
| 251 |
.minsize_util = NULL, |
| 252 |
.label_util = "fatlabel", |
| 253 |
.info_util = "fsck.vfat", |
| 254 |
.uuid_util = "fatlabel" }, |
| 255 |
/* NTFS */ |
| 256 |
{ .type = "ntfs", |
| 257 |
.mkfs_util = "mkfs.ntfs", |
| 258 |
.check_util = "ntfsfix", |
| 259 |
.repair_util = "ntfsfix", |
| 260 |
.resize_util = "ntfsresize", |
| 261 |
.minsize_util = "ntfsresize", |
| 262 |
.label_util = "ntfslabel", |
| 263 |
.info_util = "ntfsinfo", |
| 264 |
.uuid_util = "ntfslabel" }, |
| 265 |
/* F2FS */ |
| 266 |
{ .type = "f2fs", |
| 267 |
.mkfs_util = "mkfs.f2fs", |
| 268 |
.check_util = "fsck.f2fs", |
| 269 |
.repair_util = "fsck.f2fs", |
| 270 |
.resize_util = "resize.f2fs", |
| 271 |
.minsize_util = NULL, |
| 272 |
.label_util = NULL, |
| 273 |
.info_util = "dump.f2fs", |
| 274 |
.uuid_util = NULL }, |
| 275 |
/* NILFS2 */ |
| 276 |
{ .type = "nilfs2", |
| 277 |
.mkfs_util = "mkfs.nilfs2", |
| 278 |
.check_util = NULL, |
| 279 |
.repair_util = NULL, |
| 280 |
.resize_util = "nilfs-resize", |
| 281 |
.minsize_util = NULL, |
| 282 |
.label_util = "nilfs-tune", |
| 283 |
.info_util = "nilfs-tune", |
| 284 |
.uuid_util = "nilfs-tune" }, |
| 285 |
/* EXFAT */ |
| 286 |
{ .type = "exfat", |
| 287 |
.mkfs_util = "mkfs.exfat", |
| 288 |
.check_util = "fsck.exfat", |
| 289 |
.repair_util = "fsck.exfat", |
| 290 |
.resize_util = NULL, |
| 291 |
.minsize_util = NULL, |
| 292 |
.label_util = "tune.exfat", |
| 293 |
.info_util = "tune.exfat", |
| 294 |
.uuid_util = "tune.exfat" }, |
| 295 |
/* BTRFS */ |
| 296 |
{ .type = "btrfs", |
| 297 |
.mkfs_util = "mkfs.btrfs", |
| 298 |
.check_util = "btrfsck", |
| 299 |
.repair_util = "btrfsck", |
| 300 |
.resize_util = "btrfs", |
| 301 |
.minsize_util = NULL, |
| 302 |
.label_util = "btrfs", |
| 303 |
.info_util = "btrfs", |
| 304 |
.uuid_util = "btrfstune" }, |
| 305 |
/* UDF */ |
| 306 |
{ .type = "udf", |
| 307 |
.mkfs_util = "mkudffs", |
| 308 |
.check_util = NULL, |
| 309 |
.repair_util = NULL, |
| 310 |
.resize_util = NULL, |
| 311 |
.minsize_util = NULL, |
| 312 |
.label_util = "udflabel", |
| 313 |
.info_util = "udfinfo", |
| 314 |
.uuid_util = "udflabel" }, |
| 315 |
}; |
| 316 |
|
| 317 |
/** |
| 318 |
* bd_fs_supported_filesystems: |
| 319 |
* @error: (out) (optional): currently unused |
| 320 |
* |
| 321 |
* Returns: (transfer container) (array zero-terminated=1): list of filesystems supported by this plugin |
| 322 |
* |
| 323 |
* Note: This returns filesystems supported by libblockdev, but not necessarily |
| 324 |
* by the systems this is running on, for this information you need to |
| 325 |
* run one of the `bd_fs_can_` functions. |
| 326 |
* |
| 327 |
* Tech category: always available |
| 328 |
*/ |
| 329 |
const gchar** bd_fs_supported_filesystems (GError **error G_GNUC_UNUSED) { |
| 330 |
const gchar **filesystems = g_new0 (const gchar *, BD_FS_LAST_FS - BD_FS_OFFSET + 1); |
| 331 |
gint i = 0; |
| 332 |
|
| 333 |
for (i = 0; i < BD_FS_LAST_FS - BD_FS_OFFSET; i++) |
| 334 |
filesystems[i] = fs_info[i + BD_FS_OFFSET].type; |
| 335 |
|
| 336 |
return filesystems; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* fstype_to_tech: (skip) |
| 341 |
* @fstype: filesystem type to get tech for |
| 342 |
* |
| 343 |
* Returns %BDFSTech for specified @fstype. Returns |
| 344 |
* %BD_FS_TECH_GENERIC for unknown/unsupported filesystems. |
| 345 |
*/ |
| 346 |
static BDFSTech fstype_to_tech (const gchar *fstype) { |
| 347 |
if (g_strcmp0 (fstype, "exfat") == 0) { |
| 348 |
return BD_FS_TECH_EXFAT; |
| 349 |
} else if (g_strcmp0 (fstype, "ext2") == 0) { |
| 350 |
return BD_FS_TECH_EXT2; |
| 351 |
} else if (g_strcmp0 (fstype, "ext3") == 0) { |
| 352 |
return BD_FS_TECH_EXT3; |
| 353 |
} else if (g_strcmp0 (fstype, "ext4") == 0) { |
| 354 |
return BD_FS_TECH_EXT4; |
| 355 |
} else if (g_strcmp0 (fstype, "f2fs") == 0) { |
| 356 |
return BD_FS_TECH_F2FS; |
| 357 |
} else if (g_strcmp0 (fstype, "nilfs2") == 0) { |
| 358 |
return BD_FS_TECH_NILFS2; |
| 359 |
} else if (g_strcmp0 (fstype, "ntfs") == 0) { |
| 360 |
return BD_FS_TECH_NTFS; |
| 361 |
} else if (g_strcmp0 (fstype, "vfat") == 0) { |
| 362 |
return BD_FS_TECH_VFAT; |
| 363 |
} else if (g_strcmp0 (fstype, "xfs") == 0) { |
| 364 |
return BD_FS_TECH_XFS; |
| 365 |
} else if (g_strcmp0 (fstype, "btrfs") == 0) { |
| 366 |
return BD_FS_TECH_BTRFS; |
| 367 |
} else if (g_strcmp0 (fstype, "udf") == 0) { |
| 368 |
return BD_FS_TECH_UDF; |
| 369 |
} else { |
| 370 |
return BD_FS_TECH_GENERIC; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* bd_fs_wipe: |
| 376 |
* @device: the device to wipe signatures from |
| 377 |
* @all: whether to wipe all (%TRUE) signatures or just the first (%FALSE) one |
| 378 |
* @force: whether to wipe signatures on a mounted @device |
| 379 |
* @error: (out) (optional): place to store error (if any) |
| 380 |
* |
| 381 |
* Returns: whether signatures were successfully wiped on @device or not |
| 382 |
* |
| 383 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_WIPE |
| 384 |
*/ |
| 385 |
gboolean bd_fs_wipe (const gchar *device, gboolean all, gboolean force, GError **error) { |
| 386 |
blkid_probe probe = NULL; |
| 387 |
gint fd = 0; |
| 388 |
gint status = 0; |
| 389 |
guint64 progress_id = 0; |
| 390 |
gchar *msg = NULL; |
| 391 |
guint n_try = 0; |
| 392 |
gint mode = 0; |
| 393 |
GError *l_error = NULL; |
| 394 |
|
| 395 |
msg = g_strdup_printf ("Started wiping signatures from the device '%s'", device); |
| 396 |
progress_id = bd_utils_report_started (msg); |
| 397 |
g_free (msg); |
| 398 |
|
| 399 |
probe = blkid_new_probe (); |
| 400 |
if (!probe) { |
| 401 |
g_set_error_literal (&l_error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 402 |
"Failed to create a new probe"); |
| 403 |
bd_utils_report_finished (progress_id, l_error->message); |
| 404 |
g_propagate_error (error, l_error); |
| 405 |
return FALSE; |
| 406 |
} |
| 407 |
|
| 408 |
mode = O_RDWR | O_CLOEXEC; |
| 409 |
if (!force) |
| 410 |
mode |= O_EXCL; |
| 411 |
|
| 412 |
fd = open (device, mode); |
| 413 |
if (fd == -1) { |
| 414 |
g_set_error (&l_error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 415 |
"Failed to open the device '%s': %s", |
| 416 |
device, strerror_l (errno, _C_LOCALE)); |
| 417 |
blkid_free_probe (probe); |
| 418 |
bd_utils_report_finished (progress_id, l_error->message); |
| 419 |
g_propagate_error (error, l_error); |
| 420 |
return FALSE; |
| 421 |
} |
| 422 |
|
| 423 |
/* we may need to try multiple times with some delays in case the device is |
| 424 |
busy at the very moment */ |
| 425 |
for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) { |
| 426 |
status = blkid_probe_set_device (probe, fd, 0, 0); |
| 427 |
if (status != 0) |
| 428 |
g_usleep (100 * 1000); /* microseconds */ |
| 429 |
} |
| 430 |
if (status != 0) { |
| 431 |
g_set_error (&l_error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 432 |
"Failed to create a probe for the device '%s'", device); |
| 433 |
blkid_free_probe (probe); |
| 434 |
synced_close (fd); |
| 435 |
bd_utils_report_finished (progress_id, l_error->message); |
| 436 |
g_propagate_error (error, l_error); |
| 437 |
return FALSE; |
| 438 |
} |
| 439 |
|
| 440 |
blkid_probe_enable_partitions (probe, 1); |
| 441 |
blkid_probe_set_partitions_flags (probe, BLKID_PARTS_MAGIC); |
| 442 |
blkid_probe_enable_superblocks (probe, 1); |
| 443 |
blkid_probe_set_superblocks_flags (probe, BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM); |
| 444 |
|
| 445 |
/* we may need to try multiple times with some delays in case the device is |
| 446 |
busy at the very moment */ |
| 447 |
for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) { |
| 448 |
status = blkid_do_safeprobe (probe); |
| 449 |
if (status == 1) |
| 450 |
break; |
| 451 |
if (status < 0) |
| 452 |
g_usleep (100 * 1000); /* microseconds */ |
| 453 |
} |
| 454 |
if (status == 1) { |
| 455 |
g_set_error (&l_error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 456 |
"No signature detected on the device '%s'", device); |
| 457 |
blkid_free_probe (probe); |
| 458 |
synced_close (fd); |
| 459 |
bd_utils_report_finished (progress_id, l_error->message); |
| 460 |
g_propagate_error (error, l_error); |
| 461 |
return FALSE; |
| 462 |
} |
| 463 |
|
| 464 |
blkid_reset_probe (probe); |
| 465 |
status = blkid_do_probe (probe); |
| 466 |
|
| 467 |
if (status < 0) { |
| 468 |
g_set_error (&l_error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 469 |
"Failed to probe the device '%s'", device); |
| 470 |
blkid_free_probe (probe); |
| 471 |
synced_close (fd); |
| 472 |
bd_utils_report_finished (progress_id, l_error->message); |
| 473 |
g_propagate_error (error, l_error); |
| 474 |
return FALSE; |
| 475 |
} |
| 476 |
|
| 477 |
status = blkid_do_wipe (probe, FALSE); |
| 478 |
if (status != 0) { |
| 479 |
g_set_error (&l_error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 480 |
"Failed to wipe signatures on the device '%s'", device); |
| 481 |
blkid_free_probe (probe); |
| 482 |
synced_close (fd); |
| 483 |
bd_utils_report_finished (progress_id, l_error->message); |
| 484 |
g_propagate_error (error, l_error); |
| 485 |
return FALSE; |
| 486 |
} |
| 487 |
while (all && (blkid_do_probe (probe) == 0)) { |
| 488 |
status = blkid_do_wipe (probe, FALSE); |
| 489 |
if (status != 0) { |
| 490 |
g_set_error (&l_error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 491 |
"Failed to wipe signatures on the device '%s'", device); |
| 492 |
blkid_free_probe (probe); |
| 493 |
synced_close (fd); |
| 494 |
bd_utils_report_finished (progress_id, l_error->message); |
| 495 |
g_propagate_error (error, l_error); |
| 496 |
return FALSE; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
blkid_free_probe (probe); |
| 501 |
synced_close (fd); |
| 502 |
|
| 503 |
bd_utils_report_finished (progress_id, "Completed"); |
| 504 |
|
| 505 |
return TRUE; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* bd_fs_clean: |
| 510 |
* @device: the device to clean |
| 511 |
* @force: whether to wipe signatures on a mounted @device |
| 512 |
* @error: (out) (optional): place to store error (if any) |
| 513 |
* |
| 514 |
* Clean all signatures from @device. |
| 515 |
* Difference between this and bd_fs_wipe() is that this function doesn't |
| 516 |
* return error if @device is already empty. This will also always remove |
| 517 |
* all signatures from @device, not only the first one. |
| 518 |
* |
| 519 |
* Returns: whether @device was successfully cleaned or not |
| 520 |
* |
| 521 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_WIPE |
| 522 |
*/ |
| 523 |
gboolean bd_fs_clean (const gchar *device, gboolean force, GError **error) { |
| 524 |
gboolean ret = FALSE; |
| 525 |
GError *l_error = NULL; |
| 526 |
|
| 527 |
ret = bd_fs_wipe (device, TRUE, force, &l_error); |
| 528 |
|
| 529 |
if (!ret) { |
| 530 |
if (g_error_matches (l_error, BD_FS_ERROR, BD_FS_ERROR_NOFS)) { |
| 531 |
/* ignore 'empty device' error */ |
| 532 |
g_clear_error (&l_error); |
| 533 |
return TRUE; |
| 534 |
} else { |
| 535 |
g_propagate_error (error, l_error); |
| 536 |
return FALSE; |
| 537 |
} |
| 538 |
} else |
| 539 |
return TRUE; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* bd_fs_get_fstype: |
| 544 |
* @device: the device to probe |
| 545 |
* @error: (out) (optional): place to store error (if any) |
| 546 |
* |
| 547 |
* Get first signature on @device as a string. |
| 548 |
* |
| 549 |
* Returns: (transfer full): type of filesystem found on @device, %NULL in case |
| 550 |
* no signature has been detected or in case of error |
| 551 |
* (@error is set in this case) |
| 552 |
* |
| 553 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 554 |
*/ |
| 555 |
gchar* bd_fs_get_fstype (const gchar *device, GError **error) { |
| 556 |
blkid_probe probe = NULL; |
| 557 |
gint fd = 0; |
| 558 |
gint status = 0; |
| 559 |
const gchar *value = NULL; |
| 560 |
gchar *fstype = NULL; |
| 561 |
size_t len = 0; |
| 562 |
guint n_try = 0; |
| 563 |
|
| 564 |
probe = blkid_new_probe (); |
| 565 |
if (!probe) { |
| 566 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 567 |
"Failed to create a new probe"); |
| 568 |
return NULL; |
| 569 |
} |
| 570 |
|
| 571 |
fd = open (device, O_RDONLY|O_CLOEXEC); |
| 572 |
if (fd == -1) { |
| 573 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 574 |
"Failed to open the device '%s': %s", |
| 575 |
device, strerror_l (errno, _C_LOCALE)); |
| 576 |
blkid_free_probe (probe); |
| 577 |
return NULL; |
| 578 |
} |
| 579 |
|
| 580 |
/* we may need to try multiple times with some delays in case the device is |
| 581 |
busy at the very moment */ |
| 582 |
for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) { |
| 583 |
status = blkid_probe_set_device (probe, fd, 0, 0); |
| 584 |
if (status != 0) |
| 585 |
g_usleep (100 * 1000); /* microseconds */ |
| 586 |
} |
| 587 |
if (status != 0) { |
| 588 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 589 |
"Failed to create a probe for the device '%s'", device); |
| 590 |
blkid_free_probe (probe); |
| 591 |
synced_close (fd); |
| 592 |
return NULL; |
| 593 |
} |
| 594 |
|
| 595 |
blkid_probe_enable_partitions (probe, 1); |
| 596 |
blkid_probe_set_partitions_flags (probe, BLKID_PARTS_MAGIC); |
| 597 |
blkid_probe_enable_superblocks (probe, 1); |
| 598 |
blkid_probe_set_superblocks_flags (probe, BLKID_SUBLKS_USAGE | BLKID_SUBLKS_TYPE | |
| 599 |
BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM); |
| 600 |
|
| 601 |
/* we may need to try multiple times with some delays in case the device is |
| 602 |
busy at the very moment */ |
| 603 |
for (n_try=5, status=-1; !(status == 0 || status == 1) && (n_try > 0); n_try--) { |
| 604 |
status = blkid_do_safeprobe (probe); |
| 605 |
if (status < 0) |
| 606 |
g_usleep (100 * 1000); /* microseconds */ |
| 607 |
} |
| 608 |
if (status < 0) { |
| 609 |
/* -1 or -2 = error during probing*/ |
| 610 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 611 |
"Failed to probe the device '%s'", device); |
| 612 |
blkid_free_probe (probe); |
| 613 |
synced_close (fd); |
| 614 |
return NULL; |
| 615 |
} else if (status == 1) { |
| 616 |
/* 1 = nothing detected */ |
| 617 |
blkid_free_probe (probe); |
| 618 |
synced_close (fd); |
| 619 |
return NULL; |
| 620 |
} |
| 621 |
|
| 622 |
status = blkid_probe_lookup_value (probe, "USAGE", &value, &len); |
| 623 |
if (status != 0) { |
| 624 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 625 |
"Failed to get usage for the device '%s'", device); |
| 626 |
blkid_free_probe (probe); |
| 627 |
synced_close (fd); |
| 628 |
return NULL; |
| 629 |
} |
| 630 |
|
| 631 |
if (strncmp (value, "filesystem", 10) != 0) { |
| 632 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_INVAL, |
| 633 |
"The signature on the device '%s' is of type '%s', not 'filesystem'", device, value); |
| 634 |
blkid_free_probe (probe); |
| 635 |
synced_close (fd); |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
|
| 639 |
status = blkid_probe_lookup_value (probe, "TYPE", &value, &len); |
| 640 |
if (status != 0) { |
| 641 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 642 |
"Failed to get filesystem type for the device '%s'", device); |
| 643 |
blkid_free_probe (probe); |
| 644 |
synced_close (fd); |
| 645 |
return NULL; |
| 646 |
} |
| 647 |
|
| 648 |
fstype = g_strdup (value); |
| 649 |
blkid_free_probe (probe); |
| 650 |
synced_close (fd); |
| 651 |
|
| 652 |
return fstype; |
| 653 |
} |
| 654 |
|
| 655 |
/* taken from kernel source: fs/xfs/libxfs/xfs_log_format.h: */ |
| 656 |
#define _XFS_UQUOTA_ACCT 0x0001 /* user quota accounting ON */ |
| 657 |
#define _XFS_UQUOTA_ENFD 0x0002 /* user quota limits enforced */ |
| 658 |
#define _XFS_GQUOTA_ACCT 0x0040 /* group quota accounting ON */ |
| 659 |
#define _XFS_GQUOTA_ENFD 0x0080 /* group quota limits enforced */ |
| 660 |
#define _XFS_PQUOTA_ACCT 0x0008 /* project quota accounting ON */ |
| 661 |
#define _XFS_PQUOTA_ENFD 0x0200 /* project quota limits enforced */ |
| 662 |
|
| 663 |
static gboolean xfs_quota_mount_options (const gchar *device, GString *options, GError **error) { |
| 664 |
g_autofree gchar *output = NULL; |
| 665 |
gboolean success = FALSE; |
| 666 |
const gchar *args[8] = {"xfs_db", "-r", device, "-c", "sb 0", "-c", "p qflags", NULL}; |
| 667 |
guint64 flags = 0; |
| 668 |
|
| 669 |
success = bd_utils_exec_and_capture_output (args, NULL, &output, error); |
| 670 |
if (!success) { |
| 671 |
/* error is already populated */ |
| 672 |
return FALSE; |
| 673 |
} |
| 674 |
|
| 675 |
if (!g_str_has_prefix (output, "qflags = ")) { |
| 676 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 677 |
"Failed to parse XFS quota flags for %s from %s.", device, output); |
| 678 |
return FALSE; |
| 679 |
} |
| 680 |
|
| 681 |
flags = g_ascii_strtoull (output + 9, NULL, 16); |
| 682 |
if (flags & _XFS_UQUOTA_ACCT) { |
| 683 |
if (flags & _XFS_UQUOTA_ENFD) |
| 684 |
g_string_append_printf (options, "uquota,"); |
| 685 |
else |
| 686 |
g_string_append_printf (options, "uqnoenforce,"); |
| 687 |
} |
| 688 |
|
| 689 |
if (flags & _XFS_GQUOTA_ACCT) { |
| 690 |
if (flags & _XFS_GQUOTA_ENFD) |
| 691 |
g_string_append_printf (options, "gquota,"); |
| 692 |
else |
| 693 |
g_string_append_printf (options, "gqnoenforce,"); |
| 694 |
} |
| 695 |
|
| 696 |
if (flags & _XFS_PQUOTA_ACCT) { |
| 697 |
if (flags & _XFS_PQUOTA_ENFD) |
| 698 |
g_string_append_printf (options, "pquota,"); |
| 699 |
else |
| 700 |
g_string_append_printf (options, "pqnoenforce,"); |
| 701 |
} |
| 702 |
|
| 703 |
return TRUE; |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* fs_mount: |
| 708 |
* @device: the device to mount for an FS operation |
| 709 |
* @fstype: (nullable): filesystem type on @device |
| 710 |
* @read_only: whether to mount @device ro or rw |
| 711 |
* @unmount: (out): whether caller should unmount the device (was mounted by us) or |
| 712 |
* not (was already mounted before) |
| 713 |
* @error: (out) (optional): place to store error (if any) |
| 714 |
* |
| 715 |
* This is just a helper function for FS operations that need @device to be mounted. |
| 716 |
* If the device is already mounted, this will just return the existing mountpoint. |
| 717 |
* If the device is not mounted, we will mount it to a temporary directory and set |
| 718 |
* @unmount to %TRUE. |
| 719 |
* |
| 720 |
* Returns: (transfer full): mountpoint @device is mounted at (or %NULL in case of error) |
| 721 |
*/ |
| 722 |
static gchar* fs_mount (const gchar *device, gchar *fstype, gboolean read_only, gboolean *unmount, GError **error) { |
| 723 |
gchar *mountpoint = NULL; |
| 724 |
gboolean ret = FALSE; |
| 725 |
GError *l_error = NULL; |
| 726 |
GString *options = NULL; |
| 727 |
g_autofree gchar *options_str = NULL; |
| 728 |
|
| 729 |
mountpoint = bd_fs_get_mountpoint (device, &l_error); |
| 730 |
if (!mountpoint) { |
| 731 |
if (l_error == NULL) { |
| 732 |
/* device is not mounted -- we need to mount it */ |
| 733 |
mountpoint = g_dir_make_tmp ("blockdev.XXXXXX", NULL); |
| 734 |
if (!mountpoint) { |
| 735 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 736 |
"Failed to create temporary directory for mounting '%s'.", device); |
| 737 |
return NULL; |
| 738 |
} |
| 739 |
|
| 740 |
options = g_string_new (NULL); |
| 741 |
if (g_strcmp0 (fstype, "xfs") == 0) { |
| 742 |
ret = xfs_quota_mount_options (device, options, &l_error); |
| 743 |
if (!ret) { |
| 744 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Failed to get XFS mount options: %s", |
| 745 |
l_error->message); |
| 746 |
g_clear_error (&l_error); |
| 747 |
} |
| 748 |
} |
| 749 |
if (read_only) |
| 750 |
g_string_append_printf (options, "nosuid,nodev,ro"); |
| 751 |
else |
| 752 |
g_string_append_printf (options, "nosuid,nodev"); |
| 753 |
options_str = g_string_free (options, FALSE); |
| 754 |
|
| 755 |
ret = bd_fs_mount (device, mountpoint, fstype, options_str, NULL, &l_error); |
| 756 |
if (!ret) { |
| 757 |
g_propagate_prefixed_error (error, l_error, "Failed to mount '%s': ", device); |
| 758 |
if (g_rmdir (mountpoint) != 0) |
| 759 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Failed to remove temporary mountpoint '%s'", |
| 760 |
mountpoint); |
| 761 |
g_free (mountpoint); |
| 762 |
return NULL; |
| 763 |
} else |
| 764 |
*unmount = TRUE; |
| 765 |
} else { |
| 766 |
g_propagate_prefixed_error (error, l_error, |
| 767 |
"Error when trying to get mountpoint for '%s': ", device); |
| 768 |
g_free (mountpoint); |
| 769 |
return NULL; |
| 770 |
} |
| 771 |
} else |
| 772 |
*unmount = FALSE; |
| 773 |
|
| 774 |
return mountpoint; |
| 775 |
} |
| 776 |
|
| 777 |
static gboolean fs_unmount (const gchar *device, const gchar *mountpoint, const gchar *operation, GError **error) { |
| 778 |
gboolean ret = FALSE; |
| 779 |
GError *local_error = NULL; |
| 780 |
|
| 781 |
ret = bd_fs_unmount (mountpoint, FALSE, FALSE, NULL, &local_error); |
| 782 |
if (!ret) { |
| 783 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_UNMOUNT_FAIL, |
| 784 |
"Failed to unmount '%s' after %s it: %s", |
| 785 |
device, operation, local_error->message); |
| 786 |
g_clear_error (&local_error); |
| 787 |
return FALSE; |
| 788 |
} else { |
| 789 |
if (g_rmdir (mountpoint) != 0) |
| 790 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Failed to remove temporary mountpoint '%s'", |
| 791 |
mountpoint); |
| 792 |
} |
| 793 |
return TRUE; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
/** |
| 798 |
* xfs_resize_device: |
| 799 |
* @device: the device the file system of which to resize |
| 800 |
* @new_size: new requested size for the file system *in bytes* |
| 801 |
* (if 0, the file system is adapted to the underlying block device) |
| 802 |
* @extra: (nullable) (array zero-terminated=1): extra options for the resize (right now |
| 803 |
* passed to the 'xfs_growfs' utility) |
| 804 |
* @error: (out) (optional): place to store error (if any) |
| 805 |
* |
| 806 |
* This is just a helper function for bd_fs_resize. |
| 807 |
* |
| 808 |
* Returns: whether the file system on @device was successfully resized or not |
| 809 |
*/ |
| 810 |
static gboolean xfs_resize_device (const gchar *device, guint64 new_size, const BDExtraArg **extra, GError **error) { |
| 811 |
g_autofree gchar* mountpoint = NULL; |
| 812 |
gboolean ret = FALSE; |
| 813 |
gboolean success = FALSE; |
| 814 |
gboolean unmount = FALSE; |
| 815 |
GError *local_error = NULL; |
| 816 |
BDFSXfsInfo* xfs_info = NULL; |
| 817 |
|
| 818 |
xfs_info = bd_fs_xfs_get_info (device, error); |
| 819 |
if (!xfs_info) { |
| 820 |
return FALSE; |
| 821 |
} |
| 822 |
|
| 823 |
if (xfs_info->block_size == 0) { |
| 824 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 825 |
"Failed to get block size for device '%s'", device); |
| 826 |
bd_fs_xfs_info_free (xfs_info); |
| 827 |
return FALSE; |
| 828 |
} |
| 829 |
|
| 830 |
mountpoint = fs_mount (device, "xfs", FALSE, &unmount, error); |
| 831 |
if (!mountpoint) { |
| 832 |
bd_fs_xfs_info_free (xfs_info); |
| 833 |
return FALSE; |
| 834 |
} |
| 835 |
|
| 836 |
new_size = (new_size + xfs_info->block_size - 1) / xfs_info->block_size; |
| 837 |
bd_fs_xfs_info_free (xfs_info); |
| 838 |
|
| 839 |
success = bd_fs_xfs_resize (mountpoint, new_size, extra, error); |
| 840 |
|
| 841 |
if (unmount) { |
| 842 |
ret = fs_unmount (device, mountpoint, "resizing", &local_error); |
| 843 |
if (!ret) { |
| 844 |
if (success) { |
| 845 |
/* resize was successful but unmount failed */ |
| 846 |
g_propagate_error (error, local_error); |
| 847 |
return FALSE; |
| 848 |
} else |
| 849 |
/* both resize and unmount were unsuccessful but the error |
| 850 |
from the resize is more important so just ignore the |
| 851 |
unmount error */ |
| 852 |
g_clear_error (&local_error); |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
return success; |
| 857 |
} |
| 858 |
|
| 859 |
static gboolean f2fs_resize_device (const gchar *device, guint64 new_size, GError **error) { |
| 860 |
BDFSF2FSInfo *info = NULL; |
| 861 |
gboolean safe = FALSE; |
| 862 |
|
| 863 |
info = bd_fs_f2fs_get_info (device, error); |
| 864 |
if (!info) { |
| 865 |
/* error is already populated */ |
| 866 |
return FALSE; |
| 867 |
} |
| 868 |
|
| 869 |
if (info->sector_size == 0) { |
| 870 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 871 |
"Failed to get sector size for device '%s'", device); |
| 872 |
bd_fs_f2fs_info_free (info); |
| 873 |
return FALSE; |
| 874 |
} |
| 875 |
|
| 876 |
/* round to nearest sector_size multiple */ |
| 877 |
new_size = (new_size + info->sector_size - 1) / info->sector_size; |
| 878 |
|
| 879 |
/* safe must be specified for shrining */ |
| 880 |
safe = new_size < info->sector_count && new_size != 0; |
| 881 |
bd_fs_f2fs_info_free (info); |
| 882 |
|
| 883 |
return bd_fs_f2fs_resize (device, new_size, safe, NULL, error); |
| 884 |
} |
| 885 |
|
| 886 |
static gboolean nilfs2_resize_device (const gchar *device, guint64 new_size, GError **error) { |
| 887 |
g_autofree gchar* mountpoint = NULL; |
| 888 |
gboolean ret = FALSE; |
| 889 |
gboolean success = FALSE; |
| 890 |
gboolean unmount = FALSE; |
| 891 |
GError *local_error = NULL; |
| 892 |
|
| 893 |
mountpoint = fs_mount (device, "nilfs2", FALSE, &unmount, error); |
| 894 |
if (!mountpoint) |
| 895 |
return FALSE; |
| 896 |
|
| 897 |
success = bd_fs_nilfs2_resize (device, new_size, error); |
| 898 |
|
| 899 |
if (unmount) { |
| 900 |
ret = fs_unmount (device, mountpoint, "resizing", &local_error); |
| 901 |
if (!ret) { |
| 902 |
if (success) { |
| 903 |
/* resize was successful but unmount failed */ |
| 904 |
g_propagate_error (error, local_error); |
| 905 |
return FALSE; |
| 906 |
} else |
| 907 |
/* both resize and unmount were unsuccessful but the error |
| 908 |
from the resize is more important so just ignore the |
| 909 |
unmount error */ |
| 910 |
g_clear_error (&local_error); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
return success; |
| 915 |
} |
| 916 |
|
| 917 |
static BDFSBtrfsInfo* btrfs_get_info (const gchar *device, GError **error) { |
| 918 |
g_autofree gchar* mountpoint = NULL; |
| 919 |
gboolean unmount = FALSE; |
| 920 |
gboolean ret = FALSE; |
| 921 |
GError *local_error = NULL; |
| 922 |
BDFSBtrfsInfo* btrfs_info = NULL; |
| 923 |
|
| 924 |
mountpoint = fs_mount (device, "btrfs", TRUE, &unmount, error); |
| 925 |
if (!mountpoint) |
| 926 |
return NULL; |
| 927 |
|
| 928 |
btrfs_info = bd_fs_btrfs_get_info (mountpoint, error); |
| 929 |
|
| 930 |
if (unmount) { |
| 931 |
ret = fs_unmount (device, mountpoint, "getting info", &local_error); |
| 932 |
if (!ret) { |
| 933 |
if (btrfs_info) { |
| 934 |
/* info was successful but unmount failed */ |
| 935 |
g_propagate_error (error, local_error); |
| 936 |
bd_fs_btrfs_info_free (btrfs_info); |
| 937 |
return NULL; |
| 938 |
} else |
| 939 |
/* both info and unmount were unsuccessful but the error |
| 940 |
from the info is more important so just ignore the |
| 941 |
unmount error */ |
| 942 |
g_clear_error (&local_error); |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
return btrfs_info; |
| 947 |
} |
| 948 |
|
| 949 |
static gboolean btrfs_resize_device (const gchar *device, guint64 new_size, GError **error) { |
| 950 |
g_autofree gchar* mountpoint = NULL; |
| 951 |
gboolean ret = FALSE; |
| 952 |
gboolean success = FALSE; |
| 953 |
gboolean unmount = FALSE; |
| 954 |
GError *local_error = NULL; |
| 955 |
|
| 956 |
mountpoint = fs_mount (device, "btrfs", FALSE, &unmount, error); |
| 957 |
if (!mountpoint) |
| 958 |
return FALSE; |
| 959 |
|
| 960 |
success = bd_fs_btrfs_resize (mountpoint, new_size, NULL, error); |
| 961 |
|
| 962 |
if (unmount) { |
| 963 |
ret = fs_unmount (device, mountpoint, "resizing", &local_error); |
| 964 |
if (!ret) { |
| 965 |
if (success) { |
| 966 |
/* resize was successful but unmount failed */ |
| 967 |
g_propagate_error (error, local_error); |
| 968 |
return FALSE; |
| 969 |
} else |
| 970 |
/* both resize and unmount were unsuccessful but the error |
| 971 |
from the resize is more important so just ignore the |
| 972 |
unmount error */ |
| 973 |
g_clear_error (&local_error); |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
return success; |
| 978 |
} |
| 979 |
|
| 980 |
static gboolean btrfs_set_label (const gchar *device, const gchar *label, GError **error) { |
| 981 |
g_autofree gchar* mountpoint = NULL; |
| 982 |
gboolean ret = FALSE; |
| 983 |
gboolean success = FALSE; |
| 984 |
gboolean unmount = FALSE; |
| 985 |
GError *local_error = NULL; |
| 986 |
|
| 987 |
mountpoint = fs_mount (device, "btrfs", FALSE, &unmount, error); |
| 988 |
if (!mountpoint) |
| 989 |
return FALSE; |
| 990 |
|
| 991 |
success = bd_fs_btrfs_set_label (mountpoint, label, error); |
| 992 |
|
| 993 |
if (unmount) { |
| 994 |
ret = fs_unmount (device, mountpoint, "setting label", &local_error); |
| 995 |
if (!ret) { |
| 996 |
if (success) { |
| 997 |
/* label was successful but unmount failed */ |
| 998 |
g_propagate_error (error, local_error); |
| 999 |
return FALSE; |
| 1000 |
} else |
| 1001 |
/* both label and unmount were unsuccessful but the error |
| 1002 |
from the label is more important so just ignore the |
| 1003 |
unmount error */ |
| 1004 |
g_clear_error (&local_error); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
return success; |
| 1009 |
} |
| 1010 |
|
| 1011 |
static gboolean device_operation (const gchar *device, const gchar *fstype, BDFSOpType op, guint64 new_size, const gchar *label, const gchar *uuid, GError **error) { |
| 1012 |
const gchar* op_name = NULL; |
| 1013 |
g_autofree gchar* detected_fstype = NULL; |
| 1014 |
|
| 1015 |
/* MKFS is covered as a special case, it's a bug to use this function for this case */ |
| 1016 |
g_assert_true (op != BD_FS_MKFS); |
| 1017 |
|
| 1018 |
/* GET_SIZE is covered as a special case, it's a bug to use this function for this case */ |
| 1019 |
g_assert_true (op != BD_FS_GET_SIZE); |
| 1020 |
|
| 1021 |
if (!fstype) { |
| 1022 |
detected_fstype = bd_fs_get_fstype (device, error); |
| 1023 |
if (!detected_fstype) { |
| 1024 |
if (error) { |
| 1025 |
if (*error == NULL) { |
| 1026 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 1027 |
"No filesystem detected on the device '%s'", device); |
| 1028 |
return FALSE; |
| 1029 |
} else { |
| 1030 |
g_prefix_error (error, "Error when trying to detect filesystem on '%s': ", device); |
| 1031 |
return FALSE; |
| 1032 |
} |
| 1033 |
} else |
| 1034 |
return FALSE; |
| 1035 |
} |
| 1036 |
} else |
| 1037 |
detected_fstype = g_strdup (fstype); |
| 1038 |
|
| 1039 |
if (g_strcmp0 (detected_fstype, "ext2") == 0 || g_strcmp0 (detected_fstype, "ext3") == 0 |
| 1040 |
|| g_strcmp0 (detected_fstype, "ext4") == 0) { |
| 1041 |
switch (op) { |
| 1042 |
case BD_FS_RESIZE: |
| 1043 |
return bd_fs_ext4_resize (device, new_size, NULL, error); |
| 1044 |
case BD_FS_REPAIR: |
| 1045 |
return bd_fs_ext4_repair (device, TRUE, NULL, error); |
| 1046 |
case BD_FS_CHECK: |
| 1047 |
return bd_fs_ext4_check (device, NULL, error); |
| 1048 |
case BD_FS_LABEL: |
| 1049 |
return bd_fs_ext4_set_label (device, label, error); |
| 1050 |
case BD_FS_LABEL_CHECK: |
| 1051 |
return bd_fs_ext4_check_label (label, error); |
| 1052 |
case BD_FS_UUID: |
| 1053 |
return bd_fs_ext4_set_uuid (device, uuid, error); |
| 1054 |
case BD_FS_UUID_CHECK: |
| 1055 |
return bd_fs_ext4_check_uuid (uuid, error); |
| 1056 |
default: |
| 1057 |
g_assert_not_reached (); |
| 1058 |
} |
| 1059 |
} else if (g_strcmp0 (detected_fstype, "xfs") == 0) { |
| 1060 |
switch (op) { |
| 1061 |
case BD_FS_RESIZE: |
| 1062 |
return xfs_resize_device (device, new_size, NULL, error); |
| 1063 |
case BD_FS_REPAIR: |
| 1064 |
return bd_fs_xfs_repair (device, NULL, error); |
| 1065 |
case BD_FS_CHECK: |
| 1066 |
return bd_fs_xfs_check (device, NULL, error); |
| 1067 |
case BD_FS_LABEL: |
| 1068 |
return bd_fs_xfs_set_label (device, label, error); |
| 1069 |
case BD_FS_LABEL_CHECK: |
| 1070 |
return bd_fs_xfs_check_label (label, error); |
| 1071 |
case BD_FS_UUID: |
| 1072 |
return bd_fs_xfs_set_uuid (device, uuid, error); |
| 1073 |
case BD_FS_UUID_CHECK: |
| 1074 |
return bd_fs_xfs_check_uuid (uuid, error); |
| 1075 |
default: |
| 1076 |
g_assert_not_reached (); |
| 1077 |
} |
| 1078 |
} else if (g_strcmp0 (detected_fstype, "vfat") == 0) { |
| 1079 |
switch (op) { |
| 1080 |
case BD_FS_RESIZE: |
| 1081 |
return bd_fs_vfat_resize (device, new_size, error); |
| 1082 |
case BD_FS_REPAIR: |
| 1083 |
return bd_fs_vfat_repair (device, NULL, error); |
| 1084 |
case BD_FS_CHECK: |
| 1085 |
return bd_fs_vfat_check (device, NULL, error); |
| 1086 |
case BD_FS_LABEL_CHECK: |
| 1087 |
return bd_fs_vfat_check_label (label, error); |
| 1088 |
case BD_FS_LABEL: |
| 1089 |
return bd_fs_vfat_set_label (device, label, error); |
| 1090 |
case BD_FS_UUID: |
| 1091 |
return bd_fs_vfat_set_uuid (device, uuid, error); |
| 1092 |
case BD_FS_UUID_CHECK: |
| 1093 |
return bd_fs_vfat_check_uuid (uuid, error); |
| 1094 |
default: |
| 1095 |
g_assert_not_reached (); |
| 1096 |
} |
| 1097 |
} else if (g_strcmp0 (detected_fstype, "ntfs") == 0) { |
| 1098 |
switch (op) { |
| 1099 |
case BD_FS_RESIZE: |
| 1100 |
return bd_fs_ntfs_resize (device, new_size, error); |
| 1101 |
case BD_FS_REPAIR: |
| 1102 |
return bd_fs_ntfs_repair (device, NULL, error); |
| 1103 |
case BD_FS_CHECK: |
| 1104 |
return bd_fs_ntfs_check (device, NULL, error); |
| 1105 |
case BD_FS_LABEL_CHECK: |
| 1106 |
return bd_fs_ntfs_check_label (label, error); |
| 1107 |
case BD_FS_LABEL: |
| 1108 |
return bd_fs_ntfs_set_label (device, label, error); |
| 1109 |
case BD_FS_UUID: |
| 1110 |
return bd_fs_ntfs_set_uuid (device, uuid, error); |
| 1111 |
case BD_FS_UUID_CHECK: |
| 1112 |
return bd_fs_ntfs_check_uuid (uuid, error); |
| 1113 |
default: |
| 1114 |
g_assert_not_reached (); |
| 1115 |
} |
| 1116 |
} else if (g_strcmp0 (detected_fstype, "f2fs") == 0) { |
| 1117 |
switch (op) { |
| 1118 |
case BD_FS_RESIZE: |
| 1119 |
return f2fs_resize_device (device, new_size, error); |
| 1120 |
case BD_FS_REPAIR: |
| 1121 |
return bd_fs_f2fs_repair (device, NULL, error); |
| 1122 |
case BD_FS_CHECK: |
| 1123 |
return bd_fs_f2fs_check (device, NULL, error); |
| 1124 |
case BD_FS_LABEL: |
| 1125 |
break; |
| 1126 |
case BD_FS_LABEL_CHECK: |
| 1127 |
return bd_fs_f2fs_check_label (label, error); |
| 1128 |
case BD_FS_UUID: |
| 1129 |
break; |
| 1130 |
case BD_FS_UUID_CHECK: |
| 1131 |
break; |
| 1132 |
default: |
| 1133 |
g_assert_not_reached (); |
| 1134 |
} |
| 1135 |
} else if (g_strcmp0 (detected_fstype, "nilfs2") == 0) { |
| 1136 |
switch (op) { |
| 1137 |
case BD_FS_RESIZE: |
| 1138 |
return nilfs2_resize_device (device, new_size, error); |
| 1139 |
case BD_FS_REPAIR: |
| 1140 |
break; |
| 1141 |
case BD_FS_CHECK: |
| 1142 |
break; |
| 1143 |
case BD_FS_LABEL: |
| 1144 |
return bd_fs_nilfs2_set_label (device, label, error); |
| 1145 |
case BD_FS_LABEL_CHECK: |
| 1146 |
return bd_fs_nilfs2_check_label (label, error); |
| 1147 |
case BD_FS_UUID: |
| 1148 |
return bd_fs_nilfs2_set_uuid (device, uuid, error); |
| 1149 |
case BD_FS_UUID_CHECK: |
| 1150 |
return bd_fs_nilfs2_check_uuid (uuid, error); |
| 1151 |
default: |
| 1152 |
g_assert_not_reached (); |
| 1153 |
} |
| 1154 |
} else if (g_strcmp0 (detected_fstype, "exfat") == 0) { |
| 1155 |
switch (op) { |
| 1156 |
case BD_FS_RESIZE: |
| 1157 |
break; |
| 1158 |
case BD_FS_REPAIR: |
| 1159 |
return bd_fs_exfat_repair (device, NULL, error); |
| 1160 |
case BD_FS_CHECK: |
| 1161 |
return bd_fs_exfat_check (device, NULL, error); |
| 1162 |
case BD_FS_LABEL: |
| 1163 |
return bd_fs_exfat_set_label (device, label, error); |
| 1164 |
case BD_FS_LABEL_CHECK: |
| 1165 |
return bd_fs_exfat_check_label (label, error); |
| 1166 |
case BD_FS_UUID: |
| 1167 |
return bd_fs_exfat_set_uuid (device, uuid, error); |
| 1168 |
case BD_FS_UUID_CHECK: |
| 1169 |
return bd_fs_exfat_check_uuid (uuid, error); |
| 1170 |
default: |
| 1171 |
g_assert_not_reached (); |
| 1172 |
} |
| 1173 |
} else if (g_strcmp0 (detected_fstype, "btrfs") == 0) { |
| 1174 |
switch (op) { |
| 1175 |
case BD_FS_RESIZE: |
| 1176 |
return btrfs_resize_device (device, new_size, error); |
| 1177 |
case BD_FS_REPAIR: |
| 1178 |
return bd_fs_btrfs_repair (device, NULL, error); |
| 1179 |
case BD_FS_CHECK: |
| 1180 |
return bd_fs_btrfs_check (device, NULL, error); |
| 1181 |
case BD_FS_LABEL: |
| 1182 |
return btrfs_set_label (device, label, error); |
| 1183 |
case BD_FS_LABEL_CHECK: |
| 1184 |
return bd_fs_btrfs_check_label (label, error); |
| 1185 |
case BD_FS_UUID: |
| 1186 |
return bd_fs_btrfs_set_uuid (device, uuid, error); |
| 1187 |
case BD_FS_UUID_CHECK: |
| 1188 |
return bd_fs_btrfs_check_uuid (uuid, error); |
| 1189 |
default: |
| 1190 |
g_assert_not_reached (); |
| 1191 |
} |
| 1192 |
} else if (g_strcmp0 (detected_fstype, "udf") == 0) { |
| 1193 |
switch (op) { |
| 1194 |
case BD_FS_RESIZE: |
| 1195 |
break; |
| 1196 |
case BD_FS_REPAIR: |
| 1197 |
break; |
| 1198 |
case BD_FS_CHECK: |
| 1199 |
break; |
| 1200 |
case BD_FS_LABEL: |
| 1201 |
return bd_fs_udf_set_label (device, label, error); |
| 1202 |
case BD_FS_LABEL_CHECK: |
| 1203 |
return bd_fs_udf_check_label (label, error); |
| 1204 |
case BD_FS_UUID: |
| 1205 |
return bd_fs_udf_set_uuid (device, uuid, error); |
| 1206 |
case BD_FS_UUID_CHECK: |
| 1207 |
return bd_fs_udf_check_uuid (uuid, error); |
| 1208 |
default: |
| 1209 |
g_assert_not_reached (); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
switch (op) { |
| 1213 |
case BD_FS_RESIZE: |
| 1214 |
op_name = "Resizing"; |
| 1215 |
break; |
| 1216 |
case BD_FS_REPAIR: |
| 1217 |
op_name = "Repairing"; |
| 1218 |
break; |
| 1219 |
case BD_FS_CHECK: |
| 1220 |
op_name = "Checking"; |
| 1221 |
break; |
| 1222 |
case BD_FS_LABEL: |
| 1223 |
op_name = "Setting the label of"; |
| 1224 |
break; |
| 1225 |
case BD_FS_LABEL_CHECK: |
| 1226 |
op_name = "Checking label format for"; |
| 1227 |
break; |
| 1228 |
case BD_FS_UUID: |
| 1229 |
op_name = "Setting UUID of"; |
| 1230 |
break; |
| 1231 |
case BD_FS_UUID_CHECK: |
| 1232 |
op_name = "Checking UUID format for"; |
| 1233 |
break; |
| 1234 |
default: |
| 1235 |
g_assert_not_reached (); |
| 1236 |
} |
| 1237 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1238 |
"%s filesystem '%s' is not supported.", op_name, detected_fstype); |
| 1239 |
return FALSE; |
| 1240 |
} |
| 1241 |
|
| 1242 |
/** |
| 1243 |
* bd_fs_resize: |
| 1244 |
* @device: the device the file system of which to resize |
| 1245 |
* @new_size: new requested size for the file system (if 0, the file system is |
| 1246 |
* adapted to the underlying block device) |
| 1247 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1248 |
* @error: (out) (optional): place to store error (if any) |
| 1249 |
* |
| 1250 |
* Resize filesystem on @device. This calls other fs resize functions from this |
| 1251 |
* plugin based on provided or detected filesystem (e.g. bd_fs_xfs_resize for XFS). |
| 1252 |
* This function will return an error for unknown/unsupported filesystems. |
| 1253 |
* |
| 1254 |
* Note: This function will mount @device for filesystems that can be resized only |
| 1255 |
* when mounted (like XFS or Btrfs). |
| 1256 |
* |
| 1257 |
* Returns: whether the file system on @device was successfully resized or not |
| 1258 |
* |
| 1259 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_RESIZE |
| 1260 |
*/ |
| 1261 |
gboolean bd_fs_resize (const gchar *device, guint64 new_size, const gchar *fstype, GError **error) { |
| 1262 |
return device_operation (device, fstype, BD_FS_RESIZE, new_size, NULL, NULL, error); |
| 1263 |
} |
| 1264 |
|
| 1265 |
/** |
| 1266 |
* bd_fs_repair: |
| 1267 |
* @device: the device the file system of which to repair |
| 1268 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1269 |
* @error: (out) (optional): place to store error (if any) |
| 1270 |
* |
| 1271 |
* Repair filesystem on @device. This calls other fs repair functions from this |
| 1272 |
* plugin based on detected filesystem (e.g. bd_fs_xfs_repair for XFS). This |
| 1273 |
* function will return an error for unknown/unsupported filesystems. |
| 1274 |
* |
| 1275 |
* Most filesystem tools typically require the filesystem not to be mounted. |
| 1276 |
* |
| 1277 |
* Returns: whether the file system on @device was successfully repaired or not |
| 1278 |
* |
| 1279 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_REPAIR |
| 1280 |
*/ |
| 1281 |
gboolean bd_fs_repair (const gchar *device, const gchar *fstype, GError **error) { |
| 1282 |
return device_operation (device, fstype, BD_FS_REPAIR, 0, NULL, NULL, error); |
| 1283 |
} |
| 1284 |
|
| 1285 |
/** |
| 1286 |
* bd_fs_check: |
| 1287 |
* @device: the device the file system of which to check |
| 1288 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1289 |
* @error: (out) (optional): place to store error (if any) |
| 1290 |
* |
| 1291 |
* Check filesystem on @device avoiding any modifications or repairs. |
| 1292 |
* This calls other fs check functions from this plugin based on detected |
| 1293 |
* filesystem (e.g. bd_fs_xfs_check for XFS). This function will return |
| 1294 |
* an error for unknown/unsupported filesystems. |
| 1295 |
* |
| 1296 |
* Note that depending on a corresponding filesystem type and configured |
| 1297 |
* features running this function on a mounted filesystem may result |
| 1298 |
* in false errors reported. |
| 1299 |
* |
| 1300 |
* Returns: whether the file system on @device passed the consistency check or not |
| 1301 |
* |
| 1302 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_CHECK |
| 1303 |
*/ |
| 1304 |
gboolean bd_fs_check (const gchar *device, const gchar *fstype, GError **error) { |
| 1305 |
return device_operation (device, fstype, BD_FS_CHECK, 0, NULL, NULL, error); |
| 1306 |
} |
| 1307 |
|
| 1308 |
/** |
| 1309 |
* bd_fs_check_label: |
| 1310 |
* @fstype: the filesystem type to check @label for |
| 1311 |
* @label: label to check |
| 1312 |
* @error: (out) (optional): place to store error (if any) |
| 1313 |
* |
| 1314 |
* This calls other fs check label functions from this plugin based on the provided |
| 1315 |
* filesystem (e.g. bd_fs_xfs_check_label for XFS). This function will return |
| 1316 |
* an error for unknown/unsupported filesystems. |
| 1317 |
* |
| 1318 |
* Returns: whether @label is a valid label for the @fstype file system or not |
| 1319 |
* (reason is provided in @error) |
| 1320 |
* |
| 1321 |
* Tech category: always available |
| 1322 |
*/ |
| 1323 |
gboolean bd_fs_check_label (const gchar *fstype, const gchar *label, GError **error) { |
| 1324 |
if (!fstype || strlen (fstype) == 0) { |
| 1325 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 1326 |
"Filesystem type must be specified to check label format"); |
| 1327 |
return FALSE; |
| 1328 |
} |
| 1329 |
return device_operation (NULL, fstype, BD_FS_LABEL_CHECK, 0, label, NULL, error); |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
/** |
| 1334 |
* bd_fs_set_label: |
| 1335 |
* @device: the device with file system to set the label for |
| 1336 |
* @label: label to set |
| 1337 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1338 |
* @error: (out) (optional): place to store error (if any) |
| 1339 |
* |
| 1340 |
* Set label for filesystem on @device. This calls other fs label functions from this |
| 1341 |
* plugin based on detected filesystem (e.g. bd_fs_xfs_set_label for XFS). This |
| 1342 |
* function will return an error for unknown/unsupported filesystems. |
| 1343 |
* |
| 1344 |
* Note: This function will mount @device for filesystems that need to be mounted |
| 1345 |
* to set label (like btrfs). |
| 1346 |
* |
| 1347 |
* Returns: whether the file system on @device was successfully relabeled or not |
| 1348 |
* |
| 1349 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_SET_LABEL |
| 1350 |
*/ |
| 1351 |
gboolean bd_fs_set_label (const gchar *device, const gchar *label, const gchar *fstype, GError **error) { |
| 1352 |
return device_operation (device, fstype, BD_FS_LABEL, 0, label, NULL, error); |
| 1353 |
} |
| 1354 |
|
| 1355 |
/** |
| 1356 |
* bd_fs_check_uuid: |
| 1357 |
* @fstype: the filesystem type to check @uuid for |
| 1358 |
* @uuid: uuid to check |
| 1359 |
* @error: (out) (optional): place to store error (if any) |
| 1360 |
* |
| 1361 |
* This calls other fs check uuid functions from this plugin based on the provided |
| 1362 |
* filesystem (e.g. bd_fs_xfs_check_uuid for XFS). This function will return |
| 1363 |
* an error for unknown/unsupported filesystems. |
| 1364 |
* |
| 1365 |
* Returns: whether @uuid is a valid UUID for the @fstype file system or not |
| 1366 |
* (reason is provided in @error) |
| 1367 |
* |
| 1368 |
* Tech category: always available |
| 1369 |
*/ |
| 1370 |
gboolean bd_fs_check_uuid (const gchar *fstype, const gchar *uuid, GError **error) { |
| 1371 |
if (!fstype || strlen (fstype) == 0) { |
| 1372 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 1373 |
"Filesystem type must be specified to check UUID format"); |
| 1374 |
return FALSE; |
| 1375 |
} |
| 1376 |
return device_operation (NULL, fstype, BD_FS_UUID_CHECK, 0, NULL, uuid, error); |
| 1377 |
} |
| 1378 |
|
| 1379 |
/** |
| 1380 |
* bd_fs_set_uuid: |
| 1381 |
* @device: the device with file system to set the UUID for |
| 1382 |
* @uuid: (nullable): UUID to set or %NULL to generate a new one |
| 1383 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1384 |
* @error: (out) (optional): place to store error (if any) |
| 1385 |
* |
| 1386 |
* Set UUID for filesystem on @device. This calls other fs UUID functions from this |
| 1387 |
* plugin based on detected filesystem (e.g. bd_fs_xfs_set_uuid for XFS). This |
| 1388 |
* function will return an error for unknown/unsupported filesystems. |
| 1389 |
* |
| 1390 |
* Returns: whether the UUID on the file system on @device was successfully changed or not |
| 1391 |
* |
| 1392 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_SET_UUID |
| 1393 |
*/ |
| 1394 |
gboolean bd_fs_set_uuid (const gchar *device, const gchar *uuid, const gchar *fstype, GError **error) { |
| 1395 |
return device_operation (device, fstype, BD_FS_UUID, 0, NULL, uuid, error); |
| 1396 |
} |
| 1397 |
|
| 1398 |
/** |
| 1399 |
* bd_fs_get_size: |
| 1400 |
* @device: the device with file system to get size for |
| 1401 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1402 |
* @error: (out) (optional): place to store error (if any) |
| 1403 |
* |
| 1404 |
* Get size for filesystem on @device. This calls other fs info functions from this |
| 1405 |
* plugin based on detected filesystem (e.g. bd_fs_xfs_get_info for XFS). This |
| 1406 |
* function will return an error for unknown/unsupported filesystems. |
| 1407 |
* |
| 1408 |
* Note: This function will mount @device for filesystems that need to be mounted |
| 1409 |
* to gather information (like btrfs). |
| 1410 |
* |
| 1411 |
* Returns: size of filesystem on @device, 0 in case of error. |
| 1412 |
* |
| 1413 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1414 |
*/ |
| 1415 |
guint64 bd_fs_get_size (const gchar *device, const gchar *fstype, GError **error) { |
| 1416 |
g_autofree gchar* detected_fstype = NULL; |
| 1417 |
guint64 size = 0; |
| 1418 |
|
| 1419 |
if (!fstype) { |
| 1420 |
detected_fstype = bd_fs_get_fstype (device, error); |
| 1421 |
if (!detected_fstype) { |
| 1422 |
if (error) { |
| 1423 |
if (*error == NULL) { |
| 1424 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 1425 |
"No filesystem detected on the device '%s'", device); |
| 1426 |
return 0; |
| 1427 |
} else { |
| 1428 |
g_prefix_error (error, "Error when trying to detect filesystem on '%s': ", device); |
| 1429 |
return 0; |
| 1430 |
} |
| 1431 |
} else |
| 1432 |
return 0; |
| 1433 |
} |
| 1434 |
} else |
| 1435 |
detected_fstype = g_strdup (fstype); |
| 1436 |
|
| 1437 |
if (g_strcmp0 (detected_fstype, "ext2") == 0 || g_strcmp0 (detected_fstype, "ext3") == 0 |
| 1438 |
|| g_strcmp0 (detected_fstype, "ext4") == 0) { |
| 1439 |
BDFSExt4Info* info = bd_fs_ext4_get_info (device, error); |
| 1440 |
if (info) { |
| 1441 |
size = info->block_size * info->block_count; |
| 1442 |
bd_fs_ext4_info_free (info); |
| 1443 |
} |
| 1444 |
return size; |
| 1445 |
|
| 1446 |
} else if (g_strcmp0 (detected_fstype, "xfs") == 0) { |
| 1447 |
BDFSXfsInfo *info = bd_fs_xfs_get_info (device, error); |
| 1448 |
if (info) { |
| 1449 |
size = info->block_size * info->block_count; |
| 1450 |
bd_fs_xfs_info_free (info); |
| 1451 |
} |
| 1452 |
return size; |
| 1453 |
} else if (g_strcmp0 (detected_fstype, "vfat") == 0) { |
| 1454 |
BDFSVfatInfo *info = bd_fs_vfat_get_info (device, error); |
| 1455 |
if (info) { |
| 1456 |
size = info->cluster_size * info->cluster_count; |
| 1457 |
bd_fs_vfat_info_free (info); |
| 1458 |
} |
| 1459 |
return size; |
| 1460 |
} else if (g_strcmp0 (detected_fstype, "ntfs") == 0) { |
| 1461 |
BDFSNtfsInfo *info = bd_fs_ntfs_get_info (device, error); |
| 1462 |
if (info) { |
| 1463 |
size = info->size; |
| 1464 |
bd_fs_ntfs_info_free (info); |
| 1465 |
} |
| 1466 |
return size; |
| 1467 |
} else if (g_strcmp0 (detected_fstype, "f2fs") == 0) { |
| 1468 |
BDFSF2FSInfo *info = bd_fs_f2fs_get_info (device, error); |
| 1469 |
if (info) { |
| 1470 |
size = info->sector_size * info->sector_count; |
| 1471 |
bd_fs_f2fs_info_free (info); |
| 1472 |
} |
| 1473 |
return size; |
| 1474 |
} else if (g_strcmp0 (detected_fstype, "nilfs2") == 0) { |
| 1475 |
BDFSNILFS2Info *info = bd_fs_nilfs2_get_info (device, error); |
| 1476 |
if (info) { |
| 1477 |
size = info->size; |
| 1478 |
bd_fs_nilfs2_info_free (info); |
| 1479 |
} |
| 1480 |
return size; |
| 1481 |
} else if (g_strcmp0 (detected_fstype, "exfat") == 0) { |
| 1482 |
BDFSExfatInfo *info = bd_fs_exfat_get_info (device, error); |
| 1483 |
if (info) { |
| 1484 |
size = info->sector_size * info->sector_count; |
| 1485 |
bd_fs_exfat_info_free (info); |
| 1486 |
} |
| 1487 |
return size; |
| 1488 |
} else if (g_strcmp0 (detected_fstype, "btrfs") == 0) { |
| 1489 |
BDFSBtrfsInfo *info = btrfs_get_info (device, error); |
| 1490 |
if (info) { |
| 1491 |
size = info->size; |
| 1492 |
bd_fs_btrfs_info_free (info); |
| 1493 |
} |
| 1494 |
return size; |
| 1495 |
} else if (g_strcmp0 (detected_fstype, "udf") == 0) { |
| 1496 |
BDFSUdfInfo *info = bd_fs_udf_get_info (device, error); |
| 1497 |
if (info) { |
| 1498 |
size = info->block_size * info->block_count; |
| 1499 |
bd_fs_udf_info_free (info); |
| 1500 |
} |
| 1501 |
return size; |
| 1502 |
} else { |
| 1503 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1504 |
"Getting size of filesystem '%s' is not supported.", detected_fstype); |
| 1505 |
return 0; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
/** |
| 1510 |
* bd_fs_get_free_space: |
| 1511 |
* @device: the device with file system to get free space for |
| 1512 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1513 |
* @error: (out) (optional): place to store error (if any) |
| 1514 |
* |
| 1515 |
* Get free space for filesystem on @device. This calls other fs info functions from this |
| 1516 |
* plugin based on detected filesystem (e.g. bd_fs_ext4_get_info for ext4). This |
| 1517 |
* function will return an error for unknown/unsupported filesystems. |
| 1518 |
* |
| 1519 |
* Returns: free space of filesystem on @device, 0 in case of error. |
| 1520 |
* |
| 1521 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1522 |
*/ |
| 1523 |
guint64 bd_fs_get_free_space (const gchar *device, const gchar *fstype, GError **error) { |
| 1524 |
g_autofree gchar* detected_fstype = NULL; |
| 1525 |
guint64 size = 0; |
| 1526 |
|
| 1527 |
if (!fstype) { |
| 1528 |
detected_fstype = bd_fs_get_fstype (device, error); |
| 1529 |
if (!detected_fstype) { |
| 1530 |
if (error) { |
| 1531 |
if (*error == NULL) { |
| 1532 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 1533 |
"No filesystem detected on the device '%s'", device); |
| 1534 |
return 0; |
| 1535 |
} else { |
| 1536 |
g_prefix_error (error, "Error when trying to detect filesystem on '%s': ", device); |
| 1537 |
return 0; |
| 1538 |
} |
| 1539 |
} else |
| 1540 |
return 0; |
| 1541 |
} |
| 1542 |
} else |
| 1543 |
detected_fstype = g_strdup (fstype); |
| 1544 |
|
| 1545 |
if (g_strcmp0 (detected_fstype, "ext2") == 0 || g_strcmp0 (detected_fstype, "ext3") == 0 |
| 1546 |
|| g_strcmp0 (detected_fstype, "ext4") == 0) { |
| 1547 |
BDFSExt4Info* info = bd_fs_ext4_get_info (device, error); |
| 1548 |
if (info) { |
| 1549 |
size = info->block_size * info->free_blocks; |
| 1550 |
bd_fs_ext4_info_free (info); |
| 1551 |
} |
| 1552 |
return size; |
| 1553 |
|
| 1554 |
} else if (g_strcmp0 (detected_fstype, "vfat") == 0) { |
| 1555 |
BDFSVfatInfo *info = bd_fs_vfat_get_info (device, error); |
| 1556 |
if (info) { |
| 1557 |
size = info->cluster_size * info->free_cluster_count; |
| 1558 |
bd_fs_vfat_info_free (info); |
| 1559 |
} |
| 1560 |
return size; |
| 1561 |
} else if (g_strcmp0 (detected_fstype, "ntfs") == 0) { |
| 1562 |
BDFSNtfsInfo *info = bd_fs_ntfs_get_info (device, error); |
| 1563 |
if (info) { |
| 1564 |
size = info->free_space; |
| 1565 |
bd_fs_ntfs_info_free (info); |
| 1566 |
} |
| 1567 |
return size; |
| 1568 |
} else if (g_strcmp0 (detected_fstype, "nilfs2") == 0) { |
| 1569 |
BDFSNILFS2Info *info = bd_fs_nilfs2_get_info (device, error); |
| 1570 |
if (info) { |
| 1571 |
size = info->block_size * info->free_blocks; |
| 1572 |
bd_fs_nilfs2_info_free (info); |
| 1573 |
} |
| 1574 |
return size; |
| 1575 |
} else if (g_strcmp0 (detected_fstype, "btrfs") == 0) { |
| 1576 |
BDFSBtrfsInfo *info = btrfs_get_info (device, error); |
| 1577 |
if (info) { |
| 1578 |
size = info->free_space; |
| 1579 |
bd_fs_btrfs_info_free (info); |
| 1580 |
} |
| 1581 |
return size; |
| 1582 |
} else { |
| 1583 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1584 |
"Getting free space on filesystem '%s' is not supported.", detected_fstype); |
| 1585 |
return 0; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
/** |
| 1590 |
* bd_fs_get_min_size: |
| 1591 |
* @device: the device with file system to get minimum size for |
| 1592 |
* @fstype: (nullable): the filesystem type on @device or %NULL to detect |
| 1593 |
* @error: (out) (optional): place to store error (if any) |
| 1594 |
* |
| 1595 |
* Get minimum size for filesystem on @device. This calls other fs info functions from this |
| 1596 |
* plugin based on detected filesystem (e.g. bd_fs_ext4_get_min_size for ext4). This |
| 1597 |
* function will return an error for unknown/unsupported filesystems. |
| 1598 |
* |
| 1599 |
* Returns: minimum size of filesystem on @device, 0 in case of error. |
| 1600 |
* |
| 1601 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_RESIZE |
| 1602 |
*/ |
| 1603 |
guint64 bd_fs_get_min_size (const gchar *device, const gchar *fstype, GError **error) { |
| 1604 |
g_autofree gchar* detected_fstype = NULL; |
| 1605 |
|
| 1606 |
if (!fstype) { |
| 1607 |
detected_fstype = bd_fs_get_fstype (device, error); |
| 1608 |
if (!detected_fstype) { |
| 1609 |
if (error) { |
| 1610 |
if (*error == NULL) { |
| 1611 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOFS, |
| 1612 |
"No filesystem detected on the device '%s'", device); |
| 1613 |
return 0; |
| 1614 |
} else { |
| 1615 |
g_prefix_error (error, "Error when trying to detect filesystem on '%s': ", device); |
| 1616 |
return 0; |
| 1617 |
} |
| 1618 |
} else |
| 1619 |
return 0; |
| 1620 |
} |
| 1621 |
} else |
| 1622 |
detected_fstype = g_strdup (fstype); |
| 1623 |
|
| 1624 |
if (g_strcmp0 (detected_fstype, "ext2") == 0 || g_strcmp0 (detected_fstype, "ext3") == 0 |
| 1625 |
|| g_strcmp0 (detected_fstype, "ext4") == 0) |
| 1626 |
return bd_fs_ext2_get_min_size (device, error); |
| 1627 |
else if (g_strcmp0 (detected_fstype, "ntfs") == 0) { |
| 1628 |
return bd_fs_ntfs_get_min_size (device, error); |
| 1629 |
} else { |
| 1630 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1631 |
"Getting minimum size of filesystem '%s' is not supported.", detected_fstype); |
| 1632 |
return 0; |
| 1633 |
} |
| 1634 |
} |
| 1635 |
|
| 1636 |
static gboolean query_fs_operation (const gchar *fs_type, BDFSOpType op, gchar **required_utility, BDFSResizeFlags *mode, BDFSMkfsOptionsFlags *options, GError **error) { |
| 1637 |
gboolean ret; |
| 1638 |
const BDFSInfo *fsinfo = NULL; |
| 1639 |
const gchar* op_name = NULL; |
| 1640 |
const gchar* exec_util = NULL; |
| 1641 |
BDFSTech tech; |
| 1642 |
|
| 1643 |
if (required_utility != NULL) |
| 1644 |
*required_utility = NULL; |
| 1645 |
|
| 1646 |
if (mode != NULL) |
| 1647 |
*mode = 0; |
| 1648 |
|
| 1649 |
if (options != NULL) |
| 1650 |
*options = 0; |
| 1651 |
|
| 1652 |
tech = fstype_to_tech (fs_type); |
| 1653 |
if (tech == BD_FS_TECH_GENERIC) { |
| 1654 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1655 |
"Filesystem '%s' is not supported.", fs_type); |
| 1656 |
return FALSE; |
| 1657 |
} |
| 1658 |
fsinfo = &fs_info[tech]; |
| 1659 |
|
| 1660 |
switch (op) { |
| 1661 |
case BD_FS_MKFS: |
| 1662 |
op_name = "Creating"; |
| 1663 |
exec_util = fsinfo->mkfs_util; |
| 1664 |
break; |
| 1665 |
case BD_FS_RESIZE: |
| 1666 |
op_name = "Resizing"; |
| 1667 |
exec_util = fsinfo->resize_util; |
| 1668 |
break; |
| 1669 |
case BD_FS_REPAIR: |
| 1670 |
op_name = "Repairing"; |
| 1671 |
exec_util = fsinfo->repair_util; |
| 1672 |
break; |
| 1673 |
case BD_FS_CHECK: |
| 1674 |
op_name = "Checking"; |
| 1675 |
exec_util = fsinfo->check_util; |
| 1676 |
break; |
| 1677 |
case BD_FS_LABEL: |
| 1678 |
op_name = "Setting the label of"; |
| 1679 |
exec_util = fsinfo->label_util; |
| 1680 |
break; |
| 1681 |
case BD_FS_UUID: |
| 1682 |
op_name = "Setting UUID of"; |
| 1683 |
exec_util = fsinfo->uuid_util; |
| 1684 |
break; |
| 1685 |
case BD_FS_GET_SIZE: |
| 1686 |
op_name = "Getting size of"; |
| 1687 |
exec_util = fsinfo->info_util; |
| 1688 |
break; |
| 1689 |
case BD_FS_GET_FREE_SPACE: |
| 1690 |
op_name = "Getting free space on"; |
| 1691 |
exec_util = fsinfo->info_util; |
| 1692 |
break; |
| 1693 |
case BD_FS_GET_INFO: |
| 1694 |
op_name = "Getting filesystem info of"; |
| 1695 |
exec_util = fsinfo->info_util; |
| 1696 |
break; |
| 1697 |
case BD_FS_GET_MIN_SIZE: |
| 1698 |
op_name = "Getting minimum size of"; |
| 1699 |
exec_util = fsinfo->minsize_util; |
| 1700 |
break; |
| 1701 |
default: |
| 1702 |
g_assert_not_reached (); |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (exec_util == NULL) { |
| 1706 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1707 |
"%s filesystem '%s' is not supported.", op_name, fs_type); |
| 1708 |
return FALSE; |
| 1709 |
} |
| 1710 |
|
| 1711 |
if (mode != NULL) |
| 1712 |
*mode = fs_features[tech].resize; |
| 1713 |
|
| 1714 |
if (options != NULL) |
| 1715 |
*options = fs_features[tech].mkfs; |
| 1716 |
|
| 1717 |
if (strlen (exec_util) == 0) { /* empty string if no util needed */ |
| 1718 |
return TRUE; |
| 1719 |
} |
| 1720 |
|
| 1721 |
ret = bd_utils_check_util_version (exec_util, NULL, "", NULL, NULL); |
| 1722 |
if (!ret && required_utility != NULL) |
| 1723 |
*required_utility = g_strdup (exec_util); |
| 1724 |
|
| 1725 |
return ret; |
| 1726 |
} |
| 1727 |
|
| 1728 |
/** |
| 1729 |
* bd_fs_can_mkfs: |
| 1730 |
* @type: the filesystem type to be tested for installed mkfs support |
| 1731 |
* @options: (out): flags for allowed mkfs options (i.e. support for setting label or UUID when creating the filesystem) |
| 1732 |
* @required_utility: (out) (transfer full): the utility binary which is required for creating (if missing returns %FALSE but no @error) |
| 1733 |
* @error: (out) (optional): place to store error (if any) |
| 1734 |
* |
| 1735 |
* Searches for the required utility to create the given filesystem and returns whether |
| 1736 |
* it is installed. The options flags indicate what additional options can be specified for @type. |
| 1737 |
* Unknown filesystems result in errors. |
| 1738 |
* |
| 1739 |
* Returns: whether filesystem mkfs tool is available |
| 1740 |
* |
| 1741 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1742 |
*/ |
| 1743 |
gboolean bd_fs_can_mkfs (const gchar *type, BDFSMkfsOptionsFlags *options, gchar **required_utility, GError **error) { |
| 1744 |
return query_fs_operation (type, BD_FS_MKFS, required_utility, NULL, options, error); |
| 1745 |
} |
| 1746 |
|
| 1747 |
/** |
| 1748 |
* bd_fs_can_resize: |
| 1749 |
* @type: the filesystem type to be tested for installed resize support |
| 1750 |
* @mode: (out): flags for allowed resizing (i.e. growing/shrinking support for online/offline) |
| 1751 |
* @required_utility: (out) (transfer full): the utility binary which is required for resizing (if missing i.e. returns FALSE but no error) |
| 1752 |
* @error: (out) (optional): place to store error (if any) |
| 1753 |
* |
| 1754 |
* Searches for the required utility to resize the given filesystem and returns whether |
| 1755 |
* it is installed. The mode flags indicate if growing and/or shrinking resize is available if |
| 1756 |
* mounted/unmounted. |
| 1757 |
* Unknown filesystems or filesystems which do not support resizing result in errors. |
| 1758 |
* |
| 1759 |
* Returns: whether filesystem resize is available |
| 1760 |
* |
| 1761 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1762 |
*/ |
| 1763 |
gboolean bd_fs_can_resize (const gchar *type, BDFSResizeFlags *mode, gchar **required_utility, GError **error) { |
| 1764 |
return query_fs_operation (type, BD_FS_RESIZE, required_utility, mode, NULL, error); |
| 1765 |
} |
| 1766 |
|
| 1767 |
/** |
| 1768 |
* bd_fs_can_check: |
| 1769 |
* @type: the filesystem type to be tested for installed consistency check support |
| 1770 |
* @required_utility: (out) (transfer full): the utility binary which is required for checking (if missing i.e. returns FALSE but no error) |
| 1771 |
* @error: (out) (optional): place to store error (if any) |
| 1772 |
* |
| 1773 |
* Searches for the required utility to check the given filesystem and returns whether |
| 1774 |
* it is installed. |
| 1775 |
* Unknown filesystems or filesystems which do not support checking result in errors. |
| 1776 |
* |
| 1777 |
* Returns: whether filesystem check is available |
| 1778 |
* |
| 1779 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1780 |
*/ |
| 1781 |
gboolean bd_fs_can_check (const gchar *type, gchar **required_utility, GError **error) { |
| 1782 |
return query_fs_operation (type, BD_FS_CHECK, required_utility, NULL, NULL, error); |
| 1783 |
} |
| 1784 |
|
| 1785 |
/** |
| 1786 |
* bd_fs_can_repair: |
| 1787 |
* @type: the filesystem type to be tested for installed repair support |
| 1788 |
* @required_utility: (out) (transfer full): the utility binary which is required for repairing (if missing i.e. return FALSE but no error) |
| 1789 |
* @error: (out) (optional): place to store error (if any) |
| 1790 |
* |
| 1791 |
* Searches for the required utility to repair the given filesystem and returns whether |
| 1792 |
* it is installed. |
| 1793 |
* Unknown filesystems or filesystems which do not support reparing result in errors. |
| 1794 |
* |
| 1795 |
* Returns: whether filesystem repair is available |
| 1796 |
* |
| 1797 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1798 |
*/ |
| 1799 |
gboolean bd_fs_can_repair (const gchar *type, gchar **required_utility, GError **error) { |
| 1800 |
return query_fs_operation (type, BD_FS_REPAIR, required_utility, NULL, NULL, error); |
| 1801 |
} |
| 1802 |
|
| 1803 |
/** |
| 1804 |
* bd_fs_can_set_label: |
| 1805 |
* @type: the filesystem type to be tested for installed label support |
| 1806 |
* @required_utility: (out) (transfer full): the utility binary which is required for relabeling (if missing i.e. return FALSE but no error) |
| 1807 |
* @error: (out) (optional): place to store error (if any) |
| 1808 |
* |
| 1809 |
* Searches for the required utility to set the label of the given filesystem and returns whether |
| 1810 |
* it is installed. |
| 1811 |
* Unknown filesystems or filesystems which do not support setting the label result in errors. |
| 1812 |
* |
| 1813 |
* Returns: whether setting filesystem label is available |
| 1814 |
* |
| 1815 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1816 |
*/ |
| 1817 |
gboolean bd_fs_can_set_label (const gchar *type, gchar **required_utility, GError **error) { |
| 1818 |
return query_fs_operation (type, BD_FS_LABEL, required_utility, NULL, NULL, error); |
| 1819 |
} |
| 1820 |
|
| 1821 |
/** |
| 1822 |
* bd_fs_can_set_uuid: |
| 1823 |
* @type: the filesystem type to be tested for installed UUID support |
| 1824 |
* @required_utility: (out) (transfer full): the utility binary which is required for setting UUID (if missing i.e. return FALSE but no error) |
| 1825 |
* @error: (out) (optional): place to store error (if any) |
| 1826 |
* |
| 1827 |
* Searches for the required utility to set the UUID of the given filesystem and returns whether |
| 1828 |
* it is installed. |
| 1829 |
* Unknown filesystems or filesystems which do not support setting the UUID result in errors. |
| 1830 |
* |
| 1831 |
* Returns: whether setting filesystem UUID is available |
| 1832 |
* |
| 1833 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1834 |
*/ |
| 1835 |
gboolean bd_fs_can_set_uuid (const gchar *type, gchar **required_utility, GError **error) { |
| 1836 |
return query_fs_operation (type, BD_FS_UUID, required_utility, NULL, NULL, error); |
| 1837 |
} |
| 1838 |
|
| 1839 |
/** |
| 1840 |
* bd_fs_can_get_size: |
| 1841 |
* @type: the filesystem type to be tested for installed size querying support |
| 1842 |
* @required_utility: (out) (transfer full): the utility binary which is required |
| 1843 |
* for size querying (if missing i.e. return FALSE but no error) |
| 1844 |
* @error: (out) (optional): place to store error (if any) |
| 1845 |
* |
| 1846 |
* Searches for the required utility to get size of the given filesystem and |
| 1847 |
* returns whether it is installed. |
| 1848 |
* Unknown filesystems or filesystems which do not support size querying result in errors. |
| 1849 |
* |
| 1850 |
* Returns: whether getting filesystem size is available |
| 1851 |
* |
| 1852 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1853 |
*/ |
| 1854 |
gboolean bd_fs_can_get_size (const gchar *type, gchar **required_utility, GError **error) { |
| 1855 |
return query_fs_operation (type, BD_FS_GET_SIZE, required_utility, NULL, NULL, error); |
| 1856 |
} |
| 1857 |
|
| 1858 |
/** |
| 1859 |
* bd_fs_can_get_free_space: |
| 1860 |
* @type: the filesystem type to be tested for installed free space querying support |
| 1861 |
* @required_utility: (out) (transfer full): the utility binary which is required |
| 1862 |
* for free space querying (if missing i.e. return FALSE but no error) |
| 1863 |
* @error: (out) (optional): place to store error (if any) |
| 1864 |
* |
| 1865 |
* Searches for the required utility to get free space of the given filesystem and |
| 1866 |
* returns whether it is installed. |
| 1867 |
* Unknown filesystems or filesystems which do not support free space querying result in errors. |
| 1868 |
* |
| 1869 |
* Returns: whether getting filesystem free space is available |
| 1870 |
* |
| 1871 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1872 |
*/ |
| 1873 |
gboolean bd_fs_can_get_free_space (const gchar *type, gchar **required_utility, GError **error) { |
| 1874 |
/* some filesystems can't tell us free space even if we have the tools */ |
| 1875 |
if (g_strcmp0 (type, "xfs") == 0 || g_strcmp0 (type, "f2fs") == 0 || g_strcmp0 (type, "exfat") == 0 || g_strcmp0 (type, "udf") == 0) { |
| 1876 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 1877 |
"Getting free space on filesystem '%s' is not supported.", type); |
| 1878 |
return FALSE; |
| 1879 |
} |
| 1880 |
|
| 1881 |
return query_fs_operation (type, BD_FS_GET_FREE_SPACE, required_utility, NULL, NULL, error); |
| 1882 |
} |
| 1883 |
|
| 1884 |
/** |
| 1885 |
* bd_fs_can_get_info: |
| 1886 |
* @type: the filesystem type to be tested for info querying support |
| 1887 |
* @required_utility: (out) (transfer full): the utility binary which is required |
| 1888 |
* for info querying (if missing i.e. return FALSE but no error) |
| 1889 |
* @error: (out) (optional): place to store error (if any) |
| 1890 |
* |
| 1891 |
* Searches for the required utility to get info of the given filesystem and |
| 1892 |
* returns whether it is installed. |
| 1893 |
* Unknown filesystems or filesystems which do not support info querying result in errors. |
| 1894 |
* |
| 1895 |
* Returns: whether getting filesystem info is available |
| 1896 |
* |
| 1897 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1898 |
*/ |
| 1899 |
gboolean bd_fs_can_get_info (const gchar *type, gchar **required_utility, GError **error) { |
| 1900 |
return query_fs_operation (type, BD_FS_GET_INFO, required_utility, NULL, NULL, error); |
| 1901 |
} |
| 1902 |
|
| 1903 |
/** |
| 1904 |
* bd_fs_can_get_min_size: |
| 1905 |
* @type: the filesystem type to be tested for installed minimum size querying support |
| 1906 |
* @required_utility: (out) (transfer full): the utility binary which is required |
| 1907 |
* for size querying (if missing i.e. return FALSE but no error) |
| 1908 |
* @error: (out) (optional): place to store error (if any) |
| 1909 |
* |
| 1910 |
* Searches for the required utility to get minimum size of the given filesystem and |
| 1911 |
* returns whether it is installed. |
| 1912 |
* Unknown filesystems or filesystems which do not support minimum size querying result in errors. |
| 1913 |
* |
| 1914 |
* Returns: whether getting filesystem size is available |
| 1915 |
* |
| 1916 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_QUERY |
| 1917 |
*/ |
| 1918 |
gboolean bd_fs_can_get_min_size (const gchar *type, gchar **required_utility, GError **error) { |
| 1919 |
return query_fs_operation (type, BD_FS_GET_MIN_SIZE, required_utility, NULL, NULL, error); |
| 1920 |
} |
| 1921 |
|
| 1922 |
static gboolean fs_freeze (const char *mountpoint, gboolean freeze, GError **error) { |
| 1923 |
gint fd = -1; |
| 1924 |
gint status = 0; |
| 1925 |
|
| 1926 |
if (!bd_fs_is_mountpoint (mountpoint, error)) { |
| 1927 |
if (error) { |
| 1928 |
if (*error != NULL) { |
| 1929 |
g_prefix_error (error, "Failed to check mountpoint '%s': ", mountpoint); |
| 1930 |
return FALSE; |
| 1931 |
} else { |
| 1932 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_MOUNTED, |
| 1933 |
"'%s' doesn't appear to be a mountpoint.", mountpoint); |
| 1934 |
return FALSE; |
| 1935 |
} |
| 1936 |
} else |
| 1937 |
return FALSE; |
| 1938 |
} |
| 1939 |
|
| 1940 |
fd = open (mountpoint, O_RDONLY); |
| 1941 |
if (fd == -1) { |
| 1942 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 1943 |
"Failed to open the mountpoint '%s': %s", |
| 1944 |
mountpoint, strerror_l (errno, _C_LOCALE)); |
| 1945 |
return FALSE; |
| 1946 |
} |
| 1947 |
|
| 1948 |
if (freeze) |
| 1949 |
status = ioctl (fd, FIFREEZE, 0); |
| 1950 |
else |
| 1951 |
status = ioctl (fd, FITHAW, 0); |
| 1952 |
|
| 1953 |
if (status != 0) { |
| 1954 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 1955 |
"Failed to %s '%s': %s.", |
| 1956 |
freeze ? "freeze" : "unfreeze", mountpoint, |
| 1957 |
strerror_l (errno, _C_LOCALE)); |
| 1958 |
close (fd); |
| 1959 |
return FALSE; |
| 1960 |
} |
| 1961 |
|
| 1962 |
close (fd); |
| 1963 |
|
| 1964 |
return TRUE; |
| 1965 |
} |
| 1966 |
|
| 1967 |
/** |
| 1968 |
* bd_fs_freeze: |
| 1969 |
* @mountpoint: mountpoint of the device (filesystem) to freeze |
| 1970 |
* @error: (out) (optional): place to store error (if any) |
| 1971 |
* |
| 1972 |
* Freezes filesystem mounted on @mountpoint. The filesystem must |
| 1973 |
* support freezing. |
| 1974 |
* |
| 1975 |
* Returns: whether @mountpoint was successfully freezed or not |
| 1976 |
* |
| 1977 |
*/ |
| 1978 |
gboolean bd_fs_freeze (const gchar *mountpoint, GError **error) { |
| 1979 |
return fs_freeze (mountpoint, TRUE, error); |
| 1980 |
} |
| 1981 |
|
| 1982 |
/** |
| 1983 |
* bd_fs_unfreeze: |
| 1984 |
* @mountpoint: mountpoint of the device (filesystem) to un-freeze |
| 1985 |
* @error: (out) (optional): place to store error (if any) |
| 1986 |
* |
| 1987 |
* Un-freezes filesystem mounted on @mountpoint. The filesystem must |
| 1988 |
* support freezing. |
| 1989 |
* |
| 1990 |
* Returns: whether @mountpoint was successfully unfreezed or not |
| 1991 |
* |
| 1992 |
*/ |
| 1993 |
gboolean bd_fs_unfreeze (const gchar *mountpoint, GError **error) { |
| 1994 |
return fs_freeze (mountpoint, FALSE, error); |
| 1995 |
} |
| 1996 |
|
| 1997 |
extern BDExtraArg** bd_fs_exfat_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 1998 |
extern BDExtraArg** bd_fs_ext2_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 1999 |
extern BDExtraArg** bd_fs_ext3_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2000 |
extern BDExtraArg** bd_fs_ext4_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2001 |
extern BDExtraArg** bd_fs_f2fs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2002 |
extern BDExtraArg** bd_fs_nilfs2_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2003 |
extern BDExtraArg** bd_fs_ntfs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2004 |
extern BDExtraArg** bd_fs_vfat_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2005 |
extern BDExtraArg** bd_fs_xfs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2006 |
extern BDExtraArg** bd_fs_btrfs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2007 |
extern BDExtraArg** bd_fs_udf_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra); |
| 2008 |
|
| 2009 |
/** |
| 2010 |
* bd_fs_mkfs: |
| 2011 |
* @device: the device to create the new filesystem on |
| 2012 |
* @fstype: name of the filesystem to create (e.g. "ext4") |
| 2013 |
* @options: additional options like label or UUID for the filesystem |
| 2014 |
* @extra: (nullable) (array zero-terminated=1): extra mkfs options not provided in @options |
| 2015 |
* @error: (out) (optional): place to store error (if any) |
| 2016 |
* |
| 2017 |
* This is a helper function for creating filesystems with extra options. |
| 2018 |
* This is the same as running a filesystem-specific function like %bd_fs_ext4_mkfs |
| 2019 |
* and manually specifying the extra command line options. %BDFSMkfsOptions |
| 2020 |
* removes the need to specify supported options for selected filesystems, |
| 2021 |
* make sure to check whether @fstype supports these options (see %bd_fs_can_mkfs) |
| 2022 |
* for details. |
| 2023 |
* |
| 2024 |
* When specifying additional mkfs options using @extra, it's caller's |
| 2025 |
* responsibility to make sure these options do not conflict with options |
| 2026 |
* specified using @options. Extra options are added after the @options and |
| 2027 |
* there are no additional checks for duplicate and/or conflicting options. |
| 2028 |
* |
| 2029 |
* Returns: whether @fstype was successfully created on @device or not. |
| 2030 |
* |
| 2031 |
* Tech category: %BD_FS_TECH_GENERIC-%BD_FS_TECH_MODE_MKFS |
| 2032 |
* |
| 2033 |
*/ |
| 2034 |
gboolean bd_fs_mkfs (const gchar *device, const gchar *fstype, BDFSMkfsOptions *options, const BDExtraArg **extra, GError **error) { |
| 2035 |
BDExtraArg **extra_args = NULL; |
| 2036 |
gboolean ret = FALSE; |
| 2037 |
|
| 2038 |
if (g_strcmp0 (fstype, "exfat") == 0) { |
| 2039 |
extra_args = bd_fs_exfat_mkfs_options (options, extra); |
| 2040 |
ret = bd_fs_exfat_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2041 |
} else if (g_strcmp0 (fstype, "ext2") == 0) { |
| 2042 |
extra_args = bd_fs_ext2_mkfs_options (options, extra); |
| 2043 |
ret = bd_fs_ext2_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2044 |
} else if (g_strcmp0 (fstype, "ext3") == 0) { |
| 2045 |
extra_args = bd_fs_ext3_mkfs_options (options, extra); |
| 2046 |
ret = bd_fs_ext3_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2047 |
} else if (g_strcmp0 (fstype, "ext4") == 0) { |
| 2048 |
extra_args = bd_fs_ext4_mkfs_options (options, extra); |
| 2049 |
ret = bd_fs_ext4_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2050 |
} else if (g_strcmp0 (fstype, "f2fs") == 0) { |
| 2051 |
extra_args = bd_fs_f2fs_mkfs_options (options, extra); |
| 2052 |
ret = bd_fs_f2fs_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2053 |
} else if (g_strcmp0 (fstype, "nilfs2") == 0) { |
| 2054 |
extra_args = bd_fs_nilfs2_mkfs_options (options, extra); |
| 2055 |
ret = bd_fs_nilfs2_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2056 |
} else if (g_strcmp0 (fstype, "ntfs") == 0) { |
| 2057 |
extra_args = bd_fs_ntfs_mkfs_options (options, extra); |
| 2058 |
ret = bd_fs_ntfs_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2059 |
} else if (g_strcmp0 (fstype, "vfat") == 0) { |
| 2060 |
extra_args = bd_fs_vfat_mkfs_options (options, extra); |
| 2061 |
ret = bd_fs_vfat_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2062 |
} else if (g_strcmp0 (fstype, "xfs") == 0) { |
| 2063 |
extra_args = bd_fs_xfs_mkfs_options (options, extra); |
| 2064 |
ret = bd_fs_xfs_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2065 |
} else if (g_strcmp0 (fstype, "btrfs") == 0) { |
| 2066 |
extra_args = bd_fs_btrfs_mkfs_options (options, extra); |
| 2067 |
ret = bd_fs_btrfs_mkfs (device, (const BDExtraArg **) extra_args, error); |
| 2068 |
} else if (g_strcmp0 (fstype, "udf") == 0) { |
| 2069 |
extra_args = bd_fs_udf_mkfs_options (options, extra); |
| 2070 |
ret = bd_fs_udf_mkfs (device, NULL, NULL, 0, (const BDExtraArg **) extra_args, error); |
| 2071 |
} else { |
| 2072 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 2073 |
"Filesystem '%s' is not supported.", fstype); |
| 2074 |
return FALSE; |
| 2075 |
} |
| 2076 |
|
| 2077 |
bd_extra_arg_list_free (extra_args); |
| 2078 |
return ret; |
| 2079 |
} |
| 2080 |
|
| 2081 |
/** |
| 2082 |
* bd_fs_features: |
| 2083 |
* @fstype: name of the filesystem to get features for (e.g. "ext4") |
| 2084 |
* @error: (allow-none): (out): place to store error (if any) |
| 2085 |
* |
| 2086 |
* Returns (transfer-none): features supported by @fstype, see %BDFSFeatures for more information. |
| 2087 |
* |
| 2088 |
* Tech category: always available |
| 2089 |
* |
| 2090 |
*/ |
| 2091 |
const BDFSFeatures* bd_fs_features (const gchar *fstype, GError **error) { |
| 2092 |
BDFSTech tech = fstype_to_tech (fstype); |
| 2093 |
|
| 2094 |
if (tech == BD_FS_TECH_GENERIC) { |
| 2095 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_NOT_SUPPORTED, |
| 2096 |
"Filesystem '%s' is not supported.", fstype); |
| 2097 |
return NULL; |
| 2098 |
} |
| 2099 |
|
| 2100 |
return &fs_features[tech]; |
| 2101 |
} |