]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - spaceman/file.c
xfsprogs: update spdx tags in LICENSES/
[thirdparty/xfsprogs-dev.git] / spaceman / file.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
48ec2905
DC
2/*
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2012 Red Hat, Inc.
5 * All Rights Reserved.
48ec2905
DC
6 */
7
8#include "libxfs.h"
9#include <sys/mman.h>
10#include "command.h"
11#include "input.h"
12#include "init.h"
cccf6abc 13#include "path.h"
48ec2905
DC
14#include "space.h"
15
16static cmdinfo_t print_cmd;
17
18fileio_t *filetable;
19int filecount;
20fileio_t *file;
21
22static void
23print_fileio(
24 fileio_t *file,
25 int index,
26 int braces)
27{
28 printf(_("%c%03d%c %-14s\n"), braces? '[' : ' ', index,
29 braces? ']' : ' ', file->name);
30}
31
32static int
33print_f(
34 int argc,
35 char **argv)
36{
37 int i;
38
39 for (i = 0; i < filecount; i++)
40 print_fileio(&filetable[i], i, &filetable[i] == file);
41 return 0;
42}
43
44int
45openfile(
46 char *path,
2668d323 47 struct xfs_fsop_geom *geom,
cccf6abc 48 struct fs_path *fs_path)
48ec2905 49{
cccf6abc 50 struct fs_path *fsp;
48ec2905
DC
51 int fd;
52
53 fd = open(path, 0);
54 if (fd < 0) {
55 perror(path);
56 return -1;
57 }
58
59 if (ioctl(fd, XFS_IOC_FSGEOMETRY, geom) < 0) {
6fb70877
DW
60 if (errno == ENOTTY)
61 fprintf(stderr,
62_("%s: Not on a mounted XFS filesystem.\n"),
63 path);
64 else
65 perror("XFS_IOC_FSGEOMETRY");
48ec2905
DC
66 close(fd);
67 return -1;
68 }
cccf6abc
DC
69
70 if (fs_path) {
71 fsp = fs_table_lookup(path, FS_MOUNT_POINT);
72 if (!fsp) {
73 fprintf(stderr, _("%s: cannot find mount point."),
74 path);
1cda9090 75 close(fd);
cccf6abc
DC
76 return -1;
77 }
78 memcpy(fs_path, fsp, sizeof(struct fs_path));
79 }
48ec2905
DC
80 return fd;
81}
82
83int
84addfile(
85 char *name,
86 int fd,
2668d323 87 struct xfs_fsop_geom *geometry,
cccf6abc 88 struct fs_path *fs_path)
48ec2905
DC
89{
90 char *filename;
91
92 filename = strdup(name);
93 if (!filename) {
94 perror("strdup");
95 close(fd);
96 return -1;
97 }
98
99 /* Extend the table of currently open files */
100 filetable = (fileio_t *)realloc(filetable, /* growing */
101 ++filecount * sizeof(fileio_t));
102 if (!filetable) {
103 perror("realloc");
104 filecount = 0;
105 free(filename);
106 close(fd);
107 return -1;
108 }
109
110 /* Finally, make this the new active open file */
111 file = &filetable[filecount - 1];
112 file->fd = fd;
113 file->name = filename;
114 file->geom = *geometry;
cccf6abc 115 memcpy(&file->fs_path, fs_path, sizeof(file->fs_path));
48ec2905
DC
116 return 0;
117}
118
119void
120print_init(void)
121{
122 print_cmd.name = "print";
123 print_cmd.altname = "p";
124 print_cmd.cfunc = print_f;
125 print_cmd.argmin = 0;
126 print_cmd.argmax = 0;
127 print_cmd.flags = CMD_FLAG_ONESHOT;
128 print_cmd.oneline = _("list current open files");
129
130 add_command(&print_cmd);
131}