]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - spaceman/file.c
xfs_repair: don't crash if da btree is corrupt
[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) {
72 perror("XFS_IOC_FSGEOMETRY");
73 close(fd);
74 return -1;
75 }
cccf6abc
DC
76
77 if (fs_path) {
78 fsp = fs_table_lookup(path, FS_MOUNT_POINT);
79 if (!fsp) {
80 fprintf(stderr, _("%s: cannot find mount point."),
81 path);
1cda9090 82 close(fd);
cccf6abc
DC
83 return -1;
84 }
85 memcpy(fs_path, fsp, sizeof(struct fs_path));
86 }
48ec2905
DC
87 return fd;
88}
89
90int
91addfile(
92 char *name,
93 int fd,
cccf6abc
DC
94 xfs_fsop_geom_t *geometry,
95 struct fs_path *fs_path)
48ec2905
DC
96{
97 char *filename;
98
99 filename = strdup(name);
100 if (!filename) {
101 perror("strdup");
102 close(fd);
103 return -1;
104 }
105
106 /* Extend the table of currently open files */
107 filetable = (fileio_t *)realloc(filetable, /* growing */
108 ++filecount * sizeof(fileio_t));
109 if (!filetable) {
110 perror("realloc");
111 filecount = 0;
112 free(filename);
113 close(fd);
114 return -1;
115 }
116
117 /* Finally, make this the new active open file */
118 file = &filetable[filecount - 1];
119 file->fd = fd;
120 file->name = filename;
121 file->geom = *geometry;
cccf6abc 122 memcpy(&file->fs_path, fs_path, sizeof(file->fs_path));
48ec2905
DC
123 return 0;
124}
125
126void
127print_init(void)
128{
129 print_cmd.name = "print";
130 print_cmd.altname = "p";
131 print_cmd.cfunc = print_f;
132 print_cmd.argmin = 0;
133 print_cmd.argmax = 0;
134 print_cmd.flags = CMD_FLAG_ONESHOT;
135 print_cmd.oneline = _("list current open files");
136
137 add_command(&print_cmd);
138}