]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - spaceman/file.c
xfs_spaceman: space management tool
[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"
25#include "space.h"
26
27static cmdinfo_t print_cmd;
28
29fileio_t *filetable;
30int filecount;
31fileio_t *file;
32
33static void
34print_fileio(
35 fileio_t *file,
36 int index,
37 int braces)
38{
39 printf(_("%c%03d%c %-14s\n"), braces? '[' : ' ', index,
40 braces? ']' : ' ', file->name);
41}
42
43static int
44print_f(
45 int argc,
46 char **argv)
47{
48 int i;
49
50 for (i = 0; i < filecount; i++)
51 print_fileio(&filetable[i], i, &filetable[i] == file);
52 return 0;
53}
54
55int
56openfile(
57 char *path,
58 xfs_fsop_geom_t *geom)
59{
60 int fd;
61
62 fd = open(path, 0);
63 if (fd < 0) {
64 perror(path);
65 return -1;
66 }
67
68 if (ioctl(fd, XFS_IOC_FSGEOMETRY, geom) < 0) {
69 perror("XFS_IOC_FSGEOMETRY");
70 close(fd);
71 return -1;
72 }
73 return fd;
74}
75
76int
77addfile(
78 char *name,
79 int fd,
80 xfs_fsop_geom_t *geometry)
81{
82 char *filename;
83
84 filename = strdup(name);
85 if (!filename) {
86 perror("strdup");
87 close(fd);
88 return -1;
89 }
90
91 /* Extend the table of currently open files */
92 filetable = (fileio_t *)realloc(filetable, /* growing */
93 ++filecount * sizeof(fileio_t));
94 if (!filetable) {
95 perror("realloc");
96 filecount = 0;
97 free(filename);
98 close(fd);
99 return -1;
100 }
101
102 /* Finally, make this the new active open file */
103 file = &filetable[filecount - 1];
104 file->fd = fd;
105 file->name = filename;
106 file->geom = *geometry;
107 return 0;
108}
109
110void
111print_init(void)
112{
113 print_cmd.name = "print";
114 print_cmd.altname = "p";
115 print_cmd.cfunc = print_f;
116 print_cmd.argmin = 0;
117 print_cmd.argmax = 0;
118 print_cmd.flags = CMD_FLAG_ONESHOT;
119 print_cmd.oneline = _("list current open files");
120
121 add_command(&print_cmd);
122}