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