]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - io/swapext.c
xfsprogs: Release v6.7.0
[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"
9fc3ef62 11#include "libfrog/logging.h"
fee68490 12#include "libfrog/fsgeom.h"
f31b5e12 13#include "libfrog/bulkstat.h"
57e7138a
BF
14
15static cmdinfo_t swapext_cmd;
16
17static void
18swapext_help(void)
19{
20 printf(_(
21"\n"
22" Swaps extents between the open file descriptor and the supplied filename.\n"
23"\n"));
24}
25
57e7138a
BF
26static int
27swapext_f(
28 int argc,
29 char **argv)
30{
f31b5e12 31 struct xfs_fd fxfd = XFS_FD_INIT(file->fd);
4cca629d 32 struct xfs_bulkstat bulkstat;
57e7138a
BF
33 int fd;
34 int error;
35 struct xfs_swapext sx;
36 struct stat stat;
37
38 /* open the donor file */
39 fd = openfile(argv[1], NULL, 0, 0, NULL);
40 if (fd < 0)
41 return 0;
42
43 /*
44 * stat the target file to get the inode number and use the latter to
45 * get the bulkstat info for the swapext cmd.
46 */
47 error = fstat(file->fd, &stat);
48 if (error) {
49 perror("fstat");
50 goto out;
51 }
52
e6542132 53 error = -xfrog_bulkstat_single(&fxfd, stat.st_ino, 0, &bulkstat);
57e7138a 54 if (error) {
9fc3ef62 55 xfrog_perror(error, "bulkstat");
57e7138a
BF
56 goto out;
57 }
e6542132 58 error = -xfrog_bulkstat_v5_to_v1(&fxfd, &sx.sx_stat, &bulkstat);
4cca629d 59 if (error) {
9fc3ef62 60 xfrog_perror(error, "bulkstat conversion");
4cca629d
DW
61 goto out;
62 }
57e7138a
BF
63 sx.sx_version = XFS_SX_VERSION;
64 sx.sx_fdtarget = file->fd;
65 sx.sx_fdtmp = fd;
66 sx.sx_offset = 0;
67 sx.sx_length = stat.st_size;
68 error = ioctl(file->fd, XFS_IOC_SWAPEXT, &sx);
69 if (error)
70 perror("swapext");
71
72out:
73 close(fd);
74 return 0;
75}
76
77void
78swapext_init(void)
79{
80 swapext_cmd.name = "swapext";
81 swapext_cmd.cfunc = swapext_f;
82 swapext_cmd.argmin = 1;
83 swapext_cmd.argmax = 1;
84 swapext_cmd.flags = CMD_NOMAP_OK;
85 swapext_cmd.args = _("<donorfile>");
86 swapext_cmd.oneline = _("Swap extents between files.");
87 swapext_cmd.help = swapext_help;
88
89 add_command(&swapext_cmd);
90}