]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - spaceman/file.c
libfrog: convert fsgeom.c functions to negative error codes
[thirdparty/xfsprogs-dev.git] / spaceman / file.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2012 Red Hat, Inc.
5 * All Rights Reserved.
6 */
7
8 #include "libxfs.h"
9 #include <sys/mman.h>
10 #include "command.h"
11 #include "input.h"
12 #include "init.h"
13 #include "libfrog/logging.h"
14 #include "libfrog/paths.h"
15 #include "libfrog/fsgeom.h"
16 #include "space.h"
17
18 static cmdinfo_t print_cmd;
19
20 struct fileio *filetable;
21 int filecount;
22 struct fileio *file;
23
24 static void
25 print_fileio(
26 struct fileio *file,
27 int index,
28 int braces)
29 {
30 printf(_("%c%03d%c %-14s\n"), braces? '[' : ' ', index,
31 braces? ']' : ' ', file->name);
32 }
33
34 static int
35 print_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
46 int
47 openfile(
48 char *path,
49 struct xfs_fd *xfd,
50 struct fs_path *fs_path)
51 {
52 struct fs_path *fsp;
53 int ret;
54
55 ret = -xfd_open(xfd, path, O_RDONLY);
56 if (ret) {
57 if (ret == ENOTTY)
58 fprintf(stderr,
59 _("%s: Not on a mounted XFS filesystem.\n"),
60 path);
61 else
62 xfrog_perror(ret, path);
63 return -1;
64 }
65
66 fsp = fs_table_lookup(path, FS_MOUNT_POINT);
67 if (!fsp) {
68 fprintf(stderr, _("%s: cannot find mount point."),
69 path);
70 xfd_close(xfd);
71 return -1;
72 }
73 memcpy(fs_path, fsp, sizeof(struct fs_path));
74
75 return xfd->fd;
76 }
77
78 int
79 addfile(
80 char *name,
81 struct xfs_fd *xfd,
82 struct fs_path *fs_path)
83 {
84 char *filename;
85
86 filename = strdup(name);
87 if (!filename) {
88 perror("strdup");
89 xfd_close(xfd);
90 return -1;
91 }
92
93 /* Extend the table of currently open files */
94 filetable = (struct fileio *)realloc(filetable, /* growing */
95 ++filecount * sizeof(struct fileio));
96 if (!filetable) {
97 perror("realloc");
98 filecount = 0;
99 free(filename);
100 xfd_close(xfd);
101 return -1;
102 }
103
104 /* Finally, make this the new active open file */
105 file = &filetable[filecount - 1];
106 file->name = filename;
107 memcpy(&file->xfd, xfd, sizeof(struct xfs_fd));
108 memcpy(&file->fs_path, fs_path, sizeof(file->fs_path));
109 return 0;
110 }
111
112 void
113 print_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 }