]>
Commit | Line | Data |
---|---|---|
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 | 14 | #include "space.h" |
fee68490 | 15 | #include "libfrog/fsgeom.h" |
48ec2905 DC |
16 | |
17 | static cmdinfo_t print_cmd; | |
18 | ||
19 | fileio_t *filetable; | |
20 | int filecount; | |
21 | fileio_t *file; | |
22 | ||
23 | static void | |
24 | print_fileio( | |
25 | fileio_t *file, | |
26 | int index, | |
27 | int braces) | |
28 | { | |
29 | printf(_("%c%03d%c %-14s\n"), braces? '[' : ' ', index, | |
30 | braces? ']' : ' ', file->name); | |
31 | } | |
32 | ||
33 | static int | |
34 | print_f( | |
35 | int argc, | |
36 | char **argv) | |
37 | { | |
38 | int i; | |
39 | ||
40 | for (i = 0; i < filecount; i++) | |
41 | print_fileio(&filetable[i], i, &filetable[i] == file); | |
42 | return 0; | |
43 | } | |
44 | ||
45 | int | |
46 | openfile( | |
47 | char *path, | |
2668d323 | 48 | struct xfs_fsop_geom *geom, |
cccf6abc | 49 | struct fs_path *fs_path) |
48ec2905 | 50 | { |
cccf6abc | 51 | struct fs_path *fsp; |
9612817d | 52 | int fd, ret; |
48ec2905 DC |
53 | |
54 | fd = open(path, 0); | |
55 | if (fd < 0) { | |
56 | perror(path); | |
57 | return -1; | |
58 | } | |
59 | ||
9612817d DW |
60 | ret = xfrog_geometry(fd, geom); |
61 | if (ret) { | |
62 | if (ret == ENOTTY) | |
6fb70877 DW |
63 | fprintf(stderr, |
64 | _("%s: Not on a mounted XFS filesystem.\n"), | |
65 | path); | |
9612817d DW |
66 | else { |
67 | errno = ret; | |
6fb70877 | 68 | perror("XFS_IOC_FSGEOMETRY"); |
9612817d | 69 | } |
48ec2905 DC |
70 | close(fd); |
71 | return -1; | |
72 | } | |
cccf6abc DC |
73 | |
74 | if (fs_path) { | |
75 | fsp = fs_table_lookup(path, FS_MOUNT_POINT); | |
76 | if (!fsp) { | |
77 | fprintf(stderr, _("%s: cannot find mount point."), | |
78 | path); | |
1cda9090 | 79 | close(fd); |
cccf6abc DC |
80 | return -1; |
81 | } | |
82 | memcpy(fs_path, fsp, sizeof(struct fs_path)); | |
83 | } | |
48ec2905 DC |
84 | return fd; |
85 | } | |
86 | ||
87 | int | |
88 | addfile( | |
89 | char *name, | |
90 | int fd, | |
2668d323 | 91 | struct xfs_fsop_geom *geometry, |
cccf6abc | 92 | struct fs_path *fs_path) |
48ec2905 DC |
93 | { |
94 | char *filename; | |
95 | ||
96 | filename = strdup(name); | |
97 | if (!filename) { | |
98 | perror("strdup"); | |
99 | close(fd); | |
100 | return -1; | |
101 | } | |
102 | ||
103 | /* Extend the table of currently open files */ | |
104 | filetable = (fileio_t *)realloc(filetable, /* growing */ | |
105 | ++filecount * sizeof(fileio_t)); | |
106 | if (!filetable) { | |
107 | perror("realloc"); | |
108 | filecount = 0; | |
109 | free(filename); | |
110 | close(fd); | |
111 | return -1; | |
112 | } | |
113 | ||
114 | /* Finally, make this the new active open file */ | |
115 | file = &filetable[filecount - 1]; | |
116 | file->fd = fd; | |
117 | file->name = filename; | |
118 | file->geom = *geometry; | |
cccf6abc | 119 | memcpy(&file->fs_path, fs_path, sizeof(file->fs_path)); |
48ec2905 DC |
120 | return 0; |
121 | } | |
122 | ||
123 | void | |
124 | print_init(void) | |
125 | { | |
126 | print_cmd.name = "print"; | |
127 | print_cmd.altname = "p"; | |
128 | print_cmd.cfunc = print_f; | |
129 | print_cmd.argmin = 0; | |
130 | print_cmd.argmax = 0; | |
131 | print_cmd.flags = CMD_FLAG_ONESHOT; | |
132 | print_cmd.oneline = _("list current open files"); | |
133 | ||
134 | add_command(&print_cmd); | |
135 | } |