]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - spaceman/file.c
xfs: don't assert when on-disk btree pointers are garbage
[thirdparty/xfsprogs-dev.git] / spaceman / file.c
CommitLineData
48ec2905
DC
1/*
2 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2012 Red Hat, Inc.
4 * All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include "libxfs.h"
21#include <sys/mman.h>
22#include "command.h"
23#include "input.h"
24#include "init.h"
cccf6abc 25#include "path.h"
48ec2905
DC
26#include "space.h"
27
28static cmdinfo_t print_cmd;
29
30fileio_t *filetable;
31int filecount;
32fileio_t *file;
33
34static void
35print_fileio(
36 fileio_t *file,
37 int index,
38 int braces)
39{
40 printf(_("%c%03d%c %-14s\n"), braces? '[' : ' ', index,
41 braces? ']' : ' ', file->name);
42}
43
44static int
45print_f(
46 int argc,
47 char **argv)
48{
49 int i;
50
51 for (i = 0; i < filecount; i++)
52 print_fileio(&filetable[i], i, &filetable[i] == file);
53 return 0;
54}
55
56int
57openfile(
58 char *path,
cccf6abc
DC
59 xfs_fsop_geom_t *geom,
60 struct fs_path *fs_path)
48ec2905 61{
cccf6abc 62 struct fs_path *fsp;
48ec2905
DC
63 int fd;
64
65 fd = open(path, 0);
66 if (fd < 0) {
67 perror(path);
68 return -1;
69 }
70
71 if (ioctl(fd, XFS_IOC_FSGEOMETRY, geom) < 0) {
6fb70877
DW
72 if (errno == ENOTTY)
73 fprintf(stderr,
74_("%s: Not on a mounted XFS filesystem.\n"),
75 path);
76 else
77 perror("XFS_IOC_FSGEOMETRY");
48ec2905
DC
78 close(fd);
79 return -1;
80 }
cccf6abc
DC
81
82 if (fs_path) {
83 fsp = fs_table_lookup(path, FS_MOUNT_POINT);
84 if (!fsp) {
85 fprintf(stderr, _("%s: cannot find mount point."),
86 path);
1cda9090 87 close(fd);
cccf6abc
DC
88 return -1;
89 }
90 memcpy(fs_path, fsp, sizeof(struct fs_path));
91 }
48ec2905
DC
92 return fd;
93}
94
95int
96addfile(
97 char *name,
98 int fd,
cccf6abc
DC
99 xfs_fsop_geom_t *geometry,
100 struct fs_path *fs_path)
48ec2905
DC
101{
102 char *filename;
103
104 filename = strdup(name);
105 if (!filename) {
106 perror("strdup");
107 close(fd);
108 return -1;
109 }
110
111 /* Extend the table of currently open files */
112 filetable = (fileio_t *)realloc(filetable, /* growing */
113 ++filecount * sizeof(fileio_t));
114 if (!filetable) {
115 perror("realloc");
116 filecount = 0;
117 free(filename);
118 close(fd);
119 return -1;
120 }
121
122 /* Finally, make this the new active open file */
123 file = &filetable[filecount - 1];
124 file->fd = fd;
125 file->name = filename;
126 file->geom = *geometry;
cccf6abc 127 memcpy(&file->fs_path, fs_path, sizeof(file->fs_path));
48ec2905
DC
128 return 0;
129}
130
131void
132print_init(void)
133{
134 print_cmd.name = "print";
135 print_cmd.altname = "p";
136 print_cmd.cfunc = print_f;
137 print_cmd.argmin = 0;
138 print_cmd.argmax = 0;
139 print_cmd.flags = CMD_FLAG_ONESHOT;
140 print_cmd.oneline = _("list current open files");
141
142 add_command(&print_cmd);
143}