]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - io/swapext.c
libfrog: refactor open-coded bulkstat calls
[thirdparty/xfsprogs-dev.git] / io / swapext.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
57e7138a
BF
2/*
3 * Copyright (c) 2018 Red Hat, Inc.
4 * All Rights Reserved.
57e7138a
BF
5 */
6
7#include "command.h"
8#include "input.h"
9#include "init.h"
10#include "io.h"
f31b5e12
DW
11#include "fsgeom.h"
12#include "libfrog/bulkstat.h"
57e7138a
BF
13
14static cmdinfo_t swapext_cmd;
15
16static void
17swapext_help(void)
18{
19 printf(_(
20"\n"
21" Swaps extents between the open file descriptor and the supplied filename.\n"
22"\n"));
23}
24
57e7138a
BF
25static int
26swapext_f(
27 int argc,
28 char **argv)
29{
f31b5e12 30 struct xfs_fd fxfd = XFS_FD_INIT(file->fd);
57e7138a
BF
31 int fd;
32 int error;
33 struct xfs_swapext sx;
34 struct stat stat;
35
36 /* open the donor file */
37 fd = openfile(argv[1], NULL, 0, 0, NULL);
38 if (fd < 0)
39 return 0;
40
41 /*
42 * stat the target file to get the inode number and use the latter to
43 * get the bulkstat info for the swapext cmd.
44 */
45 error = fstat(file->fd, &stat);
46 if (error) {
47 perror("fstat");
48 goto out;
49 }
50
f31b5e12 51 error = xfrog_bulkstat_single(&fxfd, stat.st_ino, &sx.sx_stat);
57e7138a 52 if (error) {
f31b5e12 53 errno = error;
57e7138a
BF
54 perror("bulkstat");
55 goto out;
56 }
57 sx.sx_version = XFS_SX_VERSION;
58 sx.sx_fdtarget = file->fd;
59 sx.sx_fdtmp = fd;
60 sx.sx_offset = 0;
61 sx.sx_length = stat.st_size;
62 error = ioctl(file->fd, XFS_IOC_SWAPEXT, &sx);
63 if (error)
64 perror("swapext");
65
66out:
67 close(fd);
68 return 0;
69}
70
71void
72swapext_init(void)
73{
74 swapext_cmd.name = "swapext";
75 swapext_cmd.cfunc = swapext_f;
76 swapext_cmd.argmin = 1;
77 swapext_cmd.argmax = 1;
78 swapext_cmd.flags = CMD_NOMAP_OK;
79 swapext_cmd.args = _("<donorfile>");
80 swapext_cmd.oneline = _("Swap extents between files.");
81 swapext_cmd.help = swapext_help;
82
83 add_command(&swapext_cmd);
84}