Skip to content

Commit 04fffa4

Browse files
committed
btrfs-progs: filesystem show: introduce printing helper functions
Introduce helper functions for printing the filesystem data and list of devices. This prepares the both printing code paths of filesystem show to support json output. Signed-off-by: Jelle van der Waa <[email protected]>
1 parent 60abf7a commit 04fffa4

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

cmds/filesystem.c

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,34 @@ static void splice_device_list(struct list_head *seed_devices,
273273
list_splice(seed_devices, all_devices);
274274
}
275275

276+
static void print_filesystem_info(char *label, char uuidbuf[BTRFS_UUID_UNPARSED_SIZE],
277+
u64 bytes_used, u64 num_devices,
278+
unsigned unit_mode)
279+
{
280+
if (label)
281+
pr_verbose(LOG_DEFAULT, "Label: '%s' ", label);
282+
else
283+
pr_verbose(LOG_DEFAULT, "Label: none ");
284+
285+
pr_verbose(LOG_DEFAULT, " uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
286+
num_devices,
287+
pretty_size_mode(bytes_used,
288+
unit_mode));
289+
}
290+
291+
static void print_filesystem_device(u64 devid, u64 total_bytes, u64 bytes_used,
292+
char *path,
293+
bool missing,
294+
unsigned unit_mode)
295+
{
296+
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size %s used %s path %s%s\n",
297+
devid,
298+
pretty_size_mode(total_bytes, unit_mode),
299+
pretty_size_mode(bytes_used, unit_mode),
300+
path,
301+
missing ? " MISSING" : "");
302+
}
303+
276304
static void print_devices(struct btrfs_fs_devices *fs_devices,
277305
u64 *devs_found, unsigned unit_mode)
278306
{
@@ -290,12 +318,11 @@ static void print_devices(struct btrfs_fs_devices *fs_devices,
290318

291319
list_sort(NULL, all_devices, cmp_device_id);
292320
list_for_each_entry(device, all_devices, dev_list) {
293-
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size %s used %s path %s\n",
294-
device->devid,
295-
pretty_size_mode(device->total_bytes, unit_mode),
296-
pretty_size_mode(device->bytes_used, unit_mode),
297-
device->name);
298-
321+
print_filesystem_device(device->devid,
322+
device->total_bytes, device->bytes_used,
323+
device->name,
324+
false,
325+
unit_mode);
299326
(*devs_found)++;
300327
}
301328
}
@@ -314,14 +341,11 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices,
314341
uuid_unparse(fs_devices->fsid, uuidbuf);
315342
device = list_entry(fs_devices->devices.next, struct btrfs_device,
316343
dev_list);
317-
if (device->label && device->label[0])
318-
pr_verbose(LOG_DEFAULT, "Label: '%s' ", device->label);
319-
else
320-
pr_verbose(LOG_DEFAULT, "Label: none ");
321-
322344
total = device->total_devs;
323-
pr_verbose(LOG_DEFAULT, " uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
324-
total, pretty_size_mode(device->super_bytes_used, unit_mode));
345+
346+
print_filesystem_info(device->label && device->label[0] ? device->label : NULL, uuidbuf,
347+
device->super_bytes_used, total,
348+
unit_mode);
325349

326350
print_devices(fs_devices, &devs_found, unit_mode);
327351

@@ -360,15 +384,9 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
360384
return ret;
361385

362386
uuid_unparse(fs_info->fsid, uuidbuf);
363-
if (label && *label)
364-
pr_verbose(LOG_DEFAULT, "Label: '%s' ", label);
365-
else
366-
pr_verbose(LOG_DEFAULT, "Label: none ");
367-
368-
pr_verbose(LOG_DEFAULT, " uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
369-
fs_info->num_devices,
370-
pretty_size_mode(calc_used_bytes(space_info),
371-
unit_mode));
387+
print_filesystem_info(label && *label ? label : NULL, uuidbuf,
388+
calc_used_bytes(space_info), fs_info->num_devices,
389+
unit_mode);
372390

373391
for (i = 0; i < fs_info->num_devices; i++) {
374392
char *canonical_path;
@@ -378,18 +396,20 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
378396
/* Add check for missing devices even mounted */
379397
fd = open((char *)tmp_dev_info->path, O_RDONLY);
380398
if (fd < 0) {
381-
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size 0 used 0 path %s MISSING\n",
382-
tmp_dev_info->devid, tmp_dev_info->path);
399+
print_filesystem_device(tmp_dev_info->devid,
400+
0, 0,
401+
(char *)tmp_dev_info->path,
402+
true,
403+
unit_mode);
383404
continue;
384-
385405
}
386406
close(fd);
387407
canonical_path = path_canonicalize((char *)tmp_dev_info->path);
388-
pr_verbose(LOG_DEFAULT, "\tdevid %4llu size %s used %s path %s\n",
389-
tmp_dev_info->devid,
390-
pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
391-
pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
392-
canonical_path);
408+
print_filesystem_device(tmp_dev_info->devid,
409+
tmp_dev_info->total_bytes, tmp_dev_info->bytes_used,
410+
canonical_path,
411+
false,
412+
unit_mode);
393413

394414
free(canonical_path);
395415
}

0 commit comments

Comments
 (0)