]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/init.c
xfs_io: implement 'copy_range' command
[thirdparty/xfsprogs-dev.git] / io / init.c
1 /*
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "platform_defs.h"
20 #include "command.h"
21 #include "input.h"
22 #include "init.h"
23 #include "io.h"
24
25 char *progname;
26 int exitcode;
27 int expert;
28 size_t pagesize;
29 struct timeval stopwatch;
30
31 void
32 usage(void)
33 {
34 fprintf(stderr,
35 _("Usage: %s [-adfmnrRstVx] [-p prog] [-c cmd]... file\n"),
36 progname);
37 exit(1);
38 }
39
40 void
41 init_cvtnum(
42 size_t *blocksize,
43 size_t *sectsize)
44 {
45 if (!file || (file->flags & IO_FOREIGN)) {
46 *blocksize = 4096;
47 *sectsize = 512;
48 } else {
49 *blocksize = file->geom.blocksize;
50 *sectsize = file->geom.sectsize;
51 }
52 }
53
54 static void
55 init_commands(void)
56 {
57 attr_init();
58 bmap_init();
59 copy_range_init();
60 fadvise_init();
61 file_init();
62 flink_init();
63 freeze_init();
64 fsync_init();
65 getrusage_init();
66 help_init();
67 imap_init();
68 inject_init();
69 seek_init();
70 madvise_init();
71 mincore_init();
72 mmap_init();
73 open_init();
74 parent_init();
75 pread_init();
76 prealloc_init();
77 fiemap_init();
78 pwrite_init();
79 quit_init();
80 readdir_init();
81 resblks_init();
82 sendfile_init();
83 shutdown_init();
84 sync_init();
85 sync_range_init();
86 truncate_init();
87 reflink_init();
88 }
89
90 static int
91 init_args_command(
92 int index)
93 {
94 if (index >= filecount)
95 return 0;
96 file = &filetable[index++];
97 return index;
98 }
99
100 static int
101 init_check_command(
102 const cmdinfo_t *ct)
103 {
104 if (ct->flags & CMD_FLAG_GLOBAL)
105 return 1;
106
107 if (!file && !(ct->flags & CMD_NOFILE_OK)) {
108 fprintf(stderr, _("no files are open, try 'help open'\n"));
109 return 0;
110 }
111 if (!mapping && !(ct->flags & CMD_NOMAP_OK)) {
112 fprintf(stderr, _("no mapped regions, try 'help mmap'\n"));
113 return 0;
114 }
115 if (file && !(ct->flags & CMD_FOREIGN_OK) &&
116 (file->flags & IO_FOREIGN)) {
117 fprintf(stderr,
118 _("foreign file active, %s command is for XFS filesystems only\n"),
119 ct->name);
120 return 0;
121 }
122 return 1;
123 }
124
125 void
126 init(
127 int argc,
128 char **argv)
129 {
130 int c, flags = 0;
131 char *sp;
132 mode_t mode = 0600;
133 xfs_fsop_geom_t geometry = { 0 };
134
135 progname = basename(argv[0]);
136 setlocale(LC_ALL, "");
137 bindtextdomain(PACKAGE, LOCALEDIR);
138 textdomain(PACKAGE);
139
140 pagesize = getpagesize();
141 gettimeofday(&stopwatch, NULL);
142
143 while ((c = getopt(argc, argv, "ac:dFfmp:nrRstTVx")) != EOF) {
144 switch (c) {
145 case 'a':
146 flags |= IO_APPEND;
147 break;
148 case 'c':
149 add_user_command(optarg);
150 break;
151 case 'd':
152 flags |= IO_DIRECT;
153 break;
154 case 'F':
155 /* Ignored / deprecated now, handled automatically */
156 break;
157 case 'f':
158 flags |= IO_CREAT;
159 break;
160 case 'm':
161 mode = strtoul(optarg, &sp, 0);
162 if (!sp || sp == optarg) {
163 fprintf(stderr, _("non-numeric mode -- %s\n"),
164 optarg);
165 exit(1);
166 }
167 break;
168 case 'n':
169 flags |= IO_NONBLOCK;
170 break;
171 case 'p':
172 progname = optarg;
173 break;
174 case 'r':
175 flags |= IO_READONLY;
176 break;
177 case 's':
178 flags |= IO_OSYNC;
179 break;
180 case 't':
181 flags |= IO_TRUNC;
182 break;
183 case 'R':
184 flags |= IO_REALTIME;
185 break;
186 case 'T':
187 flags |= IO_TMPFILE;
188 break;
189 case 'x':
190 expert = 1;
191 break;
192 case 'V':
193 printf(_("%s version %s\n"), progname, VERSION);
194 exit(0);
195 default:
196 usage();
197 }
198 }
199
200 while (optind < argc) {
201 if ((c = openfile(argv[optind], &geometry, flags, mode)) < 0)
202 exit(1);
203 if (!platform_test_xfs_fd(c))
204 flags |= IO_FOREIGN;
205 if (addfile(argv[optind], c, &geometry, flags) < 0)
206 exit(1);
207 optind++;
208 }
209
210 init_commands();
211 add_args_command(init_args_command);
212 add_check_command(init_check_command);
213 }
214
215 int
216 main(
217 int argc,
218 char **argv)
219 {
220 init(argc, argv);
221 command_loop();
222 return exitcode;
223 }