]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/swapext.c
libfrog: print library errors
[thirdparty/xfsprogs-dev.git] / io / swapext.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018 Red Hat, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "command.h"
8 #include "input.h"
9 #include "init.h"
10 #include "io.h"
11 #include "libfrog/logging.h"
12 #include "libfrog/fsgeom.h"
13 #include "libfrog/bulkstat.h"
14
15 static cmdinfo_t swapext_cmd;
16
17 static void
18 swapext_help(void)
19 {
20 printf(_(
21 "\n"
22 " Swaps extents between the open file descriptor and the supplied filename.\n"
23 "\n"));
24 }
25
26 static int
27 swapext_f(
28 int argc,
29 char **argv)
30 {
31 struct xfs_fd fxfd = XFS_FD_INIT(file->fd);
32 struct xfs_bulkstat bulkstat;
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
53 error = xfrog_bulkstat_single(&fxfd, stat.st_ino, 0, &bulkstat);
54 if (error) {
55 xfrog_perror(error, "bulkstat");
56 goto out;
57 }
58 error = xfrog_bulkstat_v5_to_v1(&fxfd, &sx.sx_stat, &bulkstat);
59 if (error) {
60 xfrog_perror(error, "bulkstat conversion");
61 goto out;
62 }
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
72 out:
73 close(fd);
74 return 0;
75 }
76
77 void
78 swapext_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 }