]>
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" | |
42b4c8e8 | 13 | #include "libfrog/paths.h" |
fee68490 | 14 | #include "libfrog/fsgeom.h" |
a509ad57 | 15 | #include "space.h" |
48ec2905 DC |
16 | |
17 | static cmdinfo_t print_cmd; | |
18 | ||
10cfd61e | 19 | struct fileio *filetable; |
48ec2905 | 20 | int filecount; |
10cfd61e | 21 | struct fileio *file; |
48ec2905 DC |
22 | |
23 | static void | |
24 | print_fileio( | |
10cfd61e | 25 | struct fileio *file, |
48ec2905 DC |
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, | |
a509ad57 | 48 | struct xfs_fd *xfd, |
cccf6abc | 49 | struct fs_path *fs_path) |
48ec2905 | 50 | { |
cccf6abc | 51 | struct fs_path *fsp; |
a509ad57 | 52 | int ret; |
48ec2905 | 53 | |
a509ad57 | 54 | ret = xfd_open(xfd, path, O_RDONLY); |
9612817d DW |
55 | if (ret) { |
56 | if (ret == ENOTTY) | |
6fb70877 DW |
57 | fprintf(stderr, |
58 | _("%s: Not on a mounted XFS filesystem.\n"), | |
59 | path); | |
9612817d DW |
60 | else { |
61 | errno = ret; | |
a509ad57 | 62 | perror(path); |
9612817d | 63 | } |
48ec2905 DC |
64 | return -1; |
65 | } | |
cccf6abc | 66 | |
8990666e DW |
67 | fsp = fs_table_lookup(path, FS_MOUNT_POINT); |
68 | if (!fsp) { | |
69 | fprintf(stderr, _("%s: cannot find mount point."), | |
70 | path); | |
a509ad57 | 71 | xfd_close(xfd); |
8990666e | 72 | return -1; |
cccf6abc | 73 | } |
8990666e DW |
74 | memcpy(fs_path, fsp, sizeof(struct fs_path)); |
75 | ||
a509ad57 | 76 | return xfd->fd; |
48ec2905 DC |
77 | } |
78 | ||
79 | int | |
80 | addfile( | |
81 | char *name, | |
a509ad57 | 82 | struct xfs_fd *xfd, |
cccf6abc | 83 | struct fs_path *fs_path) |
48ec2905 DC |
84 | { |
85 | char *filename; | |
86 | ||
87 | filename = strdup(name); | |
88 | if (!filename) { | |
89 | perror("strdup"); | |
a509ad57 | 90 | xfd_close(xfd); |
48ec2905 DC |
91 | return -1; |
92 | } | |
93 | ||
94 | /* Extend the table of currently open files */ | |
10cfd61e DW |
95 | filetable = (struct fileio *)realloc(filetable, /* growing */ |
96 | ++filecount * sizeof(struct fileio)); | |
48ec2905 DC |
97 | if (!filetable) { |
98 | perror("realloc"); | |
99 | filecount = 0; | |
100 | free(filename); | |
a509ad57 | 101 | xfd_close(xfd); |
48ec2905 DC |
102 | return -1; |
103 | } | |
104 | ||
105 | /* Finally, make this the new active open file */ | |
106 | file = &filetable[filecount - 1]; | |
48ec2905 | 107 | file->name = filename; |
a509ad57 | 108 | memcpy(&file->xfd, xfd, sizeof(struct xfs_fd)); |
cccf6abc | 109 | memcpy(&file->fs_path, fs_path, sizeof(file->fs_path)); |
48ec2905 DC |
110 | return 0; |
111 | } | |
112 | ||
113 | void | |
114 | print_init(void) | |
115 | { | |
116 | print_cmd.name = "print"; | |
117 | print_cmd.altname = "p"; | |
118 | print_cmd.cfunc = print_f; | |
119 | print_cmd.argmin = 0; | |
120 | print_cmd.argmax = 0; | |
121 | print_cmd.flags = CMD_FLAG_ONESHOT; | |
122 | print_cmd.oneline = _("list current open files"); | |
123 | ||
124 | add_command(&print_cmd); | |
125 | } |